Skip to content

Reject implicit forward references to generic params in generic param defaults & const param types - #166282

Open
fmease wants to merge 3 commits into
rust-lang:mainfrom
fmease:rej-imp-param-fwd-refs
Open

fmease wants to merge 3 commits into
rust-lang:mainfrom
fmease:rej-imp-param-fwd-refs

Conversation

@fmease

@fmease fmease commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

...arising from type-relative associated item paths.

For context, (explicit) forward references to generic parameters in generic parameter defaults & const parameter types get rejected by the name resolver. However, since PR #161998 it's possible to implicitly reference generic parameters before they are declared1 which is a no-go and which I didn't think about.

We cannot detect such implicit forward references in the name resolver because type-relative associated item paths get resolved during HIR ty lowering, so I had to put a new check there (or rather in the type collection module).

I didn't copy/paste the diagnostic title from E0128 because I disliked the fact that it doesn't mention the parameter name (only the label does). I might update its phrasing in a follow-up PR to match this one. So

  • a fully-qualified and thus resolved type path like <T as Trait<U>>::Type continues to get rejected with
    • (code) E0128
    • (title) generic parameter defaults cannot reference parameters before they are declared
    • (label) cannot reference `U` before it is declared while
  • a type-relative type path like T::Type now gets rejected with
    • (title) type parameter default references generic parameter `U` before it is declared
    • (label) implicitly references parameter `U`.

Test tests/ui/associated-types/type-relative-in-ty-param-default-fwd-ref-in-alias.rs demonstrates a subtlety that's slightly unfortunate, maybe? I hope it's not a reason to revert PR #161998.

Fixes #165989.

r? @jackh726 or @lcnr (#161998)

(No LLM was or will be used by me during the entire creation process of this PR)

Footnotes

  1. Well, it's actually been possible before that ... under GCE; leading to similar ICEs: See e.g., https://github.com/rust-lang/rust/issues/105238 or https://github.com/rust-lang/rust/issues/106994. ↩

@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 20, 2026
@fmease
fmease force-pushed the rej-imp-param-fwd-refs branch from e8b1a03 to 066c02f Compare November 20, 2026 23:07
@fmease fmease added the T-types Relevant to the types team, which will review and decide on the PR/issue. label Nov 20, 2026
}
}

fn reject_implicit_forward_references_to_param<'tcx, T: ty::TypeFoldable<TyCtxt<'tcx>>>(

@fmease fmease Nov 20, 2026 •

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.

Add a doc comment. It should explain the "implicit" part. Namely that we can assume the forward references are implicit because the name resolver would've already caught all explicit forward references and led AST lowering > RBV > HIR ty lowering to resolve them to {type error}s / {const error}s.

View changes since the review

@fmease fmease changed the title Reject implicit forward references to generic parameters in generic parameter defaults & const parameter types Reject implicit forward references to generic params in generic param defaults & const param types Nov 21, 2026
subject: &'static str,
}

impl<'tcx> TypeFolder<TyCtxt<'tcx>> for RejectImplicitForwardReferencesToParam<'_, 'tcx> {

@fmease fmease Nov 21, 2026 •

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.

I could make the checks stricter by only considering parameters that are used as arguments to projections. This would allow us to crash again in EarlyBinder::instantiate if we mess up parameter handling that's unrelated to type-relative associated type paths...

View changes since the review

#[diag("{$subject} references generic parameter `{$param}` before it is declared")]
pub(crate) struct ImplicitForwardReferenceToParam {
#[primary_span]
#[label("implicitly references parameter `{$param}`")]

@fmease fmease Nov 21, 2026 •

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.

I know the diagnostic is quite terse at the moment.

After finding problematic parameters in the middle type, I could perhaps go through the in-scope HIR parameter bounds / where-clause trying to re-find the HIR "single_bound_for_assoc_item" to point at it via a span_note and say something along the lines of "since it depends on this bound which references said parameter".

"it" would be a bit awkward since we're currently highlighting the entire type and not the projection. However, finding the corresp. HIR associated item path given the middle projection is impossible at the moment (without re-HIR-ty-lowering), CC #156711.

View changes since the review

@theemathas theemathas added the beta-nominated Nominated for backporting to the compiler in the beta channel. label Nov 21, 2026
@theemathas

Copy link
Copy Markdown
Contributor

Is this intended behavior?

trait Super<X> {
    type AssocSuper;
}
trait Sub: Super<Self::Assoc> {
    type Assoc;
}

// Compiles with this PR
struct Weird<T: Sub<Assoc = V>, U = T::AssocSuper, V = i32>(T, U, V);

// Doesn't compile with this PR
// struct Weird<T: Sub<Assoc = V>, U = <T as Super<V>>::AssocSuper, V = i32>(T, U, V);

fn main() {}

@fmease

fmease commented Nov 21, 2026 •

Copy link
Copy Markdown
Member Author

That's an interesting snippet :)

Yes, I would say it's intentional behavior because the T::AssocSuper (i.e., the newly allowed kind of associated type path) doesn't get resolved to <T as Super<V>>::AssocSuper (which does contain an illegal forward reference) but to <T as Super<<T as Sub>::Assoc>>::AssocSuper which doesn't contain a "direct" / "lexical" forward reference and is thus already allowed / possible on stable.

@theemathas

theemathas commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

An LLM found the following code, which compiles on stable and beta, but errors with this PR.

pub struct S<T>(T);

#[allow(invalid_type_param_default)]
impl<T = Self> S<T> {}

fn main() {}

Error with this PR:

error: type parameter default references generic parameter `T` before it is declared
 --> src/main.rs:4:10
  |
4 | impl<T = Self> S<T> {}
  |          ^^^^ implicitly references parameter `T`

The allow is for an FCW at #36887

@fmease fmease 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 Nov 21, 2026
@rustbot

rustbot commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

beta backport declined as per compiler team on Zulip.

@rustbot rustbot removed the beta-nominated Nominated for backporting to the compiler in the beta channel. label Nov 26, 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-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ICE]: type parameter U/#1 (U/#1/1) out of range when instantiating, args=[()]

4 participants