From 590c41635952e19c3caae525a827499dbd360049 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 21 Mar 2021 13:22:22 +0100 Subject: 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. --- crates/hir_ty/src/display.rs | 48 +++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 16 deletions(-) (limited to 'crates/hir_ty/src/display.rs') 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 @@ //! FIXME: write short doc here -use std::{borrow::Cow, fmt}; +use std::fmt; use arrayvec::ArrayVec; use chalk_ir::Mutability; @@ -20,7 +20,7 @@ use crate::{ db::HirDatabase, from_assoc_type_id, from_foreign_def_id, from_placeholder_idx, primitive, to_assoc_type_id, traits::chalk::from_chalk, utils::generics, AdtId, AliasEq, AliasTy, CallableDefId, CallableSig, DomainGoal, ImplTraitId, Interner, Lifetime, OpaqueTy, - ProjectionTy, Scalar, Substitution, TraitRef, Ty, TyKind, WhereClause, + ProjectionTy, QuantifiedWhereClause, Scalar, Substitution, TraitRef, Ty, TyKind, WhereClause, }; pub struct HirFormatter<'a> { @@ -328,9 +328,9 @@ impl HirDisplay for Ty { // FIXME: all this just to decide whether to use parentheses... let datas; - let predicates = match t.interned(&Interner) { - TyKind::Dyn(predicates) if predicates.len() > 1 => { - Cow::Borrowed(predicates.as_ref()) + let predicates: Vec<_> = match t.interned(&Interner) { + TyKind::Dyn(dyn_ty) if dyn_ty.bounds.skip_binders().interned().len() > 1 => { + dyn_ty.bounds.skip_binders().interned().iter().cloned().collect() } &TyKind::Alias(AliasTy::Opaque(OpaqueTy { opaque_ty_id, @@ -345,17 +345,21 @@ impl HirDisplay for Ty { .as_ref() .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone()); let bounds = data.subst(parameters); - Cow::Owned(bounds.value) + bounds.value } else { - Cow::Borrowed(&[][..]) + Vec::new() } } - _ => Cow::Borrowed(&[][..]), + _ => Vec::new(), }; - if let [WhereClause::Implemented(trait_ref), _] = predicates.as_ref() { + if let Some(WhereClause::Implemented(trait_ref)) = + predicates.get(0).map(|b| b.skip_binders()) + { let trait_ = trait_ref.hir_trait_id(); - if fn_traits(f.db.upcast(), trait_).any(|it| it == trait_) { + if fn_traits(f.db.upcast(), trait_).any(|it| it == trait_) + && predicates.len() <= 2 + { return write!(f, "{}", ty_display); } } @@ -586,13 +590,25 @@ impl HirDisplay for Ty { _ => false, }) .collect::>(); - write_bounds_like_dyn_trait_with_prefix("impl", &bounds, f)?; + write_bounds_like_dyn_trait_with_prefix( + "impl", + &bounds + .iter() + .cloned() + .map(crate::Binders::wrap_empty) + .collect::>(), + f, + )?; } } } TyKind::BoundVar(idx) => write!(f, "?{}.{}", idx.debruijn.depth(), idx.index)?, - TyKind::Dyn(predicates) => { - write_bounds_like_dyn_trait_with_prefix("dyn", predicates, f)?; + TyKind::Dyn(dyn_ty) => { + write_bounds_like_dyn_trait_with_prefix( + "dyn", + dyn_ty.bounds.skip_binders().interned(), + f, + )?; } TyKind::Alias(AliasTy::Projection(p_ty)) => p_ty.hir_fmt(f)?, TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { @@ -661,7 +677,7 @@ fn fn_traits(db: &dyn DefDatabase, trait_: TraitId) -> impl Iterator Result<(), HirDisplayError> { write!(f, "{}", prefix)?; @@ -674,7 +690,7 @@ pub fn write_bounds_like_dyn_trait_with_prefix( } fn write_bounds_like_dyn_trait( - predicates: &[WhereClause], + predicates: &[QuantifiedWhereClause], f: &mut HirFormatter, ) -> Result<(), HirDisplayError> { // Note: This code is written to produce nice results (i.e. @@ -687,7 +703,7 @@ fn write_bounds_like_dyn_trait( let mut angle_open = false; let mut is_fn_trait = false; for p in predicates.iter() { - match p { + match p.skip_binders() { WhereClause::Implemented(trait_ref) => { let trait_ = trait_ref.hir_trait_id(); if !is_fn_trait { -- cgit v1.2.3