Skip to content

Commit 096033a

Browse files
Trait methods inherit trait const stability, do not inherit const stability from their own regular stability
1 parent 820bfff commit 096033a

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

compiler/rustc_passes/src/stability.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,27 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
300300
// const stability: inherit feature gate from regular stability.
301301
let mut const_stab = const_stab.map(|(stab, _span)| stab);
302302

303+
// `impl const Trait for Type` items forward their const stability to their
304+
// immediate children.
305+
// FIXME(const_trait_impl): how is this supposed to interact with `#[rustc_const_stable_indirect]`?
306+
// Currently, once that is set, we do not inherit anything from the parent any more.
307+
if const_stab.is_none()
308+
&& let Some(parent) = self.parent_const_stab
309+
&& parent.is_const_unstable()
310+
{
311+
// For now, `const fn` in const traits/trait impls does not exist.
312+
assert!(
313+
fn_sig.is_none_or(|s| !s.header.is_const()),
314+
"should never have parent const stability for a const fn"
315+
);
316+
self.index.const_stab_map.insert(def_id, parent);
317+
}
318+
303319
// If this is a const fn but not annotated with stability markers, see if we can inherit regular stability.
304-
if fn_sig.is_some_and(|s| s.header.is_const()) && const_stab.is_none() &&
320+
if fn_sig.is_some_and(|s| s.header.is_const())
321+
&& const_stab.is_none()
305322
// We only ever inherit unstable features.
306-
let Some(inherit_regular_stab) =
307-
final_stab.filter(|s| s.is_unstable())
323+
&& let Some(inherit_regular_stab) = final_stab.filter(|s| s.is_unstable())
308324
{
309325
const_stab = Some(ConstStability {
310326
// We subject these implicitly-const functions to recursive const stability.
@@ -329,19 +345,6 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
329345
self.index.implications.insert(implied_by, feature);
330346
}
331347

332-
// `impl const Trait for Type` items forward their const stability to their
333-
// immediate children.
334-
// FIXME(const_trait_impl): how is this supposed to interact with `#[rustc_const_stable_indirect]`?
335-
// Currently, once that is set, we do not inherit anything from the parent any more.
336-
if const_stab.is_none() {
337-
debug!("annotate: const_stab not found, parent = {:?}", self.parent_const_stab);
338-
if let Some(parent) = self.parent_const_stab {
339-
if parent.is_const_unstable() {
340-
self.index.const_stab_map.insert(def_id, parent);
341-
}
342-
}
343-
}
344-
345348
self.recurse_with_stability_attrs(
346349
depr.map(|(d, _)| DeprecationEntry::local(d, def_id)),
347350
stab,
@@ -418,6 +421,9 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
418421
kind = AnnotationKind::DeprecationProhibited;
419422
const_stab_inherit = InheritConstStability::Yes;
420423
}
424+
hir::ItemKind::Trait(..) => {
425+
const_stab_inherit = InheritConstStability::Yes;
426+
}
421427
hir::ItemKind::Struct(ref sd, _) => {
422428
if let Some(ctor_def_id) = sd.ctor_def_id() {
423429
self.annotate(

tests/ui/traits/const-traits/auxiliary/staged-api.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
pub trait MyTrait {
1010
#[stable(feature = "rust1", since = "1.0.0")]
1111
fn func();
12+
13+
#[stable(feature = "rust1", since = "1.0.0")]
14+
fn default<T: ~const MyTrait>() {
15+
// This call is const-unstable, but permitted here since we inherit
16+
// the `rustc_const_unstable` above.
17+
T::func();
18+
}
1219
}
1320

1421
#[stable(feature = "rust1", since = "1.0.0")]
@@ -18,6 +25,12 @@ pub struct Unstable;
1825
#[rustc_const_unstable(feature = "unstable", issue = "none")]
1926
impl const MyTrait for Unstable {
2027
fn func() {}
28+
29+
fn default<T: ~const MyTrait>() {
30+
// This call is const-unstable, but permitted here since we inherit
31+
// the `rustc_const_unstable` above.
32+
T::func();
33+
}
2134
}
2235

2336
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)