aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/display.rs
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2021-03-21 12:22:22 +0000
committerFlorian Diebold <[email protected]>2021-03-21 17:01:14 +0000
commit590c41635952e19c3caae525a827499dbd360049 (patch)
treeeca7c162975ef901b723d255512e61c047e838b8 /crates/hir_ty/src/display.rs
parent35868c4f7dc479dd5f731a2785ec6a203046ea9c (diff)
Introduce QuantifiedWhereClause and DynTy analogous to Chalk
This introduces a bunch of new binders in lots of places, which we have to be careful about, but we had to add them at some point.
Diffstat (limited to 'crates/hir_ty/src/display.rs')
-rw-r--r--crates/hir_ty/src/display.rs48
1 files changed, 32 insertions, 16 deletions
diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs
index 9d3b79be3..372671405 100644
--- a/crates/hir_ty/src/display.rs
+++ b/crates/hir_ty/src/display.rs
@@ -1,6 +1,6 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use std::{borrow::Cow, fmt}; 3use std::fmt;
4 4
5use arrayvec::ArrayVec; 5use arrayvec::ArrayVec;
6use chalk_ir::Mutability; 6use chalk_ir::Mutability;
@@ -20,7 +20,7 @@ use crate::{
20 db::HirDatabase, from_assoc_type_id, from_foreign_def_id, from_placeholder_idx, primitive, 20 db::HirDatabase, from_assoc_type_id, from_foreign_def_id, from_placeholder_idx, primitive,
21 to_assoc_type_id, traits::chalk::from_chalk, utils::generics, AdtId, AliasEq, AliasTy, 21 to_assoc_type_id, traits::chalk::from_chalk, utils::generics, AdtId, AliasEq, AliasTy,
22 CallableDefId, CallableSig, DomainGoal, ImplTraitId, Interner, Lifetime, OpaqueTy, 22 CallableDefId, CallableSig, DomainGoal, ImplTraitId, Interner, Lifetime, OpaqueTy,
23 ProjectionTy, Scalar, Substitution, TraitRef, Ty, TyKind, WhereClause, 23 ProjectionTy, QuantifiedWhereClause, Scalar, Substitution, TraitRef, Ty, TyKind, WhereClause,
24}; 24};
25 25
26pub struct HirFormatter<'a> { 26pub struct HirFormatter<'a> {
@@ -328,9 +328,9 @@ impl HirDisplay for Ty {
328 328
329 // FIXME: all this just to decide whether to use parentheses... 329 // FIXME: all this just to decide whether to use parentheses...
330 let datas; 330 let datas;
331 let predicates = match t.interned(&Interner) { 331 let predicates: Vec<_> = match t.interned(&Interner) {
332 TyKind::Dyn(predicates) if predicates.len() > 1 => { 332 TyKind::Dyn(dyn_ty) if dyn_ty.bounds.skip_binders().interned().len() > 1 => {
333 Cow::Borrowed(predicates.as_ref()) 333 dyn_ty.bounds.skip_binders().interned().iter().cloned().collect()
334 } 334 }
335 &TyKind::Alias(AliasTy::Opaque(OpaqueTy { 335 &TyKind::Alias(AliasTy::Opaque(OpaqueTy {
336 opaque_ty_id, 336 opaque_ty_id,
@@ -345,17 +345,21 @@ impl HirDisplay for Ty {
345 .as_ref() 345 .as_ref()
346 .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone()); 346 .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone());
347 let bounds = data.subst(parameters); 347 let bounds = data.subst(parameters);
348 Cow::Owned(bounds.value) 348 bounds.value
349 } else { 349 } else {
350 Cow::Borrowed(&[][..]) 350 Vec::new()
351 } 351 }
352 } 352 }
353 _ => Cow::Borrowed(&[][..]), 353 _ => Vec::new(),
354 }; 354 };
355 355
356 if let [WhereClause::Implemented(trait_ref), _] = predicates.as_ref() { 356 if let Some(WhereClause::Implemented(trait_ref)) =
357 predicates.get(0).map(|b| b.skip_binders())
358 {
357 let trait_ = trait_ref.hir_trait_id(); 359 let trait_ = trait_ref.hir_trait_id();
358 if fn_traits(f.db.upcast(), trait_).any(|it| it == trait_) { 360 if fn_traits(f.db.upcast(), trait_).any(|it| it == trait_)
361 && predicates.len() <= 2
362 {
359 return write!(f, "{}", ty_display); 363 return write!(f, "{}", ty_display);
360 } 364 }
361 } 365 }
@@ -586,13 +590,25 @@ impl HirDisplay for Ty {
586 _ => false, 590 _ => false,
587 }) 591 })
588 .collect::<Vec<_>>(); 592 .collect::<Vec<_>>();
589 write_bounds_like_dyn_trait_with_prefix("impl", &bounds, f)?; 593 write_bounds_like_dyn_trait_with_prefix(
594 "impl",
595 &bounds
596 .iter()
597 .cloned()
598 .map(crate::Binders::wrap_empty)
599 .collect::<Vec<_>>(),
600 f,
601 )?;
590 } 602 }
591 } 603 }
592 } 604 }
593 TyKind::BoundVar(idx) => write!(f, "?{}.{}", idx.debruijn.depth(), idx.index)?, 605 TyKind::BoundVar(idx) => write!(f, "?{}.{}", idx.debruijn.depth(), idx.index)?,
594 TyKind::Dyn(predicates) => { 606 TyKind::Dyn(dyn_ty) => {
595 write_bounds_like_dyn_trait_with_prefix("dyn", predicates, f)?; 607 write_bounds_like_dyn_trait_with_prefix(
608 "dyn",
609 dyn_ty.bounds.skip_binders().interned(),
610 f,
611 )?;
596 } 612 }
597 TyKind::Alias(AliasTy::Projection(p_ty)) => p_ty.hir_fmt(f)?, 613 TyKind::Alias(AliasTy::Projection(p_ty)) => p_ty.hir_fmt(f)?,
598 TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { 614 TyKind::Alias(AliasTy::Opaque(opaque_ty)) => {
@@ -661,7 +677,7 @@ fn fn_traits(db: &dyn DefDatabase, trait_: TraitId) -> impl Iterator<Item = Trai
661 677
662pub fn write_bounds_like_dyn_trait_with_prefix( 678pub fn write_bounds_like_dyn_trait_with_prefix(
663 prefix: &str, 679 prefix: &str,
664 predicates: &[WhereClause], 680 predicates: &[QuantifiedWhereClause],
665 f: &mut HirFormatter, 681 f: &mut HirFormatter,
666) -> Result<(), HirDisplayError> { 682) -> Result<(), HirDisplayError> {
667 write!(f, "{}", prefix)?; 683 write!(f, "{}", prefix)?;
@@ -674,7 +690,7 @@ pub fn write_bounds_like_dyn_trait_with_prefix(
674} 690}
675 691
676fn write_bounds_like_dyn_trait( 692fn write_bounds_like_dyn_trait(
677 predicates: &[WhereClause], 693 predicates: &[QuantifiedWhereClause],
678 f: &mut HirFormatter, 694 f: &mut HirFormatter,
679) -> Result<(), HirDisplayError> { 695) -> Result<(), HirDisplayError> {
680 // Note: This code is written to produce nice results (i.e. 696 // Note: This code is written to produce nice results (i.e.
@@ -687,7 +703,7 @@ fn write_bounds_like_dyn_trait(
687 let mut angle_open = false; 703 let mut angle_open = false;
688 let mut is_fn_trait = false; 704 let mut is_fn_trait = false;
689 for p in predicates.iter() { 705 for p in predicates.iter() {
690 match p { 706 match p.skip_binders() {
691 WhereClause::Implemented(trait_ref) => { 707 WhereClause::Implemented(trait_ref) => {
692 let trait_ = trait_ref.hir_trait_id(); 708 let trait_ = trait_ref.hir_trait_id();
693 if !is_fn_trait { 709 if !is_fn_trait {