Skip to content

Trait methods inherit trait const stability, do not inherit const stability from their own regular stability #136319

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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions compiler/rustc_passes/src/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,27 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
// const stability: inherit feature gate from regular stability.
let mut const_stab = const_stab.map(|(stab, _span)| stab);

// `impl const Trait for Type` items forward their const stability to their
// immediate children.
// FIXME(const_trait_impl): how is this supposed to interact with `#[rustc_const_stable_indirect]`?
// Currently, once that is set, we do not inherit anything from the parent any more.
if const_stab.is_none()
&& let Some(parent) = self.parent_const_stab
&& parent.is_const_unstable()
{
// For now, `const fn` in const traits/trait impls does not exist.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// For now, `const fn` in const traits/trait impls does not exist.
// For now, `const fn` in const traits/trait impls does not exist.
// If we ever support that, the logic here and also the const-stability enforcement
// logic needs some re-thinking.

assert!(
fn_sig.is_none_or(|s| !s.header.is_const()),
"should never have parent const stability for a const fn"
);
self.index.const_stab_map.insert(def_id, parent);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be const_stab = parent?

Right now, it seems like we could then enter the if below which actually sets const_stab to Some and then that gets inserted into the table.

}

// If this is a const fn but not annotated with stability markers, see if we can inherit regular stability.
if fn_sig.is_some_and(|s| s.header.is_const()) && const_stab.is_none() &&
if fn_sig.is_some_and(|s| s.header.is_const())
&& const_stab.is_none()
// We only ever inherit unstable features.
let Some(inherit_regular_stab) =
final_stab.filter(|s| s.is_unstable())
&& let Some(inherit_regular_stab) = final_stab.filter(|s| s.is_unstable())
{
const_stab = Some(ConstStability {
// We subject these implicitly-const functions to recursive const stability.
Expand All @@ -329,19 +345,6 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
self.index.implications.insert(implied_by, feature);
}

// `impl const Trait for Type` items forward their const stability to their
// immediate children.
// FIXME(const_trait_impl): how is this supposed to interact with `#[rustc_const_stable_indirect]`?
// Currently, once that is set, we do not inherit anything from the parent any more.
if const_stab.is_none() {
debug!("annotate: const_stab not found, parent = {:?}", self.parent_const_stab);
if let Some(parent) = self.parent_const_stab {
if parent.is_const_unstable() {
self.index.const_stab_map.insert(def_id, parent);
}
}
}

self.recurse_with_stability_attrs(
depr.map(|(d, _)| DeprecationEntry::local(d, def_id)),
stab,
Expand Down Expand Up @@ -418,6 +421,9 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
kind = AnnotationKind::DeprecationProhibited;
const_stab_inherit = InheritConstStability::Yes;
}
hir::ItemKind::Trait(..) => {
const_stab_inherit = InheritConstStability::Yes;
}
hir::ItemKind::Struct(ref sd, _) => {
if let Some(ctor_def_id) = sd.ctor_def_id() {
self.annotate(
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/traits/const-traits/auxiliary/staged-api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
pub trait MyTrait {
#[stable(feature = "rust1", since = "1.0.0")]
fn func();

#[stable(feature = "rust1", since = "1.0.0")]
fn default<T: ~const MyTrait>() {
// This call is const-unstable, but permitted here since we inherit
// the `rustc_const_unstable` above.
T::func();
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -18,6 +25,12 @@ pub struct Unstable;
#[rustc_const_unstable(feature = "unstable", issue = "none")]
impl const MyTrait for Unstable {
fn func() {}

fn default<T: ~const MyTrait>() {
// This call is const-unstable, but permitted here since we inherit
// the `rustc_const_unstable` above.
T::func();
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
Loading