Skip to content

mGCA: Validate const literal against expected type#152001

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

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

Conversation

@reddevilmidzy
Copy link
Member

@reddevilmidzy reddevilmidzy commented Feb 2, 2026

close: #151625
close: #150983

also fix: #133966 (moved crashes test)

@rustbot rustbot 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
@rust-log-analyzer

This comment has been minimized.

@BoxyUwU
Copy link
Member

BoxyUwU commented Feb 4, 2026

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#152139 rust-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 :)

Fixes rust-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#152139 rust-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 :)

Fixes rust-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#152139 rust-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 :)

Fixes rust-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#152139 rust-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 :)

Fixes rust-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 :)

Fixes rust-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-log-analyzer

This comment has been minimized.

@reddevilmidzy
Copy link
Member Author

Haha, ICE occurs in other tests...

Copy link
Member Author

@reddevilmidzy reddevilmidzy left a comment

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

@rust-log-analyzer

This comment has been minimized.

Copy link
Member Author

@reddevilmidzy reddevilmidzy left a comment

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
Member Author

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?

Copy link
Member

@BoxyUwU BoxyUwU Feb 8, 2026

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

@reddevilmidzy
Copy link
Member Author

reddevilmidzy commented Feb 8, 2026

Could the following issues also be closed? 🤔

#119824
#133966
#136416

already closed
#94846
#106473
#116554

@rust-log-analyzer

This comment has been minimized.

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed tests related to generic_const_args that were causing crashes.

Comment on lines +14 to +25
foo::<{ Foo { field: -1_usize } }>();
//~^ ERROR: type annotations needed for the literal
foo::<{ Foo { field: { -1_usize } } }>();
//~^ ERROR: complex const arguments must be placed inside of a `const` block
foo::<{ Foo { field: -true } }>();
//~^ ERROR: the constant `true` is not of type `isize`
foo::<{ Foo { field: { -true } } }>();
//~^ ERROR: complex const arguments must be placed inside of a `const` block
foo::<{ Foo { field: -"<3" } }>();
//~^ ERROR: the constant `"<3"` is not of type `isize`
foo::<{ Foo { field: { -"<3" } } }>();
//~^ ERROR: complex const arguments must be placed inside of a `const` block
Copy link
Member Author

Choose a reason for hiding this comment

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

Added the test mentioned in #152139.

@reddevilmidzy reddevilmidzy marked this pull request as ready for review February 11, 2026 06:44
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 11, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 11, 2026

HIR ty lowering was modified

cc @fmease

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr

Some changes occurred in match checking

cc @Nadrieril

Some changes occurred in rustc_ty_utils::consts.rs

cc @BoxyUwU

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Feb 11, 2026
pub neg: bool,
}

pub fn const_lit_matches_ty<'tcx>(
Copy link
Member

Choose a reason for hiding this comment

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

This does not seem like a great place for this function -- it doesn't really have anything to do with the interpreter, does it?

I guess you put it next to LitToConstInput, but that type also doesn't belong here either IMO. It has very little to do with everything else in this module.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, yes, I placed it here to place it in the same place as LitToConstInput.
I'll look for something more meaningful! LitToConstInput too.

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 created rustc_middle/src/ty/consts/lit.rs and moved it there. Does it look okay?

4f93ae7

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 ok to me, we might wind up getting more logic for this as mGCA progresses so seems nice to have a module 🤔

reddevilmidzy and others added 3 commits February 14, 2026 04:12
Co-authored-by: Boxy <rust@boxyuwu.dev>
Relocate LitToConstInput and const_lit_matches_ty from mir::interpret
to ty::consts::lit
foo::<{ Foo { field: -1_usize } }>();
//~^ ERROR: type annotations needed for the literal
foo::<{ Foo { field: { -1_usize } } }>();
//~^ ERROR: complex const arguments must be placed inside of a `const` block
Copy link
Member

Choose a reason for hiding this comment

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

This error message implies we handle negated literals incorrectly during ast lowering I think. lower_expr_to_const_arg_direct is presumably not handling { -1_usize } even though it should (I think probably there is no reason for the arm for blocks to match on what kind of expr is being wrapped).

If you'd be interested, once this PR lands would you be interested in fixing that and #152246 in a followup PR?

Copy link
Member

Choose a reason for hiding this comment

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

I think we probably also handle fn foo<const F: &str>() { foo::<{ -"hi" }>(); } wrong (i.e. we accept it?). I think this is also a problem with ast lowering and we should be checking there that the literal is an integer. Could also do that in a followup PR if you like?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I'd be happy to work on that in a follow-up PR! I'll take a closer look at the AST lowering behavior you mentioned.

@BoxyUwU
Copy link
Member

BoxyUwU commented Feb 14, 2026

@bors r+

@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 14, 2026

📌 Commit 01d48c6 has been approved by BoxyUwU

It is now in the queue for this repository.

@rust-bors rust-bors bot 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 Feb 14, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 14, 2026
mGCA: Validate const literal against expected type

close: rust-lang#151625
close: rust-lang#150983

also fix: rust-lang#133966 (moved crashes test)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 14, 2026
mGCA: Validate const literal against expected type

close: rust-lang#151625
close: rust-lang#150983

also fix: rust-lang#133966 (moved crashes test)
rust-bors bot pushed a commit that referenced this pull request Feb 14, 2026
…uwer

Rollup of 7 pull requests

Successful merges:

 - #152001 (mGCA: Validate const literal against expected type)
 - #152120 (Don't ICE on layout error in vtable computation)
 - #152512 (core: Implement feature `float_exact_integer_constants`)
 - #152531 (`proc_macro::bridge`: simplify `ExecutionStrategy` and `DispatcherTrait`)
 - #152577 (Port #[rustc_proc_macro_decls] to the new attribute parser.)
 - #152590 (DepGraphQuery: correctly skip adding edges with not-yet-added nodes)
 - #152612 (Rename `inline_fluent!` to `msg!`)
rust-bors bot 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-bors bot merged commit 5ae6bb3 into rust-lang:main Feb 14, 2026
11 checks passed
@rustbot rustbot 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: #151625
close: #150983

also fix: #133966 (moved crashes test)
@reddevilmidzy reddevilmidzy deleted the mgca-i branch February 15, 2026 00:41
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.

Projects

None yet

5 participants