Merge pull request #20238 from rust-lang/gat-infer-lifetimes

fix: Infer lifetimes for GATs in expression/pattern position
This commit is contained in:
Shoyu Vanilla (Flint) 2025-07-15 11:36:56 +00:00 committed by GitHub
commit 4faef6f904
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 30 additions and 2 deletions

View file

@ -1018,8 +1018,12 @@ fn check_generic_args_len(
}
let lifetime_args_len = def_generics.len_lifetimes_self();
if provided_lifetimes_count == 0 && lifetime_args_len > 0 && !lowering_assoc_type_generics {
// In generic associated types, we never allow inferring the lifetimes.
if provided_lifetimes_count == 0
&& lifetime_args_len > 0
&& (!lowering_assoc_type_generics || infer_args)
{
// In generic associated types, we never allow inferring the lifetimes, but only in type context, that is
// when `infer_args == false`. In expression/pattern context we always allow inferring them, even for GATs.
match lifetime_elision {
&LifetimeElisionKind::AnonymousCreateParameter { report_in_path } => {
ctx.report_elided_lifetimes_in_path(def, lifetime_args_len as u32, report_in_path);

View file

@ -183,4 +183,28 @@ fn main() {
"#,
);
}
#[test]
fn generic_assoc_type_infer_lifetime_in_expr_position() {
check_diagnostics(
r#"
//- minicore: sized
struct Player;
struct Foo<'c, C> {
_v: &'c C,
}
trait WithSignals: Sized {
type SignalCollection<'c, C>;
fn __signals_from_external(&self) -> Self::SignalCollection<'_, Self>;
}
impl WithSignals for Player {
type SignalCollection<'c, C> = Foo<'c, C>;
fn __signals_from_external(&self) -> Self::SignalCollection<'_, Self> {
Self::SignalCollection { _v: self }
}
}
"#,
);
}
}