Skip to content

Commit

Permalink
Merge rust-bitcoin#3182: Refactor Rust version checking
Browse files Browse the repository at this point in the history
ad34a98 Refactor Rust version checking (Martin Habovstiak)
7d5ce89 Fix type ambiguity in IO tests (Martin Habovstiak)

Pull request description:

  Conditional compilation depending on Rust version using `cfg` had the disadvantage that we had to write the same code multiple times, compile it multiple times, execute it multiple times, update it multiple times... Apart from obvious maintenance issues the build script wasn't generating the list of allowed `cfg`s so those had to be maintained manually in `Cargo.toml`. This was fixable by printing an appropriate line but it's best to do it together with the other changes.

  Because we cannot export `cfg` flags from a crate to different crates we take a completely different approach: we define a macro called `rust_version` that takes a very naturally looking condition such as `if >= 1.70 {}`. This macro is auto-generated so that it produces different results based on the compiler version - it either expands to first block or the second block (after `else`).

  This way, the other crates can simply call the macro when needed.

  Unfortunately some minimal maintenance is still needed: to update the max version number when a newer version is used. (Note that code will still work with higher versions, it only limits which conditions can be used in downstream code.) This can be automated with the pin update script or we could just put the pin file into the `internals` directory and read the value from there. Not automating isn't terrible either since anyone adding a cfg with higher version will see a nice error about unknown version of Rust and can update it manually.

  Because this changes syntax to a more naturally looking version number, as a side effect the `cond_const` macro could be also updated to use the new macro under the hood, providing much nicer experience - it is no longer needed to provide human-readable version of the version string to put in the note about `const`ness requiring a newer version. As such the note is now always there using a single source of truth.

  It's also a great moment to introduce this change right now since there's currently no conditional compilation used in `bitcoin` crate making the changes minimal. However it is not yet added to `bitcoin-io` since `bitcoin-io` is not depending on `internals`. It might be a reason to start depending on it but that's for later discussion.

ACKs for top commit:
  apoelstra:
    ACK ad34a98 successfully ran local tests
  tcharding:
    ACK ad34a98

Tree-SHA512: 7a82017fc51ba1b473ca638c7bdbf5c893da0a78c70ea8f1a0241049e7874592fad328abd60b3e09a00439b771e7cfc5ebad5e874314d15a48271ec6cb2d7bb7
  • Loading branch information
