dtolnay / proc-macro2

Apache License 2.0
745 stars 117 forks source link

Make `proc_macro_span` optional with nightly #415

Open andreeaflorescu opened 1 year ago

andreeaflorescu commented 1 year ago

From the documentation of proc_macro_span I was understanding that if you don't allow unstable features in your nightly build, then the build of the crate should still work: "Enabled when building with nightly, unless -Z allow-feature in RUSTFLAGS disallows unstable features."

I am using the following configuration in my .cargo/config.toml:

[unstable]
allow-features = []

And the following rust nightly: nightly-2023-09-25.

When building a dummy crate that just has proc-macro2 as a dependency, the following happens:

 cargo build --verbose
       Dirty proc-macro2 v1.0.67: the config settings changed
   Compiling proc-macro2 v1.0.67
       Dirty unicode-ident v1.0.12: the config settings changed
   Compiling unicode-ident v1.0.12
     Running `/Users/fandree/.rustup/toolchains/nightly-2023-09-25-aarch64-apple-darwin/bin/rustc --crate-name build_script_build --edition=2021 /Users/fandree/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.67/build.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=312 -Zallow-features= --crate-type bin --emit=dep-info,link -C embed-bitcode=no --cfg 'feature="default"' --cfg 'feature="proc-macro"' -C metadata=643d0f678a6ab910 -C extra-filename=-643d0f678a6ab910 --out-dir /Users/fandree/sources/rust-crates/test-crate/target/debug/build/proc-macro2-643d0f678a6ab910 -L dependency=/Users/fandree/sources/rust-crates/test-crate/target/debug/deps --cap-lints allow`
     Running `/Users/fandree/.rustup/toolchains/nightly-2023-09-25-aarch64-apple-darwin/bin/rustc --crate-name unicode_ident --edition=2018 /Users/fandree/.cargo/registry/src/index.crates.io-6f17d22bba15001f/unicode-ident-1.0.12/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=312 -Zallow-features= --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 -C split-debuginfo=unpacked -C metadata=19688c2ad055990d -C extra-filename=-19688c2ad055990d --out-dir /Users/fandree/sources/rust-crates/test-crate/target/debug/deps -L dependency=/Users/fandree/sources/rust-crates/test-crate/target/debug/deps --cap-lints allow`
     Running `/Users/fandree/sources/rust-crates/test-crate/target/debug/build/proc-macro2-643d0f678a6ab910/build-script-build`
     Running `/Users/fandree/.rustup/toolchains/nightly-2023-09-25-aarch64-apple-darwin/bin/rustc --crate-name proc_macro2 --edition=2021 /Users/fandree/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.67/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=312 -Zallow-features= --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 -C split-debuginfo=unpacked --cfg 'feature="default"' --cfg 'feature="proc-macro"' -C metadata=aef881fdd2157960 -C extra-filename=-aef881fdd2157960 --out-dir /Users/fandree/sources/rust-crates/test-crate/target/debug/deps -L dependency=/Users/fandree/sources/rust-crates/test-crate/target/debug/deps --extern unicode_ident=/Users/fandree/sources/rust-crates/test-crate/target/debug/deps/libunicode_ident-19688c2ad055990d.rmeta --cap-lints allow --cfg wrap_proc_macro --cfg proc_macro_span`
error[E0725]: the feature `proc_macro_span` is not in the list of allowed features
  --> /Users/fandree/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.67/src/lib.rs:90:59
   |
90 | #![cfg_attr(any(proc_macro_span, super_unstable), feature(proc_macro_span))]

The --cfg proc_macro_span was added even though we also pass the -Zallow-features= flag. Is this the expected behavior?

I would like to be able to build proc-macro2 with nightly without having to enable the unstable feature. Is this possible?

dtolnay commented 1 year ago

I suspect this is a Cargo bug. Cargo is not propagating that -Zallow-features= in the CARGO_ENCODED_RUSTFLAGS seen by the build script.

I tested this by creating a 2-line .cargo/config.toml as you showed, and a build script containing:

// build.rs

fn main() {
    std::process::Command::new("env").status().unwrap();
    std::process::exit(1);
}

The output of env printed by cargo build shows that CARGO_ENCODED_RUSTFLAGS is empty. I believe it should contain -Zallow-features=. The whole point of Cargo providing CARGO_ENCODED_RUSTFLAGS to build scripts is so that the build script can make rustc invocations using the same flags that Cargo would use for its own rustc invocations.

andreeaflorescu commented 1 year ago

Indeed, it looks like the flag is not passed when using [unstable], but it is passed when using:

[build]
rustflags = ["-Zallow-features="]

I am not very sure if I should open an issue in cargo though, as it might be expected based on this 🤷‍♀️ https://github.com/rust-lang/cargo/issues/12437#issuecomment-1677584903.

Out of curiosity, what is the reasoning for always enabling proc_macro_span? Considering it's unstable, my expectation would be for it to be enabled only by using a non-default feature, and not the other way around.

tqwewe commented 6 months ago

I belive I'm having the same issue. Recently when trying to build any crate on nightly, I'm always getting errors from proc-macro2. Eg:

error[E0635]: unknown feature `proc_macro_span_shrink`
  --> /Users/ari/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.47/src/lib.rs:92:30
   |
92 |     feature(proc_macro_span, proc_macro_span_shrink)

This forces me to use stable as I don't know how to fix this.

workingjubilee commented 6 months ago

@tqwewe You are required to also update the crate to the latest version, so,

cargo update -p proc-macro2 --precise 1.0.81

as proc-macro2 enables nightly features implicitly to save you the inconvenience of having to think about and enable a feature on your own. This also means that each version of this software is neither forward nor backward compatible if anything changes even slightly about versioning or build flags or etc.