Skip to content

Comments

Fix ICE when a suffixed literal's type doesn't match the expected const arg type#152906

Open
lapla-cogito wants to merge 1 commit intorust-lang:mainfrom
lapla-cogito:issue_152653
Open

Fix ICE when a suffixed literal's type doesn't match the expected const arg type#152906
lapla-cogito wants to merge 1 commit intorust-lang:mainfrom
lapla-cogito:issue_152653

Conversation

@lapla-cogito
Copy link
Contributor

@lapla-cogito lapla-cogito commented Feb 20, 2026

close #152653

try_lower_anon_const_lit() is a fast path that directly converts literal expressions into ty::Const::Value for const arg contexts, bypassing typeck. It did not verify that the type produced by lit_to_const() matched the expected type, allowing a mistyped constant (e.g. i32 where usize was expected) to reach MIR and cause an ICE during const propagation. Therefore added a type-equality check on the lit_to_const() result so that mismatches fall back to the unevaluated-const path, where regular typeck reports the error.

@rustbot
Copy link
Collaborator

rustbot commented Feb 20, 2026

HIR ty lowering was modified

cc @fmease

@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 Feb 20, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 20, 2026

r? @wesleywiser

rustbot has assigned @wesleywiser.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 68 candidates
  • Random selection from 15 candidates

@@ -0,0 +1,10 @@
//@ compile-flags: --emit=link
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was required to perform MIR analysis.

Comment on lines -1 to -18
error: the constant `5` is not of type `u32`
--> $DIR/type_const-mismatched-types.rs:4:1
|
LL | type const FREE: u32 = 5_usize;
| ^^^^^^^^^^^^^^^^^^^^ expected `u32`, found `usize`

error: the constant `5` is not of type `isize`
--> $DIR/type_const-mismatched-types.rs:8:1
|
LL | type const FREE2: isize = FREE;
| ^^^^^^^^^^^^^^^^^^^^^^^ expected `isize`, found `usize`

error[E0308]: mismatched types
--> $DIR/type_const-mismatched-types.rs:16:27
|
LL | type const N: usize = false;
| ^^^^^ expected `usize`, found `bool`

Copy link
Contributor Author

@lapla-cogito lapla-cogito Feb 20, 2026

Choose a reason for hiding this comment

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

This fix, as mentioned above, fixes the ICE while simultaneously reverting the seemingly redundant error output introduced in #152001 (in fact, this ICE was identified as originating from this PR in the original issue).

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ICE]: attempting to project to field at offset 0 with size 8 into immediate

3 participants