diff options
Diffstat (limited to 'crates/hir_ty/src/display.rs')
-rw-r--r-- | crates/hir_ty/src/display.rs | 40 |
1 files changed, 24 insertions, 16 deletions
diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 9d3b79be3..cc6b93d37 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 | ||
3 | use std::{borrow::Cow, fmt}; | 3 | use std::fmt; |
4 | 4 | ||
5 | use arrayvec::ArrayVec; | 5 | use arrayvec::ArrayVec; |
6 | use chalk_ir::Mutability; | 6 | use 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 | ||
26 | pub struct HirFormatter<'a> { | 26 | pub 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 | } |
@@ -577,7 +581,7 @@ impl HirDisplay for Ty { | |||
577 | .generic_predicates(id.parent) | 581 | .generic_predicates(id.parent) |
578 | .into_iter() | 582 | .into_iter() |
579 | .map(|pred| pred.clone().subst(&substs)) | 583 | .map(|pred| pred.clone().subst(&substs)) |
580 | .filter(|wc| match &wc { | 584 | .filter(|wc| match &wc.skip_binders() { |
581 | WhereClause::Implemented(tr) => tr.self_type_parameter() == self, | 585 | WhereClause::Implemented(tr) => tr.self_type_parameter() == self, |
582 | WhereClause::AliasEq(AliasEq { | 586 | WhereClause::AliasEq(AliasEq { |
583 | alias: AliasTy::Projection(proj), | 587 | alias: AliasTy::Projection(proj), |
@@ -591,8 +595,12 @@ impl HirDisplay for Ty { | |||
591 | } | 595 | } |
592 | } | 596 | } |
593 | TyKind::BoundVar(idx) => write!(f, "?{}.{}", idx.debruijn.depth(), idx.index)?, | 597 | TyKind::BoundVar(idx) => write!(f, "?{}.{}", idx.debruijn.depth(), idx.index)?, |
594 | TyKind::Dyn(predicates) => { | 598 | TyKind::Dyn(dyn_ty) => { |
595 | write_bounds_like_dyn_trait_with_prefix("dyn", predicates, f)?; | 599 | write_bounds_like_dyn_trait_with_prefix( |
600 | "dyn", | ||
601 | dyn_ty.bounds.skip_binders().interned(), | ||
602 | f, | ||
603 | )?; | ||
596 | } | 604 | } |
597 | TyKind::Alias(AliasTy::Projection(p_ty)) => p_ty.hir_fmt(f)?, | 605 | TyKind::Alias(AliasTy::Projection(p_ty)) => p_ty.hir_fmt(f)?, |
598 | TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { | 606 | TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { |
@@ -661,7 +669,7 @@ fn fn_traits(db: &dyn DefDatabase, trait_: TraitId) -> impl Iterator<Item = Trai | |||
661 | 669 | ||
662 | pub fn write_bounds_like_dyn_trait_with_prefix( | 670 | pub fn write_bounds_like_dyn_trait_with_prefix( |
663 | prefix: &str, | 671 | prefix: &str, |
664 | predicates: &[WhereClause], | 672 | predicates: &[QuantifiedWhereClause], |
665 | f: &mut HirFormatter, | 673 | f: &mut HirFormatter, |
666 | ) -> Result<(), HirDisplayError> { | 674 | ) -> Result<(), HirDisplayError> { |
667 | write!(f, "{}", prefix)?; | 675 | write!(f, "{}", prefix)?; |
@@ -674,7 +682,7 @@ pub fn write_bounds_like_dyn_trait_with_prefix( | |||
674 | } | 682 | } |
675 | 683 | ||
676 | fn write_bounds_like_dyn_trait( | 684 | fn write_bounds_like_dyn_trait( |
677 | predicates: &[WhereClause], | 685 | predicates: &[QuantifiedWhereClause], |
678 | f: &mut HirFormatter, | 686 | f: &mut HirFormatter, |
679 | ) -> Result<(), HirDisplayError> { | 687 | ) -> Result<(), HirDisplayError> { |
680 | // Note: This code is written to produce nice results (i.e. | 688 | // Note: This code is written to produce nice results (i.e. |
@@ -687,7 +695,7 @@ fn write_bounds_like_dyn_trait( | |||
687 | let mut angle_open = false; | 695 | let mut angle_open = false; |
688 | let mut is_fn_trait = false; | 696 | let mut is_fn_trait = false; |
689 | for p in predicates.iter() { | 697 | for p in predicates.iter() { |
690 | match p { | 698 | match p.skip_binders() { |
691 | WhereClause::Implemented(trait_ref) => { | 699 | WhereClause::Implemented(trait_ref) => { |
692 | let trait_ = trait_ref.hir_trait_id(); | 700 | let trait_ = trait_ref.hir_trait_id(); |
693 | if !is_fn_trait { | 701 | if !is_fn_trait { |