apoelstra committed Aug 20, 2024
2 parents d0b6a4c + ad34a98 commit 95a7805
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 170 deletions.
3 changes: 3 additions & 0 deletions Cargo-minimal.lock
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ dependencies = [
[[package]]
name = "bitcoin-io"
version = "0.1.2"
dependencies = [
"bitcoin-internals",
]

[[package]]
name = "bitcoin-primitives"
Expand Down
3 changes: 3 additions & 0 deletions Cargo-recent.lock
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ dependencies = [
[[package]]
name = "bitcoin-io"
version = "0.1.2"
dependencies = [
"bitcoin-internals",
]

[[package]]
name = "bitcoin-primitives"
Expand Down
37 changes: 0 additions & 37 deletions bitcoin/build.rs

This file was deleted.

2 changes: 1 addition & 1 deletion internals/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[lints.rust]
unexpected_cfgs = { level = "deny", check-cfg = ['cfg(rust_v_1_64)'] }
unexpected_cfgs = { level = "deny" }
45 changes: 43 additions & 2 deletions internals/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
const MAX_USED_VERSION: u64 = 80;

use std::io;

fn main() {
let rustc = std::env::var_os("RUSTC");
let rustc = rustc.as_ref().map(std::path::Path::new).unwrap_or_else(|| "rustc".as_ref());
Expand Down Expand Up @@ -30,8 +34,45 @@ fn main() {
assert_eq!(msrv_major, "1", "unexpected Rust major version");
let msrv_minor = msrv.next().unwrap().parse::<u64>().unwrap();

// print cfg for all interesting versions less than or equal to minor
let out_dir = std::env::var_os("OUT_DIR").expect("missing OUT_DIR env var");
let out_dir = std::path::PathBuf::from(out_dir);
let macro_file = std::fs::File::create(out_dir.join("rust_version.rs")).expect("failed to create rust_version.rs");
let macro_file = io::BufWriter::new(macro_file);
write_macro(macro_file, msrv_minor, minor).expect("failed to write to rust_version.rs");
}

fn write_macro(mut macro_file: impl io::Write, msrv_minor: u64, minor: u64) -> io::Result<()> {
writeln!(macro_file, "/// Expands code based on Rust version this is compiled under.")?;
writeln!(macro_file, "///")?;
writeln!(macro_file, "/// Example:")?;
writeln!(macro_file, "/// ```")?;
writeln!(macro_file, "/// bitcoin_internals::rust_version! {{")?;
writeln!(macro_file, "/// if >= 1.70 {{")?;
writeln!(macro_file, "/// println!(\"This is Rust 1.70+\");")?;
writeln!(macro_file, "/// }} else {{")?;
writeln!(macro_file, "/// println!(\"This is Rust < 1.70\");")?;
writeln!(macro_file, "/// }}")?;
writeln!(macro_file, "/// }}")?;
writeln!(macro_file, "/// ```")?;
writeln!(macro_file, "///")?;
writeln!(macro_file, "/// The `else` branch is optional.")?;
writeln!(macro_file, "/// Currently only the `>=` operator is supported.")?;
writeln!(macro_file, "#[macro_export]")?;
writeln!(macro_file, "macro_rules! rust_version {{")?;
for version in msrv_minor..=minor {
println!("cargo:rustc-cfg=rust_v_1_{}", version);
writeln!(macro_file, " (if >= 1.{} {{ $($if_yes:tt)* }} $(else {{ $($if_no:tt)* }})?) => {{", version)?;
writeln!(macro_file, " $($if_yes)*")?;
writeln!(macro_file, " }};")?;
}
for version in (minor + 1)..(MAX_USED_VERSION + 1) {
writeln!(macro_file, " (if >= 1.{} {{ $($if_yes:tt)* }} $(else {{ $($if_no:tt)* }})?) => {{", version)?;
writeln!(macro_file, " $($($if_no)*)?")?;
writeln!(macro_file, " }};")?;
}
writeln!(macro_file, " (if >= $unknown:tt $($rest:tt)*) => {{")?;
writeln!(macro_file, " compile_error!(concat!(\"unknown Rust version \", stringify!($unknown)));")?;
writeln!(macro_file, " }};")?;
writeln!(macro_file, "}}")?;
writeln!(macro_file, "pub use rust_version;")?;
macro_file.flush()
}
2 changes: 1 addition & 1 deletion internals/src/array_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mod safety_boundary {
// from_raw_parts is const-unstable until 1.64
cond_const! {
/// Returns a reference to the underlying data.
pub const(in rust_v_1_64 = "1.64") fn as_slice(&self) -> &[T] {
pub const(in 1.64) fn as_slice(&self) -> &[T] {
let ptr = &self.data as *const _ as *const T;
unsafe { core::slice::from_raw_parts(ptr, self.len) }
}
Expand Down
50 changes: 24 additions & 26 deletions internals/src/const_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,36 +56,34 @@ pub use concat_bytes_to_arr;
#[macro_export]
/// Enables const fn in specified Rust version
macro_rules! cond_const {
($($(#[$attr:meta])* $vis:vis const(in $ver:ident $(= $human_ver:literal)?) fn $name:ident$(<$($gen:tt)*>)?($($args:tt)*) $(-> $ret:ty)? $body:block)+ ) => {
($($(#[$attr:meta])* $vis:vis const(in $version:tt) fn $name:ident$(<$($gen:tt)*>)?($($args:tt)*) $(-> $ret:ty)? $body:block)+ ) => {
$(
#[cfg($ver)]
$(#[$attr])*
$(
#[doc = "\nNote: the function is only `const` in Rust "]
#[doc = $human_ver]
#[doc = "."]
)?
$vis const fn $name$(<$($gen)*>)?($($args)*) $(-> $ret)? $body

#[cfg(not($ver))]
$(#[$attr])*
$vis fn $name$(<$($gen)*>)?($($args)*) $(-> $ret)? $body
$crate::rust_version::rust_version! {
if >= $version {
$(#[$attr])*
#[doc = concat!("\nNote: the function is only `const` in Rust ", stringify!($version), ".")]
$vis const fn $name$(<$($gen)*>)?($($args)*) $(-> $ret)? $body
} else {
$(#[$attr])*
#[doc = concat!("\nNote: the function is `const` in Rust ", stringify!($version), ".")]
$vis fn $name$(<$($gen)*>)?($($args)*) $(-> $ret)? $body
}
}
)+
};
($($(#[$attr:meta])* $vis:vis const(in $ver:ident $(= $human_ver:literal)?) unsafe fn $name:ident$(<$($gen:tt)*>)?($($args:tt)*) $(-> $ret:ty)? $body:block)+ ) => {
($($(#[$attr:meta])* $vis:vis const(in $version:tt) unsafe fn $name:ident$(<$($gen:tt)*>)?($($args:tt)*) $(-> $ret:ty)? $body:block)+ ) => {
$(
#[cfg($ver)]
$(#[$attr])*
$(
#[doc = "\nNote: the function is only `const` in Rust "]
#[doc = $human_ver]
#[doc = " and newer."]
)?
$vis const unsafe fn $name$(<$($gen)*>)?($($args)*) $(-> $ret)? $body

#[cfg(not($ver))]
$(#[$attr])*
$vis unsafe fn $name$(<$($gen)*>)?($($args)*) $(-> $ret)? $body
$crate::rust_version::rust_version! {
if >= $version {
$(#[$attr])*
#[doc = concat!("\nNote: the function is only `const` in Rust ", stringify!($version), ".")]
$vis const unsafe fn $name$(<$($gen)*>)?($($args)*) $(-> $ret)? $body
} else {
$(#[$attr])*
#[doc = concat!("\nNote: the function is `const` in Rust ", stringify!($version), ".")]
$vis unsafe fn $name$(<$($gen)*>)?($($args)*) $(-> $ret)? $body
}
}
)+
};
}
Expand Down
7 changes: 7 additions & 0 deletions internals/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ pub extern crate serde_json;
#[cfg(feature = "test-serde")]
pub extern crate bincode;

// The pub module is a workaround for strange error:
// "macro-expanded `macro_export` macros from the current crate cannot be referred to by absolute paths"
#[doc(hidden)]
pub mod rust_version {
include!(concat!(env!("OUT_DIR"), "/rust_version.rs"));
}

pub mod array_vec;
pub mod const_tools;
pub mod error;
Expand Down
5 changes: 4 additions & 1 deletion io/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ default = ["std"]
std = ["alloc"]
alloc = []

[dependencies]
internals = { package = "bitcoin-internals", version = "0.3.0" }

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[lints.rust]
unexpected_cfgs = { level = "deny", check-cfg = ['cfg(rust_v_1_72)', 'cfg(rust_v_1_73)', 'cfg(rust_v_1_75)', 'cfg(rust_v_1_78)'] }
unexpected_cfgs = { level = "deny" }
37 changes: 0 additions & 37 deletions io/build.rs

This file was deleted.

Loading

0 comments on commit 95a7805

Please sign in to comment.