Skip to content
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

Recover statics better #125555

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Noratrieb
Copy link
Member

@Noratrieb Noratrieb commented May 25, 2024

The const interner uses the body to intern static allocations.
This means that an allocation will be mutable if the resulting type contains interior mutability.
Const eval contains an assertion that types that are Freeze do not have mutable allocations, which was failing for cases where the type was a type error.

The first two commits fix this by forwarding the inferred type through type_of, while the last commit disables the assertion for remaining cases where type errors are involved.

Note that the erased region mapping is not fully correct, and causes this spurious error now:

struct X<'a>(&'a ());
impl<'a> X<'a> {
  const A: &'a () = &();
  const B = Self::A;
}
error: lifetime may not live long enough
 --> uwu.rs:4:13
  |
2 | impl<'a> X<'a> {
  |      -- lifetime `'a` defined here
3 |   const A: &'a () = &();
4 |   const B = Self::A;
  |             ^^^^^^^ using this value as a constant requires that `'a` must outlive `'static`

I deem that acceptable, that you may disagree.

note: the commit messages contain more information

fixes #124164

r? oli-obk

The const interner uses the body to intern static allocations.
This means that an allocation will be mutable if the resulting type
contains interior mutability.
Const eval contains an assertion that types that are `Freeze` do not
have mutable allocations, which was failing for cases where the type was
a type error.

An alternative fix for this would be to avoid the assertion in const
eval for type errors, allowing those to both be mutable and immutable.
This is implemented in a follow-up commit as well.
@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. labels May 25, 2024
@rustbot
Copy link
Collaborator

rustbot commented May 25, 2024

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

@Noratrieb Noratrieb force-pushed the recover-statics-better branch from 333296f to 4d97ffb Compare May 25, 2024 20:45
@rust-log-analyzer

This comment has been minimized.

@oli-obk
Copy link
Contributor

oli-obk commented May 25, 2024

Does the first commit actually have user-visible changes anywhere?

Noratrieb added 2 commits May 25, 2024 23:36
When the type is an inferred placeholder or an error, we used to infer
the type for an error message and then return `TyKind::Error`.
Now we return the proper type, which helps follow-up code do more
checking.

The primary motivation for this was fixing the const-eval mutability
assertion bug, because this commit forwards the inferred type
to that assertion, but it is also just nicer in general.
We do not know whether a type error was supposed to be mutable or not.
@Noratrieb
Copy link
Member Author

The only change in the test suite is that it adds that one cycle error.

@Noratrieb Noratrieb force-pushed the recover-statics-better branch from 4d97ffb to 44a576c Compare May 25, 2024 21:47
if let Some((_, alloc)) = ecx.memory.alloc_map.get(alloc_id) {
if let Some((_, alloc)) = ecx.memory.alloc_map.get(alloc_id)
// For type errors, we do not know whether they are supposed to be mutable or not.
&& !ty.references_error()
Copy link
Member

Choose a reason for hiding this comment

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

It is a bad sign that this check is needed. We should never even evaluate a static whose type references an error. So something still seems wrong here.

// Assoc consts can reference generic lifetimes from the parent generics, but treating them
// as static is unlikely to cause issues.
let ty = tcx.fold_regions(ty, |region, _| match region.kind() {
ty::ReErased => tcx.lifetimes.re_static,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
ty::ReErased => tcx.lifetimes.re_static,
ty::ReErased => ty::Region::new_error(tcx, guar),

would also fix this issue without requiring any of the other changes, because then the error type is tainted, without replacing any of the types. So you'd still get the suggestions, but const eval won't attempt to run.

@oli-obk
Copy link
Contributor

oli-obk commented Jun 28, 2024

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 28, 2024
@bors
Copy link
Contributor

bors commented Jul 10, 2024

☔ The latest upstream changes (presumably #127358) made this pull request unmergeable. Please resolve the merge conflicts.

@JohnCSimon
Copy link
Member

@Noratrieb ping from triage - can you post your status on this PR? This PR has not received an update in a few months.

@alex-semenyuk alex-semenyuk added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
Status: In Progress
8 participants