1 //! The `HirDisplay` trait, which serves two purposes: Turning various bits from
2 //! HIR back into source code, and just displaying them for debugging/testing
10 use chalk_ir::BoundVar;
15 generics::TypeParamProvenance,
16 intern::{Internable, Interned},
18 path::{Path, PathKind},
19 type_ref::{TypeBound, TypeRef},
20 visibility::Visibility,
21 AssocContainerId, Lookup, ModuleId, TraitId,
23 use hir_expand::{hygiene::Hygiene, name::Name};
24 use itertools::Itertools;
27 const_from_placeholder_idx, db::HirDatabase, from_assoc_type_id, from_foreign_def_id,
28 from_placeholder_idx, lt_from_placeholder_idx, mapping::from_chalk, primitive, subst_prefix,
29 to_assoc_type_id, utils::generics, AdtId, AliasEq, AliasTy, CallableDefId, CallableSig, Const,
30 ConstValue, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, LifetimeData,
31 LifetimeOutlives, Mutability, OpaqueTy, ProjectionTy, ProjectionTyExt, QuantifiedWhereClause,
32 Scalar, TraitRef, TraitRefExt, Ty, TyExt, TyKind, WhereClause,
35 pub struct HirFormatter<'a> {
36 pub db: &'a dyn HirDatabase,
37 fmt: &'a mut dyn fmt::Write,
40 pub(crate) max_size: Option<usize>,
41 omit_verbose_types: bool,
42 display_target: DisplayTarget,
45 pub trait HirDisplay {
46 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError>;
48 /// Returns a `Display`able type that is human-readable.
49 fn into_displayable<'a>(
51 db: &'a dyn HirDatabase,
52 max_size: Option<usize>,
53 omit_verbose_types: bool,
54 display_target: DisplayTarget,
55 ) -> HirDisplayWrapper<'a, Self>
60 !matches!(display_target, DisplayTarget::SourceCode { .. }),
61 "HirDisplayWrapper cannot fail with DisplaySourceCodeError, use HirDisplay::hir_fmt directly instead"
63 HirDisplayWrapper { db, t: self, max_size, omit_verbose_types, display_target }
66 /// Returns a `Display`able type that is human-readable.
67 /// Use this for showing types to the user (e.g. diagnostics)
68 fn display<'a>(&'a self, db: &'a dyn HirDatabase) -> HirDisplayWrapper<'a, Self>
76 omit_verbose_types: false,
77 display_target: DisplayTarget::Diagnostics,
81 /// Returns a `Display`able type that is human-readable and tries to be succinct.
82 /// Use this for showing types to the user where space is constrained (e.g. doc popups)
83 fn display_truncated<'a>(
85 db: &'a dyn HirDatabase,
86 max_size: Option<usize>,
87 ) -> HirDisplayWrapper<'a, Self>
95 omit_verbose_types: true,
96 display_target: DisplayTarget::Diagnostics,
100 /// Returns a String representation of `self` that can be inserted into the given module.
101 /// Use this when generating code (e.g. assists)
102 fn display_source_code<'a>(
104 db: &'a dyn HirDatabase,
106 ) -> Result<String, DisplaySourceCodeError> {
107 let mut result = String::new();
108 match self.hir_fmt(&mut HirFormatter {
111 buf: String::with_capacity(20),
114 omit_verbose_types: false,
115 display_target: DisplayTarget::SourceCode { module_id },
118 Err(HirDisplayError::FmtError) => panic!("Writing to String can't fail!"),
119 Err(HirDisplayError::DisplaySourceCodeError(e)) => return Err(e),
124 /// Returns a String representation of `self` for test purposes
125 fn display_test<'a>(&'a self, db: &'a dyn HirDatabase) -> HirDisplayWrapper<'a, Self>
133 omit_verbose_types: false,
134 display_target: DisplayTarget::Test,
139 impl<'a> HirFormatter<'a> {
140 pub fn write_joined<T: HirDisplay>(
142 iter: impl IntoIterator<Item = T>,
144 ) -> Result<(), HirDisplayError> {
145 let mut first = true;
148 write!(self, "{}", sep)?;
156 /// This allows using the `write!` macro directly with a `HirFormatter`.
157 pub fn write_fmt(&mut self, args: fmt::Arguments) -> Result<(), HirDisplayError> {
158 // We write to a buffer first to track output size
160 fmt::write(&mut self.buf, args)?;
161 self.curr_size += self.buf.len();
163 // Then we write to the internal formatter from the buffer
164 self.fmt.write_str(&self.buf).map_err(HirDisplayError::from)
167 pub fn should_truncate(&self) -> bool {
168 if let Some(max_size) = self.max_size {
169 self.curr_size >= max_size
175 pub fn omit_verbose_types(&self) -> bool {
176 self.omit_verbose_types
180 #[derive(Clone, Copy)]
181 pub enum DisplayTarget {
182 /// Display types for inlays, doc popups, autocompletion, etc...
183 /// Showing `{unknown}` or not qualifying paths is fine here.
184 /// There's no reason for this to fail.
186 /// Display types for inserting them in source files.
187 /// The generated code should compile, so paths need to be qualified.
188 SourceCode { module_id: ModuleId },
189 /// Only for test purpose to keep real types
194 fn is_source_code(&self) -> bool {
195 matches!(self, Self::SourceCode { .. })
197 fn is_test(&self) -> bool {
198 matches!(self, Self::Test)
203 pub enum DisplaySourceCodeError {
209 pub enum HirDisplayError {
210 /// Errors that can occur when generating source code
211 DisplaySourceCodeError(DisplaySourceCodeError),
212 /// `FmtError` is required to be compatible with std::fmt::Display
215 impl From<fmt::Error> for HirDisplayError {
216 fn from(_: fmt::Error) -> Self {
221 pub struct HirDisplayWrapper<'a, T> {
222 db: &'a dyn HirDatabase,
224 max_size: Option<usize>,
225 omit_verbose_types: bool,
226 display_target: DisplayTarget,
229 impl<'a, T> fmt::Display for HirDisplayWrapper<'a, T>
233 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
234 match self.t.hir_fmt(&mut HirFormatter {
237 buf: String::with_capacity(20),
239 max_size: self.max_size,
240 omit_verbose_types: self.omit_verbose_types,
241 display_target: self.display_target,
244 Err(HirDisplayError::FmtError) => Err(fmt::Error),
245 Err(HirDisplayError::DisplaySourceCodeError(_)) => {
246 // This should never happen
247 panic!("HirDisplay::hir_fmt failed with DisplaySourceCodeError when calling Display::fmt!")
253 const TYPE_HINT_TRUNCATION: &str = "…";
255 impl<T: HirDisplay> HirDisplay for &'_ T {
256 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
257 HirDisplay::hir_fmt(*self, f)
261 impl<T: HirDisplay + Internable> HirDisplay for Interned<T> {
262 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
263 HirDisplay::hir_fmt(self.as_ref(), f)
267 impl HirDisplay for ProjectionTy {
268 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
269 if f.should_truncate() {
270 return write!(f, "{}", TYPE_HINT_TRUNCATION);
273 let trait_ = f.db.trait_data(self.trait_(f.db));
275 self.self_type_parameter(&Interner).hir_fmt(f)?;
276 write!(f, " as {}", trait_.name)?;
277 if self.substitution.len(&Interner) > 1 {
279 f.write_joined(&self.substitution.as_slice(&Interner)[1..], ", ")?;
282 write!(f, ">::{}", f.db.type_alias_data(from_assoc_type_id(self.associated_ty_id)).name)?;
287 impl HirDisplay for OpaqueTy {
288 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
289 if f.should_truncate() {
290 return write!(f, "{}", TYPE_HINT_TRUNCATION);
293 self.substitution.at(&Interner, 0).hir_fmt(f)
297 impl HirDisplay for GenericArg {
298 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
299 match self.interned() {
300 crate::GenericArgData::Ty(ty) => ty.hir_fmt(f),
301 crate::GenericArgData::Lifetime(lt) => lt.hir_fmt(f),
302 crate::GenericArgData::Const(c) => c.hir_fmt(f),
307 impl HirDisplay for Const {
308 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
309 let data = self.interned();
311 ConstValue::BoundVar(idx) => idx.hir_fmt(f),
312 ConstValue::InferenceVar(..) => write!(f, "_"),
313 ConstValue::Placeholder(idx) => {
314 let id = const_from_placeholder_idx(f.db, idx);
315 let generics = generics(f.db.upcast(), id.parent);
316 let param_data = &generics.params.consts[id.local_id];
317 write!(f, "{}", param_data.name)
319 ConstValue::Concrete(c) => write!(f, "{}", c.interned),
324 impl HirDisplay for BoundVar {
325 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
326 write!(f, "?{}.{}", self.debruijn.depth(), self.index)
330 impl HirDisplay for Ty {
331 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
332 if f.should_truncate() {
333 return write!(f, "{}", TYPE_HINT_TRUNCATION);
336 match self.kind(&Interner) {
337 TyKind::Never => write!(f, "!")?,
338 TyKind::Str => write!(f, "str")?,
339 TyKind::Scalar(Scalar::Bool) => write!(f, "bool")?,
340 TyKind::Scalar(Scalar::Char) => write!(f, "char")?,
341 &TyKind::Scalar(Scalar::Float(t)) => write!(f, "{}", primitive::float_ty_to_string(t))?,
342 &TyKind::Scalar(Scalar::Int(t)) => write!(f, "{}", primitive::int_ty_to_string(t))?,
343 &TyKind::Scalar(Scalar::Uint(t)) => write!(f, "{}", primitive::uint_ty_to_string(t))?,
344 TyKind::Slice(t) => {
349 TyKind::Array(t, c) => {
356 TyKind::Raw(m, t) | TyKind::Ref(m, _, t) => {
357 if matches!(self.kind(&Interner), TyKind::Raw(..)) {
362 Mutability::Not => "const ",
363 Mutability::Mut => "mut ",
371 Mutability::Not => "",
372 Mutability::Mut => "mut ",
377 // FIXME: all this just to decide whether to use parentheses...
379 let predicates: Vec<_> = match t.kind(&Interner) {
380 TyKind::Dyn(dyn_ty) if dyn_ty.bounds.skip_binders().interned().len() > 1 => {
381 dyn_ty.bounds.skip_binders().interned().iter().cloned().collect()
383 &TyKind::Alias(AliasTy::Opaque(OpaqueTy {
385 substitution: ref parameters,
387 let impl_trait_id = f.db.lookup_intern_impl_trait_id(opaque_ty_id.into());
388 if let ImplTraitId::ReturnTypeImplTrait(func, idx) = impl_trait_id {
390 f.db.return_type_impl_traits(func)
391 .expect("impl trait id without data");
394 .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone());
395 let bounds = data.substitute(&Interner, parameters);
396 bounds.into_value_and_skipped_binders().0
404 if let Some(WhereClause::Implemented(trait_ref)) =
405 predicates.get(0).map(|b| b.skip_binders())
407 let trait_ = trait_ref.hir_trait_id();
408 if fn_traits(f.db.upcast(), trait_).any(|it| it == trait_)
409 && predicates.len() <= 2
415 if predicates.len() > 1 {
423 TyKind::Tuple(_, substs) => {
424 if substs.len(&Interner) == 1 {
426 substs.at(&Interner, 0).hir_fmt(f)?;
430 f.write_joined(&*substs.as_slice(&Interner), ", ")?;
434 TyKind::Function(fn_ptr) => {
435 let sig = CallableSig::from_fn_ptr(fn_ptr);
438 TyKind::FnDef(def, parameters) => {
439 let def = from_chalk(f.db, *def);
440 let sig = f.db.callable_item_signature(def).substitute(&Interner, parameters);
442 CallableDefId::FunctionId(ff) => {
443 write!(f, "fn {}", f.db.function_data(ff).name)?
445 CallableDefId::StructId(s) => write!(f, "{}", f.db.struct_data(s).name)?,
446 CallableDefId::EnumVariantId(e) => {
447 write!(f, "{}", f.db.enum_data(e.parent).variants[e.local_id].name)?
450 if parameters.len(&Interner) > 0 {
451 let generics = generics(f.db.upcast(), def.into());
452 let (parent_params, self_param, type_params, _impl_trait_params) =
453 generics.provenance_split();
454 let total_len = parent_params + self_param + type_params;
455 // We print all params except implicit impl Trait params. Still a bit weird; should we leave out parent and self?
458 f.write_joined(¶meters.as_slice(&Interner)[..total_len], ", ")?;
463 f.write_joined(sig.params(), ", ")?;
471 TyKind::Adt(AdtId(def_id), parameters) => {
472 match f.display_target {
473 DisplayTarget::Diagnostics | DisplayTarget::Test => {
474 let name = match *def_id {
475 hir_def::AdtId::StructId(it) => f.db.struct_data(it).name.clone(),
476 hir_def::AdtId::UnionId(it) => f.db.union_data(it).name.clone(),
477 hir_def::AdtId::EnumId(it) => f.db.enum_data(it).name.clone(),
479 write!(f, "{}", name)?;
481 DisplayTarget::SourceCode { module_id } => {
482 if let Some(path) = find_path::find_path(
484 ItemInNs::Types((*def_id).into()),
487 write!(f, "{}", path)?;
489 return Err(HirDisplayError::DisplaySourceCodeError(
490 DisplaySourceCodeError::PathNotFound,
496 if parameters.len(&Interner) > 0 {
497 let parameters_to_write = if f.display_target.is_source_code()
498 || f.omit_verbose_types()
501 .as_generic_def(f.db)
502 .map(|generic_def_id| f.db.generic_defaults(generic_def_id))
503 .filter(|defaults| !defaults.is_empty())
505 None => parameters.as_slice(&Interner),
506 Some(default_parameters) => {
507 let mut default_from = 0;
508 for (i, parameter) in parameters.iter(&Interner).enumerate() {
510 parameter.assert_ty_ref(&Interner).kind(&Interner),
511 default_parameters.get(i),
513 (&TyKind::Error, _) | (_, None) => {
514 default_from = i + 1;
516 (_, Some(default_parameter)) => {
518 default_parameter.clone().substitute(
520 &subst_prefix(parameters, i),
522 if parameter.assert_ty_ref(&Interner) != &actual_default
524 default_from = i + 1;
529 ¶meters.as_slice(&Interner)[0..default_from]
533 parameters.as_slice(&Interner)
535 if !parameters_to_write.is_empty() {
537 f.write_joined(parameters_to_write, ", ")?;
542 TyKind::AssociatedType(assoc_type_id, parameters) => {
543 let type_alias = from_assoc_type_id(*assoc_type_id);
544 let trait_ = match type_alias.lookup(f.db.upcast()).container {
545 AssocContainerId::TraitId(it) => it,
546 _ => panic!("not an associated type"),
548 let trait_ = f.db.trait_data(trait_);
549 let type_alias_data = f.db.type_alias_data(type_alias);
551 // Use placeholder associated types when the target is test (https://rust-lang.github.io/chalk/book/clauses/type_equality.html#placeholder-associated-types)
552 if f.display_target.is_test() {
553 write!(f, "{}::{}", trait_.name, type_alias_data.name)?;
554 if parameters.len(&Interner) > 0 {
556 f.write_joined(&*parameters.as_slice(&Interner), ", ")?;
560 let projection_ty = ProjectionTy {
561 associated_ty_id: to_assoc_type_id(type_alias),
562 substitution: parameters.clone(),
565 projection_ty.hir_fmt(f)?;
568 TyKind::Foreign(type_alias) => {
569 let type_alias = f.db.type_alias_data(from_foreign_def_id(*type_alias));
570 write!(f, "{}", type_alias.name)?;
572 TyKind::OpaqueType(opaque_ty_id, parameters) => {
573 let impl_trait_id = f.db.lookup_intern_impl_trait_id((*opaque_ty_id).into());
574 match impl_trait_id {
575 ImplTraitId::ReturnTypeImplTrait(func, idx) => {
577 f.db.return_type_impl_traits(func).expect("impl trait id without data");
580 .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone());
581 let bounds = data.substitute(&Interner, ¶meters);
582 write_bounds_like_dyn_trait_with_prefix("impl", bounds.skip_binders(), f)?;
583 // FIXME: it would maybe be good to distinguish this from the alias type (when debug printing), and to show the substitution
585 ImplTraitId::AsyncBlockTypeImplTrait(..) => {
586 write!(f, "impl Future<Output = ")?;
587 parameters.at(&Interner, 0).hir_fmt(f)?;
592 TyKind::Closure(.., substs) => {
593 if f.display_target.is_source_code() {
594 return Err(HirDisplayError::DisplaySourceCodeError(
595 DisplaySourceCodeError::Closure,
598 let sig = substs.at(&Interner, 0).assert_ty_ref(&Interner).callable_sig(f.db);
599 if let Some(sig) = sig {
600 if sig.params().is_empty() {
602 } else if f.omit_verbose_types() {
603 write!(f, "|{}|", TYPE_HINT_TRUNCATION)?;
606 f.write_joined(sig.params(), ", ")?;
611 sig.ret().hir_fmt(f)?;
613 write!(f, "{{closure}}")?;
616 TyKind::Placeholder(idx) => {
617 let id = from_placeholder_idx(f.db, *idx);
618 let generics = generics(f.db.upcast(), id.parent);
619 let param_data = &generics.params.types[id.local_id];
620 match param_data.provenance {
621 TypeParamProvenance::TypeParamList | TypeParamProvenance::TraitSelf => {
622 write!(f, "{}", param_data.name.clone().unwrap_or_else(Name::missing))?
624 TypeParamProvenance::ArgumentImplTrait => {
625 let substs = generics.type_params_subst(f.db);
627 f.db.generic_predicates(id.parent)
629 .map(|pred| pred.clone().substitute(&Interner, &substs))
630 .filter(|wc| match &wc.skip_binders() {
631 WhereClause::Implemented(tr) => {
632 &tr.self_type_parameter(&Interner) == self
634 WhereClause::AliasEq(AliasEq {
635 alias: AliasTy::Projection(proj),
637 }) => &proj.self_type_parameter(&Interner) == self,
640 .collect::<Vec<_>>();
641 write_bounds_like_dyn_trait_with_prefix("impl", &bounds, f)?;
645 TyKind::BoundVar(idx) => idx.hir_fmt(f)?,
646 TyKind::Dyn(dyn_ty) => {
647 write_bounds_like_dyn_trait_with_prefix(
649 dyn_ty.bounds.skip_binders().interned(),
653 TyKind::Alias(AliasTy::Projection(p_ty)) => p_ty.hir_fmt(f)?,
654 TyKind::Alias(AliasTy::Opaque(opaque_ty)) => {
655 let impl_trait_id = f.db.lookup_intern_impl_trait_id(opaque_ty.opaque_ty_id.into());
656 match impl_trait_id {
657 ImplTraitId::ReturnTypeImplTrait(func, idx) => {
659 f.db.return_type_impl_traits(func).expect("impl trait id without data");
662 .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone());
663 let bounds = data.substitute(&Interner, &opaque_ty.substitution);
664 write_bounds_like_dyn_trait_with_prefix("impl", bounds.skip_binders(), f)?;
666 ImplTraitId::AsyncBlockTypeImplTrait(..) => {
667 write!(f, "{{async block}}")?;
672 if f.display_target.is_source_code() {
673 return Err(HirDisplayError::DisplaySourceCodeError(
674 DisplaySourceCodeError::UnknownType,
677 write!(f, "{{unknown}}")?;
679 TyKind::InferenceVar(..) => write!(f, "_")?,
680 TyKind::Generator(..) => write!(f, "{{generator}}")?,
681 TyKind::GeneratorWitness(..) => write!(f, "{{generator witness}}")?,
687 impl HirDisplay for CallableSig {
688 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
690 f.write_joined(self.params(), ", ")?;
692 if self.params().is_empty() {
699 let ret = self.ret();
708 fn fn_traits(db: &dyn DefDatabase, trait_: TraitId) -> impl Iterator<Item = TraitId> {
709 let krate = trait_.lookup(db).container.krate();
711 db.lang_item(krate, "fn".into()),
712 db.lang_item(krate, "fn_mut".into()),
713 db.lang_item(krate, "fn_once".into()),
715 array::IntoIter::new(fn_traits).into_iter().flatten().flat_map(|it| it.as_trait())
718 pub fn write_bounds_like_dyn_trait_with_prefix(
720 predicates: &[QuantifiedWhereClause],
721 f: &mut HirFormatter,
722 ) -> Result<(), HirDisplayError> {
723 write!(f, "{}", prefix)?;
724 if !predicates.is_empty() {
726 write_bounds_like_dyn_trait(predicates, f)
732 fn write_bounds_like_dyn_trait(
733 predicates: &[QuantifiedWhereClause],
734 f: &mut HirFormatter,
735 ) -> Result<(), HirDisplayError> {
736 // Note: This code is written to produce nice results (i.e.
737 // corresponding to surface Rust) for types that can occur in
738 // actual Rust. It will have weird results if the predicates
739 // aren't as expected (i.e. self types = $0, projection
740 // predicates for a certain trait come after the Implemented
741 // predicate for that trait).
742 let mut first = true;
743 let mut angle_open = false;
744 let mut is_fn_trait = false;
745 for p in predicates.iter() {
746 match p.skip_binders() {
747 WhereClause::Implemented(trait_ref) => {
748 let trait_ = trait_ref.hir_trait_id();
750 is_fn_trait = fn_traits(f.db.upcast(), trait_).any(|it| it == trait_);
752 if !is_fn_trait && angle_open {
759 // We assume that the self type is ^0.0 (i.e. the
760 // existential) here, which is the only thing that's
761 // possible in actual Rust, and hence don't print it
762 write!(f, "{}", f.db.trait_data(trait_).name)?;
763 if let [_, params @ ..] = &*trait_ref.substitution.as_slice(&Interner) {
766 params.first().and_then(|it| it.assert_ty_ref(&Interner).as_tuple())
769 f.write_joined(args.as_slice(&Interner), ", ")?;
772 } else if !params.is_empty() {
774 f.write_joined(params, ", ")?;
775 // there might be assoc type bindings, so we leave the angle brackets open
780 WhereClause::AliasEq(alias_eq) if is_fn_trait => {
782 if !alias_eq.ty.is_unit() {
784 alias_eq.ty.hir_fmt(f)?;
787 WhereClause::AliasEq(AliasEq { ty, alias }) => {
788 // in types in actual Rust, these will always come
789 // after the corresponding Implemented predicate
796 if let AliasTy::Projection(proj) = alias {
798 f.db.type_alias_data(from_assoc_type_id(proj.associated_ty_id));
799 write!(f, "{} = ", type_alias.name)?;
804 // FIXME implement these
805 WhereClause::LifetimeOutlives(_) => {}
806 WhereClause::TypeOutlives(_) => {}
816 fn fmt_trait_ref(tr: &TraitRef, f: &mut HirFormatter, use_as: bool) -> Result<(), HirDisplayError> {
817 if f.should_truncate() {
818 return write!(f, "{}", TYPE_HINT_TRUNCATION);
821 tr.self_type_parameter(&Interner).hir_fmt(f)?;
827 write!(f, "{}", f.db.trait_data(tr.hir_trait_id()).name)?;
828 if tr.substitution.len(&Interner) > 1 {
830 f.write_joined(&tr.substitution.as_slice(&Interner)[1..], ", ")?;
836 impl HirDisplay for TraitRef {
837 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
838 fmt_trait_ref(self, f, false)
842 impl HirDisplay for WhereClause {
843 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
844 if f.should_truncate() {
845 return write!(f, "{}", TYPE_HINT_TRUNCATION);
849 WhereClause::Implemented(trait_ref) => trait_ref.hir_fmt(f)?,
850 WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(projection_ty), ty }) => {
852 fmt_trait_ref(&projection_ty.trait_ref(f.db), f, true)?;
856 f.db.type_alias_data(from_assoc_type_id(projection_ty.associated_ty_id)).name,
860 WhereClause::AliasEq(_) => write!(f, "{{error}}")?,
862 // FIXME implement these
863 WhereClause::TypeOutlives(..) => {}
864 WhereClause::LifetimeOutlives(..) => {}
870 impl HirDisplay for LifetimeOutlives {
871 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
878 impl HirDisplay for Lifetime {
879 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
880 self.interned().hir_fmt(f)
884 impl HirDisplay for LifetimeData {
885 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
887 LifetimeData::BoundVar(idx) => idx.hir_fmt(f),
888 LifetimeData::InferenceVar(_) => write!(f, "_"),
889 LifetimeData::Placeholder(idx) => {
890 let id = lt_from_placeholder_idx(f.db, *idx);
891 let generics = generics(f.db.upcast(), id.parent);
892 let param_data = &generics.params.lifetimes[id.local_id];
893 write!(f, "{}", param_data.name)
895 LifetimeData::Static => write!(f, "'static"),
896 LifetimeData::Empty(_) => Ok(()),
897 LifetimeData::Erased => Ok(()),
898 LifetimeData::Phantom(_, _) => Ok(()),
903 impl HirDisplay for DomainGoal {
904 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
906 DomainGoal::Holds(wc) => {
907 write!(f, "Holds(")?;
911 _ => write!(f, "?")?,
917 pub fn write_visibility(
920 f: &mut HirFormatter,
921 ) -> Result<(), HirDisplayError> {
923 Visibility::Public => write!(f, "pub "),
924 Visibility::Module(vis_id) => {
925 let def_map = module_id.def_map(f.db.upcast());
926 let root_module_id = def_map.module_id(def_map.root());
927 if vis_id == module_id {
928 // pub(self) or omitted
930 } else if root_module_id == vis_id {
931 write!(f, "pub(crate) ")
932 } else if module_id.containing_module(f.db.upcast()) == Some(vis_id) {
933 write!(f, "pub(super) ")
935 write!(f, "pub(in ...) ")
941 impl HirDisplay for TypeRef {
942 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
944 TypeRef::Never => write!(f, "!")?,
945 TypeRef::Placeholder => write!(f, "_")?,
946 TypeRef::Tuple(elems) => {
948 f.write_joined(elems, ", ")?;
949 if elems.len() == 1 {
954 TypeRef::Path(path) => path.hir_fmt(f)?,
955 TypeRef::RawPtr(inner, mutability) => {
956 let mutability = match mutability {
957 hir_def::type_ref::Mutability::Shared => "*const ",
958 hir_def::type_ref::Mutability::Mut => "*mut ",
960 write!(f, "{}", mutability)?;
963 TypeRef::Reference(inner, lifetime, mutability) => {
964 let mutability = match mutability {
965 hir_def::type_ref::Mutability::Shared => "",
966 hir_def::type_ref::Mutability::Mut => "mut ",
969 if let Some(lifetime) = lifetime {
970 write!(f, "{} ", lifetime.name)?;
972 write!(f, "{}", mutability)?;
975 TypeRef::Array(inner, len) => {
978 write!(f, "; {}]", len)?;
980 TypeRef::Slice(inner) => {
985 TypeRef::Fn(tys, is_varargs) => {
986 // FIXME: Function pointer qualifiers.
988 f.write_joined(&tys[..tys.len() - 1], ", ")?;
990 write!(f, "{}...", if tys.len() == 1 { "" } else { ", " })?;
993 let ret_ty = tys.last().unwrap();
995 TypeRef::Tuple(tup) if tup.is_empty() => {}
1002 TypeRef::ImplTrait(bounds) => {
1003 write!(f, "impl ")?;
1004 f.write_joined(bounds, " + ")?;
1006 TypeRef::DynTrait(bounds) => {
1008 f.write_joined(bounds, " + ")?;
1010 TypeRef::Macro(macro_call) => {
1011 let macro_call = macro_call.to_node(f.db.upcast());
1012 let ctx = body::LowerCtx::with_hygiene(f.db.upcast(), &Hygiene::new_unhygienic());
1013 match macro_call.path() {
1014 Some(path) => match Path::from_src(path, &ctx) {
1015 Some(path) => path.hir_fmt(f)?,
1016 None => write!(f, "{{macro}}")?,
1018 None => write!(f, "{{macro}}")?,
1020 write!(f, "!(..)")?;
1022 TypeRef::Error => write!(f, "{{error}}")?,
1028 impl HirDisplay for TypeBound {
1029 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
1031 TypeBound::Path(path) => path.hir_fmt(f),
1032 TypeBound::Lifetime(lifetime) => write!(f, "{}", lifetime.name),
1033 TypeBound::ForLifetime(lifetimes, path) => {
1034 write!(f, "for<{}> ", lifetimes.iter().format(", "))?;
1037 TypeBound::Error => write!(f, "{{error}}"),
1042 impl HirDisplay for Path {
1043 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
1044 match (self.type_anchor(), self.kind()) {
1045 (Some(anchor), _) => {
1050 (_, PathKind::Plain) => {}
1051 (_, PathKind::Abs) => write!(f, "::")?,
1052 (_, PathKind::Crate) => write!(f, "crate")?,
1053 (_, PathKind::Super(0)) => write!(f, "self")?,
1054 (_, PathKind::Super(n)) => {
1055 write!(f, "super")?;
1057 write!(f, "::super")?;
1060 (_, PathKind::DollarCrate(_)) => write!(f, "{{extern_crate}}")?,
1063 for (seg_idx, segment) in self.segments().iter().enumerate() {
1067 write!(f, "{}", segment.name)?;
1068 if let Some(generic_args) = segment.args_and_bindings {
1069 // We should be in type context, so format as `Foo<Bar>` instead of `Foo::<Bar>`.
1070 // Do we actually format expressions?
1072 let mut first = true;
1073 for arg in &generic_args.args {
1076 if generic_args.has_self_type {
1077 // FIXME: Convert to `<Ty as Trait>` form.
1078 write!(f, "Self = ")?;
1085 for binding in &generic_args.bindings {
1091 write!(f, "{}", binding.name)?;
1092 match &binding.type_ref {
1099 f.write_joined(&binding.bounds, " + ")?;
1110 impl HirDisplay for hir_def::path::GenericArg {
1111 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
1113 hir_def::path::GenericArg::Type(ty) => ty.hir_fmt(f),
1114 hir_def::path::GenericArg::Lifetime(lifetime) => write!(f, "{}", lifetime.name),