Skip to content

Enforce syntactical stability of const traits in HIR #135423

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

Merged

Conversation

compiler-errors
Copy link
Member

This PR enforces what I'm calling syntactical const stability of traits. In other words, it enforces the ability to name ~const/const traits in trait bounds in various syntax positions in HIR (including in the trait of an impl header). This functionality is analogous to the regular item stability checker, which is concerned with making sure that you cannot refer to unstable items by name, and is implemented as an extension of that pass.

This is separate from enforcing the recursive const stability of const trait methods, which is implemented in MIR and runs on MIR bodies. That will require adding a new NonConstOp to the const checker and probably adjusting some logic to deduplicate redundant errors.

However, this check is separate and necessary for making sure that users don't add ~const/const bounds to items when the trait is not const-stable in the first place. I chose to separate enforcing recursive const stability out of this PR to make it easier to review. I'll probably open a follow-up following this one, blocked on this PR.

r? @RalfJung cc @rust-lang/project-const-traits

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 13, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jan 13, 2025

Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter
gets adapted for the changes, if necessary.

cc @rust-lang/miri, @rust-lang/wg-const-eval

),
EvalResult::Unmarked => unmarked(span, def_id),
}

is_allowed
}

pub fn check_const_stability(self, def_id: DefId, span: Span, const_kw_span: Span) {
Copy link
Member Author

@compiler-errors compiler-errors Jan 13, 2025

Choose a reason for hiding this comment

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

This function is analogous to check_optional_stability but with the logic in eval_stability_allow_unstable inlined, and which operating on const stability instead of regular stability.

And I don't think we have soft unstability for consts, so I asserted against that.

Copy link
Member

Choose a reason for hiding this comment

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

That seems like a comment worth putting into the code.

@RalfJung
Copy link
Member

Isn't #133999 doing essentially the same thing? Cc @fee1-dead

Copy link
Member

@fee1-dead fee1-dead left a comment

Choose a reason for hiding this comment

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

lgtm

@fee1-dead
Copy link
Member

fee1-dead commented Jan 13, 2025

This is separate from enforcing the recursive const stability of const trait methods, which is implemented in MIR and runs on MIR bodies. That will require adding a new NonConstOp to the const checker and probably adjusting some logic to deduplicate redundant errors.

FWIW, you can't do all the recursive stability checks in MIR. Currently, we allow something like

#[rustc_const_stable(feature = "dummy", since = "1.0.0")]
const fn foo<T: ~const UnstableTrait>() {  }

If the feature for that unstable trait is enabled. For recursive stability, we would need to check the bounds (which is different than running passes on MIR bodies). Several more places for this check would be const impls, const traits, and associated type bounds on const traits, etc.

@compiler-errors
Copy link
Member Author

compiler-errors commented Jan 13, 2025

@RalfJung: Kind of, but no. This implements a subset of that PR in a totally different way, and I put it up bc @fee1-dead mentioned that they wouldn't have time to finish up this work.

@compiler-errors
Copy link
Member Author

compiler-errors commented Jan 13, 2025

FWIW, you can't do all the recursive stability checks in MIR. [...]

@fee1-dead: I think that over complicates the problem a bit? Recursive const stability shouldn't need to be concerned with things that don't end up in the MIR, method calls in this case.

Simply adding a where clause to a function feels separate from that -- after all, isn't recursive const stability checking motivated by making sure we don't expose new const intrinsics (and other functionality that would make the evaluator do new things) to stable const Rust? naming a where clause does not feel like that to me.

@RalfJung
Copy link
Member

Having talked with @compiler-errors, I agree with them. We should ensure that method calls follow the established recursive const stability rules, but the trait system so far hasn't done recursive stability and I don't think it has to start now.

What is important is that if Trait::method can be called from a stable const fn, then in all impls of trait, method follows recursive const stability rules.

Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

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

I agree with the general approach and the test results look good. I'm not familiar enough with the implementation of our general stability checks to sign off on that, though.

r? compiler

),
EvalResult::Unmarked => unmarked(span, def_id),
}

is_allowed
}

pub fn check_const_stability(self, def_id: DefId, span: Span, const_kw_span: Span) {
Copy link
Member

Choose a reason for hiding this comment

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

That seems like a comment worth putting into the code.

@RalfJung
Copy link
Member

Uh what, we got two reviewers?^^

r? compiler

@rustbot rustbot assigned Nadrieril and unassigned chenyukang and SparrowLii Jan 14, 2025
@oli-obk oli-obk assigned oli-obk and unassigned Nadrieril Jan 14, 2025
@oli-obk
Copy link
Contributor

oli-obk commented Jan 14, 2025

r=me with ralf's nits

@compiler-errors compiler-errors force-pushed the enforce-const-trait-syntactical branch from 3cd9780 to 6c3c1e4 Compare January 14, 2025 18:53
@compiler-errors compiler-errors force-pushed the enforce-const-trait-syntactical branch from 6c3c1e4 to 55c1f36 Compare January 14, 2025 18:55
Copy link
Member

Choose a reason for hiding this comment

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

Hm, this still completely changes the test results... what is this even testing?

Copy link
Member Author

Choose a reason for hiding this comment

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

See the PR I linked where oli fixed const promotion, it's just testing that we don't promote random things.

@@ -1,6 +1,6 @@
//@ known-bug: #103507
Copy link
Member

Choose a reason for hiding this comment

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

This bug is long-closed. Something is fishy here.

Copy link
Member Author

Choose a reason for hiding this comment

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

I bet it was a typo lol

Copy link
Member Author

Choose a reason for hiding this comment

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

This is not a known bug, but actually just a check-fail test: #105085

I'll remvoe the known-bug.

Copy link
Member

Choose a reason for hiding this comment

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

That's concerning, how did that get added in the first place... this was in #114134.

Copy link
Member

Choose a reason for hiding this comment

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

We should probably check the other tests that reference that issue

tests/ui/consts/issue-94675.rs
1://@ known-bug: #103507

tests/ui/consts/const-block-const-bound.rs
1://@ known-bug: #103507

tests/ui/consts/promoted_const_call.rs
1://@ known-bug: #103507

tests/ui/impl-trait/normalize-tait-in-const.rs
1://@ known-bug: #103507

(not in this PR)

Copy link
Contributor

Choose a reason for hiding this comment

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

Just a sweeping change where almost everything const traits related was known bug as the impl was ripped out

@compiler-errors compiler-errors force-pushed the enforce-const-trait-syntactical branch from 55c1f36 to 2743df8 Compare January 14, 2025 19:12
@RalfJung
Copy link
Member

@bors r=oli-obk,RalfJung

@bors
Copy link
Collaborator

bors commented Jan 14, 2025

📌 Commit 2743df8 has been approved by oli-obk,RalfJung

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 14, 2025
@bors bors merged commit 11ac57a into rust-lang:master Jan 15, 2025
6 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Jan 15, 2025
@Randl Randl mentioned this pull request Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants