Skip to content

do generic_const_args Const::Ty hack only on projection consts - #162937

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
khyperia:gca-fix-unreachable
Sep 24, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
khyperia:gca-fix-unreachable

Conversation

@khyperia

@khyperia khyperia commented Sep 18, 2026 •

Copy link
Copy Markdown
Member

Under generic_const_args, we currently do a bit of a hack to lower all consts to Const::Ty, because under generic_const_args, projection consts not marked with #[rustc_always_gca] could be impl'd by a const with a gca! rhs, and Const::Unevaluated does not support direct consts.

Instead, only lower a const to Const::Ty if:

  • it is a direct const (under min_generic_const_args), OR
  • it is a projection const (under generic_const_args)

This excludes all anon consts, and regular (not direct) free and inherent consts from being lowered as Const::Ty under full GCA. It does not fix the underlying issue that lowering a potentially regular projection const to Const::Ty is incorrect.

Fixes #162923

Introduced in #162760

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. labels Sep 18, 2026
@rustbot

rustbot commented Sep 18, 2026

Copy link
Copy Markdown
Collaborator

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

@khyperia

khyperia commented Sep 18, 2026 •

Copy link
Copy Markdown
Member Author

wait no I haven't had my coffee yet what am I doing, this isn't the right fix, one moment... (I mean, it worked, but, yeah, actual fix pushed now)

if tcx.features().generic_const_args()
|| matches!(def_kind, DefKind::Const | DefKind::AssocConst)
&& tcx.is_direct_const(def_id)
if matches!(def_kind, DefKind::Const | DefKind::AssocConst)

@BoxyUwU BoxyUwU Sep 18, 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.

haha when I was reviewing that PR I thought about how I hate precedence on these things and can never tell where the parens go 😆

View changes since the review

@BoxyUwU

BoxyUwU commented Sep 18, 2026

Copy link
Copy Markdown
Member

what does the THIR look like for this test, where does a NamedConst come from with a defid of an anon 🤔

@khyperia

khyperia commented Sep 18, 2026 •

Copy link
Copy Markdown
Member Author

what does the THIR look like for this test, where does a NamedConst come from with a defid of an anon 🤔

comes from here

let kind = ExprKind::NamedConst { def_id: did, args, user_ty: None };

for reference this is what the code looks like:

enum T<const N: u8 = { T::<0>::B as u8 }> {
    A = 2,
    B,
}

