From 5531d46c95f4a2745458790a640abc0ec198fd78 Mon Sep 17 00:00:00 2001 From: hkalbasi Date: Mon, 5 Jun 2023 10:55:47 +0330 Subject: [PATCH] Emit `'_` for lifetime generics in `HirDisplay` --- crates/hir-ty/src/display.rs | 41 ++++++++++--------- crates/hir-ty/src/tests/patterns.rs | 2 +- crates/hir-ty/src/tests/regression.rs | 4 +- .../src/handlers/extract_function.rs | 24 +++++++++++ 4 files changed, 49 insertions(+), 22 deletions(-) diff --git a/crates/hir-ty/src/display.rs b/crates/hir-ty/src/display.rs index d3dba52d5f0..f90e025c7cc 100644 --- a/crates/hir-ty/src/display.rs +++ b/crates/hir-ty/src/display.rs @@ -1223,7 +1223,8 @@ fn hir_fmt_generics( generic_def: Option, ) -> Result<(), HirDisplayError> { let db = f.db; - if parameters.len(Interner) > 0 { + let lifetime_args_count = generic_def.map_or(0, |g| db.generic_params(g).lifetimes.len()); + if parameters.len(Interner) + lifetime_args_count > 0 { let parameters_to_write = if f.display_target.is_source_code() || f.omit_verbose_types() { match generic_def .map(|generic_def_id| db.generic_defaults(generic_def_id)) @@ -1268,26 +1269,28 @@ fn hir_fmt_generics( } else { parameters.as_slice(Interner) }; - if !parameters_to_write.is_empty() { + if !parameters_to_write.is_empty() || lifetime_args_count != 0 { write!(f, "<")?; - - if f.display_target.is_source_code() { - let mut first = true; - for generic_arg in parameters_to_write { - if !first { - write!(f, ", ")?; - } - first = false; - - if generic_arg.ty(Interner).map(|ty| ty.kind(Interner)) == Some(&TyKind::Error) - { - write!(f, "_")?; - } else { - generic_arg.hir_fmt(f)?; - } + let mut first = true; + for _ in 0..lifetime_args_count { + if !first { + write!(f, ", ")?; + } + first = false; + write!(f, "'_")?; + } + for generic_arg in parameters_to_write { + if !first { + write!(f, ", ")?; + } + first = false; + if f.display_target.is_source_code() + && generic_arg.ty(Interner).map(|ty| ty.kind(Interner)) == Some(&TyKind::Error) + { + write!(f, "_")?; + } else { + generic_arg.hir_fmt(f)?; } - } else { - f.write_joined(parameters_to_write, ", ")?; } write!(f, ">")?; diff --git a/crates/hir-ty/src/tests/patterns.rs b/crates/hir-ty/src/tests/patterns.rs index d683113d5d6..0f5a3e1752c 100644 --- a/crates/hir-ty/src/tests/patterns.rs +++ b/crates/hir-ty/src/tests/patterns.rs @@ -1109,7 +1109,7 @@ fn var_args() { #[lang = "va_list"] pub struct VaListImpl<'f>; fn my_fn(foo: ...) {} - //^^^ VaListImpl + //^^^ VaListImpl<'_> "#, ); } diff --git a/crates/hir-ty/src/tests/regression.rs b/crates/hir-ty/src/tests/regression.rs index a7b0f46e5e5..f18c953a7af 100644 --- a/crates/hir-ty/src/tests/regression.rs +++ b/crates/hir-ty/src/tests/regression.rs @@ -896,13 +896,13 @@ fn flush(&self) { "#, expect![[r#" 123..127 'self': &Mutex - 150..152 '{}': MutexGuard + 150..152 '{}': MutexGuard<'_, T> 234..238 'self': &{unknown} 240..290 '{ ...()); }': () 250..251 'w': &Mutex 276..287 '*(w.lock())': BufWriter 278..279 'w': &Mutex - 278..286 'w.lock()': MutexGuard + 278..286 'w.lock()': MutexGuard<'_, BufWriter> "#]], ); } diff --git a/crates/ide-assists/src/handlers/extract_function.rs b/crates/ide-assists/src/handlers/extract_function.rs index 7587aea55cf..2a67909e637 100644 --- a/crates/ide-assists/src/handlers/extract_function.rs +++ b/crates/ide-assists/src/handlers/extract_function.rs @@ -5478,6 +5478,30 @@ fn $0fun_name(i: T) { ); } + #[test] + fn dont_emit_type_with_hidden_lifetime_parameter() { + // FIXME: We should emit a `` generic argument for the generated function + check_assist( + extract_function, + r#" +struct Struct<'a, T>(&'a T); +fn func(i: Struct<'_, T>) { + $0foo(i);$0 +} +"#, + r#" +struct Struct<'a, T>(&'a T); +fn func(i: Struct<'_, T>) { + fun_name(i); +} + +fn $0fun_name(i: Struct<'_, T>) { + foo(i); +} +"#, + ); + } + #[test] fn preserve_generics_from_body() { check_assist(