From 9dad25a7cb6a3f439317f2e34297f8ddf70d4fb2 Mon Sep 17 00:00:00 2001 From: Aleksei Trifonov Date: Mon, 26 Aug 2024 03:03:26 +0300 Subject: [PATCH 1/2] Show and render error lifetime args as `'_` --- .../crates/hir-ty/src/display.rs | 20 +++++++------------ .../src/handlers/extract_function.rs | 2 +- .../ide-completion/src/tests/predicate.rs | 4 ++-- .../ide-completion/src/tests/type_pos.rs | 8 ++++---- .../crates/ide/src/inlay_hints/bind_pat.rs | 2 +- 5 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/display.rs b/src/tools/rust-analyzer/crates/hir-ty/src/display.rs index 4ef053130a8..151cc3dc491 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/display.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/display.rs @@ -1523,18 +1523,6 @@ fn hir_fmt_generic_arguments( None => (parameters, &[][..]), }; for generic_arg in lifetimes.iter().chain(ty_or_const) { - // FIXME: Remove this - // most of our lifetimes will be errors as we lack elision and inference - // so don't render them for now - if !cfg!(test) - && matches!( - generic_arg.lifetime(Interner), - Some(l) if ***l.interned() == LifetimeData::Error - ) - { - continue; - } - if !mem::take(&mut first) { write!(f, ", ")?; } @@ -1872,7 +1860,13 @@ fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> { LifetimeData::BoundVar(idx) => idx.hir_fmt(f), LifetimeData::InferenceVar(_) => write!(f, "_"), LifetimeData::Static => write!(f, "'static"), - LifetimeData::Error => write!(f, "'?"), + LifetimeData::Error => { + if cfg!(test) { + write!(f, "'?") + } else { + write!(f, "'_") + } + } LifetimeData::Erased => write!(f, "'"), LifetimeData::Phantom(void, _) => match *void {}, } diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_function.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_function.rs index fd379cb88da..b6781c8e129 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_function.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_function.rs @@ -5649,7 +5649,7 @@ fn func(i: Struct<'_, T>) { fun_name(i); } -fn $0fun_name(i: Struct) { +fn $0fun_name(i: Struct<'_, T>) { foo(i); } "#, diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/tests/predicate.rs b/src/tools/rust-analyzer/crates/ide-completion/src/tests/predicate.rs index 62eb642b3bc..46a3e97d3e9 100644 --- a/src/tools/rust-analyzer/crates/ide-completion/src/tests/predicate.rs +++ b/src/tools/rust-analyzer/crates/ide-completion/src/tests/predicate.rs @@ -19,7 +19,7 @@ struct Foo<'lt, T, const C: usize> where $0 {} en Enum Enum ma makro!(…) macro_rules! makro md module - st Foo<…> Foo<{unknown}, _> + st Foo<…> Foo<'_, {unknown}, _> st Record Record st Tuple Tuple st Unit Unit @@ -92,7 +92,7 @@ struct Foo<'lt, T, const C: usize> where for<'a> $0 {} en Enum Enum ma makro!(…) macro_rules! makro md module - st Foo<…> Foo<{unknown}, _> + st Foo<…> Foo<'_, {unknown}, _> st Record Record st Tuple Tuple st Unit Unit diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/tests/type_pos.rs b/src/tools/rust-analyzer/crates/ide-completion/src/tests/type_pos.rs index ff38c161087..db4ac9381ce 100644 --- a/src/tools/rust-analyzer/crates/ide-completion/src/tests/type_pos.rs +++ b/src/tools/rust-analyzer/crates/ide-completion/src/tests/type_pos.rs @@ -20,8 +20,8 @@ struct Foo<'lt, T, const C: usize> { en Enum Enum ma makro!(…) macro_rules! makro md module - sp Self Foo<{unknown}, _> - st Foo<…> Foo<{unknown}, _> + sp Self Foo<'_, {unknown}, _> + st Foo<…> Foo<'_, {unknown}, _> st Record Record st Tuple Tuple st Unit Unit @@ -45,8 +45,8 @@ fn tuple_struct_field() { en Enum Enum ma makro!(…) macro_rules! makro md module - sp Self Foo<{unknown}, _> - st Foo<…> Foo<{unknown}, _> + sp Self Foo<'_, {unknown}, _> + st Foo<…> Foo<'_, {unknown}, _> st Record Record st Tuple Tuple st Unit Unit diff --git a/src/tools/rust-analyzer/crates/ide/src/inlay_hints/bind_pat.rs b/src/tools/rust-analyzer/crates/ide/src/inlay_hints/bind_pat.rs index d67afe14d7c..82b0a6ffcf1 100644 --- a/src/tools/rust-analyzer/crates/ide/src/inlay_hints/bind_pat.rs +++ b/src/tools/rust-analyzer/crates/ide/src/inlay_hints/bind_pat.rs @@ -342,7 +342,7 @@ fn f<'a>() { let x = S::<'static>; //^ S<'static> let y = S::<'_>; - //^ S + //^ S<'_> let z = S::<'a>; //^ S<'a> From 9d4fdc015767d2d14760e5ade31376362c586358 Mon Sep 17 00:00:00 2001 From: Aleksei Trifonov Date: Mon, 26 Aug 2024 03:06:49 +0300 Subject: [PATCH 2/2] Show lifetime args if there are only error ones --- .../rust-analyzer/crates/hir-ty/src/display.rs | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/display.rs b/src/tools/rust-analyzer/crates/hir-ty/src/display.rs index 151cc3dc491..4a5eaaa72ae 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/display.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/display.rs @@ -1438,17 +1438,7 @@ fn hir_fmt_generics( let parameters_to_write = generic_args_sans_defaults(f, generic_def, parameters); - // FIXME: Remote this - // most of our lifetimes will be errors as we lack elision and inference - // so don't render them for now - let only_err_lifetimes = !cfg!(test) - && parameters_to_write.iter().all(|arg| { - matches!( - arg.data(Interner), - chalk_ir::GenericArgData::Lifetime(it) if *it.data(Interner) == LifetimeData::Error - ) - }); - if !parameters_to_write.is_empty() && !only_err_lifetimes { + if !parameters_to_write.is_empty() { write!(f, "<")?; hir_fmt_generic_arguments(f, parameters_to_write, self_)?; write!(f, ">")?; @@ -1881,7 +1871,7 @@ fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> { wc.hir_fmt(f)?; write!(f, ")")?; } - _ => write!(f, "?")?, + _ => write!(f, "_")?, } Ok(()) }