the anon const for the default in enum T<const N: u8 = { this one here }> is called T::{constant#0}. the anon const for the A = { this one here }, is called T::A::{constant#0}. lowering a reference to T::<0>::B takes the discr_did (T::A::{constant#0}) and adds discr_offset (1) to it to get T::<0>::B. this THIR expr is then used, casted to u8, and that's the body of the anon const of the default generic param.

T::{constant#0} = (T::A::{constant#0} + 1) as u8;

@bit-aloo bit-aloo left a comment •

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.

@@ -80,9 +80,8 @@ pub(crate) fn as_constant_inner<'tcx>(

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.

Should we mention about anon const exception here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

when considering other wordings for this, I found myself unable write a wording to justify not doing the exception for just projection consts, rather than all const items. So I rewrote this to only do the exception for just projection consts.

Comment thread compiler/rustc_mir_build/src/builder/expr/as_constant.rs
Comment thread compiler/rustc_mir_build/src/builder/expr/as_constant.rs
@BoxyUwU

BoxyUwU commented Sep 23, 2026

Copy link
Copy Markdown
Member

can you write a different PR title and description :3

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 23, 2026
@khyperia khyperia changed the title generic_const_args: fix unreachable do generic_const_args Const::Ty hack only on projection consts Sep 24, 2026
@khyperia

Copy link
Copy Markdown
Member Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 24, 2026
@BoxyUwU

BoxyUwU commented Sep 24, 2026

Copy link
Copy Markdown
Member

@bors r+ rollup

@rust-bors

rust-bors Bot commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

📌 Commit acc7501 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 Sep 24, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 24, 2026
…xyUwU

do generic_const_args Const::Ty hack only on projection consts

Under `generic_const_args`, we currently do a bit of a hack to lower all consts to `Const::Ty`, because under `generic_const_args`, projection consts not marked with `#[rustc_always_gca]` could be `impl`'d by a const with a `gca!` rhs, and `Const::Unevaluated` does not support direct consts.

Instead, only lower a const to `Const::Ty` if:

- it is a direct const (under `min_generic_const_args`), OR
- it is a projection const (under `generic_const_args`)

This excludes all anon consts, and regular (not direct) free and inherent consts from being lowered as `Const::Ty` under full GCA. It does not fix the underlying issue that lowering a potentially regular projection const to `Const::Ty` is incorrect.

Fixes rust-lang#162923

Introduced in rust-lang#162760

r? @BoxyUwU
rust-bors Bot pushed a commit that referenced this pull request Sep 24, 2026
…uwer

Rollup of 7 pull requests

Successful merges:

 - #163218 (stdarch subtree update)
 - #159287 (Build a new incr comp session dir from scratch every time)
 - #163222 (Enable EII tests for cg_gcc)
 - #163251 (cg_gcc subtree sync 2026-09-24)
 - #162595 (avoid accessing uninferred closure upvars in diagnostics)
 - #162937 (do generic_const_args Const::Ty hack only on projection consts)
 - #163238 (Fix incorrect use of await in parser suggestion)
@rust-bors
rust-bors Bot merged commit a9ddedf into rust-lang:main Sep 24, 2026
13 checks passed
@rustbot rustbot added this to the 1.100.0 milestone Sep 24, 2026
rust-bors Bot pushed a commit that referenced this pull request Sep 24, 2026
Rollup merge of #162937 - khyperia:gca-fix-unreachable, r=BoxyUwU

do generic_const_args Const::Ty hack only on projection consts

Under `generic_const_args`, we currently do a bit of a hack to lower all consts to `Const::Ty`, because under `generic_const_args`, projection consts not marked with `#[rustc_always_gca]` could be `impl`'d by a const with a `gca!` rhs, and `Const::Unevaluated` does not support direct consts.

Instead, only lower a const to `Const::Ty` if:

- it is a direct const (under `min_generic_const_args`), OR
- it is a projection const (under `generic_const_args`)

This excludes all anon consts, and regular (not direct) free and inherent consts from being lowered as `Const::Ty` under full GCA. It does not fix the underlying issue that lowering a potentially regular projection const to `Const::Ty` is incorrect.

Fixes #162923

Introduced in #162760

r? @BoxyUwU
pull Bot pushed a commit to xtqqczze/rust-lang-miri that referenced this pull request Sep 25, 2026
…uwer

Rollup of 7 pull requests

Successful merges:

 - rust-lang/rust#163218 (stdarch subtree update)
 - rust-lang/rust#159287 (Build a new incr comp session dir from scratch every time)
 - rust-lang/rust#163222 (Enable EII tests for cg_gcc)
 - rust-lang/rust#163251 (cg_gcc subtree sync 2026-09-24)
 - rust-lang/rust#162595 (avoid accessing uninferred closure upvars in diagnostics)
 - rust-lang/rust#162937 (do generic_const_args Const::Ty hack only on projection consts)
 - rust-lang/rust#163238 (Fix incorrect use of await in parser suggestion)
@khyperia
khyperia deleted the gca-fix-unreachable branch September 27, 2026 16:11
bjorn3 pushed a commit to rust-lang/rustc_codegen_cranelift that referenced this pull request Sep 28, 2026
…uwer

Rollup of 7 pull requests

Successful merges:

 - rust-lang/rust#163218 (stdarch subtree update)
 - rust-lang/rust#159287 (Build a new incr comp session dir from scratch every time)
 - rust-lang/rust#163222 (Enable EII tests for cg_gcc)
 - rust-lang/rust#163251 (cg_gcc subtree sync 2026-09-24)
 - rust-lang/rust#162595 (avoid accessing uninferred closure upvars in diagnostics)
 - rust-lang/rust#162937 (do generic_const_args Const::Ty hack only on projection consts)
 - rust-lang/rust#163238 (Fix incorrect use of await in parser suggestion)
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

Development

Successfully merging this pull request may close these issues.

[ICE]: internal error: entered unreachable code

4 participants