Skip to content

fix: Emit '_ for lifetime generics in HirDisplay #14978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions crates/hir-ty/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,8 @@ fn hir_fmt_generics(
generic_def: Option<hir_def::GenericDefId>,
) -> 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))
Expand Down Expand Up @@ -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, ">")?;
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/tests/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,7 @@ fn var_args() {
#[lang = "va_list"]
pub struct VaListImpl<'f>;
fn my_fn(foo: ...) {}
//^^^ VaListImpl
//^^^ VaListImpl<'_>
"#,
);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-ty/src/tests/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,13 +896,13 @@ fn flush(&self) {
"#,
expect![[r#"
123..127 'self': &Mutex<T>
150..152 '{}': MutexGuard<T>
150..152 '{}': MutexGuard<'_, T>
234..238 'self': &{unknown}
240..290 '{ ...()); }': ()
250..251 'w': &Mutex<BufWriter>
276..287 '*(w.lock())': BufWriter
278..279 'w': &Mutex<BufWriter>
278..286 'w.lock()': MutexGuard<BufWriter>
278..286 'w.lock()': MutexGuard<'_, BufWriter>
"#]],
);
}
Expand Down
24 changes: 24 additions & 0 deletions crates/ide-assists/src/handlers/extract_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5478,6 +5478,30 @@ fn $0fun_name<T: Debug>(i: T) {
);
}

#[test]
fn dont_emit_type_with_hidden_lifetime_parameter() {
// FIXME: We should emit a `<T: Debug>` generic argument for the generated function
check_assist(
extract_function,
r#"
struct Struct<'a, T>(&'a T);
fn func<T: Debug>(i: Struct<'_, T>) {
$0foo(i);$0
}
"#,
r#"
struct Struct<'a, T>(&'a T);
fn func<T: Debug>(i: Struct<'_, T>) {
fun_name(i);
}

fn $0fun_name(i: Struct<'_, T>) {
foo(i);
}
"#,
);
}

#[test]
fn preserve_generics_from_body() {
check_assist(
Expand Down