Skip to content

mGCA: Validate const literal against expected type - #152001

Merged
rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
reddevilmidzy:mgca-i
Feb 14, 2026
Merged

mGCA: Validate const literal against expected type#152001
rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
reddevilmidzy:mgca-i

Conversation

@reddevilmidzy

@reddevilmidzyreddevilmidzy commented Feb 2, 2026

Copy link
Copy Markdown
Member

close: #151625
close: #150983

also fix: #133966 (moved crashes test)

@rustbotrustbot added 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. labels Feb 2, 2026
Comment threadtests/ui/const-generics/mgca/literal-type-mismatch.rs Outdated
@rust-log-analyzer

This comment has been minimized.

Comment threadcompiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs Outdated
@BoxyUwU

Copy link
Copy Markdown
Member

very cool :) thanks for working on this

@rust-bors

This comment has been minimized.

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 6, 2026
…=oli-obk
Dont strip const blocks in array lengths
r? oli-obk
mGCA now handles const blocks by *always* handling them during `lower_expr_to_const_arg_direct` instead of *sometimes* stripping them out at parse time. This is just generally a lot clearer/nicer but also means parsing isn't lossy which is just straight up wrong.
We now use `MgcaDisambiguation::Direct` for const blocks because we "directly" represent a const block as `hir::ConstArgKind::Anon` :> The only time that an anon const for const generics uses `MgcaDisambiguation::AnonConst` is for unbraced literals.
Once we properly support literals in `hir::ConstArgKind` (see rust-lang#152139rust-lang#152001) then `MgcaDisambiguation` can be renamed to `AnonConstKind` with `TypeSystem` and `NonTypeSystem` variants. We can also get rid of `mgca_direct_lit_hack`. I expect this to be a very nice cleanup :)
Fixesrust-lang/rustfmt#6788
The diff relating to passing spans around is to avoid a bunch of mGCA diagnostics changing from `const {}` to `{}`. I'm not entirely sure why this was happening.
cc @rust-lang/rustfmt
How do I run the tests in the rustfmt repo from here? `x test rustfmt` only seems to run like 100 tests and doesn't result in a `target/issue-6788.rs` getting created. I've verified locally that this formats correctly though
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 6, 2026
…=oli-obk
Dont strip const blocks in array lengths
r? oli-obk
mGCA now handles const blocks by *always* handling them during `lower_expr_to_const_arg_direct` instead of *sometimes* stripping them out at parse time. This is just generally a lot clearer/nicer but also means parsing isn't lossy which is just straight up wrong.
We now use `MgcaDisambiguation::Direct` for const blocks because we "directly" represent a const block as `hir::ConstArgKind::Anon` :> The only time that an anon const for const generics uses `MgcaDisambiguation::AnonConst` is for unbraced literals.
Once we properly support literals in `hir::ConstArgKind` (see rust-lang#152139rust-lang#152001) then `MgcaDisambiguation` can be renamed to `AnonConstKind` with `TypeSystem` and `NonTypeSystem` variants. We can also get rid of `mgca_direct_lit_hack`. I expect this to be a very nice cleanup :)
Fixesrust-lang/rustfmt#6788
The diff relating to passing spans around is to avoid a bunch of mGCA diagnostics changing from `const {}` to `{}`. I'm not entirely sure why this was happening.
cc @rust-lang/rustfmt
How do I run the tests in the rustfmt repo from here? `x test rustfmt` only seems to run like 100 tests and doesn't result in a `target/issue-6788.rs` getting created. I've verified locally that this formats correctly though
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 6, 2026
…=oli-obk
Dont strip const blocks in array lengths
r? oli-obk
mGCA now handles const blocks by *always* handling them during `lower_expr_to_const_arg_direct` instead of *sometimes* stripping them out at parse time. This is just generally a lot clearer/nicer but also means parsing isn't lossy which is just straight up wrong.
We now use `MgcaDisambiguation::Direct` for const blocks because we "directly" represent a const block as `hir::ConstArgKind::Anon` :> The only time that an anon const for const generics uses `MgcaDisambiguation::AnonConst` is for unbraced literals.
Once we properly support literals in `hir::ConstArgKind` (see rust-lang#152139rust-lang#152001) then `MgcaDisambiguation` can be renamed to `AnonConstKind` with `TypeSystem` and `NonTypeSystem` variants. We can also get rid of `mgca_direct_lit_hack`. I expect this to be a very nice cleanup :)
Fixesrust-lang/rustfmt#6788
The diff relating to passing spans around is to avoid a bunch of mGCA diagnostics changing from `const {}` to `{}`. I'm not entirely sure why this was happening.
cc @rust-lang/rustfmt
How do I run the tests in the rustfmt repo from here? `x test rustfmt` only seems to run like 100 tests and doesn't result in a `target/issue-6788.rs` getting created. I've verified locally that this formats correctly though
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 7, 2026
…=oli-obk
Dont strip const blocks in array lengths
r? oli-obk
mGCA now handles const blocks by *always* handling them during `lower_expr_to_const_arg_direct` instead of *sometimes* stripping them out at parse time. This is just generally a lot clearer/nicer but also means parsing isn't lossy which is just straight up wrong.
We now use `MgcaDisambiguation::Direct` for const blocks because we "directly" represent a const block as `hir::ConstArgKind::Anon` :> The only time that an anon const for const generics uses `MgcaDisambiguation::AnonConst` is for unbraced literals.
Once we properly support literals in `hir::ConstArgKind` (see rust-lang#152139rust-lang#152001) then `MgcaDisambiguation` can be renamed to `AnonConstKind` with `TypeSystem` and `NonTypeSystem` variants. We can also get rid of `mgca_direct_lit_hack`. I expect this to be a very nice cleanup :)
Fixesrust-lang/rustfmt#6788
The diff relating to passing spans around is to avoid a bunch of mGCA diagnostics changing from `const {}` to `{}`. I'm not entirely sure why this was happening.
cc @rust-lang/rustfmt
How do I run the tests in the rustfmt repo from here? `x test rustfmt` only seems to run like 100 tests and doesn't result in a `target/issue-6788.rs` getting created. I've verified locally that this formats correctly though
rust-timer added a commit that referenced this pull request Feb 7, 2026
Rollup merge of #152234 - BoxyUwU:dont_strip_const_blocks, r=oli-obk
Dont strip const blocks in array lengths
r? oli-obk
mGCA now handles const blocks by *always* handling them during `lower_expr_to_const_arg_direct` instead of *sometimes* stripping them out at parse time. This is just generally a lot clearer/nicer but also means parsing isn't lossy which is just straight up wrong.
We now use `MgcaDisambiguation::Direct` for const blocks because we "directly" represent a const block as `hir::ConstArgKind::Anon` :> The only time that an anon const for const generics uses `MgcaDisambiguation::AnonConst` is for unbraced literals.
Once we properly support literals in `hir::ConstArgKind` (see #152139#152001) then `MgcaDisambiguation` can be renamed to `AnonConstKind` with `TypeSystem` and `NonTypeSystem` variants. We can also get rid of `mgca_direct_lit_hack`. I expect this to be a very nice cleanup :)
Fixesrust-lang/rustfmt#6788
The diff relating to passing spans around is to avoid a bunch of mGCA diagnostics changing from `const {}` to `{}`. I'm not entirely sure why this was happening.
cc @rust-lang/rustfmt
How do I run the tests in the rustfmt repo from here? `x test rustfmt` only seems to run like 100 tests and doesn't result in a `target/issue-6788.rs` getting created. I've verified locally that this formats correctly though
Comment threadcompiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs Outdated
Comment threadcompiler/rustc_mir_build/src/thir/constant.rs Outdated
@rust-log-analyzer

This comment has been minimized.

@reddevilmidzy

Copy link
Copy Markdown
MemberAuthor

Haha, ICE occurs in other tests...

@reddevilmidzyreddevilmidzy left a comment

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Thanks for your quick review. 😊

View changes since this review

Comment threadcompiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs Outdated
Comment threadtests/ui/const-generics/mgca/type_const-mismatched-types.rs Outdated
@rust-log-analyzer

This comment has been minimized.

@reddevilmidzyreddevilmidzy left a comment

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

In ten crash tests, the ICE disappeared; however, in tests/crashes/117460.rs, tests/crashes/114212-2.rs and tests/crashes/114212.rs, adding the main function caused the ICE to occur again.

View changes since this review

@@ -0,0 +1,23 @@
//! Regression test for <https://github.com/rust-lang/rust/issues/116554>

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

This issue was closed; is there a need to add further testing?

@BoxyUwUBoxyUwUFeb 8, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

unsure, I will look over all the GCE tests (outside of this PR) at some point soon and see which ones are worth keeping

Comment threadtests/crashes/101036.rs
@reddevilmidzy

reddevilmidzy commented Feb 8, 2026

Copy link
Copy Markdown
MemberAuthor

Could the following issues also be closed? 🤔

#119824
#133966
#136416

already closed
#94846
#106473
#116554

@rust-log-analyzer

This comment has been minimized.

rust-borsBot pushed a commit that referenced this pull request Feb 14, 2026
…uwer
Rollup of 8 pull requests
Successful merges:
- #152618 (stdarch subtree update)
- #152001 (mGCA: Validate const literal against expected type)
- #152120 (Don't ICE on layout error in vtable computation)
- #152531 (`proc_macro::bridge`: simplify `ExecutionStrategy` and `DispatcherTrait`)
- #152577 (Port #[rustc_proc_macro_decls] to the new attribute parser.)
- #152570 (Port #[rustc_test_marker] to the attribute parser)
- #152590 (DepGraphQuery: correctly skip adding edges with not-yet-added nodes)
- #152612 (Rename `inline_fluent!` to `msg!`)
@rust-bors
rust-borsBot merged commit 5ae6bb3 into rust-lang:mainFeb 14, 2026
11 checks passed
@rustbotrustbot added this to the 1.95.0 milestone Feb 14, 2026
rust-timer added a commit that referenced this pull request Feb 14, 2026
Rollup merge of #152001 - reddevilmidzy:mgca-i, r=BoxyUwU
mGCA: Validate const literal against expected type
close: #151625close: #150983
also fix: #133966 (moved crashes test)
@reddevilmidzy
reddevilmidzy deleted the mgca-i branch February 15, 2026 00:41
github-actionsBot pushed a commit to rust-lang/stdarch that referenced this pull request Feb 16, 2026
…uwer
Rollup of 8 pull requests
Successful merges:
- rust-lang/rust#152618 (stdarch subtree update)
- rust-lang/rust#152001 (mGCA: Validate const literal against expected type)
- rust-lang/rust#152120 (Don't ICE on layout error in vtable computation)
- rust-lang/rust#152531 (`proc_macro::bridge`: simplify `ExecutionStrategy` and `DispatcherTrait`)
- rust-lang/rust#152577 (Port #[rustc_proc_macro_decls] to the new attribute parser.)
- rust-lang/rust#152570 (Port #[rustc_test_marker] to the attribute parser)
- rust-lang/rust#152590 (DepGraphQuery: correctly skip adding edges with not-yet-added nodes)
- rust-lang/rust#152612 (Rename `inline_fluent!` to `msg!`)
github-actionsBot pushed a commit to rust-lang/rust-analyzer that referenced this pull request Feb 16, 2026
…uwer
Rollup of 8 pull requests
Successful merges:
- rust-lang/rust#152618 (stdarch subtree update)
- rust-lang/rust#152001 (mGCA: Validate const literal against expected type)
- rust-lang/rust#152120 (Don't ICE on layout error in vtable computation)
- rust-lang/rust#152531 (`proc_macro::bridge`: simplify `ExecutionStrategy` and `DispatcherTrait`)
- rust-lang/rust#152577 (Port #[rustc_proc_macro_decls] to the new attribute parser.)
- rust-lang/rust#152570 (Port #[rustc_test_marker] to the attribute parser)
- rust-lang/rust#152590 (DepGraphQuery: correctly skip adding edges with not-yet-added nodes)
- rust-lang/rust#152612 (Rename `inline_fluent!` to `msg!`)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 25, 2026
mGCA: Lower negated literals directly and reject non-integer negations
follow up rust-lang#152001resolve: rust-lang#152246
r? BoxyUwU
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 25, 2026
mGCA: Lower negated literals directly and reject non-integer negations
follow up rust-lang#152001resolve: rust-lang#152246
r? BoxyUwU
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 25, 2026
mGCA: Lower negated literals directly and reject non-integer negations
follow up rust-lang#152001resolve: rust-lang#152246
r? BoxyUwU
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 25, 2026
mGCA: Lower negated literals directly and reject non-integer negations
follow up rust-lang#152001resolve: rust-lang#152246
r? BoxyUwU
jhpratt added a commit to jhpratt/rust that referenced this pull request Feb 26, 2026
mGCA: Lower negated literals directly and reject non-integer negations
follow up rust-lang#152001resolve: rust-lang#152246
r? BoxyUwU
rust-timer added a commit that referenced this pull request Feb 26, 2026
Rollup merge of #153075 - reddevilmidzy:mgca-neg, r=BoxyUwU
mGCA: Lower negated literals directly and reject non-integer negations
follow up #152001resolve: #152246
r? BoxyUwU
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 28, 2026
Fix ICE in `try_to_raw_bytes` when array elements have mismatched
close: rust-lang#152683
After rust-lang#152001, suffixed integer literals preserve their own type during const lowering, so `try_to_raw_bytes` could call `.to_u8()` on a scalar with size > 1, causing an ICE. Fix by using `try_to_bits(Size::from_bytes(1)).ok()` instead.
r? BoxyUwU
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 28, 2026
Fix ICE in `try_to_raw_bytes` when array elements have mismatched
close: rust-lang#152683
After rust-lang#152001, suffixed integer literals preserve their own type during const lowering, so `try_to_raw_bytes` could call `.to_u8()` on a scalar with size > 1, causing an ICE. Fix by using `try_to_bits(Size::from_bytes(1)).ok()` instead.
r? BoxyUwU
rust-timer added a commit that referenced this pull request Mar 1, 2026
Rollup merge of #152794 - reddevilmidzy:mgca-print, r=BoxyUwU
Fix ICE in `try_to_raw_bytes` when array elements have mismatched
close: #152683
After #152001, suffixed integer literals preserve their own type during const lowering, so `try_to_raw_bytes` could call `.to_u8()` on a scalar with size > 1, causing an ICE. Fix by using `try_to_bits(Size::from_bytes(1)).ok()` instead.
r? BoxyUwU
makai410 pushed a commit to makai410/rustc_public that referenced this pull request Mar 19, 2026
Fix ICE in `try_to_raw_bytes` when array elements have mismatched
close: rust-lang/rust#152683
After rust-lang/rust#152001, suffixed integer literals preserve their own type during const lowering, so `try_to_raw_bytes` could call `.to_u8()` on a scalar with size > 1, causing an ICE. Fix by using `try_to_bits(Size::from_bytes(1)).ok()` instead.
r? BoxyUwU
makai410 pushed a commit to makai410/rustc_public that referenced this pull request Mar 19, 2026
Fix ICE in `try_to_raw_bytes` when array elements have mismatched
close: rust-lang/rust#152683
After rust-lang/rust#152001, suffixed integer literals preserve their own type during const lowering, so `try_to_raw_bytes` could call `.to_u8()` on a scalar with size > 1, causing an ICE. Fix by using `try_to_bits(Size::from_bytes(1)).ok()` instead.
r? BoxyUwU
makai410 pushed a commit to makai410/rustc_public that referenced this pull request Mar 27, 2026
Fix ICE in `try_to_raw_bytes` when array elements have mismatched
close: rust-lang/rust#152683
After rust-lang/rust#152001, suffixed integer literals preserve their own type during const lowering, so `try_to_raw_bytes` could call `.to_u8()` on a scalar with size > 1, causing an ICE. Fix by using `try_to_bits(Size::from_bytes(1)).ok()` instead.
r? BoxyUwU
match tcx.at(span).lit_to_const(input) {
Some(value) => ty::Const::new_value(tcx, value.valtree, value.ty),
None => {
let e = tcx.dcx().span_err(span, "type annotations needed for the literal");

@oli-obkoli-obkMay 12, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is the cause of the ICEs you're seeing I think. You need to do self.dcx() to make sure that the lowering context gets tainted

View changes since the review

ghaaj pushed a commit to ghaaj/polygrammar that referenced this pull request Aug 7, 2026
…uwer
Rollup of 8 pull requests
Successful merges:
- rust-lang/rust#152618 (stdarch subtree update)
- rust-lang/rust#152001 (mGCA: Validate const literal against expected type)
- rust-lang/rust#152120 (Don't ICE on layout error in vtable computation)
- rust-lang/rust#152531 (`proc_macro::bridge`: simplify `ExecutionStrategy` and `DispatcherTrait`)
- rust-lang/rust#152577 (Port #[rustc_proc_macro_decls] to the new attribute parser.)
- rust-lang/rust#152570 (Port #[rustc_test_marker] to the attribute parser)
- rust-lang/rust#152590 (DepGraphQuery: correctly skip adding edges with not-yet-added nodes)
- rust-lang/rust#152612 (Rename `inline_fluent!` to `msg!`)
flip1995 pushed a commit to flip1995/rust-clippy that referenced this pull request Aug 17, 2026
…uwer
Rollup of 8 pull requests
Successful merges:
- rust-lang/rust#152618 (stdarch subtree update)
- rust-lang/rust#152001 (mGCA: Validate const literal against expected type)
- rust-lang/rust#152120 (Don't ICE on layout error in vtable computation)
- rust-lang/rust#152531 (`proc_macro::bridge`: simplify `ExecutionStrategy` and `DispatcherTrait`)
- rust-lang/rust#152577 (Port #[rustc_proc_macro_decls] to the new attribute parser.)
- rust-lang/rust#152570 (Port #[rustc_test_marker] to the attribute parser)
- rust-lang/rust#152590 (DepGraphQuery: correctly skip adding edges with not-yet-added nodes)
- rust-lang/rust#152612 (Rename `inline_fluent!` to `msg!`)
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

6 participants

@reddevilmidzy@rust-log-analyzer@BoxyUwU@rustbot@RalfJung@oli-obk