Cfg-gating `impl const Trait` generates feature-gate error

⚓ Rust    📅 2026-05-01    👤 surdeus    👁️ 3      

surdeus

The following code does not compile, because it requires that the const_trait_impl feature be enabled, even though it is cfg'd out:

#![cfg_attr(
    feature = "nightly",
    feature(const_trait_impl, const_default)
]

struct A(u8);

#[cfg(feature = "nightly")]
impl const Default for A {
    fn default() -> Self {
        Self(0)
    }
}

... which results in the following error:

error[E0658]: const trait impls are experimental
  --> src/lib.rs:49:6
   |
49 | impl const Default for A {
   |      ^^^^^
   |
   = note: see issue #143874 <https://github.com/rust-lang/rust/issues/143874> for more information
   = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
   = note: this compiler was built on 2026-04-29; consider upgrading it if it is out of date

... and even when I test with #[cfg(target_arch = "aarch64")] (and I'm not building for ARM64), the same error is generated. So far as I understand, the code shouldn't even be enumerated by the compiler. Am I doing something wrong, or is this a bug?

Thanks.

2 posts - 2 participants

Read full topic

🏷️ Rust_feed