diff options
Diffstat (limited to 'crates/hir_ty/src/display.rs')
-rw-r--r-- | crates/hir_ty/src/display.rs | 49 |
1 files changed, 32 insertions, 17 deletions
diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index d7a3977e5..4fb7d9cf2 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs | |||
@@ -1,4 +1,6 @@ | |||
1 | //! FIXME: write short doc here | 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 | ||
3 | //! purposes. | ||
2 | 4 | ||
3 | use std::{ | 5 | use std::{ |
4 | array, | 6 | array, |
@@ -7,6 +9,7 @@ use std::{ | |||
7 | 9 | ||
8 | use chalk_ir::BoundVar; | 10 | use chalk_ir::BoundVar; |
9 | use hir_def::{ | 11 | use hir_def::{ |
12 | body, | ||
10 | db::DefDatabase, | 13 | db::DefDatabase, |
11 | find_path, | 14 | find_path, |
12 | generics::TypeParamProvenance, | 15 | generics::TypeParamProvenance, |
@@ -16,15 +19,15 @@ use hir_def::{ | |||
16 | visibility::Visibility, | 19 | visibility::Visibility, |
17 | AssocContainerId, Lookup, ModuleId, TraitId, | 20 | AssocContainerId, Lookup, ModuleId, TraitId, |
18 | }; | 21 | }; |
19 | use hir_expand::name::Name; | 22 | use hir_expand::{hygiene::Hygiene, name::Name}; |
20 | 23 | ||
21 | use crate::{ | 24 | use crate::{ |
22 | const_from_placeholder_idx, db::HirDatabase, from_assoc_type_id, from_foreign_def_id, | 25 | 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, | 26 | from_placeholder_idx, lt_from_placeholder_idx, mapping::from_chalk, primitive, subst_prefix, |
24 | traits::chalk::from_chalk, utils::generics, AdtId, AliasEq, AliasTy, CallableDefId, | 27 | to_assoc_type_id, utils::generics, AdtId, AliasEq, AliasTy, CallableDefId, CallableSig, Const, |
25 | CallableSig, Const, ConstValue, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, | 28 | ConstValue, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, LifetimeData, |
26 | LifetimeData, LifetimeOutlives, Mutability, OpaqueTy, ProjectionTy, ProjectionTyExt, | 29 | LifetimeOutlives, Mutability, OpaqueTy, ProjectionTy, ProjectionTyExt, QuantifiedWhereClause, |
27 | QuantifiedWhereClause, Scalar, TraitRef, TraitRefExt, Ty, TyExt, TyKind, WhereClause, | 30 | Scalar, TraitRef, TraitRefExt, Ty, TyExt, TyKind, WhereClause, |
28 | }; | 31 | }; |
29 | 32 | ||
30 | pub struct HirFormatter<'a> { | 33 | pub struct HirFormatter<'a> { |
@@ -265,7 +268,7 @@ impl HirDisplay for ProjectionTy { | |||
265 | write!(f, " as {}", trait_.name)?; | 268 | write!(f, " as {}", trait_.name)?; |
266 | if self.substitution.len(&Interner) > 1 { | 269 | if self.substitution.len(&Interner) > 1 { |
267 | write!(f, "<")?; | 270 | write!(f, "<")?; |
268 | f.write_joined(&self.substitution.interned()[1..], ", ")?; | 271 | f.write_joined(&self.substitution.as_slice(&Interner)[1..], ", ")?; |
269 | write!(f, ">")?; | 272 | write!(f, ">")?; |
270 | } | 273 | } |
271 | write!(f, ">::{}", f.db.type_alias_data(from_assoc_type_id(self.associated_ty_id)).name)?; | 274 | write!(f, ">::{}", f.db.type_alias_data(from_assoc_type_id(self.associated_ty_id)).name)?; |
@@ -416,7 +419,7 @@ impl HirDisplay for Ty { | |||
416 | write!(f, ",)")?; | 419 | write!(f, ",)")?; |
417 | } else { | 420 | } else { |
418 | write!(f, "(")?; | 421 | write!(f, "(")?; |
419 | f.write_joined(&*substs.interned(), ", ")?; | 422 | f.write_joined(&*substs.as_slice(&Interner), ", ")?; |
420 | write!(f, ")")?; | 423 | write!(f, ")")?; |
421 | } | 424 | } |
422 | } | 425 | } |
@@ -444,7 +447,7 @@ impl HirDisplay for Ty { | |||
444 | // We print all params except implicit impl Trait params. Still a bit weird; should we leave out parent and self? | 447 | // We print all params except implicit impl Trait params. Still a bit weird; should we leave out parent and self? |
445 | if total_len > 0 { | 448 | if total_len > 0 { |
446 | write!(f, "<")?; | 449 | write!(f, "<")?; |
447 | f.write_joined(¶meters.interned()[..total_len], ", ")?; | 450 | f.write_joined(¶meters.as_slice(&Interner)[..total_len], ", ")?; |
448 | write!(f, ">")?; | 451 | write!(f, ">")?; |
449 | } | 452 | } |
450 | } | 453 | } |
@@ -491,7 +494,7 @@ impl HirDisplay for Ty { | |||
491 | .map(|generic_def_id| f.db.generic_defaults(generic_def_id)) | 494 | .map(|generic_def_id| f.db.generic_defaults(generic_def_id)) |
492 | .filter(|defaults| !defaults.is_empty()) | 495 | .filter(|defaults| !defaults.is_empty()) |
493 | { | 496 | { |
494 | None => parameters.interned().as_ref(), | 497 | None => parameters.as_slice(&Interner), |
495 | Some(default_parameters) => { | 498 | Some(default_parameters) => { |
496 | let mut default_from = 0; | 499 | let mut default_from = 0; |
497 | for (i, parameter) in parameters.iter(&Interner).enumerate() { | 500 | for (i, parameter) in parameters.iter(&Interner).enumerate() { |
@@ -515,11 +518,11 @@ impl HirDisplay for Ty { | |||
515 | } | 518 | } |
516 | } | 519 | } |
517 | } | 520 | } |
518 | ¶meters.interned()[0..default_from] | 521 | ¶meters.as_slice(&Interner)[0..default_from] |
519 | } | 522 | } |
520 | } | 523 | } |
521 | } else { | 524 | } else { |
522 | parameters.interned().as_ref() | 525 | parameters.as_slice(&Interner) |
523 | }; | 526 | }; |
524 | if !parameters_to_write.is_empty() { | 527 | if !parameters_to_write.is_empty() { |
525 | write!(f, "<")?; | 528 | write!(f, "<")?; |
@@ -542,7 +545,7 @@ impl HirDisplay for Ty { | |||
542 | write!(f, "{}::{}", trait_.name, type_alias_data.name)?; | 545 | write!(f, "{}::{}", trait_.name, type_alias_data.name)?; |
543 | if parameters.len(&Interner) > 0 { | 546 | if parameters.len(&Interner) > 0 { |
544 | write!(f, "<")?; | 547 | write!(f, "<")?; |
545 | f.write_joined(&*parameters.interned(), ", ")?; | 548 | f.write_joined(&*parameters.as_slice(&Interner), ", ")?; |
546 | write!(f, ">")?; | 549 | write!(f, ">")?; |
547 | } | 550 | } |
548 | } else { | 551 | } else { |
@@ -749,13 +752,13 @@ fn write_bounds_like_dyn_trait( | |||
749 | // existential) here, which is the only thing that's | 752 | // existential) here, which is the only thing that's |
750 | // possible in actual Rust, and hence don't print it | 753 | // possible in actual Rust, and hence don't print it |
751 | write!(f, "{}", f.db.trait_data(trait_).name)?; | 754 | write!(f, "{}", f.db.trait_data(trait_).name)?; |
752 | if let [_, params @ ..] = &*trait_ref.substitution.interned().as_slice() { | 755 | if let [_, params @ ..] = &*trait_ref.substitution.as_slice(&Interner) { |
753 | if is_fn_trait { | 756 | if is_fn_trait { |
754 | if let Some(args) = | 757 | if let Some(args) = |
755 | params.first().and_then(|it| it.assert_ty_ref(&Interner).as_tuple()) | 758 | params.first().and_then(|it| it.assert_ty_ref(&Interner).as_tuple()) |
756 | { | 759 | { |
757 | write!(f, "(")?; | 760 | write!(f, "(")?; |
758 | f.write_joined(&*args.interned(), ", ")?; | 761 | f.write_joined(args.as_slice(&Interner), ", ")?; |
759 | write!(f, ")")?; | 762 | write!(f, ")")?; |
760 | } | 763 | } |
761 | } else if !params.is_empty() { | 764 | } else if !params.is_empty() { |
@@ -814,7 +817,7 @@ fn fmt_trait_ref(tr: &TraitRef, f: &mut HirFormatter, use_as: bool) -> Result<() | |||
814 | write!(f, "{}", f.db.trait_data(tr.hir_trait_id()).name)?; | 817 | write!(f, "{}", f.db.trait_data(tr.hir_trait_id()).name)?; |
815 | if tr.substitution.len(&Interner) > 1 { | 818 | if tr.substitution.len(&Interner) > 1 { |
816 | write!(f, "<")?; | 819 | write!(f, "<")?; |
817 | f.write_joined(&tr.substitution.interned()[1..], ", ")?; | 820 | f.write_joined(&tr.substitution.as_slice(&Interner)[1..], ", ")?; |
818 | write!(f, ">")?; | 821 | write!(f, ">")?; |
819 | } | 822 | } |
820 | Ok(()) | 823 | Ok(()) |
@@ -995,6 +998,18 @@ impl HirDisplay for TypeRef { | |||
995 | write!(f, "dyn ")?; | 998 | write!(f, "dyn ")?; |
996 | f.write_joined(bounds, " + ")?; | 999 | f.write_joined(bounds, " + ")?; |
997 | } | 1000 | } |
1001 | TypeRef::Macro(macro_call) => { | ||
1002 | let macro_call = macro_call.to_node(f.db.upcast()); | ||
1003 | let ctx = body::LowerCtx::with_hygiene(&Hygiene::new_unhygienic()); | ||
1004 | match macro_call.path() { | ||
1005 | Some(path) => match Path::from_src(path, &ctx) { | ||
1006 | Some(path) => path.hir_fmt(f)?, | ||
1007 | None => write!(f, "{{macro}}")?, | ||
1008 | }, | ||
1009 | None => write!(f, "{{macro}}")?, | ||
1010 | } | ||
1011 | write!(f, "!(..)")?; | ||
1012 | } | ||
998 | TypeRef::Error => write!(f, "{{error}}")?, | 1013 | TypeRef::Error => write!(f, "{{error}}")?, |
999 | } | 1014 | } |
1000 | Ok(()) | 1015 | Ok(()) |