2021-03-14 04:36:04 -05:00
|
|
|
//! HirDisplay implementations for various hir types.
|
2021-03-14 07:03:39 -05:00
|
|
|
use hir_def::{
|
2023-04-06 12:23:29 -05:00
|
|
|
data::adt::VariantData,
|
2021-12-29 07:35:59 -06:00
|
|
|
generics::{
|
|
|
|
TypeOrConstParamData, TypeParamProvenance, WherePredicate, WherePredicateTypeTarget,
|
|
|
|
},
|
2023-01-21 10:29:07 -06:00
|
|
|
lang_item::LangItem,
|
2021-03-14 07:03:39 -05:00
|
|
|
type_ref::{TypeBound, TypeRef},
|
2021-03-15 11:05:03 -05:00
|
|
|
AdtId, GenericDefId,
|
2021-03-14 07:03:39 -05:00
|
|
|
};
|
2023-04-06 13:14:51 -05:00
|
|
|
use hir_expand::name;
|
2021-09-10 10:48:39 -05:00
|
|
|
use hir_ty::{
|
|
|
|
display::{
|
|
|
|
write_bounds_like_dyn_trait_with_prefix, write_visibility, HirDisplay, HirDisplayError,
|
|
|
|
HirFormatter, SizedByDefault,
|
|
|
|
},
|
|
|
|
Interner, TraitRefExt, WhereClause,
|
2021-03-14 04:36:04 -05:00
|
|
|
};
|
2021-03-14 07:03:39 -05:00
|
|
|
|
2021-03-15 11:05:03 -05:00
|
|
|
use crate::{
|
2023-02-19 08:32:24 -06:00
|
|
|
Adt, AsAssocItem, AssocItemContainer, Const, ConstParam, Enum, Field, Function, GenericParam,
|
2023-03-03 09:24:07 -06:00
|
|
|
HasCrate, HasVisibility, LifetimeParam, Macro, Module, Static, Struct, Trait, TraitAlias,
|
|
|
|
TyBuilder, Type, TypeAlias, TypeOrConstParam, TypeParam, Union, Variant,
|
2021-03-15 11:05:03 -05:00
|
|
|
};
|
2021-03-14 07:03:39 -05:00
|
|
|
|
|
|
|
impl HirDisplay for Function {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
2023-02-19 08:32:24 -06:00
|
|
|
let db = f.db;
|
|
|
|
let data = db.function_data(self.id);
|
|
|
|
let container = self.as_assoc_item(db).map(|it| it.container(db));
|
|
|
|
let mut module = self.module(db);
|
|
|
|
if let Some(AssocItemContainer::Impl(_)) = container {
|
|
|
|
// Block-local impls are "hoisted" to the nearest (non-block) module.
|
|
|
|
module = module.nearest_non_block_module(db);
|
|
|
|
}
|
|
|
|
let module_id = module.id;
|
|
|
|
write_visibility(module_id, self.visibility(db), f)?;
|
2022-04-07 11:33:03 -05:00
|
|
|
if data.has_default_kw() {
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str("default ")?;
|
2021-03-14 07:03:39 -05:00
|
|
|
}
|
2022-04-07 11:33:03 -05:00
|
|
|
if data.has_const_kw() {
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str("const ")?;
|
2021-03-14 07:03:39 -05:00
|
|
|
}
|
2022-04-07 11:33:03 -05:00
|
|
|
if data.has_async_kw() {
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str("async ")?;
|
2021-03-14 07:03:39 -05:00
|
|
|
}
|
2023-02-19 08:32:24 -06:00
|
|
|
if self.is_unsafe_to_call(db) {
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str("unsafe ")?;
|
2021-03-14 07:03:39 -05:00
|
|
|
}
|
2021-04-03 13:58:42 -05:00
|
|
|
if let Some(abi) = &data.abi {
|
2021-03-14 07:03:39 -05:00
|
|
|
// FIXME: String escape?
|
2021-04-03 13:58:42 -05:00
|
|
|
write!(f, "extern \"{}\" ", &**abi)?;
|
2021-03-14 07:03:39 -05:00
|
|
|
}
|
|
|
|
write!(f, "fn {}", data.name)?;
|
|
|
|
|
|
|
|
write_generic_params(GenericDefId::FunctionId(self.id), f)?;
|
|
|
|
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_char('(')?;
|
2021-03-14 04:36:04 -05:00
|
|
|
|
2022-07-20 08:02:08 -05:00
|
|
|
let write_self_param = |ty: &TypeRef, f: &mut HirFormatter<'_>| match ty {
|
2022-04-07 08:41:07 -05:00
|
|
|
TypeRef::Path(p) if p.is_self_type() => f.write_str("self"),
|
2023-02-19 04:02:51 -06:00
|
|
|
TypeRef::Reference(inner, lifetime, mut_) if matches!(&**inner, TypeRef::Path(p) if p.is_self_type()) =>
|
2021-03-14 07:03:39 -05:00
|
|
|
{
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_char('&')?;
|
2021-03-14 07:03:39 -05:00
|
|
|
if let Some(lifetime) = lifetime {
|
|
|
|
write!(f, "{} ", lifetime.name)?;
|
|
|
|
}
|
|
|
|
if let hir_def::type_ref::Mutability::Mut = mut_ {
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str("mut ")?;
|
2021-03-14 07:03:39 -05:00
|
|
|
}
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str("self")
|
2021-03-14 07:03:39 -05:00
|
|
|
}
|
|
|
|
_ => {
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str("self: ")?;
|
2021-03-14 07:03:39 -05:00
|
|
|
ty.hir_fmt(f)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut first = true;
|
2023-04-06 13:14:51 -05:00
|
|
|
// FIXME: Use resolved `param.ty` once we no longer discard lifetimes
|
|
|
|
for (type_ref, param) in data.params.iter().zip(self.assoc_fn_params(db)) {
|
|
|
|
let local = param.as_local(db).map(|it| it.name(db));
|
2021-03-14 07:03:39 -05:00
|
|
|
if !first {
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str(", ")?;
|
2021-03-14 07:03:39 -05:00
|
|
|
} else {
|
|
|
|
first = false;
|
2023-04-06 13:14:51 -05:00
|
|
|
if local == Some(name!(self)) {
|
2021-03-14 07:03:39 -05:00
|
|
|
write_self_param(type_ref, f)?;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2023-04-06 13:14:51 -05:00
|
|
|
match local {
|
2022-12-23 12:42:58 -06:00
|
|
|
Some(name) => write!(f, "{name}: ")?,
|
2022-04-07 08:41:07 -05:00
|
|
|
None => f.write_str("_: ")?,
|
2021-03-14 07:03:39 -05:00
|
|
|
}
|
|
|
|
type_ref.hir_fmt(f)?;
|
|
|
|
}
|
2022-03-04 15:24:13 -06:00
|
|
|
|
|
|
|
if data.is_varargs() {
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str(", ...")?;
|
2022-03-04 15:24:13 -06:00
|
|
|
}
|
|
|
|
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_char(')')?;
|
2021-03-14 07:03:39 -05:00
|
|
|
|
|
|
|
// `FunctionData::ret_type` will be `::core::future::Future<Output = ...>` for async fns.
|
|
|
|
// Use ugly pattern match to strip the Future trait.
|
|
|
|
// Better way?
|
2022-04-07 11:33:03 -05:00
|
|
|
let ret_type = if !data.has_async_kw() {
|
2021-03-14 07:03:39 -05:00
|
|
|
&data.ret_type
|
|
|
|
} else {
|
2021-04-01 12:46:43 -05:00
|
|
|
match &*data.ret_type {
|
2021-05-24 08:13:23 -05:00
|
|
|
TypeRef::ImplTrait(bounds) => match bounds[0].as_ref() {
|
2021-06-06 13:41:15 -05:00
|
|
|
TypeBound::Path(path, _) => {
|
2021-03-14 07:03:39 -05:00
|
|
|
path.segments().iter().last().unwrap().args_and_bindings.unwrap().bindings
|
|
|
|
[0]
|
|
|
|
.type_ref
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
_ => panic!("Async fn ret_type should be impl Future"),
|
|
|
|
},
|
|
|
|
_ => panic!("Async fn ret_type should be impl Future"),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match ret_type {
|
|
|
|
TypeRef::Tuple(tup) if tup.is_empty() => {}
|
|
|
|
ty => {
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str(" -> ")?;
|
2021-03-14 07:03:39 -05:00
|
|
|
ty.hir_fmt(f)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
write_where_clause(GenericDefId::FunctionId(self.id), f)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2021-03-14 04:36:04 -05:00
|
|
|
|
2021-03-16 10:36:34 -05:00
|
|
|
impl HirDisplay for Adt {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
2021-03-16 10:36:34 -05:00
|
|
|
match self {
|
|
|
|
Adt::Struct(it) => it.hir_fmt(f),
|
|
|
|
Adt::Union(it) => it.hir_fmt(f),
|
|
|
|
Adt::Enum(it) => it.hir_fmt(f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 11:05:03 -05:00
|
|
|
impl HirDisplay for Struct {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
2021-03-15 11:05:03 -05:00
|
|
|
write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str("struct ")?;
|
2021-03-15 11:05:03 -05:00
|
|
|
write!(f, "{}", self.name(f.db))?;
|
|
|
|
let def_id = GenericDefId::AdtId(AdtId::StructId(self.id));
|
|
|
|
write_generic_params(def_id, f)?;
|
|
|
|
write_where_clause(def_id, f)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HirDisplay for Enum {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
2021-03-15 11:05:03 -05:00
|
|
|
write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str("enum ")?;
|
2021-03-15 11:05:03 -05:00
|
|
|
write!(f, "{}", self.name(f.db))?;
|
|
|
|
let def_id = GenericDefId::AdtId(AdtId::EnumId(self.id));
|
|
|
|
write_generic_params(def_id, f)?;
|
|
|
|
write_where_clause(def_id, f)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HirDisplay for Union {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
2021-03-15 11:05:03 -05:00
|
|
|
write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str("union ")?;
|
2021-03-15 11:05:03 -05:00
|
|
|
write!(f, "{}", self.name(f.db))?;
|
|
|
|
let def_id = GenericDefId::AdtId(AdtId::UnionId(self.id));
|
|
|
|
write_generic_params(def_id, f)?;
|
|
|
|
write_where_clause(def_id, f)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HirDisplay for Field {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
2021-03-15 11:05:03 -05:00
|
|
|
write_visibility(self.parent.module(f.db).id, self.visibility(f.db), f)?;
|
|
|
|
write!(f, "{}: ", self.name(f.db))?;
|
2021-04-30 03:52:31 -05:00
|
|
|
self.ty(f.db).hir_fmt(f)
|
2021-03-15 11:05:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HirDisplay for Variant {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
2021-03-15 11:05:03 -05:00
|
|
|
write!(f, "{}", self.name(f.db))?;
|
|
|
|
let data = self.variant_data(f.db);
|
|
|
|
match &*data {
|
|
|
|
VariantData::Unit => {}
|
|
|
|
VariantData::Tuple(fields) => {
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_char('(')?;
|
2021-03-15 11:05:03 -05:00
|
|
|
let mut first = true;
|
|
|
|
for (_, field) in fields.iter() {
|
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
} else {
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str(", ")?;
|
2021-03-15 11:05:03 -05:00
|
|
|
}
|
|
|
|
// Enum variant fields must be pub.
|
|
|
|
field.type_ref.hir_fmt(f)?;
|
|
|
|
}
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_char(')')?;
|
2021-03-15 11:05:03 -05:00
|
|
|
}
|
|
|
|
VariantData::Record(fields) => {
|
2022-05-02 05:38:33 -05:00
|
|
|
f.write_str(" {")?;
|
2021-03-15 11:05:03 -05:00
|
|
|
let mut first = true;
|
|
|
|
for (_, field) in fields.iter() {
|
|
|
|
if first {
|
|
|
|
first = false;
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_char(' ')?;
|
2021-03-15 11:05:03 -05:00
|
|
|
} else {
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str(", ")?;
|
2021-03-15 11:05:03 -05:00
|
|
|
}
|
|
|
|
// Enum variant fields must be pub.
|
|
|
|
write!(f, "{}: ", field.name)?;
|
|
|
|
field.type_ref.hir_fmt(f)?;
|
|
|
|
}
|
2022-05-02 05:38:33 -05:00
|
|
|
f.write_str(" }")?;
|
2021-03-15 11:05:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 04:36:04 -05:00
|
|
|
impl HirDisplay for Type {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
2021-03-21 14:19:07 -05:00
|
|
|
self.ty.hir_fmt(f)
|
2021-03-14 04:36:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 10:36:34 -05:00
|
|
|
impl HirDisplay for GenericParam {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
2021-03-16 10:36:34 -05:00
|
|
|
match self {
|
|
|
|
GenericParam::TypeParam(it) => it.hir_fmt(f),
|
|
|
|
GenericParam::ConstParam(it) => it.hir_fmt(f),
|
2021-12-29 07:35:59 -06:00
|
|
|
GenericParam::LifetimeParam(it) => it.hir_fmt(f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HirDisplay for TypeOrConstParam {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
2021-12-29 07:35:59 -06:00
|
|
|
match self.split(f.db) {
|
|
|
|
either::Either::Left(x) => x.hir_fmt(f),
|
|
|
|
either::Either::Right(x) => x.hir_fmt(f),
|
2021-03-16 10:36:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 04:36:04 -05:00
|
|
|
impl HirDisplay for TypeParam {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
2021-03-14 04:36:04 -05:00
|
|
|
write!(f, "{}", self.name(f.db))?;
|
2021-09-10 10:48:39 -05:00
|
|
|
if f.omit_verbose_types() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2021-12-29 07:35:59 -06:00
|
|
|
let bounds = f.db.generic_predicates_for_param(self.id.parent(), self.id.into(), None);
|
2022-03-09 12:50:24 -06:00
|
|
|
let substs = TyBuilder::placeholder_subst(f.db, self.id.parent());
|
2021-09-10 10:48:39 -05:00
|
|
|
let predicates: Vec<_> =
|
2021-12-19 10:58:39 -06:00
|
|
|
bounds.iter().cloned().map(|b| b.substitute(Interner, &substs)).collect();
|
2021-12-29 07:35:59 -06:00
|
|
|
let krate = self.id.parent().krate(f.db).id;
|
2021-09-10 10:48:39 -05:00
|
|
|
let sized_trait =
|
2023-01-21 10:29:07 -06:00
|
|
|
f.db.lang_item(krate, LangItem::Sized).and_then(|lang_item| lang_item.as_trait());
|
2021-09-10 10:48:39 -05:00
|
|
|
let has_only_sized_bound = predicates.iter().all(move |pred| match pred.skip_binders() {
|
|
|
|
WhereClause::Implemented(it) => Some(it.hir_trait_id()) == sized_trait,
|
|
|
|
_ => false,
|
|
|
|
});
|
|
|
|
let has_only_not_sized_bound = predicates.is_empty();
|
|
|
|
if !has_only_sized_bound || has_only_not_sized_bound {
|
|
|
|
let default_sized = SizedByDefault::Sized { anchor: krate };
|
2023-01-14 06:27:32 -06:00
|
|
|
write_bounds_like_dyn_trait_with_prefix(f, ":", &predicates, default_sized)?;
|
2021-03-14 04:36:04 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2021-03-14 07:03:39 -05:00
|
|
|
|
2021-03-16 10:36:34 -05:00
|
|
|
impl HirDisplay for LifetimeParam {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
2021-03-16 10:36:34 -05:00
|
|
|
write!(f, "{}", self.name(f.db))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 11:05:03 -05:00
|
|
|
impl HirDisplay for ConstParam {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
2021-03-15 11:05:03 -05:00
|
|
|
write!(f, "const {}: ", self.name(f.db))?;
|
|
|
|
self.ty(f.db).hir_fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:06:15 -05:00
|
|
|
fn write_generic_params(
|
|
|
|
def: GenericDefId,
|
|
|
|
f: &mut HirFormatter<'_>,
|
|
|
|
) -> Result<(), HirDisplayError> {
|
2021-03-14 07:03:39 -05:00
|
|
|
let params = f.db.generic_params(def);
|
2021-03-15 11:58:29 -05:00
|
|
|
if params.lifetimes.is_empty()
|
2022-03-09 12:50:24 -06:00
|
|
|
&& params.type_or_consts.iter().all(|x| x.1.const_param().is_none())
|
2021-03-15 11:58:29 -05:00
|
|
|
&& params
|
2022-03-09 12:50:24 -06:00
|
|
|
.type_or_consts
|
2021-03-15 11:58:29 -05:00
|
|
|
.iter()
|
2021-12-29 07:35:59 -06:00
|
|
|
.filter_map(|x| x.1.type_param())
|
|
|
|
.all(|param| !matches!(param.provenance, TypeParamProvenance::TypeParamList))
|
2021-03-15 11:58:29 -05:00
|
|
|
{
|
2021-03-14 07:03:39 -05:00
|
|
|
return Ok(());
|
|
|
|
}
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_char('<')?;
|
2021-03-14 07:03:39 -05:00
|
|
|
|
|
|
|
let mut first = true;
|
2022-07-20 08:02:08 -05:00
|
|
|
let mut delim = |f: &mut HirFormatter<'_>| {
|
2021-03-14 07:03:39 -05:00
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str(", ")
|
2021-03-14 07:03:39 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
for (_, lifetime) in params.lifetimes.iter() {
|
|
|
|
delim(f)?;
|
|
|
|
write!(f, "{}", lifetime.name)?;
|
|
|
|
}
|
2022-03-09 12:50:24 -06:00
|
|
|
for (_, ty) in params.type_or_consts.iter() {
|
2021-12-29 07:35:59 -06:00
|
|
|
if let Some(name) = &ty.name() {
|
|
|
|
match ty {
|
|
|
|
TypeOrConstParamData::TypeParamData(ty) => {
|
|
|
|
if ty.provenance != TypeParamProvenance::TypeParamList {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
delim(f)?;
|
2022-12-23 12:42:58 -06:00
|
|
|
write!(f, "{name}")?;
|
2021-12-29 07:35:59 -06:00
|
|
|
if let Some(default) = &ty.default {
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str(" = ")?;
|
2021-12-29 07:35:59 -06:00
|
|
|
default.hir_fmt(f)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TypeOrConstParamData::ConstParamData(c) => {
|
|
|
|
delim(f)?;
|
2022-12-23 12:42:58 -06:00
|
|
|
write!(f, "const {name}: ")?;
|
2021-12-29 07:35:59 -06:00
|
|
|
c.ty.hir_fmt(f)?;
|
|
|
|
}
|
2021-03-14 07:03:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_char('>')?;
|
2021-03-14 07:03:39 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:02:08 -05:00
|
|
|
fn write_where_clause(def: GenericDefId, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
2021-03-14 07:03:39 -05:00
|
|
|
let params = f.db.generic_params(def);
|
2021-11-20 05:30:53 -06:00
|
|
|
|
|
|
|
// unnamed type targets are displayed inline with the argument itself, e.g. `f: impl Y`.
|
|
|
|
let is_unnamed_type_target = |target: &WherePredicateTypeTarget| match target {
|
|
|
|
WherePredicateTypeTarget::TypeRef(_) => false,
|
2022-03-09 12:50:24 -06:00
|
|
|
WherePredicateTypeTarget::TypeOrConstParam(id) => {
|
|
|
|
params.type_or_consts[*id].name().is_none()
|
|
|
|
}
|
2021-11-20 05:30:53 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
let has_displayable_predicate = params
|
|
|
|
.where_predicates
|
|
|
|
.iter()
|
|
|
|
.any(|pred| {
|
|
|
|
!matches!(pred, WherePredicate::TypeBound { target, .. } if is_unnamed_type_target(target))
|
|
|
|
});
|
|
|
|
|
|
|
|
if !has_displayable_predicate {
|
2021-03-14 07:03:39 -05:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:02:08 -05:00
|
|
|
let write_target = |target: &WherePredicateTypeTarget, f: &mut HirFormatter<'_>| match target {
|
2021-03-14 07:03:39 -05:00
|
|
|
WherePredicateTypeTarget::TypeRef(ty) => ty.hir_fmt(f),
|
2022-03-09 12:50:24 -06:00
|
|
|
WherePredicateTypeTarget::TypeOrConstParam(id) => {
|
|
|
|
match ¶ms.type_or_consts[*id].name() {
|
2022-12-23 12:42:58 -06:00
|
|
|
Some(name) => write!(f, "{name}"),
|
2022-05-02 05:38:33 -05:00
|
|
|
None => f.write_str("{unnamed}"),
|
2022-03-09 12:50:24 -06:00
|
|
|
}
|
|
|
|
}
|
2021-03-14 07:03:39 -05:00
|
|
|
};
|
|
|
|
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str("\nwhere")?;
|
2021-03-14 07:03:39 -05:00
|
|
|
|
|
|
|
for (pred_idx, pred) in params.where_predicates.iter().enumerate() {
|
|
|
|
let prev_pred =
|
|
|
|
if pred_idx == 0 { None } else { Some(¶ms.where_predicates[pred_idx - 1]) };
|
|
|
|
|
2022-07-20 08:06:15 -05:00
|
|
|
let new_predicate = |f: &mut HirFormatter<'_>| {
|
|
|
|
f.write_str(if pred_idx == 0 { "\n " } else { ",\n " })
|
|
|
|
};
|
2021-03-14 07:03:39 -05:00
|
|
|
|
|
|
|
match pred {
|
2021-11-20 05:30:53 -06:00
|
|
|
WherePredicate::TypeBound { target, .. } if is_unnamed_type_target(target) => {}
|
2021-03-14 07:03:39 -05:00
|
|
|
WherePredicate::TypeBound { target, bound } => {
|
|
|
|
if matches!(prev_pred, Some(WherePredicate::TypeBound { target: target_, .. }) if target_ == target)
|
|
|
|
{
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str(" + ")?;
|
2021-03-14 07:03:39 -05:00
|
|
|
} else {
|
|
|
|
new_predicate(f)?;
|
|
|
|
write_target(target, f)?;
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str(": ")?;
|
2021-03-14 07:03:39 -05:00
|
|
|
}
|
|
|
|
bound.hir_fmt(f)?;
|
|
|
|
}
|
|
|
|
WherePredicate::Lifetime { target, bound } => {
|
|
|
|
if matches!(prev_pred, Some(WherePredicate::Lifetime { target: target_, .. }) if target_ == target)
|
|
|
|
{
|
|
|
|
write!(f, " + {}", bound.name)?;
|
|
|
|
} else {
|
|
|
|
new_predicate(f)?;
|
|
|
|
write!(f, "{}: {}", target.name, bound.name)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WherePredicate::ForLifetime { lifetimes, target, bound } => {
|
|
|
|
if matches!(
|
|
|
|
prev_pred,
|
|
|
|
Some(WherePredicate::ForLifetime { lifetimes: lifetimes_, target: target_, .. })
|
|
|
|
if lifetimes_ == lifetimes && target_ == target,
|
|
|
|
) {
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str(" + ")?;
|
2021-03-14 07:03:39 -05:00
|
|
|
} else {
|
|
|
|
new_predicate(f)?;
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str("for<")?;
|
2021-03-14 07:03:39 -05:00
|
|
|
for (idx, lifetime) in lifetimes.iter().enumerate() {
|
|
|
|
if idx != 0 {
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str(", ")?;
|
2021-03-14 07:03:39 -05:00
|
|
|
}
|
2022-12-23 12:42:58 -06:00
|
|
|
write!(f, "{lifetime}")?;
|
2021-03-14 07:03:39 -05:00
|
|
|
}
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str("> ")?;
|
2021-03-14 07:03:39 -05:00
|
|
|
write_target(target, f)?;
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str(": ")?;
|
2021-03-14 07:03:39 -05:00
|
|
|
}
|
|
|
|
bound.hir_fmt(f)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// End of final predicate. There must be at least one predicate here.
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_char(',')?;
|
2021-03-14 07:03:39 -05:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-03-15 11:05:03 -05:00
|
|
|
|
|
|
|
impl HirDisplay for Const {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
2023-02-19 08:32:24 -06:00
|
|
|
let db = f.db;
|
|
|
|
let container = self.as_assoc_item(db).map(|it| it.container(db));
|
|
|
|
let mut module = self.module(db);
|
|
|
|
if let Some(AssocItemContainer::Impl(_)) = container {
|
|
|
|
// Block-local impls are "hoisted" to the nearest (non-block) module.
|
|
|
|
module = module.nearest_non_block_module(db);
|
|
|
|
}
|
|
|
|
write_visibility(module.id, self.visibility(db), f)?;
|
|
|
|
let data = db.const_data(self.id);
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str("const ")?;
|
2021-03-15 11:05:03 -05:00
|
|
|
match &data.name {
|
2022-12-23 12:42:58 -06:00
|
|
|
Some(name) => write!(f, "{name}: ")?,
|
2022-04-07 08:41:07 -05:00
|
|
|
None => f.write_str("_: ")?,
|
2021-03-15 11:05:03 -05:00
|
|
|
}
|
|
|
|
data.type_ref.hir_fmt(f)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HirDisplay for Static {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
2021-03-15 11:05:03 -05:00
|
|
|
write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
|
|
|
|
let data = f.db.static_data(self.id);
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str("static ")?;
|
2021-03-15 11:05:03 -05:00
|
|
|
if data.mutable {
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str("mut ")?;
|
2021-03-15 11:05:03 -05:00
|
|
|
}
|
2021-11-10 15:02:50 -06:00
|
|
|
write!(f, "{}: ", &data.name)?;
|
2021-03-15 11:05:03 -05:00
|
|
|
data.type_ref.hir_fmt(f)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HirDisplay for Trait {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
2021-03-15 11:05:03 -05:00
|
|
|
write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
|
|
|
|
let data = f.db.trait_data(self.id);
|
|
|
|
if data.is_unsafe {
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str("unsafe ")?;
|
2021-03-15 11:05:03 -05:00
|
|
|
}
|
|
|
|
if data.is_auto {
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str("auto ")?;
|
2021-03-15 11:05:03 -05:00
|
|
|
}
|
|
|
|
write!(f, "trait {}", data.name)?;
|
|
|
|
let def_id = GenericDefId::TraitId(self.id);
|
|
|
|
write_generic_params(def_id, f)?;
|
|
|
|
write_where_clause(def_id, f)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 09:24:07 -06:00
|
|
|
impl HirDisplay for TraitAlias {
|
|
|
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
|
|
|
write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
|
|
|
|
let data = f.db.trait_alias_data(self.id);
|
|
|
|
write!(f, "trait {}", data.name)?;
|
|
|
|
let def_id = GenericDefId::TraitAliasId(self.id);
|
|
|
|
write_generic_params(def_id, f)?;
|
|
|
|
f.write_str(" = ")?;
|
|
|
|
// FIXME: Currently we lower every bounds in a trait alias as a trait bound on `Self` i.e.
|
|
|
|
// `trait Foo = Bar` is stored and displayed as `trait Foo = where Self: Bar`, which might
|
|
|
|
// be less readable.
|
|
|
|
write_where_clause(def_id, f)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 11:05:03 -05:00
|
|
|
impl HirDisplay for TypeAlias {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
2021-03-15 11:05:03 -05:00
|
|
|
write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
|
|
|
|
let data = f.db.type_alias_data(self.id);
|
|
|
|
write!(f, "type {}", data.name)?;
|
2022-09-30 17:21:29 -05:00
|
|
|
let def_id = GenericDefId::TypeAliasId(self.id);
|
|
|
|
write_generic_params(def_id, f)?;
|
|
|
|
write_where_clause(def_id, f)?;
|
2021-03-15 11:05:03 -05:00
|
|
|
if !data.bounds.is_empty() {
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str(": ")?;
|
2021-03-15 11:05:03 -05:00
|
|
|
f.write_joined(&data.bounds, " + ")?;
|
|
|
|
}
|
|
|
|
if let Some(ty) = &data.type_ref {
|
2022-04-07 08:41:07 -05:00
|
|
|
f.write_str(" = ")?;
|
2021-03-15 11:05:03 -05:00
|
|
|
ty.hir_fmt(f)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HirDisplay for Module {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
2021-03-15 11:05:03 -05:00
|
|
|
// FIXME: Module doesn't have visibility saved in data.
|
|
|
|
match self.name(f.db) {
|
2022-12-23 12:42:58 -06:00
|
|
|
Some(name) => write!(f, "mod {name}"),
|
2022-07-14 16:22:54 -05:00
|
|
|
None if self.is_crate_root(f.db) => match self.krate(f.db).display_name(f.db) {
|
2022-12-23 12:42:58 -06:00
|
|
|
Some(name) => write!(f, "extern crate {name}"),
|
2022-05-02 05:38:33 -05:00
|
|
|
None => f.write_str("extern crate {unknown}"),
|
2021-03-15 12:24:21 -05:00
|
|
|
},
|
2022-05-02 05:38:33 -05:00
|
|
|
None => f.write_str("mod {unnamed}"),
|
2021-03-15 11:05:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-10 15:21:58 -06:00
|
|
|
|
|
|
|
impl HirDisplay for Macro {
|
2022-07-20 08:02:08 -05:00
|
|
|
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
|
2022-03-10 15:21:58 -06:00
|
|
|
match self.id {
|
2022-04-07 08:41:07 -05:00
|
|
|
hir_def::MacroId::Macro2Id(_) => f.write_str("macro"),
|
|
|
|
hir_def::MacroId::MacroRulesId(_) => f.write_str("macro_rules!"),
|
|
|
|
hir_def::MacroId::ProcMacroId(_) => f.write_str("proc_macro"),
|
2022-03-10 15:21:58 -06:00
|
|
|
}?;
|
|
|
|
write!(f, " {}", self.name(f.db))
|
|
|
|
}
|
|
|
|
}
|