From 86feac18e8f96a99830defd707eb221f12d02924 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 7 Apr 2021 20:26:27 +0200 Subject: Change TraitRef::hir_fmt_ext to free-standing function --- crates/hir_ty/src/display.rs | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) (limited to 'crates/hir_ty/src') diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 9e6bbcdf1..266016786 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -792,31 +792,29 @@ fn write_bounds_like_dyn_trait( Ok(()) } -impl TraitRef { - fn hir_fmt_ext(&self, f: &mut HirFormatter, use_as: bool) -> Result<(), HirDisplayError> { - if f.should_truncate() { - return write!(f, "{}", TYPE_HINT_TRUNCATION); - } +fn fmt_trait_ref(tr: &TraitRef, f: &mut HirFormatter, use_as: bool) -> Result<(), HirDisplayError> { + if f.should_truncate() { + return write!(f, "{}", TYPE_HINT_TRUNCATION); + } - self.self_type_parameter(&Interner).hir_fmt(f)?; - if use_as { - write!(f, " as ")?; - } else { - write!(f, ": ")?; - } - write!(f, "{}", f.db.trait_data(self.hir_trait_id()).name)?; - if self.substitution.len(&Interner) > 1 { - write!(f, "<")?; - f.write_joined(&self.substitution.interned()[1..], ", ")?; - write!(f, ">")?; - } - Ok(()) + tr.self_type_parameter(&Interner).hir_fmt(f)?; + if use_as { + write!(f, " as ")?; + } else { + write!(f, ": ")?; } + write!(f, "{}", f.db.trait_data(tr.hir_trait_id()).name)?; + if tr.substitution.len(&Interner) > 1 { + write!(f, "<")?; + f.write_joined(&tr.substitution.interned()[1..], ", ")?; + write!(f, ">")?; + } + Ok(()) } impl HirDisplay for TraitRef { fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { - self.hir_fmt_ext(f, false) + fmt_trait_ref(self, f, false) } } @@ -830,7 +828,7 @@ impl HirDisplay for WhereClause { WhereClause::Implemented(trait_ref) => trait_ref.hir_fmt(f)?, WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(projection_ty), ty }) => { write!(f, "<")?; - projection_ty.trait_ref(f.db).hir_fmt_ext(f, true)?; + fmt_trait_ref(&projection_ty.trait_ref(f.db), f, true)?; write!( f, ">::{} = ", -- cgit v1.2.3 From 6777a4975d6a88928fda3e3b3f0bb05d98c61060 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 7 Apr 2021 20:40:01 +0200 Subject: Move hir_trait_id to extension trait --- crates/hir_ty/src/chalk_ext.rs | 10 ++++++++++ crates/hir_ty/src/display.rs | 2 +- crates/hir_ty/src/infer/path.rs | 3 ++- crates/hir_ty/src/lib.rs | 8 +------- crates/hir_ty/src/lower.rs | 2 +- crates/hir_ty/src/method_resolution.rs | 4 ++-- crates/hir_ty/src/traits.rs | 2 +- crates/hir_ty/src/traits/chalk.rs | 2 +- crates/hir_ty/src/traits/chalk/mapping.rs | 6 +++--- crates/hir_ty/src/utils.rs | 4 +++- 10 files changed, 25 insertions(+), 18 deletions(-) (limited to 'crates/hir_ty/src') diff --git a/crates/hir_ty/src/chalk_ext.rs b/crates/hir_ty/src/chalk_ext.rs index 8e8a1aa48..95538eff7 100644 --- a/crates/hir_ty/src/chalk_ext.rs +++ b/crates/hir_ty/src/chalk_ext.rs @@ -260,3 +260,13 @@ impl ProjectionTyExt for ProjectionTy { } } } + +pub trait TraitRefExt { + fn hir_trait_id(&self) -> TraitId; +} + +impl TraitRefExt for TraitRef { + fn hir_trait_id(&self) -> TraitId { + from_chalk_trait_id(self.trait_id) + } +} diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 266016786..52c2d6347 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -24,7 +24,7 @@ use crate::{ traits::chalk::from_chalk, utils::generics, AdtId, AliasEq, AliasTy, CallableDefId, CallableSig, Const, ConstValue, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, LifetimeData, LifetimeOutlives, Mutability, OpaqueTy, ProjectionTy, ProjectionTyExt, - QuantifiedWhereClause, Scalar, TraitRef, Ty, TyExt, TyKind, WhereClause, + QuantifiedWhereClause, Scalar, TraitRef, TraitRefExt, Ty, TyExt, TyKind, WhereClause, }; pub struct HirFormatter<'a> { diff --git a/crates/hir_ty/src/infer/path.rs b/crates/hir_ty/src/infer/path.rs index b19d67bb1..f8955aa32 100644 --- a/crates/hir_ty/src/infer/path.rs +++ b/crates/hir_ty/src/infer/path.rs @@ -11,7 +11,8 @@ use hir_def::{ use hir_expand::name::Name; use crate::{ - method_resolution, Interner, Substitution, Ty, TyBuilder, TyExt, TyKind, ValueTyDefId, + method_resolution, Interner, Substitution, TraitRefExt, Ty, TyBuilder, TyExt, TyKind, + ValueTyDefId, }; use super::{ExprOrPatId, InferenceContext, TraitRef}; diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 84645c435..7538e0874 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -41,7 +41,7 @@ use crate::{db::HirDatabase, display::HirDisplay, utils::generics}; pub use autoderef::autoderef; pub use builder::TyBuilder; -pub use chalk_ext::{ProjectionTyExt, TyExt}; +pub use chalk_ext::*; pub use infer::{could_unify, InferenceResult}; pub use lower::{ associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode, @@ -107,12 +107,6 @@ pub fn make_only_type_binders(num_vars: usize, value: T) -> Binders { ) } -impl TraitRef { - pub fn hir_trait_id(&self) -> TraitId { - from_chalk_trait_id(self.trait_id) - } -} - impl Canonical { pub fn new(value: T, kinds: impl IntoIterator) -> Self { let kinds = kinds.into_iter().map(|tk| { diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index 4ca6aa538..e6903e189 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -35,7 +35,7 @@ use crate::{ AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig, FnSubst, ImplTraitId, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause, QuantifiedWhereClauses, ReturnTypeImplTrait, ReturnTypeImplTraits, Substitution, - TraitEnvironment, TraitRef, Ty, TyBuilder, TyKind, TypeWalk, WhereClause, + TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyKind, TypeWalk, WhereClause, }; #[derive(Debug)] diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs index c601f2d53..7380b8613 100644 --- a/crates/hir_ty/src/method_resolution.rs +++ b/crates/hir_ty/src/method_resolution.rs @@ -22,8 +22,8 @@ use crate::{ static_lifetime, utils::all_super_traits, AdtId, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSig, ForeignDefId, - InEnvironment, Interner, Scalar, Substitution, TraitEnvironment, Ty, TyBuilder, TyExt, TyKind, - TypeWalk, + InEnvironment, Interner, Scalar, Substitution, TraitEnvironment, TraitRefExt, Ty, TyBuilder, + TyExt, TyKind, TypeWalk, }; /// This is used as a key for indexing impls. diff --git a/crates/hir_ty/src/traits.rs b/crates/hir_ty/src/traits.rs index 3374532c3..7d87741b8 100644 --- a/crates/hir_ty/src/traits.rs +++ b/crates/hir_ty/src/traits.rs @@ -9,7 +9,7 @@ use stdx::panic_context; use crate::{ db::HirDatabase, AliasEq, AliasTy, Canonical, DomainGoal, Guidance, HirDisplay, InEnvironment, - Solution, Ty, TyKind, WhereClause, + Solution, TraitRefExt, Ty, TyKind, WhereClause, }; use self::chalk::{from_chalk, Interner, ToChalk}; diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs index f03b92422..090f6492b 100644 --- a/crates/hir_ty/src/traits/chalk.rs +++ b/crates/hir_ty/src/traits/chalk.rs @@ -22,7 +22,7 @@ use crate::{ to_assoc_type_id, to_chalk_trait_id, utils::generics, AliasEq, AliasTy, BoundVar, CallableDefId, DebruijnIndex, FnDefId, ProjectionTy, Substitution, - TraitRef, Ty, TyBuilder, TyExt, TyKind, WhereClause, + TraitRef, TraitRefExt, Ty, TyBuilder, TyExt, TyKind, WhereClause, }; use mapping::{ convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue, diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index 84abd99b2..f64b5f4f8 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs @@ -10,9 +10,9 @@ use base_db::salsa::InternKey; use hir_def::{GenericDefId, TypeAliasId}; use crate::{ - chalk_ext::ProjectionTyExt, db::HirDatabase, static_lifetime, AliasTy, CallableDefId, - Canonical, ConstrainedSubst, DomainGoal, FnPointer, GenericArg, InEnvironment, OpaqueTy, - ProjectionTy, QuantifiedWhereClause, Substitution, TraitRef, Ty, TypeWalk, WhereClause, + db::HirDatabase, static_lifetime, AliasTy, CallableDefId, Canonical, ConstrainedSubst, + DomainGoal, FnPointer, GenericArg, InEnvironment, OpaqueTy, ProjectionTy, ProjectionTyExt, + QuantifiedWhereClause, Substitution, TraitRef, Ty, TypeWalk, WhereClause, }; use super::interner::*; diff --git a/crates/hir_ty/src/utils.rs b/crates/hir_ty/src/utils.rs index 0a424f607..8d5d5cd73 100644 --- a/crates/hir_ty/src/utils.rs +++ b/crates/hir_ty/src/utils.rs @@ -16,7 +16,9 @@ use hir_def::{ }; use hir_expand::name::{name, Name}; -use crate::{db::HirDatabase, Interner, Substitution, TraitRef, TyKind, TypeWalk, WhereClause}; +use crate::{ + db::HirDatabase, Interner, Substitution, TraitRef, TraitRefExt, TyKind, TypeWalk, WhereClause, +}; fn direct_super_traits(db: &dyn DefDatabase, trait_: TraitId) -> Vec { let resolver = trait_.resolver(db); -- cgit v1.2.3 From 9b4ecd3723c0184706d1276759846ebf3fdff944 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 7 Apr 2021 20:41:52 +0200 Subject: Fix return type of `self_type_parameter` --- crates/hir_ty/src/chalk_ext.rs | 4 ++-- crates/hir_ty/src/display.rs | 4 ++-- crates/hir_ty/src/traits/chalk/mapping.rs | 4 ++-- crates/hir_ty/src/types.rs | 8 ++++---- 4 files changed, 10 insertions(+), 10 deletions(-) (limited to 'crates/hir_ty/src') diff --git a/crates/hir_ty/src/chalk_ext.rs b/crates/hir_ty/src/chalk_ext.rs index 95538eff7..4ea91787e 100644 --- a/crates/hir_ty/src/chalk_ext.rs +++ b/crates/hir_ty/src/chalk_ext.rs @@ -199,12 +199,12 @@ impl TyExt for Ty { .map(|pred| pred.clone().substitute(&Interner, &substs)) .filter(|wc| match &wc.skip_binders() { WhereClause::Implemented(tr) => { - tr.self_type_parameter(&Interner) == self + &tr.self_type_parameter(&Interner) == self } WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(proj), ty: _, - }) => proj.self_type_parameter(&Interner) == self, + }) => &proj.self_type_parameter(&Interner) == self, _ => false, }) .collect::>(); diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 52c2d6347..90801ef2d 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -616,12 +616,12 @@ impl HirDisplay for Ty { .map(|pred| pred.clone().substitute(&Interner, &substs)) .filter(|wc| match &wc.skip_binders() { WhereClause::Implemented(tr) => { - tr.self_type_parameter(&Interner) == self + &tr.self_type_parameter(&Interner) == self } WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(proj), ty: _, - }) => proj.self_type_parameter(&Interner) == self, + }) => &proj.self_type_parameter(&Interner) == self, _ => false, }) .collect::>(); diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index f64b5f4f8..701359e6f 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs @@ -509,7 +509,7 @@ pub(super) fn generic_predicate_to_inline_bound( let (pred, binders) = pred.as_ref().into_value_and_skipped_binders(); match pred { WhereClause::Implemented(trait_ref) => { - if trait_ref.self_type_parameter(&Interner) != &self_ty_shifted_in { + if trait_ref.self_type_parameter(&Interner) != self_ty_shifted_in { // we can only convert predicates back to type bounds if they // have the expected self type return None; @@ -522,7 +522,7 @@ pub(super) fn generic_predicate_to_inline_bound( Some(chalk_ir::Binders::new(binders, rust_ir::InlineBound::TraitBound(trait_bound))) } WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(projection_ty), ty }) => { - if projection_ty.self_type_parameter(&Interner) != &self_ty_shifted_in { + if projection_ty.self_type_parameter(&Interner) != self_ty_shifted_in { return None; } let trait_ = projection_ty.trait_(db); diff --git a/crates/hir_ty/src/types.rs b/crates/hir_ty/src/types.rs index c25bc2d6a..72be7e04f 100644 --- a/crates/hir_ty/src/types.rs +++ b/crates/hir_ty/src/types.rs @@ -30,8 +30,8 @@ pub struct ProjectionTy { } impl ProjectionTy { - pub fn self_type_parameter(&self, interner: &Interner) -> &Ty { - &self.substitution.interned()[0].assert_ty_ref(interner) + pub fn self_type_parameter(&self, interner: &Interner) -> Ty { + self.substitution.interned()[0].assert_ty_ref(interner).clone() } } @@ -413,8 +413,8 @@ pub struct TraitRef { } impl TraitRef { - pub fn self_type_parameter(&self, interner: &Interner) -> &Ty { - &self.substitution.at(interner, 0).assert_ty_ref(interner) + pub fn self_type_parameter(&self, interner: &Interner) -> Ty { + self.substitution.at(interner, 0).assert_ty_ref(interner).clone() } } -- cgit v1.2.3 From d1b645d2360fb6e74aaa774ff713af02f685a110 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 7 Apr 2021 20:47:04 +0200 Subject: Make Canonical::new a free-standing function --- crates/hir_ty/src/lib.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'crates/hir_ty/src') diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 7538e0874..915b1028f 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -107,16 +107,18 @@ pub fn make_only_type_binders(num_vars: usize, value: T) -> Binders { ) } -impl Canonical { - pub fn new(value: T, kinds: impl IntoIterator) -> Self { - let kinds = kinds.into_iter().map(|tk| { - chalk_ir::CanonicalVarKind::new( - chalk_ir::VariableKind::Ty(tk), - chalk_ir::UniverseIndex::ROOT, - ) - }); - Self { value, binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds) } - } +// FIXME: get rid of this +pub fn make_canonical( + value: T, + kinds: impl IntoIterator, +) -> Canonical { + let kinds = kinds.into_iter().map(|tk| { + chalk_ir::CanonicalVarKind::new( + chalk_ir::VariableKind::Ty(tk), + chalk_ir::UniverseIndex::ROOT, + ) + }); + Canonical { value, binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds) } } /// A function signature as seen by type inference: Several parameter types and -- cgit v1.2.3 From be0084a0bc903544835d5c87df9eb9ce29a191d1 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 7 Apr 2021 20:48:58 +0200 Subject: InEnvironment::new takes a reference --- crates/hir_ty/src/infer.rs | 2 +- crates/hir_ty/src/infer/coerce.rs | 2 +- crates/hir_ty/src/method_resolution.rs | 2 +- crates/hir_ty/src/types.rs | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) (limited to 'crates/hir_ty/src') diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs index 6af0c59b8..531159e54 100644 --- a/crates/hir_ty/src/infer.rs +++ b/crates/hir_ty/src/infer.rs @@ -336,7 +336,7 @@ impl<'a> InferenceContext<'a> { self.last_obligations_check = Some(self.table.revision); let obligations = mem::replace(&mut self.obligations, Vec::new()); for obligation in obligations { - let in_env = InEnvironment::new(self.trait_env.env.clone(), obligation.clone()); + let in_env = InEnvironment::new(&self.trait_env.env, obligation.clone()); let canonicalized = self.canonicalizer().canonicalize_obligation(in_env); let solution = self.db.trait_solve(self.resolver.krate().unwrap(), canonicalized.value.clone()); diff --git a/crates/hir_ty/src/infer/coerce.rs b/crates/hir_ty/src/infer/coerce.rs index f1af2a0bd..fd679f444 100644 --- a/crates/hir_ty/src/infer/coerce.rs +++ b/crates/hir_ty/src/infer/coerce.rs @@ -139,7 +139,7 @@ impl<'a> InferenceContext<'a> { b.push(from_ty.clone()).push(to_ty.clone()).build() }; - let goal = InEnvironment::new(self.trait_env.env.clone(), trait_ref.cast(&Interner)); + let goal = InEnvironment::new(&self.trait_env.env, trait_ref.cast(&Interner)); let canonicalizer = self.canonicalizer(); let canonicalized = canonicalizer.canonicalize_obligation(goal); diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs index 7380b8613..7e09a1539 100644 --- a/crates/hir_ty/src/method_resolution.rs +++ b/crates/hir_ty/src/method_resolution.rs @@ -845,7 +845,7 @@ fn generic_implements_goal( let obligation = trait_ref.cast(&Interner); Canonical { binders: CanonicalVarKinds::from_iter(&Interner, kinds), - value: InEnvironment::new(env.env.clone(), obligation), + value: InEnvironment::new(&env.env, obligation), } } diff --git a/crates/hir_ty/src/types.rs b/crates/hir_ty/src/types.rs index 72be7e04f..e1a28f5f7 100644 --- a/crates/hir_ty/src/types.rs +++ b/crates/hir_ty/src/types.rs @@ -470,8 +470,8 @@ pub struct InEnvironment { } impl InEnvironment { - pub fn new(environment: chalk_ir::Environment, value: T) -> InEnvironment { - InEnvironment { environment, goal: value } + pub fn new(environment: &chalk_ir::Environment, value: T) -> InEnvironment { + InEnvironment { environment: environment.clone(), goal: value } } } -- cgit v1.2.3 From dc116f7ce2192433c9491441a11d294e7d294fbf Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 7 Apr 2021 20:50:26 +0200 Subject: Fix return type of Substitution::interned --- crates/hir_ty/src/display.rs | 2 +- crates/hir_ty/src/infer/pat.rs | 2 +- crates/hir_ty/src/types.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/hir_ty/src') diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 90801ef2d..e0ca96c6d 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -745,7 +745,7 @@ fn write_bounds_like_dyn_trait( // existential) here, which is the only thing that's // possible in actual Rust, and hence don't print it write!(f, "{}", f.db.trait_data(trait_).name)?; - if let [_, params @ ..] = &*trait_ref.substitution.interned() { + if let [_, params @ ..] = &*trait_ref.substitution.interned().as_slice() { if is_fn_trait { if let Some(args) = params.first().and_then(|it| it.assert_ty_ref(&Interner).as_tuple()) diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs index f88d5c5d3..a41e8e116 100644 --- a/crates/hir_ty/src/infer/pat.rs +++ b/crates/hir_ty/src/infer/pat.rs @@ -122,7 +122,7 @@ impl<'a> InferenceContext<'a> { let ty = match &body[pat] { &Pat::Tuple { ref args, ellipsis } => { let expectations = match expected.as_tuple() { - Some(parameters) => &*parameters.interned(), + Some(parameters) => &*parameters.interned().as_slice(), _ => &[], }; diff --git a/crates/hir_ty/src/types.rs b/crates/hir_ty/src/types.rs index e1a28f5f7..89adad108 100644 --- a/crates/hir_ty/src/types.rs +++ b/crates/hir_ty/src/types.rs @@ -282,7 +282,7 @@ impl GenericArg { pub struct Substitution(SmallVec<[GenericArg; 2]>); impl Substitution { - pub fn interned(&self) -> &[GenericArg] { + pub fn interned(&self) -> &SmallVec<[GenericArg; 2]> { &self.0 } -- cgit v1.2.3