Skip to content

mgca: Register ConstArgHasType when normalizing projection consts#154853

Open
lapla-cogito wants to merge 2 commits intorust-lang:mainfrom
lapla-cogito:normlize_projecttion_const
Open

mgca: Register ConstArgHasType when normalizing projection consts#154853
lapla-cogito wants to merge 2 commits intorust-lang:mainfrom
lapla-cogito:normlize_projecttion_const

Conversation

@lapla-cogito
Copy link
Copy Markdown
Contributor

@lapla-cogito lapla-cogito commented Apr 5, 2026

fixes #152962

When a type const has a value whose type doesn't match the declared type, normalizing a reference to it can trigger CTFE, which in turn runs MIR validation on the const body. The MIR body contains a type mismatch, and since no prior error has been reported at that point, MIR validation fires a span_bug!. The existing ConstArgHasType check in wfcheck::check_type_const does catch this at the definition site, but due to query evaluation ordering, the normalization path can reach MIR validation before that check has run.

Fix this by registering a ConstArgHasType(ct, expected_ty) obligation/goal when normalizing projection consts (both trait and inherent), in both the old and new trait solvers. This ensures the type mismatch is reported as an error during normalization itself, which taints the MIR before validation runs.

The first commit fixes the original case reported in the issue. The second commit fixes a different ICE pattern reported within the issue (see #152962 (comment)).

r? BoxyUwU

@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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Apr 5, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 5, 2026

BoxyUwU is currently at their maximum review capacity.
They may take a while to respond.

@rustbot

This comment has been minimized.

@lapla-cogito lapla-cogito force-pushed the normlize_projecttion_const branch from b93677c to 672dfb8 Compare April 5, 2026 14:54
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 5, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

assoc_term_own_obligations(selcx, obligation, &mut nested);
Progress { term: term.instantiate(tcx, args), obligations: nested }
let instantiated_term: Term<'tcx> = term.instantiate(tcx, args);
if let Some(ct) = instantiated_term.as_const() {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Boxy and I discussed the fix approach and settled on registering a nested goal when normalizing type consts to verify that the normalized term has the type of the type const item. However, this implementation applies the same nested goal to all associated consts, not just type consts. This better expresses the invariant that the normalized result of a const should match its declared type (I think only type consts currently go through this path, but this also guards against future changes).

@rust-log-analyzer

This comment has been minimized.

@lapla-cogito lapla-cogito force-pushed the normlize_projecttion_const branch from 672dfb8 to 90e8301 Compare April 5, 2026 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ICE]: mgca: broken mir Failed subtyping u8 and usize

4 participants