Skip to content

Commit 4faef6f

Browse files
authored
Merge pull request #20238 from rust-lang/gat-infer-lifetimes
fix: Infer lifetimes for GATs in expression/pattern position
2 parents 356a54d + 1d3ea1f commit 4faef6f

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/tools/rust-analyzer/crates/hir-ty/src/lower/path.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,8 +1018,12 @@ fn check_generic_args_len(
10181018
}
10191019

10201020
let lifetime_args_len = def_generics.len_lifetimes_self();
1021-
if provided_lifetimes_count == 0 && lifetime_args_len > 0 && !lowering_assoc_type_generics {
1022-
// In generic associated types, we never allow inferring the lifetimes.
1021+
if provided_lifetimes_count == 0
1022+
&& lifetime_args_len > 0
1023+
&& (!lowering_assoc_type_generics || infer_args)
1024+
{
1025+
// In generic associated types, we never allow inferring the lifetimes, but only in type context, that is
1026+
// when `infer_args == false`. In expression/pattern context we always allow inferring them, even for GATs.
10231027
match lifetime_elision {
10241028
&LifetimeElisionKind::AnonymousCreateParameter { report_in_path } => {
10251029
ctx.report_elided_lifetimes_in_path(def, lifetime_args_len as u32, report_in_path);

src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/incorrect_generics_len.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,28 @@ fn main() {
183183
"#,
184184
);
185185
}
186+
187+
#[test]
188+
fn generic_assoc_type_infer_lifetime_in_expr_position() {
189+
check_diagnostics(
190+
r#"
191+
//- minicore: sized
192+
struct Player;
193+
194+
struct Foo<'c, C> {
195+
_v: &'c C,
196+
}
197+
trait WithSignals: Sized {
198+
type SignalCollection<'c, C>;
199+
fn __signals_from_external(&self) -> Self::SignalCollection<'_, Self>;
200+
}
201+
impl WithSignals for Player {
202+
type SignalCollection<'c, C> = Foo<'c, C>;
203+
fn __signals_from_external(&self) -> Self::SignalCollection<'_, Self> {
204+
Self::SignalCollection { _v: self }
205+
}
206+
}
207+
"#,
208+
);
209+
}
186210
}

0 commit comments

Comments
 (0)