aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/display.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src/display.rs')
-rw-r--r--crates/hir_ty/src/display.rs52
1 files changed, 34 insertions, 18 deletions
diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs
index e0ca96c6d..e7c9dabc2 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
3use std::{ 5use std::{
4 array, 6 array,
@@ -20,11 +22,11 @@ use hir_expand::name::Name;
20 22
21use crate::{ 23use crate::{
22 const_from_placeholder_idx, db::HirDatabase, from_assoc_type_id, from_foreign_def_id, 24 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, 25 from_placeholder_idx, lt_from_placeholder_idx, mapping::from_chalk, primitive, subst_prefix,
24 traits::chalk::from_chalk, utils::generics, AdtId, AliasEq, AliasTy, CallableDefId, 26 to_assoc_type_id, utils::generics, AdtId, AliasEq, AliasTy, CallableDefId, CallableSig, Const,
25 CallableSig, Const, ConstValue, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, 27 ConstValue, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, LifetimeData,
26 LifetimeData, LifetimeOutlives, Mutability, OpaqueTy, ProjectionTy, ProjectionTyExt, 28 LifetimeOutlives, Mutability, OpaqueTy, ProjectionTy, ProjectionTyExt, QuantifiedWhereClause,
27 QuantifiedWhereClause, Scalar, TraitRef, TraitRefExt, Ty, TyExt, TyKind, WhereClause, 29 Scalar, TraitRef, TraitRefExt, Ty, TyExt, TyKind, WhereClause,
28}; 30};
29 31
30pub struct HirFormatter<'a> { 32pub struct HirFormatter<'a> {
@@ -265,7 +267,7 @@ impl HirDisplay for ProjectionTy {
265 write!(f, " as {}", trait_.name)?; 267 write!(f, " as {}", trait_.name)?;
266 if self.substitution.len(&Interner) > 1 { 268 if self.substitution.len(&Interner) > 1 {
267 write!(f, "<")?; 269 write!(f, "<")?;
268 f.write_joined(&self.substitution.interned()[1..], ", ")?; 270 f.write_joined(&self.substitution.as_slice(&Interner)[1..], ", ")?;
269 write!(f, ">")?; 271 write!(f, ">")?;
270 } 272 }
271 write!(f, ">::{}", f.db.type_alias_data(from_assoc_type_id(self.associated_ty_id)).name)?; 273 write!(f, ">::{}", f.db.type_alias_data(from_assoc_type_id(self.associated_ty_id)).name)?;
@@ -287,6 +289,8 @@ impl HirDisplay for GenericArg {
287 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { 289 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
288 match self.interned() { 290 match self.interned() {
289 crate::GenericArgData::Ty(ty) => ty.hir_fmt(f), 291 crate::GenericArgData::Ty(ty) => ty.hir_fmt(f),
292 crate::GenericArgData::Lifetime(lt) => lt.hir_fmt(f),
293 crate::GenericArgData::Const(c) => c.hir_fmt(f),
290 } 294 }
291 } 295 }
292} 296}
@@ -414,7 +418,7 @@ impl HirDisplay for Ty {
414 write!(f, ",)")?; 418 write!(f, ",)")?;
415 } else { 419 } else {
416 write!(f, "(")?; 420 write!(f, "(")?;
417 f.write_joined(&*substs.interned(), ", ")?; 421 f.write_joined(&*substs.as_slice(&Interner), ", ")?;
418 write!(f, ")")?; 422 write!(f, ")")?;
419 } 423 }
420 } 424 }
@@ -442,7 +446,7 @@ impl HirDisplay for Ty {
442 // We print all params except implicit impl Trait params. Still a bit weird; should we leave out parent and self? 446 // We print all params except implicit impl Trait params. Still a bit weird; should we leave out parent and self?
443 if total_len > 0 { 447 if total_len > 0 {
444 write!(f, "<")?; 448 write!(f, "<")?;
445 f.write_joined(&parameters.interned()[..total_len], ", ")?; 449 f.write_joined(&parameters.as_slice(&Interner)[..total_len], ", ")?;
446 write!(f, ">")?; 450 write!(f, ">")?;
447 } 451 }
448 } 452 }
@@ -489,7 +493,7 @@ impl HirDisplay for Ty {
489 .map(|generic_def_id| f.db.generic_defaults(generic_def_id)) 493 .map(|generic_def_id| f.db.generic_defaults(generic_def_id))
490 .filter(|defaults| !defaults.is_empty()) 494 .filter(|defaults| !defaults.is_empty())
491 { 495 {
492 None => parameters.interned().as_ref(), 496 None => parameters.as_slice(&Interner),
493 Some(default_parameters) => { 497 Some(default_parameters) => {
494 let mut default_from = 0; 498 let mut default_from = 0;
495 for (i, parameter) in parameters.iter(&Interner).enumerate() { 499 for (i, parameter) in parameters.iter(&Interner).enumerate() {
@@ -513,11 +517,11 @@ impl HirDisplay for Ty {
513 } 517 }
514 } 518 }
515 } 519 }
516 &parameters.interned()[0..default_from] 520 &parameters.as_slice(&Interner)[0..default_from]
517 } 521 }
518 } 522 }
519 } else { 523 } else {
520 parameters.interned().as_ref() 524 parameters.as_slice(&Interner)
521 }; 525 };
522 if !parameters_to_write.is_empty() { 526 if !parameters_to_write.is_empty() {
523 write!(f, "<")?; 527 write!(f, "<")?;
@@ -540,7 +544,7 @@ impl HirDisplay for Ty {
540 write!(f, "{}::{}", trait_.name, type_alias_data.name)?; 544 write!(f, "{}::{}", trait_.name, type_alias_data.name)?;
541 if parameters.len(&Interner) > 0 { 545 if parameters.len(&Interner) > 0 {
542 write!(f, "<")?; 546 write!(f, "<")?;
543 f.write_joined(&*parameters.interned(), ", ")?; 547 f.write_joined(&*parameters.as_slice(&Interner), ", ")?;
544 write!(f, ">")?; 548 write!(f, ">")?;
545 } 549 }
546 } else { 550 } else {
@@ -664,6 +668,8 @@ impl HirDisplay for Ty {
664 write!(f, "{{unknown}}")?; 668 write!(f, "{{unknown}}")?;
665 } 669 }
666 TyKind::InferenceVar(..) => write!(f, "_")?, 670 TyKind::InferenceVar(..) => write!(f, "_")?,
671 TyKind::Generator(..) => write!(f, "{{generator}}")?,
672 TyKind::GeneratorWitness(..) => write!(f, "{{generator witness}}")?,
667 } 673 }
668 Ok(()) 674 Ok(())
669 } 675 }
@@ -741,17 +747,17 @@ fn write_bounds_like_dyn_trait(
741 if !first { 747 if !first {
742 write!(f, " + ")?; 748 write!(f, " + ")?;
743 } 749 }
744 // We assume that the self type is $0 (i.e. the 750 // We assume that the self type is ^0.0 (i.e. the
745 // existential) here, which is the only thing that's 751 // existential) here, which is the only thing that's
746 // possible in actual Rust, and hence don't print it 752 // possible in actual Rust, and hence don't print it
747 write!(f, "{}", f.db.trait_data(trait_).name)?; 753 write!(f, "{}", f.db.trait_data(trait_).name)?;
748 if let [_, params @ ..] = &*trait_ref.substitution.interned().as_slice() { 754 if let [_, params @ ..] = &*trait_ref.substitution.as_slice(&Interner) {
749 if is_fn_trait { 755 if is_fn_trait {
750 if let Some(args) = 756 if let Some(args) =
751 params.first().and_then(|it| it.assert_ty_ref(&Interner).as_tuple()) 757 params.first().and_then(|it| it.assert_ty_ref(&Interner).as_tuple())
752 { 758 {
753 write!(f, "(")?; 759 write!(f, "(")?;
754 f.write_joined(&*args.interned(), ", ")?; 760 f.write_joined(args.as_slice(&Interner), ", ")?;
755 write!(f, ")")?; 761 write!(f, ")")?;
756 } 762 }
757 } else if !params.is_empty() { 763 } else if !params.is_empty() {
@@ -783,6 +789,10 @@ fn write_bounds_like_dyn_trait(
783 } 789 }
784 ty.hir_fmt(f)?; 790 ty.hir_fmt(f)?;
785 } 791 }
792
793 // FIXME implement these
794 WhereClause::LifetimeOutlives(_) => {}
795 WhereClause::TypeOutlives(_) => {}
786 } 796 }
787 first = false; 797 first = false;
788 } 798 }
@@ -806,7 +816,7 @@ fn fmt_trait_ref(tr: &TraitRef, f: &mut HirFormatter, use_as: bool) -> Result<()
806 write!(f, "{}", f.db.trait_data(tr.hir_trait_id()).name)?; 816 write!(f, "{}", f.db.trait_data(tr.hir_trait_id()).name)?;
807 if tr.substitution.len(&Interner) > 1 { 817 if tr.substitution.len(&Interner) > 1 {
808 write!(f, "<")?; 818 write!(f, "<")?;
809 f.write_joined(&tr.substitution.interned()[1..], ", ")?; 819 f.write_joined(&tr.substitution.as_slice(&Interner)[1..], ", ")?;
810 write!(f, ">")?; 820 write!(f, ">")?;
811 } 821 }
812 Ok(()) 822 Ok(())
@@ -837,6 +847,10 @@ impl HirDisplay for WhereClause {
837 ty.hir_fmt(f)?; 847 ty.hir_fmt(f)?;
838 } 848 }
839 WhereClause::AliasEq(_) => write!(f, "{{error}}")?, 849 WhereClause::AliasEq(_) => write!(f, "{{error}}")?,
850
851 // FIXME implement these
852 WhereClause::TypeOutlives(..) => {}
853 WhereClause::LifetimeOutlives(..) => {}
840 } 854 }
841 Ok(()) 855 Ok(())
842 } 856 }
@@ -881,9 +895,11 @@ impl HirDisplay for DomainGoal {
881 DomainGoal::Holds(wc) => { 895 DomainGoal::Holds(wc) => {
882 write!(f, "Holds(")?; 896 write!(f, "Holds(")?;
883 wc.hir_fmt(f)?; 897 wc.hir_fmt(f)?;
884 write!(f, ")") 898 write!(f, ")")?;
885 } 899 }
900 _ => write!(f, "?")?,
886 } 901 }
902 Ok(())
887 } 903 }
888} 904}
889 905