diff options
| author | Chayim Refael Friedman <chayimfr@gmail.com> | 2025-07-15 01:15:33 +0300 |
|---|---|---|
| committer | Chayim Refael Friedman <chayimfr@gmail.com> | 2025-07-15 01:15:33 +0300 |
| commit | 1d3ea1fd6e8c665145c559cdc94bb3e5736715a9 (patch) | |
| tree | 5f2c2d9a28271e495b805f8e7b69895d8127f341 | |
| parent | 356a54df2e7717f2191d358d17207a9332656ce5 (diff) | |
| download | rust-1d3ea1fd6e8c665145c559cdc94bb3e5736715a9.tar.gz rust-1d3ea1fd6e8c665145c559cdc94bb3e5736715a9.zip | |
Infer lifetimes for GATs in expression/pattern position
We should not only in type position.
| -rw-r--r-- | src/tools/rust-analyzer/crates/hir-ty/src/lower/path.rs | 8 | ||||
| -rw-r--r-- | src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/incorrect_generics_len.rs | 24 |
2 files changed, 30 insertions, 2 deletions
diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/lower/path.rs b/src/tools/rust-analyzer/crates/hir-ty/src/lower/path.rs index 06686b6a164..5c06234fa07 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/lower/path.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/lower/path.rs @@ -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); diff --git a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/incorrect_generics_len.rs b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/incorrect_generics_len.rs index 06f35759420..7402133f747 100644 --- a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/incorrect_generics_len.rs +++ b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/incorrect_generics_len.rs @@ -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 } + } +} + "#, + ); + } } |
