Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-ty/src/display.rs')
-rw-r--r--crates/hir-ty/src/display.rs38
1 files changed, 21 insertions, 17 deletions
diff --git a/crates/hir-ty/src/display.rs b/crates/hir-ty/src/display.rs
index 8baa022038..a79b8cdf8e 100644
--- a/crates/hir-ty/src/display.rs
+++ b/crates/hir-ty/src/display.rs
@@ -621,9 +621,8 @@ impl HirDisplay for ProjectionTy {
.name
.display(f.db, f.edition())
)?;
- let proj_params_count =
- self.substitution.len(Interner) - trait_ref.substitution.len(Interner);
- let proj_params = &self.substitution.as_slice(Interner)[..proj_params_count];
+ let proj_params =
+ &self.substitution.as_slice(Interner)[trait_ref.substitution.len(Interner)..];
hir_fmt_generics(f, proj_params, None, None)
}
}
@@ -1196,27 +1195,31 @@ impl HirDisplay for Ty {
// Normally, functions cannot have default parameters, but they can,
// for function-like things such as struct names or enum variants.
- // The former cannot have defaults but parents, and the later cannot have
- // parents but defaults.
- // So, if `parent_len` > 0, it have a parent and thus it doesn't have any
- // default. Therefore, we shouldn't subtract defaults because those defaults
- // are from their parents.
- // And if `parent_len` == 0, either parents don't exists or they don't have
- // any defaults. Thus, we can - and should - subtract defaults.
- let without_impl = if parent_len > 0 {
- params_len - parent_len - impl_
+ // The former cannot have defaults but does have parents,
+ // but the latter cannot have parents but can have defaults.
+ //
+ // However, it's also true that *traits* can have defaults too.
+ // In this case, there can be no function params.
+ let parent_end = if parent_len > 0 {
+ // If `parent_len` > 0, then there cannot be defaults on the function
+ // and all defaults must come from the parent.
+ parent_len - defaults
} else {
- params_len - parent_len - impl_ - defaults
+ parent_len
};
- // parent's params (those from enclosing impl or trait, if any).
- let (fn_params, parent_params) = parameters.split_at(without_impl + impl_);
+ let fn_params_no_impl_or_defaults = parameters.len() - parent_end - impl_;
+ let (parent_params, fn_params) = parameters.split_at(parent_end);
write!(f, "<")?;
hir_fmt_generic_arguments(f, parent_params, None)?;
if !parent_params.is_empty() && !fn_params.is_empty() {
write!(f, ", ")?;
}
- hir_fmt_generic_arguments(f, &fn_params[0..without_impl], None)?;
+ hir_fmt_generic_arguments(
+ f,
+ &fn_params[..fn_params_no_impl_or_defaults],
+ None,
+ )?;
write!(f, ">")?;
}
}
@@ -1872,11 +1875,12 @@ fn write_bounds_like_dyn_trait(
f.end_location_link();
let proj_arg_count = generics(f.db, assoc_ty_id.into()).len_self();
+ let parent_len = proj.substitution.len(Interner) - proj_arg_count;
if proj_arg_count > 0 {
write!(f, "<")?;
hir_fmt_generic_arguments(
f,
- &proj.substitution.as_slice(Interner)[..proj_arg_count],
+ &proj.substitution.as_slice(Interner)[parent_len..],
None,
)?;
write!(f, ">")?;