From 1bf6b7360c3f1d0e20dece5227979bc4d74a352f Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 13 Mar 2021 19:47:34 +0100 Subject: Use chalk_ir::PlaceholderIndex --- crates/hir_ty/src/display.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 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 b7e85e024..0a13c8636 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -11,10 +11,10 @@ use hir_def::{ use hir_expand::name::Name; use crate::{ - db::HirDatabase, from_assoc_type_id, from_foreign_def_id, primitive, to_assoc_type_id, - traits::chalk::from_chalk, utils::generics, AdtId, AliasTy, CallableDefId, CallableSig, - GenericPredicate, Interner, Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, - Substs, TraitRef, Ty, TyKind, + 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, AliasTy, CallableDefId, + CallableSig, GenericPredicate, Interner, Lifetime, Obligation, OpaqueTy, OpaqueTyId, + ProjectionTy, Scalar, Substs, TraitRef, Ty, TyKind, }; pub struct HirFormatter<'a> { @@ -541,7 +541,8 @@ impl HirDisplay for Ty { write!(f, "{{closure}}")?; } } - TyKind::Placeholder(id) => { + TyKind::Placeholder(idx) => { + let id = from_placeholder_idx(f.db, *idx); let generics = generics(f.db.upcast(), id.parent); let param_data = &generics.params.types[id.local_id]; match param_data.provenance { @@ -549,8 +550,8 @@ impl HirDisplay for Ty { write!(f, "{}", param_data.name.clone().unwrap_or_else(Name::missing))? } TypeParamProvenance::ArgumentImplTrait => { - let bounds = f.db.generic_predicates_for_param(*id); - let substs = Substs::type_params_for_generics(&generics); + let bounds = f.db.generic_predicates_for_param(id); + let substs = Substs::type_params_for_generics(f.db, &generics); write_bounds_like_dyn_trait_with_prefix( "impl", &bounds.iter().map(|b| b.clone().subst(&substs)).collect::>(), -- cgit v1.2.3 From b035c314b4b0ecd2477fde216dbe7e8801f94d0d Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 13 Mar 2021 20:05:47 +0100 Subject: Use chalk_ir::OpaqueTyId --- crates/hir_ty/src/display.rs | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 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 0a13c8636..e6473586b 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -13,7 +13,7 @@ use hir_expand::name::Name; 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, AliasTy, CallableDefId, - CallableSig, GenericPredicate, Interner, Lifetime, Obligation, OpaqueTy, OpaqueTyId, + CallableSig, GenericPredicate, ImplTraitId, Interner, Lifetime, Obligation, OpaqueTy, ProjectionTy, Scalar, Substs, TraitRef, Ty, TyKind, }; @@ -313,22 +313,26 @@ 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()) } - &TyKind::Alias(AliasTy::Opaque(OpaqueTy { - opaque_ty_id: OpaqueTyId::ReturnTypeImplTrait(func, idx), - ref parameters, - })) => { - datas = - f.db.return_type_impl_traits(func).expect("impl trait id without data"); - let data = (*datas) - .as_ref() - .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone()); - let bounds = data.subst(parameters); - Cow::Owned(bounds.value) + &TyKind::Alias(AliasTy::Opaque(OpaqueTy { opaque_ty_id, ref parameters })) => { + let impl_trait_id = f.db.lookup_intern_impl_trait_id(opaque_ty_id.into()); + if let ImplTraitId::ReturnTypeImplTrait(func, idx) = impl_trait_id { + datas = + f.db.return_type_impl_traits(func) + .expect("impl trait id without data"); + let data = (*datas) + .as_ref() + .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone()); + let bounds = data.subst(parameters); + Cow::Owned(bounds.value) + } else { + Cow::Borrowed(&[][..]) + } } _ => Cow::Borrowed(&[][..]), }; @@ -499,8 +503,9 @@ impl HirDisplay for Ty { write!(f, "{}", type_alias.name)?; } TyKind::OpaqueType(opaque_ty_id, parameters) => { - match opaque_ty_id { - &OpaqueTyId::ReturnTypeImplTrait(func, idx) => { + let impl_trait_id = f.db.lookup_intern_impl_trait_id((*opaque_ty_id).into()); + match impl_trait_id { + ImplTraitId::ReturnTypeImplTrait(func, idx) => { let datas = f.db.return_type_impl_traits(func).expect("impl trait id without data"); let data = (*datas) @@ -510,7 +515,7 @@ impl HirDisplay for Ty { write_bounds_like_dyn_trait_with_prefix("impl", &bounds.value, f)?; // FIXME: it would maybe be good to distinguish this from the alias type (when debug printing), and to show the substitution } - OpaqueTyId::AsyncBlockTypeImplTrait(..) => { + ImplTraitId::AsyncBlockTypeImplTrait(..) => { write!(f, "impl Future")?; @@ -566,8 +571,9 @@ impl HirDisplay for Ty { } TyKind::Alias(AliasTy::Projection(p_ty)) => p_ty.hir_fmt(f)?, TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { - match opaque_ty.opaque_ty_id { - OpaqueTyId::ReturnTypeImplTrait(func, idx) => { + let impl_trait_id = f.db.lookup_intern_impl_trait_id(opaque_ty.opaque_ty_id.into()); + match impl_trait_id { + ImplTraitId::ReturnTypeImplTrait(func, idx) => { let datas = f.db.return_type_impl_traits(func).expect("impl trait id without data"); let data = (*datas) @@ -576,7 +582,7 @@ impl HirDisplay for Ty { let bounds = data.subst(&opaque_ty.parameters); write_bounds_like_dyn_trait_with_prefix("impl", &bounds.value, f)?; } - OpaqueTyId::AsyncBlockTypeImplTrait(..) => { + ImplTraitId::AsyncBlockTypeImplTrait(..) => { write!(f, "{{async block}}")?; } }; -- cgit v1.2.3