Skip to content

mGCA: Support directly represented negated literals#152139

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
khyperia:mgca-negative-literals
Feb 6, 2026
Merged

mGCA: Support directly represented negated literals#152139
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
khyperia:mgca-negative-literals

Conversation

@khyperia
Copy link
Contributor

@khyperia khyperia commented Feb 4, 2026

fixes #152123

PatExprKind already awkwardly tacks on a negated: bool for the same purpose:

Lit {
lit: Lit,
// FIXME: move this into `Lit` and handle negated literal expressions
// once instead of matching on unop neg expressions everywhere.
negated: bool,
},

perhaps one day we should indeed do that FIXME...

r? @BoxyUwU

@rustbot
Copy link
Collaborator

rustbot commented Feb 4, 2026

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

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-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Feb 4, 2026
@BoxyUwU
Copy link
Member

BoxyUwU commented Feb 5, 2026

can you add tests for "nonsensical" negated literals, e.g. -true { -true } -":3" { ":3" }, ah and I guess also -1_usize and { -1_usize }.

@khyperia
Copy link
Contributor Author

khyperia commented Feb 6, 2026

can you add tests for "nonsensical" negated literals, e.g. -true { -true } -":3" { ":3" }, ah and I guess also -1_usize and { -1_usize }.

aha, finally stumbled across #152001 - I can't add tests for things like -1_usize until that PR (or another fix for #151625) is merged, because the place an error should be reported for -1_usize is inside that PR's code.

@khyperia
Copy link
Contributor Author

khyperia commented Feb 6, 2026

perhaps one day we should indeed do that FIXME...

discussed in the Zulip: https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/Move.20.60negated.3A.20bool.60.20into.20Lit/with/572102179 relevant quote:

oli: Unfortunately, while I still think this would be better, my previous attempt did not garner much support and some opposition. rust-lang/compiler-team#843
oli: If you want you can just nuke the FIXME in your PR entirely

@khyperia khyperia force-pushed the mgca-negative-literals branch from 7969389 to 54a9be4 Compare February 6, 2026 13:07
ConstArg {
hir_id: self.lower_node_id(expr.id),
kind: hir::ConstArgKind::Literal { lit: literal.node, negated: true },
span,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

note to reviewers: in addition to nuking the FIXME in my last force-push, I changed the span for this ConstArg to include the negative sign in the span (before the push, it was excluded, just the inner_expr's span). Realized it was excluded when trying to add errors for -1_usize.

(the diff in the force push is rather unclear what's happening so I figured I'd point it out)

@BoxyUwU
Copy link
Member

BoxyUwU commented Feb 6, 2026

I can't add tests for things like -1_usize until that PR is merged

oh, right yeah :3

@reddevilmidzy
Copy link
Member

can you add tests for "nonsensical" negated literals, e.g. -true { -true } -":3" { ":3" }, ah and I guess also -1_usize and { -1_usize }.

aha, finally stumbled across #152001 - I can't add tests for things like -1_usize until that PR (or another fix for #151625) is merged, because the place an error should be reported for -1_usize is inside that PR's code.

Oh, sorry for the delay. I'll work on it over the weekend.

@BoxyUwU
Copy link
Member

BoxyUwU commented Feb 6, 2026

@bors r+

I'm happy to land this with it being a little broken we can always fix it going forwards :)

@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 6, 2026

📌 Commit 54a9be4 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 6, 2026
rust-bors bot pushed a commit that referenced this pull request Feb 6, 2026
…uwer

Rollup of 3 pull requests

Successful merges:

 - #152129 (MGCA: require #[type_const] on free consts too)
 - #152139 (mGCA: Support directly represented negated literals)
 - #152189 (Convert to inline diagnostics in `rustc_passes`)
@rust-bors rust-bors bot merged commit 30bbeae into rust-lang:main Feb 6, 2026
11 checks passed
@rustbot rustbot added this to the 1.95.0 milestone Feb 6, 2026
rust-timer added a commit that referenced this pull request Feb 6, 2026
Rollup merge of #152139 - khyperia:mgca-negative-literals, r=BoxyUwU

mGCA: Support directly represented negated literals

fixes #152123

PatExprKind already awkwardly tacks on a `negated: bool` for the same purpose:

https://github.com/rust-lang/rust/blob/8bccf1224deab49b54694c9090e577bfe90a94e6/compiler/rustc_hir/src/hir.rs#L1954-L1959

perhaps one day we should indeed do that FIXME...

r? @BoxyUwU
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
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-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mGCA: Support directly represented negated literals

4 participants