Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make proc_macro_span optional with nightly #415

Open
andreeaflorescu opened this issue Oct 11, 2023 · 4 comments
Open

Make proc_macro_span optional with nightly #415

andreeaflorescu opened this issue Oct 11, 2023 · 4 comments

Comments

@andreeaflorescu
Copy link

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
Copy link
Owner

dtolnay commented Oct 12, 2023

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
Copy link
Author

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 🤷‍♀️ rust-lang/cargo#12437 (comment).

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

This comment was marked as off-topic.

@workingjubilee

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants