From 6cdfb609f9dba3dfc9e412bb6a6d87ee9efcf462 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Fri, 28 Nov 2025 23:31:41 -0500 Subject: [PATCH] make assoc fn inherit const stability from inherent `const impl` blocks --- compiler/rustc_passes/src/stability.rs | 2 +- .../traits/const-traits/auxiliary/staged-api.rs | 9 ++++++++- .../const-traits/inherent-impl-stability.rs | 16 ++++++++++++++++ .../const-traits/inherent-impl-stability.stderr | 13 +++++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 tests/ui/traits/const-traits/inherent-impl-stability.rs create mode 100644 tests/ui/traits/const-traits/inherent-impl-stability.stderr diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index b7e6e2d451e3f..39830db2b11db 100644 --- a/compiler/rustc_passes/src/stability.rs +++ b/compiler/rustc_passes/src/stability.rs @@ -54,7 +54,7 @@ fn inherit_const_stability(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { match def_kind { DefKind::AssocFn | DefKind::AssocTy | DefKind::AssocConst => { match tcx.def_kind(tcx.local_parent(def_id)) { - DefKind::Impl { of_trait: true } => true, + DefKind::Impl { .. } => true, _ => false, } } diff --git a/tests/ui/traits/const-traits/auxiliary/staged-api.rs b/tests/ui/traits/const-traits/auxiliary/staged-api.rs index b2b1e0615c37f..65e75864ff825 100644 --- a/tests/ui/traits/const-traits/auxiliary/staged-api.rs +++ b/tests/ui/traits/const-traits/auxiliary/staged-api.rs @@ -1,4 +1,3 @@ -//@ compile-flags: -Znext-solver #![feature(const_trait_impl)] #![feature(staged_api)] #![stable(feature = "rust1", since = "1.0.0")] @@ -19,6 +18,14 @@ impl const MyTrait for Unstable { fn func() {} } +// tested in inherent-impl-stability.rs instead to avoid clutter +#[stable(feature = "rust1", since = "1.0.0")] +#[rustc_const_unstable(feature = "unstable", issue = "none")] +const impl Unstable { + #[stable(feature = "rust1", since = "1.0.0")] + pub fn inherent_func() {} +} + #[stable(feature = "rust1", since = "1.0.0")] pub struct Unstable2; diff --git a/tests/ui/traits/const-traits/inherent-impl-stability.rs b/tests/ui/traits/const-traits/inherent-impl-stability.rs new file mode 100644 index 0000000000000..e520e5af2e094 --- /dev/null +++ b/tests/ui/traits/const-traits/inherent-impl-stability.rs @@ -0,0 +1,16 @@ +//@ aux-build: staged-api.rs +extern crate staged_api; + +use staged_api::*; + +// Const stability has no impact on usage in non-const contexts. +fn non_const_context() { + Unstable::inherent_func(); +} + +const fn stable_const_context() { + Unstable::inherent_func(); + //~^ ERROR: `staged_api::Unstable::inherent_func` is not yet stable as a const fn +} + +fn main() {} diff --git a/tests/ui/traits/const-traits/inherent-impl-stability.stderr b/tests/ui/traits/const-traits/inherent-impl-stability.stderr new file mode 100644 index 0000000000000..018065c1f4f06 --- /dev/null +++ b/tests/ui/traits/const-traits/inherent-impl-stability.stderr @@ -0,0 +1,13 @@ +error: `staged_api::Unstable::inherent_func` is not yet stable as a const fn + --> $DIR/inherent-impl-stability.rs:12:5 + | +LL | Unstable::inherent_func(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: add `#![feature(unstable)]` to the crate attributes to enable + | +LL + #![feature(unstable)] + | + +error: aborting due to 1 previous error +