Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir/src/display.rs')
-rw-r--r--crates/hir/src/display.rs89
1 files changed, 59 insertions, 30 deletions
diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs
index a71851ea8c..c3af5fa7ce 100644
--- a/crates/hir/src/display.rs
+++ b/crates/hir/src/display.rs
@@ -2,7 +2,8 @@
use either::Either;
use hir_def::{
- AdtId, BuiltinDeriveImplId, FunctionId, GenericDefId, ImplId, ItemContainerId,
+ AdtId, BuiltinDeriveImplId, DefWithBodyId, ExpressionStoreOwnerId, FunctionId, GenericDefId,
+ ImplId, ItemContainerId,
builtin_derive::BuiltinDeriveImplMethod,
expr_store::{Body, ExpressionStore},
hir::generics::{GenericParams, TypeOrConstParamData, TypeParamProvenance, WherePredicate},
@@ -102,8 +103,11 @@ impl<'db> HirDisplay<'db> for Function {
if f.show_container_bounds() && !params.is_empty() {
write_trait_header(trait_.into(), f)?;
f.write_char('\n')?;
- has_disaplayable_predicates(f.db, params, params_store)
- .then_some((params, params_store))
+ has_disaplayable_predicates(f.db, params, params_store).then_some((
+ params,
+ trait_.into(),
+ params_store,
+ ))
} else {
None
}
@@ -113,8 +117,11 @@ impl<'db> HirDisplay<'db> for Function {
if f.show_container_bounds() && !params.is_empty() {
write_impl_header(impl_, f)?;
f.write_char('\n')?;
- has_disaplayable_predicates(f.db, params, params_store)
- .then_some((params, params_store))
+ has_disaplayable_predicates(f.db, params, params_store).then_some((
+ params,
+ impl_.into(),
+ params_store,
+ ))
} else {
None
}
@@ -125,7 +132,7 @@ impl<'db> HirDisplay<'db> for Function {
// Write signature of the function
let has_written_where = write_function(f, id)?;
- if let Some((container_params, container_params_store)) = container_params {
+ if let Some((container_params, owner, container_params_store)) = container_params {
if !has_written_where {
f.write_str("\nwhere")?;
}
@@ -135,7 +142,12 @@ impl<'db> HirDisplay<'db> for Function {
_ => unreachable!(),
};
write!(f, "\n // Bounds from {container_name}:",)?;
- write_where_predicates(container_params, container_params_store, f)?;
+ write_where_predicates(
+ container_params,
+ ExpressionStoreOwnerId::Signature(owner),
+ container_params_store,
+ f,
+ )?;
}
Ok(())
}
@@ -197,6 +209,7 @@ fn write_function<'db>(f: &mut HirFormatter<'_, 'db>, func_id: FunctionId) -> Re
let comma = if too_long_param { ",\n " } else { ", " };
// FIXME: Use resolved `param.ty` once we no longer discard lifetimes
let body = Body::of(db, func_id.into());
+ let owner = DefWithBodyId::FunctionId(func_id).into();
for (type_ref, param) in data.params.iter().zip(func.assoc_fn_params(db)).skip(skip_self) {
if !first {
f.write_str(comma)?;
@@ -205,11 +218,11 @@ fn write_function<'db>(f: &mut HirFormatter<'_, 'db>, func_id: FunctionId) -> Re
}
let pat_id = body.params[param.idx - body.self_param().is_some() as usize];
- let pat_str = body.pretty_print_pat(db, func_id.into(), pat_id, true, f.edition());
+ let pat_str = body.pretty_print_pat(db, owner, pat_id, true, f.edition());
f.write_str(&pat_str)?;
f.write_str(": ")?;
- type_ref.hir_fmt(f, &data.store)?;
+ type_ref.hir_fmt(f, owner, &data.store)?;
}
if data.is_varargs() {
@@ -258,7 +271,7 @@ fn write_function<'db>(f: &mut HirFormatter<'_, 'db>, func_id: FunctionId) -> Re
TypeRef::Tuple(tup) if tup.is_empty() => {}
_ => {
f.write_str(" -> ")?;
- ret_type.hir_fmt(f, &data.store)?;
+ ret_type.hir_fmt(f, owner, &data.store)?;
}
}
}
@@ -278,7 +291,8 @@ fn write_impl_header<'db>(impl_: ImplId, f: &mut HirFormatter<'_, 'db>) -> Resul
let impl_data = ImplSignature::of(db, impl_);
if let Some(target_trait) = &impl_data.target_trait {
f.write_char(' ')?;
- hir_display_with_store(&impl_data.store[target_trait.path], &impl_data.store).hir_fmt(f)?;
+ hir_display_with_store(&impl_data.store[target_trait.path], impl_.into(), &impl_data.store)
+ .hir_fmt(f)?;
f.write_str(" for")?;
}
@@ -306,13 +320,14 @@ impl<'db> HirDisplay<'db> for SelfParam {
};
let data = FunctionSignature::of(f.db, func);
let param = *data.params.first().unwrap();
+ let owner = ExpressionStoreOwnerId::Body(func.into());
match &data.store[param] {
TypeRef::Path(p) if p.is_self_type() => f.write_str("self"),
TypeRef::Reference(ref_) if matches!(&data.store[ref_.ty], TypeRef::Path(p) if p.is_self_type()) =>
{
f.write_char('&')?;
if let Some(lifetime) = &ref_.lifetime {
- lifetime.hir_fmt(f, &data.store)?;
+ lifetime.hir_fmt(f, owner, &data.store)?;
f.write_char(' ')?;
}
if let hir_def::type_ref::Mutability::Mut = ref_.mutability {
@@ -322,7 +337,7 @@ impl<'db> HirDisplay<'db> for SelfParam {
}
_ => {
f.write_str("self: ")?;
- param.hir_fmt(f, &data.store)
+ param.hir_fmt(f, owner, &data.store)
}
}
}
@@ -517,7 +532,11 @@ impl<'db> HirDisplay<'db> for EnumVariant {
f.write_str(", ")?;
}
// Enum variant fields must be pub.
- field.type_ref.hir_fmt(f, &data.store)?;
+ field.type_ref.hir_fmt(
+ f,
+ ExpressionStoreOwnerId::VariantFields(self.id.into()),
+ &data.store,
+ )?;
}
f.write_char(')')?;
}
@@ -666,6 +685,7 @@ fn write_generic_params_or_args<'db>(
include_defaults: bool,
) -> Result {
let (params, store) = GenericParams::with_store(f.db, def);
+ let owner = def.into();
if params.iter_lt().next().is_none()
&& params.iter_type_or_consts().all(|it| it.1.const_param().is_none())
&& params
@@ -701,17 +721,17 @@ fn write_generic_params_or_args<'db>(
write!(f, "{}", name.display(f.db, f.edition()))?;
if include_defaults && let Some(default) = &ty.default {
f.write_str(" = ")?;
- default.hir_fmt(f, store)?;
+ default.hir_fmt(f, owner, store)?;
}
}
TypeOrConstParamData::ConstParamData(c) => {
delim(f)?;
write!(f, "const {}: ", name.display(f.db, f.edition()))?;
- c.ty.hir_fmt(f, store)?;
+ c.ty.hir_fmt(f, owner, store)?;
if include_defaults && let Some(default) = &c.default {
f.write_str(" = ")?;
- default.hir_fmt(f, store)?;
+ default.hir_fmt(f, owner, store)?;
}
}
}
@@ -729,7 +749,7 @@ fn write_where_clause<'db>(def: GenericDefId, f: &mut HirFormatter<'_, 'db>) ->
}
f.write_str("\nwhere")?;
- write_where_predicates(params, store, f)?;
+ write_where_predicates(params, def.into(), store, f)?;
Ok(true)
}
@@ -752,6 +772,7 @@ fn has_disaplayable_predicates(
fn write_where_predicates<'db>(
params: &GenericParams,
+ owner: ExpressionStoreOwnerId,
store: &ExpressionStore,
f: &mut HirFormatter<'_, 'db>,
) -> Result {
@@ -783,29 +804,31 @@ fn write_where_predicates<'db>(
f.write_str("\n ")?;
match pred {
TypeBound { target, bound } => {
- target.hir_fmt(f, store)?;
+ target.hir_fmt(f, owner, store)?;
f.write_str(": ")?;
- bound.hir_fmt(f, store)?;
+ bound.hir_fmt(f, owner, store)?;
}
Lifetime { target, bound } => {
- target.hir_fmt(f, store)?;
+ target.hir_fmt(f, owner, store)?;
write!(f, ": ")?;
- bound.hir_fmt(f, store)?;
+ bound.hir_fmt(f, owner, store)?;
}
ForLifetime { lifetimes, target, bound } => {
let lifetimes = lifetimes.iter().map(|it| it.display(f.db, f.edition())).join(", ");
write!(f, "for<{lifetimes}> ")?;
- target.hir_fmt(f, store)?;
+ target.hir_fmt(f, owner, store)?;
f.write_str(": ")?;
- bound.hir_fmt(f, store)?;
+ bound.hir_fmt(f, owner, store)?;
}
}
while let Some(nxt) = iter.next_if(|nxt| check_same_target(pred, nxt)) {
f.write_str(" + ")?;
match nxt {
- TypeBound { bound, .. } | ForLifetime { bound, .. } => bound.hir_fmt(f, store)?,
- Lifetime { bound, .. } => bound.hir_fmt(f, store)?,
+ TypeBound { bound, .. } | ForLifetime { bound, .. } => {
+ bound.hir_fmt(f, owner, store)?
+ }
+ Lifetime { bound, .. } => bound.hir_fmt(f, owner, store)?,
}
}
f.write_str(",")?;
@@ -830,7 +853,7 @@ impl<'db> HirDisplay<'db> for Const {
Some(name) => write!(f, "{}: ", name.display(f.db, f.edition()))?,
None => f.write_str("_: ")?,
}
- data.type_ref.hir_fmt(f, &data.store)?;
+ data.type_ref.hir_fmt(f, ExpressionStoreOwnerId::Signature(self.id.into()), &data.store)?;
Ok(())
}
}
@@ -844,7 +867,7 @@ impl<'db> HirDisplay<'db> for Static {
f.write_str("mut ")?;
}
write!(f, "{}: ", data.name.display(f.db, f.edition()))?;
- data.type_ref.hir_fmt(f, &data.store)?;
+ data.type_ref.hir_fmt(f, ExpressionStoreOwnerId::Signature(self.id.into()), &data.store)?;
Ok(())
}
}
@@ -925,13 +948,19 @@ impl<'db> HirDisplay<'db> for TypeAlias {
if !data.bounds.is_empty() {
f.write_str(": ")?;
f.write_joined(
- data.bounds.iter().map(|bound| hir_display_with_store(bound, &data.store)),
+ data.bounds.iter().map(|bound| {
+ hir_display_with_store(
+ bound,
+ ExpressionStoreOwnerId::Signature(self.id.into()),
+ &data.store,
+ )
+ }),
" + ",
)?;
}
if let Some(ty) = data.ty {
f.write_str(" = ")?;
- ty.hir_fmt(f, &data.store)?;
+ ty.hir_fmt(f, ExpressionStoreOwnerId::Signature(self.id.into()), &data.store)?;
}
write_where_clause(def_id, f)?;
Ok(())