Repository navigation
Reject implicit forward references to generic params in generic param defaults & const param types - #166282
Reject implicit forward references to generic params in generic param defaults & const param types#166282fmease wants to merge 3 commits into
Conversation
in generic parameter defaults and const parameter types.
e8b1a03 to
066c02f
Compare
| } | ||
| } | ||
|
|
||
| fn reject_implicit_forward_references_to_param<'tcx, T: ty::TypeFoldable<TyCtxt<'tcx>>>( |
There was a problem hiding this comment.
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.
| subject: &'static str, | ||
| } | ||
|
|
||
| impl<'tcx> TypeFolder<TyCtxt<'tcx>> for RejectImplicitForwardReferencesToParam<'_, 'tcx> { |
There was a problem hiding this comment.
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...
| #[diag("{$subject} references generic parameter `{$param}` before it is declared")] | ||
| pub(crate) struct ImplicitForwardReferenceToParam { | ||
| #[primary_span] | ||
| #[label("implicitly references parameter `{$param}`")] |
There was a problem hiding this comment.
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.
|
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() {} |
|
That's an interesting snippet :) Yes, I would say it's intentional behavior because the |
|
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: The allow is for an FCW at #36887 |
|
beta backport declined as per compiler team on Zulip. |
...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
<T as Trait<U>>::Typecontinues to get rejected withE0128generic parameter defaults cannot reference parameters before they are declaredcannot reference `U` before it is declaredwhileT::Typenow gets rejected withtype parameter default references generic parameter `U` before it is declaredimplicitly references parameter `U`.Test
tests/ui/associated-types/type-relative-in-ty-param-default-fwd-ref-in-alias.rsdemonstrates 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
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. ↩