Skip to content

hir_analysis: Reject escaping bound vars in anon const arg types - #166167

Open
Dnreikronos wants to merge 1 commit into
rust-lang:mainfrom
Dnreikronos:const_generics/anon_const_late_bound_lifetime
Open

Dnreikronos wants to merge 1 commit into
rust-lang:mainfrom
Dnreikronos:const_generics/anon_const_late_bound_lifetime

Conversation

@Dnreikronos

@Dnreikronos Dnreikronos commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #166129

With generic_const_parameter_types on, something like fn foo(_: S<{}>) with struct 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's type_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_arg that'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 added has_escaping_bound_vars() to it. My first try was has_bound_regions() but that broke bound-var-in-ty.rs, which wants stuff like for<'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 'static and '_ 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 under non_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.

@rustbot

rustbot commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

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-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 18, 2026
@rustbot

rustbot commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 79 candidates
  • Random selection from 21 candidates

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
Dnreikronos force-pushed the const_generics/anon_const_late_bound_lifetime branch from 04b1ecf to 585be3c Compare November 18, 2026 21:54
@nnethercote

Copy link
Copy Markdown
Contributor

Looks ok to me but I don't know much about this code. Let's consult someone who does:

r? @fmease

@rustbot rustbot assigned fmease and unassigned nnethercote Nov 20, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

None yet

Development

Successfully merging this pull request may close these issues.

[ICE]: WellFormed(Term::Ty( ... has escaping bound vars, so it cannot be wrapped in a dummy binder.

4 participants