Skip to content

Nightly compiler complains about a disabled nightly feature used in an impl block, but the block is cfg-gated by a disabled feature #140960

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

Closed
gendx opened this issue May 13, 2025 · 2 comments
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues.

Comments

@gendx
Copy link

gendx commented May 13, 2025

I tried this code in the playground (using Rust nightly):

#![allow(unexpected_cfgs)]
#![cfg_attr(
    feature = "nightly",
    feature(associated_const_equality)
)]

pub trait NeedsDrop {
    const NEEDS_DROP: bool;
}

impl<T> NeedsDrop for T {
    const NEEDS_DROP: bool = std::mem::needs_drop::<T>();
}

pub trait Foo {}

#[cfg(feature = "nightly")]
impl<T> Foo for T where T: NeedsDrop<NEEDS_DROP = true> {}

I expected to see this happen:

  • Given that the playground knows no feature called "nightly" (no Cargo.toml defined), the cfg_attr at the beginning doesn't apply i.e. the associated_const_equality feature isn't enabled.
  • Additionally, I also expected the impl block at the last line (which depends on associated_const_equality) to be excluded from conditional compilation.

Instead, this happened: the compiler complained that the impl block uses a nightly feature that isn't enabled (rather than ignoring the impl block altogether).

   Compiling playground v0.0.1 (/playground)
error[E0658]: associated const equality is incomplete
  --> src/lib.rs:18:38
   |
18 | impl<T> Foo for T where T: NeedsDrop<NEEDS_DROP = true> {}
   |                                      ^^^^^^^^^^^^^^^^^
   |
   = note: see issue #92827 <https://github.com/rust-lang/rust/issues/92827> for more information
   = help: add `#![feature(associated_const_equality)]` to the crate attributes to enable
   = note: this compiler was built on 2025-05-12; consider upgrading it if it is out of date

For more information about this error, try `rustc --explain E0658`.
error: could not compile `playground` (lib) due to 1 previous error

Meta

Using the nightly compiler in the playground (https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024).

From the error message:

   = note: this compiler was built on 2025-05-12; consider upgrading it if it is out of date
@gendx gendx added the C-bug Category: This is a bug. label May 13, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 13, 2025
@fmease
Copy link
Member

fmease commented May 13, 2025

This works exactly as intended: All (modern) syntax feature gates apply pre-expansion. This is to prevent the situation where stable users start depending on crates that actually use experimental syntax features behind a cfg since the removal of or any modifications to the latter would still break stable users which would be incompatible with Rust's stability guarantees.

@fmease fmease removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 13, 2025
@fmease
Copy link
Member

fmease commented May 13, 2025

Consider feature auto_traits (auto trait Trait { … }) for example which uses a legacy post-expansion feature gate: This lead to crates like https://github.com/leptos-rs/leptos and https://github.com/PyO3/pyo3 using auto traits behind a "nightly" feature flag1 while having tons of stable users! Modifying or removing the syntax of auto traits is no longer an option (at least not a straightforward one that can take effect immediately).

We strictly want to avoid this kind of impasse!

Footnotes

  1. Disclaimer: Last time I checked out those crates was in 2023, so I don't know if that's still the case.

@fmease fmease added the C-discussion Category: Discussion or questions that doesn't represent real issues. label May 13, 2025
@fmease fmease closed this as not planned Won't fix, can't repro, duplicate, stale May 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues.
Projects
None yet
Development

No branches or pull requests

3 participants