1 //! FIXME: write short doc here
8 use chalk_ir::BoundVar;
12 generics::TypeParamProvenance,
14 path::{Path, PathKind},
15 type_ref::{TypeBound, TypeRef},
16 visibility::Visibility,
17 AssocContainerId, Lookup, ModuleId, TraitId,
19 use hir_expand::name::Name;
22 const_from_placeholder_idx, db::HirDatabase, from_assoc_type_id, from_foreign_def_id,
23 from_placeholder_idx, lt_from_placeholder_idx, primitive, subst_prefix, to_assoc_type_id,
24 traits::chalk::from_chalk, utils::generics, AdtId, AliasEq, AliasTy, CallableDefId,
25 CallableSig, Const, ConstValue, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime,
26 LifetimeData, LifetimeOutlives, Mutability, OpaqueTy, ProjectionTy, ProjectionTyExt,
27 QuantifiedWhereClause, Scalar, TraitRef, Ty, TyExt, TyKind, WhereClause,
30 pub struct HirFormatter<'a> {
31 pub db: &'a dyn HirDatabase,
32 fmt: &'a mut dyn fmt::Write,
35 pub(crate) max_size: Option<usize>,
36 omit_verbose_types: bool,
37 display_target: DisplayTarget,
40 pub trait HirDisplay {
41 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError>;
43 /// Returns a `Display`able type that is human-readable.
44 fn into_displayable<'a>(
46 db: &'a dyn HirDatabase,
47 max_size: Option<usize>,
48 omit_verbose_types: bool,
49 display_target: DisplayTarget,
50 ) -> HirDisplayWrapper<'a, Self>
54 HirDisplayWrapper { db, t: self, max_size, omit_verbose_types, display_target }
57 /// Returns a `Display`able type that is human-readable.
58 /// Use this for showing types to the user (e.g. diagnostics)
59 fn display<'a>(&'a self, db: &'a dyn HirDatabase) -> HirDisplayWrapper<'a, Self>
67 omit_verbose_types: false,
68 display_target: DisplayTarget::Diagnostics,
72 /// Returns a `Display`able type that is human-readable and tries to be succinct.
73 /// Use this for showing types to the user where space is constrained (e.g. doc popups)
74 fn display_truncated<'a>(
76 db: &'a dyn HirDatabase,
77 max_size: Option<usize>,
78 ) -> HirDisplayWrapper<'a, Self>
86 omit_verbose_types: true,
87 display_target: DisplayTarget::Diagnostics,
91 /// Returns a String representation of `self` that can be inserted into the given module.
92 /// Use this when generating code (e.g. assists)
93 fn display_source_code<'a>(
95 db: &'a dyn HirDatabase,
97 ) -> Result<String, DisplaySourceCodeError> {
98 let mut result = String::new();
99 match self.hir_fmt(&mut HirFormatter {
102 buf: String::with_capacity(20),
105 omit_verbose_types: false,
106 display_target: DisplayTarget::SourceCode { module_id },
109 Err(HirDisplayError::FmtError) => panic!("Writing to String can't fail!"),
110 Err(HirDisplayError::DisplaySourceCodeError(e)) => return Err(e),
115 /// Returns a String representation of `self` for test purposes
116 fn display_test<'a>(&'a self, db: &'a dyn HirDatabase) -> HirDisplayWrapper<'a, Self>
124 omit_verbose_types: false,
125 display_target: DisplayTarget::Test,
130 impl<'a> HirFormatter<'a> {
131 pub fn write_joined<T: HirDisplay>(
133 iter: impl IntoIterator<Item = T>,
135 ) -> Result<(), HirDisplayError> {
136 let mut first = true;
139 write!(self, "{}", sep)?;
147 /// This allows using the `write!` macro directly with a `HirFormatter`.
148 pub fn write_fmt(&mut self, args: fmt::Arguments) -> Result<(), HirDisplayError> {
149 // We write to a buffer first to track output size
151 fmt::write(&mut self.buf, args)?;
152 self.curr_size += self.buf.len();
154 // Then we write to the internal formatter from the buffer
155 self.fmt.write_str(&self.buf).map_err(HirDisplayError::from)
158 pub fn should_truncate(&self) -> bool {
159 if let Some(max_size) = self.max_size {
160 self.curr_size >= max_size
166 pub fn omit_verbose_types(&self) -> bool {
167 self.omit_verbose_types
171 #[derive(Clone, Copy)]
172 pub enum DisplayTarget {
173 /// Display types for inlays, doc popups, autocompletion, etc...
174 /// Showing `{unknown}` or not qualifying paths is fine here.
175 /// There's no reason for this to fail.
177 /// Display types for inserting them in source files.
178 /// The generated code should compile, so paths need to be qualified.
179 SourceCode { module_id: ModuleId },
180 /// Only for test purpose to keep real types
185 fn is_source_code(&self) -> bool {
186 matches!(self, Self::SourceCode { .. })
188 fn is_test(&self) -> bool {
189 matches!(self, Self::Test)
194 pub enum DisplaySourceCodeError {
200 pub enum HirDisplayError {
201 /// Errors that can occur when generating source code
202 DisplaySourceCodeError(DisplaySourceCodeError),
203 /// `FmtError` is required to be compatible with std::fmt::Display
206 impl From<fmt::Error> for HirDisplayError {
207 fn from(_: fmt::Error) -> Self {
212 pub struct HirDisplayWrapper<'a, T> {
213 db: &'a dyn HirDatabase,
215 max_size: Option<usize>,
216 omit_verbose_types: bool,
217 display_target: DisplayTarget,
220 impl<'a, T> fmt::Display for HirDisplayWrapper<'a, T>
224 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
225 match self.t.hir_fmt(&mut HirFormatter {
228 buf: String::with_capacity(20),
230 max_size: self.max_size,
231 omit_verbose_types: self.omit_verbose_types,
232 display_target: self.display_target,
235 Err(HirDisplayError::FmtError) => Err(fmt::Error),
236 Err(HirDisplayError::DisplaySourceCodeError(_)) => {
237 // This should never happen
238 panic!("HirDisplay failed when calling Display::fmt!")
244 const TYPE_HINT_TRUNCATION: &str = "…";
246 impl<T: HirDisplay> HirDisplay for &'_ T {
247 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
248 HirDisplay::hir_fmt(*self, f)
252 impl HirDisplay for ProjectionTy {
253 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
254 if f.should_truncate() {
255 return write!(f, "{}", TYPE_HINT_TRUNCATION);
258 let trait_ = f.db.trait_data(self.trait_(f.db));
260 self.self_type_parameter(&Interner).hir_fmt(f)?;
261 write!(f, " as {}", trait_.name)?;
262 if self.substitution.len(&Interner) > 1 {
264 f.write_joined(&self.substitution.interned()[1..], ", ")?;
267 write!(f, ">::{}", f.db.type_alias_data(from_assoc_type_id(self.associated_ty_id)).name)?;
272 impl HirDisplay for OpaqueTy {
273 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
274 if f.should_truncate() {
275 return write!(f, "{}", TYPE_HINT_TRUNCATION);
278 self.substitution.at(&Interner, 0).hir_fmt(f)
282 impl HirDisplay for GenericArg {
283 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
284 match self.interned() {
285 crate::GenericArgData::Ty(ty) => ty.hir_fmt(f),
290 impl HirDisplay for Const {
291 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
292 let data = self.interned();
294 ConstValue::BoundVar(idx) => idx.hir_fmt(f),
295 ConstValue::InferenceVar(..) => write!(f, "_"),
296 ConstValue::Placeholder(idx) => {
297 let id = const_from_placeholder_idx(f.db, idx);
298 let generics = generics(f.db.upcast(), id.parent);
299 let param_data = &generics.params.consts[id.local_id];
300 write!(f, "{}", param_data.name)
302 ConstValue::Concrete(_) => write!(f, "_"),
307 impl HirDisplay for BoundVar {
308 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
309 write!(f, "?{}.{}", self.debruijn.depth(), self.index)
313 impl HirDisplay for Ty {
314 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
315 if f.should_truncate() {
316 return write!(f, "{}", TYPE_HINT_TRUNCATION);
319 match self.kind(&Interner) {
320 TyKind::Never => write!(f, "!")?,
321 TyKind::Str => write!(f, "str")?,
322 TyKind::Scalar(Scalar::Bool) => write!(f, "bool")?,
323 TyKind::Scalar(Scalar::Char) => write!(f, "char")?,
324 &TyKind::Scalar(Scalar::Float(t)) => write!(f, "{}", primitive::float_ty_to_string(t))?,
325 &TyKind::Scalar(Scalar::Int(t)) => write!(f, "{}", primitive::int_ty_to_string(t))?,
326 &TyKind::Scalar(Scalar::Uint(t)) => write!(f, "{}", primitive::uint_ty_to_string(t))?,
327 TyKind::Slice(t) => {
332 TyKind::Array(t, c) => {
339 TyKind::Raw(m, t) | TyKind::Ref(m, _, t) => {
340 if matches!(self.kind(&Interner), TyKind::Raw(..)) {
345 Mutability::Not => "const ",
346 Mutability::Mut => "mut ",
354 Mutability::Not => "",
355 Mutability::Mut => "mut ",
360 // FIXME: all this just to decide whether to use parentheses...
362 let predicates: Vec<_> = match t.kind(&Interner) {
363 TyKind::Dyn(dyn_ty) if dyn_ty.bounds.skip_binders().interned().len() > 1 => {
364 dyn_ty.bounds.skip_binders().interned().iter().cloned().collect()
366 &TyKind::Alias(AliasTy::Opaque(OpaqueTy {
368 substitution: ref parameters,
370 let impl_trait_id = f.db.lookup_intern_impl_trait_id(opaque_ty_id.into());
371 if let ImplTraitId::ReturnTypeImplTrait(func, idx) = impl_trait_id {
373 f.db.return_type_impl_traits(func)
374 .expect("impl trait id without data");
377 .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone());
378 let bounds = data.substitute(&Interner, parameters);
379 bounds.into_value_and_skipped_binders().0
387 if let Some(WhereClause::Implemented(trait_ref)) =
388 predicates.get(0).map(|b| b.skip_binders())
390 let trait_ = trait_ref.hir_trait_id();
391 if fn_traits(f.db.upcast(), trait_).any(|it| it == trait_)
392 && predicates.len() <= 2
398 if predicates.len() > 1 {
406 TyKind::Tuple(_, substs) => {
407 if substs.len(&Interner) == 1 {
409 substs.at(&Interner, 0).hir_fmt(f)?;
413 f.write_joined(&*substs.interned(), ", ")?;
417 TyKind::Function(fn_ptr) => {
418 let sig = CallableSig::from_fn_ptr(fn_ptr);
421 TyKind::FnDef(def, parameters) => {
422 let def = from_chalk(f.db, *def);
423 let sig = f.db.callable_item_signature(def).substitute(&Interner, parameters);
425 CallableDefId::FunctionId(ff) => {
426 write!(f, "fn {}", f.db.function_data(ff).name)?
428 CallableDefId::StructId(s) => write!(f, "{}", f.db.struct_data(s).name)?,
429 CallableDefId::EnumVariantId(e) => {
430 write!(f, "{}", f.db.enum_data(e.parent).variants[e.local_id].name)?
433 if parameters.len(&Interner) > 0 {
434 let generics = generics(f.db.upcast(), def.into());
435 let (parent_params, self_param, type_params, _impl_trait_params) =
436 generics.provenance_split();
437 let total_len = parent_params + self_param + type_params;
438 // We print all params except implicit impl Trait params. Still a bit weird; should we leave out parent and self?
441 f.write_joined(¶meters.interned()[..total_len], ", ")?;
446 f.write_joined(sig.params(), ", ")?;
454 TyKind::Adt(AdtId(def_id), parameters) => {
455 match f.display_target {
456 DisplayTarget::Diagnostics | DisplayTarget::Test => {
457 let name = match *def_id {
458 hir_def::AdtId::StructId(it) => f.db.struct_data(it).name.clone(),
459 hir_def::AdtId::UnionId(it) => f.db.union_data(it).name.clone(),
460 hir_def::AdtId::EnumId(it) => f.db.enum_data(it).name.clone(),
462 write!(f, "{}", name)?;
464 DisplayTarget::SourceCode { module_id } => {
465 if let Some(path) = find_path::find_path(
467 ItemInNs::Types((*def_id).into()),
470 write!(f, "{}", path)?;
472 return Err(HirDisplayError::DisplaySourceCodeError(
473 DisplaySourceCodeError::PathNotFound,
479 if parameters.len(&Interner) > 0 {
480 let parameters_to_write = if f.display_target.is_source_code()
481 || f.omit_verbose_types()
484 .as_generic_def(f.db)
485 .map(|generic_def_id| f.db.generic_defaults(generic_def_id))
486 .filter(|defaults| !defaults.is_empty())
488 None => parameters.interned().as_ref(),
489 Some(default_parameters) => {
490 let mut default_from = 0;
491 for (i, parameter) in parameters.iter(&Interner).enumerate() {
493 parameter.assert_ty_ref(&Interner).kind(&Interner),
494 default_parameters.get(i),
496 (&TyKind::Error, _) | (_, None) => {
497 default_from = i + 1;
499 (_, Some(default_parameter)) => {
501 default_parameter.clone().substitute(
503 &subst_prefix(parameters, i),
505 if parameter.assert_ty_ref(&Interner) != &actual_default
507 default_from = i + 1;
512 ¶meters.interned()[0..default_from]
516 parameters.interned().as_ref()
518 if !parameters_to_write.is_empty() {
520 f.write_joined(parameters_to_write, ", ")?;
525 TyKind::AssociatedType(assoc_type_id, parameters) => {
526 let type_alias = from_assoc_type_id(*assoc_type_id);
527 let trait_ = match type_alias.lookup(f.db.upcast()).container {
528 AssocContainerId::TraitId(it) => it,
529 _ => panic!("not an associated type"),
531 let trait_ = f.db.trait_data(trait_);
532 let type_alias_data = f.db.type_alias_data(type_alias);
534 // Use placeholder associated types when the target is test (https://rust-lang.github.io/chalk/book/clauses/type_equality.html#placeholder-associated-types)
535 if f.display_target.is_test() {
536 write!(f, "{}::{}", trait_.name, type_alias_data.name)?;
537 if parameters.len(&Interner) > 0 {
539 f.write_joined(&*parameters.interned(), ", ")?;
543 let projection_ty = ProjectionTy {
544 associated_ty_id: to_assoc_type_id(type_alias),
545 substitution: parameters.clone(),
548 projection_ty.hir_fmt(f)?;
551 TyKind::Foreign(type_alias) => {
552 let type_alias = f.db.type_alias_data(from_foreign_def_id(*type_alias));
553 write!(f, "{}", type_alias.name)?;
555 TyKind::OpaqueType(opaque_ty_id, parameters) => {
556 let impl_trait_id = f.db.lookup_intern_impl_trait_id((*opaque_ty_id).into());
557 match impl_trait_id {
558 ImplTraitId::ReturnTypeImplTrait(func, idx) => {
560 f.db.return_type_impl_traits(func).expect("impl trait id without data");
563 .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone());
564 let bounds = data.substitute(&Interner, ¶meters);
565 write_bounds_like_dyn_trait_with_prefix("impl", bounds.skip_binders(), f)?;
566 // FIXME: it would maybe be good to distinguish this from the alias type (when debug printing), and to show the substitution
568 ImplTraitId::AsyncBlockTypeImplTrait(..) => {
569 write!(f, "impl Future<Output = ")?;
570 parameters.at(&Interner, 0).hir_fmt(f)?;
575 TyKind::Closure(.., substs) => {
576 if f.display_target.is_source_code() {
577 return Err(HirDisplayError::DisplaySourceCodeError(
578 DisplaySourceCodeError::Closure,
581 let sig = substs.at(&Interner, 0).assert_ty_ref(&Interner).callable_sig(f.db);
582 if let Some(sig) = sig {
583 if sig.params().is_empty() {
585 } else if f.omit_verbose_types() {
586 write!(f, "|{}|", TYPE_HINT_TRUNCATION)?;
589 f.write_joined(sig.params(), ", ")?;
594 sig.ret().hir_fmt(f)?;
596 write!(f, "{{closure}}")?;
599 TyKind::Placeholder(idx) => {
600 let id = from_placeholder_idx(f.db, *idx);
601 let generics = generics(f.db.upcast(), id.parent);
602 let param_data = &generics.params.types[id.local_id];
603 match param_data.provenance {
604 TypeParamProvenance::TypeParamList | TypeParamProvenance::TraitSelf => {
605 write!(f, "{}", param_data.name.clone().unwrap_or_else(Name::missing))?
607 TypeParamProvenance::ArgumentImplTrait => {
608 let substs = generics.type_params_subst(f.db);
610 f.db.generic_predicates(id.parent)
612 .map(|pred| pred.clone().substitute(&Interner, &substs))
613 .filter(|wc| match &wc.skip_binders() {
614 WhereClause::Implemented(tr) => {
615 tr.self_type_parameter(&Interner) == self
617 WhereClause::AliasEq(AliasEq {
618 alias: AliasTy::Projection(proj),
620 }) => proj.self_type_parameter(&Interner) == self,
623 .collect::<Vec<_>>();
624 write_bounds_like_dyn_trait_with_prefix("impl", &bounds, f)?;
628 TyKind::BoundVar(idx) => idx.hir_fmt(f)?,
629 TyKind::Dyn(dyn_ty) => {
630 write_bounds_like_dyn_trait_with_prefix(
632 dyn_ty.bounds.skip_binders().interned(),
636 TyKind::Alias(AliasTy::Projection(p_ty)) => p_ty.hir_fmt(f)?,
637 TyKind::Alias(AliasTy::Opaque(opaque_ty)) => {
638 let impl_trait_id = f.db.lookup_intern_impl_trait_id(opaque_ty.opaque_ty_id.into());
639 match impl_trait_id {
640 ImplTraitId::ReturnTypeImplTrait(func, idx) => {
642 f.db.return_type_impl_traits(func).expect("impl trait id without data");
645 .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone());
646 let bounds = data.substitute(&Interner, &opaque_ty.substitution);
647 write_bounds_like_dyn_trait_with_prefix("impl", bounds.skip_binders(), f)?;
649 ImplTraitId::AsyncBlockTypeImplTrait(..) => {
650 write!(f, "{{async block}}")?;
655 if f.display_target.is_source_code() {
656 return Err(HirDisplayError::DisplaySourceCodeError(
657 DisplaySourceCodeError::UnknownType,
660 write!(f, "{{unknown}}")?;
662 TyKind::InferenceVar(..) => write!(f, "_")?,
668 impl HirDisplay for CallableSig {
669 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
671 f.write_joined(self.params(), ", ")?;
673 if self.params().is_empty() {
680 let ret = self.ret();
689 fn fn_traits(db: &dyn DefDatabase, trait_: TraitId) -> impl Iterator<Item = TraitId> {
690 let krate = trait_.lookup(db).container.krate();
692 db.lang_item(krate, "fn".into()),
693 db.lang_item(krate, "fn_mut".into()),
694 db.lang_item(krate, "fn_once".into()),
696 array::IntoIter::new(fn_traits).into_iter().flatten().flat_map(|it| it.as_trait())
699 pub fn write_bounds_like_dyn_trait_with_prefix(
701 predicates: &[QuantifiedWhereClause],
702 f: &mut HirFormatter,
703 ) -> Result<(), HirDisplayError> {
704 write!(f, "{}", prefix)?;
705 if !predicates.is_empty() {
707 write_bounds_like_dyn_trait(predicates, f)
713 fn write_bounds_like_dyn_trait(
714 predicates: &[QuantifiedWhereClause],
715 f: &mut HirFormatter,
716 ) -> Result<(), HirDisplayError> {
717 // Note: This code is written to produce nice results (i.e.
718 // corresponding to surface Rust) for types that can occur in
719 // actual Rust. It will have weird results if the predicates
720 // aren't as expected (i.e. self types = $0, projection
721 // predicates for a certain trait come after the Implemented
722 // predicate for that trait).
723 let mut first = true;
724 let mut angle_open = false;
725 let mut is_fn_trait = false;
726 for p in predicates.iter() {
727 match p.skip_binders() {
728 WhereClause::Implemented(trait_ref) => {
729 let trait_ = trait_ref.hir_trait_id();
731 is_fn_trait = fn_traits(f.db.upcast(), trait_).any(|it| it == trait_);
733 if !is_fn_trait && angle_open {
740 // We assume that the self type is $0 (i.e. the
741 // existential) here, which is the only thing that's
742 // possible in actual Rust, and hence don't print it
743 write!(f, "{}", f.db.trait_data(trait_).name)?;
744 if let [_, params @ ..] = &*trait_ref.substitution.interned() {
747 params.first().and_then(|it| it.assert_ty_ref(&Interner).as_tuple())
750 f.write_joined(&*args.interned(), ", ")?;
753 } else if !params.is_empty() {
755 f.write_joined(params, ", ")?;
756 // there might be assoc type bindings, so we leave the angle brackets open
761 WhereClause::AliasEq(alias_eq) if is_fn_trait => {
764 alias_eq.ty.hir_fmt(f)?;
766 WhereClause::AliasEq(AliasEq { ty, alias }) => {
767 // in types in actual Rust, these will always come
768 // after the corresponding Implemented predicate
775 if let AliasTy::Projection(proj) = alias {
777 f.db.type_alias_data(from_assoc_type_id(proj.associated_ty_id));
778 write!(f, "{} = ", type_alias.name)?;
792 fn hir_fmt_ext(&self, f: &mut HirFormatter, use_as: bool) -> Result<(), HirDisplayError> {
793 if f.should_truncate() {
794 return write!(f, "{}", TYPE_HINT_TRUNCATION);
797 self.self_type_parameter(&Interner).hir_fmt(f)?;
803 write!(f, "{}", f.db.trait_data(self.hir_trait_id()).name)?;
804 if self.substitution.len(&Interner) > 1 {
806 f.write_joined(&self.substitution.interned()[1..], ", ")?;
813 impl HirDisplay for TraitRef {
814 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
815 self.hir_fmt_ext(f, false)
819 impl HirDisplay for WhereClause {
820 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
821 if f.should_truncate() {
822 return write!(f, "{}", TYPE_HINT_TRUNCATION);
826 WhereClause::Implemented(trait_ref) => trait_ref.hir_fmt(f)?,
827 WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(projection_ty), ty }) => {
829 projection_ty.trait_ref(f.db).hir_fmt_ext(f, true)?;
833 f.db.type_alias_data(from_assoc_type_id(projection_ty.associated_ty_id)).name,
837 WhereClause::AliasEq(_) => write!(f, "{{error}}")?,
843 impl HirDisplay for LifetimeOutlives {
844 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
851 impl HirDisplay for Lifetime {
852 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
853 self.interned().hir_fmt(f)
857 impl HirDisplay for LifetimeData {
858 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
860 LifetimeData::BoundVar(idx) => idx.hir_fmt(f),
861 LifetimeData::InferenceVar(_) => write!(f, "_"),
862 LifetimeData::Placeholder(idx) => {
863 let id = lt_from_placeholder_idx(f.db, *idx);
864 let generics = generics(f.db.upcast(), id.parent);
865 let param_data = &generics.params.lifetimes[id.local_id];
866 write!(f, "{}", param_data.name)
868 LifetimeData::Static => write!(f, "'static"),
869 LifetimeData::Empty(_) => Ok(()),
870 LifetimeData::Erased => Ok(()),
871 LifetimeData::Phantom(_, _) => Ok(()),
876 impl HirDisplay for DomainGoal {
877 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
879 DomainGoal::Holds(wc) => {
880 write!(f, "Holds(")?;
888 pub fn write_visibility(
891 f: &mut HirFormatter,
892 ) -> Result<(), HirDisplayError> {
894 Visibility::Public => write!(f, "pub "),
895 Visibility::Module(vis_id) => {
896 let def_map = module_id.def_map(f.db.upcast());
897 let root_module_id = def_map.module_id(def_map.root());
898 if vis_id == module_id {
899 // pub(self) or omitted
901 } else if root_module_id == vis_id {
902 write!(f, "pub(crate) ")
903 } else if module_id.containing_module(f.db.upcast()) == Some(vis_id) {
904 write!(f, "pub(super) ")
906 write!(f, "pub(in ...) ")
912 impl HirDisplay for TypeRef {
913 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
915 TypeRef::Never => write!(f, "!")?,
916 TypeRef::Placeholder => write!(f, "_")?,
917 TypeRef::Tuple(elems) => {
919 f.write_joined(elems, ", ")?;
920 if elems.len() == 1 {
925 TypeRef::Path(path) => path.hir_fmt(f)?,
926 TypeRef::RawPtr(inner, mutability) => {
927 let mutability = match mutability {
928 hir_def::type_ref::Mutability::Shared => "*const ",
929 hir_def::type_ref::Mutability::Mut => "*mut ",
931 write!(f, "{}", mutability)?;
934 TypeRef::Reference(inner, lifetime, mutability) => {
935 let mutability = match mutability {
936 hir_def::type_ref::Mutability::Shared => "",
937 hir_def::type_ref::Mutability::Mut => "mut ",
940 if let Some(lifetime) = lifetime {
941 write!(f, "{} ", lifetime.name)?;
943 write!(f, "{}", mutability)?;
946 TypeRef::Array(inner) => {
949 // FIXME: Array length?
952 TypeRef::Slice(inner) => {
957 TypeRef::Fn(tys, is_varargs) => {
958 // FIXME: Function pointer qualifiers.
960 f.write_joined(&tys[..tys.len() - 1], ", ")?;
962 write!(f, "{}...", if tys.len() == 1 { "" } else { ", " })?;
965 let ret_ty = tys.last().unwrap();
967 TypeRef::Tuple(tup) if tup.is_empty() => {}
974 TypeRef::ImplTrait(bounds) => {
976 f.write_joined(bounds, " + ")?;
978 TypeRef::DynTrait(bounds) => {
980 f.write_joined(bounds, " + ")?;
982 TypeRef::Error => write!(f, "{{error}}")?,
988 impl HirDisplay for TypeBound {
989 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
991 TypeBound::Path(path) => path.hir_fmt(f),
992 TypeBound::Lifetime(lifetime) => write!(f, "{}", lifetime.name),
993 TypeBound::Error => write!(f, "{{error}}"),
998 impl HirDisplay for Path {
999 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
1000 match (self.type_anchor(), self.kind()) {
1001 (Some(anchor), _) => {
1006 (_, PathKind::Plain) => {}
1007 (_, PathKind::Abs) => write!(f, "::")?,
1008 (_, PathKind::Crate) => write!(f, "crate")?,
1009 (_, PathKind::Super(0)) => write!(f, "self")?,
1010 (_, PathKind::Super(n)) => {
1011 write!(f, "super")?;
1013 write!(f, "::super")?;
1016 (_, PathKind::DollarCrate(_)) => write!(f, "{{extern_crate}}")?,
1019 for (seg_idx, segment) in self.segments().iter().enumerate() {
1023 write!(f, "{}", segment.name)?;
1024 if let Some(generic_args) = segment.args_and_bindings {
1025 // We should be in type context, so format as `Foo<Bar>` instead of `Foo::<Bar>`.
1026 // Do we actually format expressions?
1028 let mut first = true;
1029 for arg in &generic_args.args {
1032 if generic_args.has_self_type {
1033 // FIXME: Convert to `<Ty as Trait>` form.
1034 write!(f, "Self = ")?;
1041 for binding in &generic_args.bindings {
1047 write!(f, "{}", binding.name)?;
1048 match &binding.type_ref {
1055 f.write_joined(&binding.bounds, " + ")?;
1066 impl HirDisplay for hir_def::path::GenericArg {
1067 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
1069 hir_def::path::GenericArg::Type(ty) => ty.hir_fmt(f),
1070 hir_def::path::GenericArg::Lifetime(lifetime) => write!(f, "{}", lifetime.name),