Skip to content

GCI: During reachability analysis don't try to evaluate the initializer of overly generic free const items#153269

Open
fmease wants to merge 1 commit intorust-lang:mainfrom
fmease:gci-reach-no-eval
Open

GCI: During reachability analysis don't try to evaluate the initializer of overly generic free const items#153269
fmease wants to merge 1 commit intorust-lang:mainfrom
fmease:gci-reach-no-eval

Conversation

@fmease
Copy link
Copy Markdown
Member

@fmease fmease commented Mar 2, 2026

We generally don't want the initializer of free const items to get evaluated if they have any non-lifetime generic parameters. However, while I did account for that in HIR analysis & mono item collection (#136168 & #136429), I didn't account for reachability analysis so far which means that on main we still evaluate such items if they are public for example.

The closed PR #142293 from a year ago did address that as a byproduct but of course it wasn't merged since its primary goal was misguided. This PR extracts & improves upon the relevant parts of that PR which are necessary to fix said issue.

Follow up to #136168 & #136429.
Partially supersedes #142293.
Part of #113521.

r? @BoxyUwU

@fmease fmease added the F-generic_const_items `#![feature(generic_const_items)]` label Mar 2, 2026
@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 Mar 2, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 2, 2026

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

Copy link
Copy Markdown
Member Author

@fmease fmease Mar 2, 2026

Choose a reason for hiding this comment

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

Going off on a tangent, the whole "don't eval the initializer for non-lifetime-parametric free const items" thing doesn't apply to mGCA's type-level consts which I'm pretty sure is very intentional (IINM) and thus fine, it's also not GCI-specific, e.g., trait T { type const K: usize = const { panic!(); }; } (Self in scope).

I'm only concerned about const items with bodies anyway since the whole idea is to prevent const eval's "bespoke" handling of "too generic" consts "being user observable" / load-bearing for program correctness (the other motivation being consts should behave like fns in this specific scenario) or rephrased "type based" > "value based".

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.

yeah type const stuff is whatever here. those should be handled by whatever normal:tm: thing we do for wfchecks of type system stuff

Err(ErrorHandled::TooGeneric(_)) => self.visit_const_item_rhs(init),
// We've checked at the start that there aren't any non-lifetime params
// in scope. The const initializer can't possibly be too generic.
Err(ErrorHandled::TooGeneric(_)) => bug!(),
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.

I'm unsure if this is true 🤔 I would expect it to be possible to get TooGeneric back when there are trivial bounds on the const item which prevent normalization in the const's body.

I guess that's fine for now anyhow... The whole setup here needs to be completely reworked to not evaluate stuff with false bounds or with generic parameters. And once that's done we should start evaluating with TypingEnv::fully_monomorphized which would fix that 🤔

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.

I intend to get back to that work.... somewhat soon, I think mGCA stuff is finally calming down so I should be able to put more time towards pushing on that again

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.

You are right, it crashes on the following snippet:

#![feature(generic_const_items)]

pub const X: () = <i32 as Trait<'static>>::K
where
    for<'a> i32: Trait<'a>;

trait Trait<'a> { const K: (); }

I'm gonna fix this.

I guess that's fine for now anyhow... The whole setup here needs to be completely reworked to not evaluate stuff with false bounds or with generic parameters

For sure! Still, I consider this PR to be a stop-gap since it's a bit janky that pub const _<T>: () = panic!(); panics while const _<T>: () = panic!(); does not.

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.

Addressed.

@fmease
Copy link
Copy Markdown
Member Author

fmease commented Mar 31, 2026

@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 Mar 31, 2026
@fmease fmease force-pushed the gci-reach-no-eval branch from 7d5e35d to cf4e8c6 Compare April 3, 2026 01:22
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 3, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@fmease
Copy link
Copy Markdown
Member Author

fmease commented Apr 3, 2026

@rustbot review

@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 Apr 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

F-generic_const_items `#![feature(generic_const_items)]` 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

Status: In Progress

Development

Successfully merging this pull request may close these issues.

3 participants