Auto merge of #17072 - Veykril:hir-ty-display, r=Veykril

internal: Use `hir_fmt_args` everywhere in `hir_ty::display`
This commit is contained in:
bors 2024-04-15 14:19:04 +00:00
commit c0371309b4

View File

@ -422,13 +422,7 @@ fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
let proj_params_count = let proj_params_count =
self.substitution.len(Interner) - trait_ref.substitution.len(Interner); 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)[..proj_params_count];
if !proj_params.is_empty() { hir_fmt_generics(f, proj_params, None)
write!(f, "<")?;
// FIXME use `hir_fmt_generics` here
f.write_joined(proj_params, ", ")?;
write!(f, ">")?;
}
Ok(())
} }
} }
@ -469,7 +463,11 @@ fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
ConstScalar::Bytes(b, m) => render_const_scalar(f, b, m, &data.ty), ConstScalar::Bytes(b, m) => render_const_scalar(f, b, m, &data.ty),
ConstScalar::UnevaluatedConst(c, parameters) => { ConstScalar::UnevaluatedConst(c, parameters) => {
write!(f, "{}", c.name(f.db.upcast()))?; write!(f, "{}", c.name(f.db.upcast()))?;
hir_fmt_generics(f, parameters, c.generic_def(f.db.upcast()))?; hir_fmt_generics(
f,
parameters.as_slice(Interner),
c.generic_def(f.db.upcast()),
)?;
Ok(()) Ok(())
} }
ConstScalar::Unknown => f.write_char('_'), ConstScalar::Unknown => f.write_char('_'),
@ -945,37 +943,31 @@ fn hir_fmt(
} }
}; };
f.end_location_link(); f.end_location_link();
if parameters.len(Interner) > 0 { if parameters.len(Interner) > 0 {
let generics = generics(db.upcast(), def.into()); let generics = generics(db.upcast(), def.into());
let ( let (parent_len, self_, type_, const_, impl_, lifetime) =
parent_params, generics.provenance_split();
self_param, let parameters = parameters.as_slice(Interner);
type_params,
const_params,
_impl_trait_params,
lifetime_params,
) = generics.provenance_split();
let total_len =
parent_params + self_param + type_params + const_params + lifetime_params;
// We print all params except implicit impl Trait params. Still a bit weird; should we leave out parent and self? // We print all params except implicit impl Trait params. Still a bit weird; should we leave out parent and self?
if total_len > 0 { if parameters.len() - impl_ > 0 {
// `parameters` are in the order of fn's params (including impl traits), fn's lifetimes // `parameters` are in the order of fn's params (including impl traits), fn's lifetimes
// parent's params (those from enclosing impl or trait, if any). // parent's params (those from enclosing impl or trait, if any).
let parameters = parameters.as_slice(Interner); let (fn_params, other) =
let fn_params_len = self_param + type_params + const_params; parameters.split_at(self_ + type_ + const_ + lifetime);
// This will give slice till last type or const let (_impl, parent_params) = other.split_at(impl_);
let fn_params = parameters.get(..fn_params_len); debug_assert_eq!(parent_params.len(), parent_len);
let fn_lt_params =
parameters.get(fn_params_len..(fn_params_len + lifetime_params)); let parent_params =
let parent_params = parameters.get(parameters.len() - parent_params..); generic_args_sans_defaults(f, Some(def.into()), parent_params);
let params = parent_params let fn_params = generic_args_sans_defaults(f, Some(def.into()), fn_params);
.into_iter()
.chain(fn_lt_params)
.chain(fn_params)
.flatten();
write!(f, "<")?; write!(f, "<")?;
// FIXME use `hir_fmt_generics` here hir_fmt_generic_arguments(f, parent_params)?;
f.write_joined(params, ", ")?; if !parent_params.is_empty() && !fn_params.is_empty() {
write!(f, ", ")?;
}
hir_fmt_generic_arguments(f, fn_params)?;
write!(f, ">")?; write!(f, ">")?;
} }
} }
@ -1019,7 +1011,7 @@ fn hir_fmt(
let generic_def = self.as_generic_def(db); let generic_def = self.as_generic_def(db);
hir_fmt_generics(f, parameters, generic_def)?; hir_fmt_generics(f, parameters.as_slice(Interner), generic_def)?;
} }
TyKind::AssociatedType(assoc_type_id, parameters) => { TyKind::AssociatedType(assoc_type_id, parameters) => {
let type_alias = from_assoc_type_id(*assoc_type_id); let type_alias = from_assoc_type_id(*assoc_type_id);
@ -1042,21 +1034,15 @@ fn hir_fmt(
f.end_location_link(); f.end_location_link();
// Note that the generic args for the associated type come before those for the // Note that the generic args for the associated type come before those for the
// trait (including the self type). // trait (including the self type).
// FIXME: reconsider the generic args order upon formatting? hir_fmt_generics(f, parameters.as_slice(Interner), None)
if parameters.len(Interner) > 0 {
write!(f, "<")?;
// FIXME use `hir_fmt_generics` here
f.write_joined(parameters.as_slice(Interner), ", ")?;
write!(f, ">")?;
}
} else { } else {
let projection_ty = ProjectionTy { let projection_ty = ProjectionTy {
associated_ty_id: to_assoc_type_id(type_alias), associated_ty_id: to_assoc_type_id(type_alias),
substitution: parameters.clone(), substitution: parameters.clone(),
}; };
projection_ty.hir_fmt(f)?; projection_ty.hir_fmt(f)
} }?;
} }
TyKind::Foreign(type_alias) => { TyKind::Foreign(type_alias) => {
let alias = from_foreign_def_id(*type_alias); let alias = from_foreign_def_id(*type_alias);
@ -1150,7 +1136,7 @@ fn hir_fmt(
} }
ClosureStyle::ClosureWithSubst => { ClosureStyle::ClosureWithSubst => {
write!(f, "{{closure#{:?}}}", id.0.as_u32())?; write!(f, "{{closure#{:?}}}", id.0.as_u32())?;
return hir_fmt_generics(f, substs, None); return hir_fmt_generics(f, substs.as_slice(Interner), None);
} }
_ => (), _ => (),
} }
@ -1336,15 +1322,14 @@ fn hir_fmt(
fn hir_fmt_generics( fn hir_fmt_generics(
f: &mut HirFormatter<'_>, f: &mut HirFormatter<'_>,
parameters: &Substitution, parameters: &[GenericArg],
generic_def: Option<hir_def::GenericDefId>, generic_def: Option<hir_def::GenericDefId>,
) -> Result<(), HirDisplayError> { ) -> Result<(), HirDisplayError> {
if parameters.is_empty(Interner) { if parameters.is_empty() {
return Ok(()); return Ok(());
} }
let parameters_to_write = let parameters_to_write = generic_args_sans_defaults(f, generic_def, parameters);
generic_args_sans_defaults(f, generic_def, parameters.as_slice(Interner));
if !parameters_to_write.is_empty() { if !parameters_to_write.is_empty() {
write!(f, "<")?; write!(f, "<")?;
hir_fmt_generic_arguments(f, parameters_to_write)?; hir_fmt_generic_arguments(f, parameters_to_write)?;
@ -1677,13 +1662,7 @@ fn fmt_trait_ref(
f.start_location_link(trait_.into()); f.start_location_link(trait_.into());
write!(f, "{}", f.db.trait_data(trait_).name.display(f.db.upcast()))?; write!(f, "{}", f.db.trait_data(trait_).name.display(f.db.upcast()))?;
f.end_location_link(); f.end_location_link();
if tr.substitution.len(Interner) > 1 { hir_fmt_generics(f, &tr.substitution.as_slice(Interner)[1..], None)
write!(f, "<")?;
// FIXME use `hir_fmt_generics` here
f.write_joined(&tr.substitution.as_slice(Interner)[1..], ", ")?;
write!(f, ">")?;
}
Ok(())
} }
impl HirDisplay for TraitRef { impl HirDisplay for TraitRef {