Repository navigation
hir_analysis: Reject escaping bound vars in anon const arg types - #166167
Open
Dnreikronos wants to merge 1 commit into
Open
Dnreikronos wants to merge 1 commit into
Dnreikronos wants to merge 1 commit into
Conversation
Collaborator
|
HIR ty lowering was modified cc @fmease |
Collaborator
|
r? @nnethercote rustbot has assigned @nnethercote. Use Why was this reviewer chosen?The reviewer was selected based on:
|
A lifetime elided in a fn signature is late-bound, so the type of an
anon const argument like `S<{}>` can contain a region bound by the
signature. Feeding that type as the anon const's `type_of` leaves the
region escaping, and typeck of the anon const ICEs when it registers a
WF obligation for it. Bound vars bound inside the type itself are
still accepted.
The test covers elided and named late-bound lifetimes as well as
regions bound by a `for<'a>` fn pointer or trait object.
Dnreikronos
force-pushed
the
const_generics/anon_const_late_bound_lifetime
branch
from
November 18, 2026 21:54
04b1ecf to
585be3c
Compare
Contributor
|
Looks ok to me but I don't know much about this code. Let's consult someone who does: r? @fmease |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #166129
With
generic_const_parameter_typeson, something likefn foo(_: S<{}>)withstruct S<'a, const N: &'a str>ICEs in typeck. The lifetime that gets elided there is late-bound, so the expected type of the{}const ends up as&'^0 str, with a region that belongs to the fn signature. That type gets fed as the anon const'stype_of, and when typeck registers a WF obligation for it the region is escaping and it blows up trying to wrap it in a dummy binder.There's already a check in
lower_const_argthat's meant to reject anon consts with lifetimes in their type, it just only looked at free and erased regions, so late-bound ones slipped past. I addedhas_escaping_bound_vars()to it. My first try washas_bound_regions()but that brokebound-var-in-ty.rs, which wants stuff likefor<'a> fn(&'a str)in the type to keep working. That's fair since those regions are bound inside the type and never escape, so now it only catches the escaping ones.This is really a "turn the ICE into an error" fix and not actual support. I think that's fine for now because the same check already rejects
'staticand'_with the same message, and supporting lifetimes here properly needs the anon const typeck to be mixed in with the parent body, which is a much bigger change (there's a FIXME about it right above). One thing I'm not totally happy with is that escaping bound types, like undernon_lifetime_binders, would also hit this and get an error that talks about lifetimes, which is a bit off. Didn't seem worth a separate error for that but I can change it if people want.