From 788533d38091472ed05c8b55fd7a2002c49f4eb7 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 5 Apr 2021 21:14:49 +0200 Subject: Move ProjectionTy methods to extension trait --- crates/hir_ty/src/autoderef.rs | 2 +- crates/hir_ty/src/chalk_ext.rs | 28 +++++++++++++++++++++++++++- crates/hir_ty/src/display.rs | 3 ++- crates/hir_ty/src/infer/expr.rs | 4 ++-- crates/hir_ty/src/lib.rs | 26 ++------------------------ crates/hir_ty/src/traits/chalk/mapping.rs | 6 +++--- crates/hir_ty/src/types.rs | 6 ++++++ 7 files changed, 43 insertions(+), 32 deletions(-) diff --git a/crates/hir_ty/src/autoderef.rs b/crates/hir_ty/src/autoderef.rs index 7ca4af80e..c5890e24d 100644 --- a/crates/hir_ty/src/autoderef.rs +++ b/crates/hir_ty/src/autoderef.rs @@ -13,7 +13,7 @@ use log::{info, warn}; use crate::{ db::HirDatabase, AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, - InEnvironment, Interner, Solution, Ty, TyBuilder, TyKind, + InEnvironment, Interner, ProjectionTyExt, Solution, Ty, TyBuilder, TyKind, }; const AUTODEREF_RECURSION_LIMIT: usize = 10; diff --git a/crates/hir_ty/src/chalk_ext.rs b/crates/hir_ty/src/chalk_ext.rs index b7463366b..0f4cb43e9 100644 --- a/crates/hir_ty/src/chalk_ext.rs +++ b/crates/hir_ty/src/chalk_ext.rs @@ -1,6 +1,11 @@ //! Various extensions traits for Chalk types. -use crate::{Interner, Ty, TyKind}; +use hir_def::{AssocContainerId, Lookup, TraitId}; + +use crate::{ + db::HirDatabase, from_assoc_type_id, to_chalk_trait_id, Interner, ProjectionTy, TraitRef, Ty, + TyKind, +}; pub trait TyExt { fn is_unit(&self) -> bool; @@ -11,3 +16,24 @@ impl TyExt for Ty { matches!(self.kind(&Interner), TyKind::Tuple(0, _)) } } + +pub trait ProjectionTyExt { + fn trait_ref(&self, db: &dyn HirDatabase) -> TraitRef; + fn trait_(&self, db: &dyn HirDatabase) -> TraitId; +} + +impl ProjectionTyExt for ProjectionTy { + fn trait_ref(&self, db: &dyn HirDatabase) -> TraitRef { + TraitRef { + trait_id: to_chalk_trait_id(self.trait_(db)), + substitution: self.substitution.clone(), + } + } + + fn trait_(&self, db: &dyn HirDatabase) -> TraitId { + match from_assoc_type_id(self.associated_ty_id).lookup(db.upcast()).container { + AssocContainerId::TraitId(it) => it, + _ => panic!("projection ty without parent trait"), + } + } +} diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 01c7ef91f..91d657ce2 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -22,7 +22,8 @@ use crate::{ lt_from_placeholder_idx, primitive, to_assoc_type_id, traits::chalk::from_chalk, utils::generics, AdtId, AliasEq, AliasTy, CallableDefId, CallableSig, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, LifetimeData, LifetimeOutlives, Mutability, OpaqueTy, - ProjectionTy, QuantifiedWhereClause, Scalar, TraitRef, Ty, TyExt, TyKind, WhereClause, + ProjectionTy, ProjectionTyExt, QuantifiedWhereClause, Scalar, TraitRef, Ty, TyExt, TyKind, + WhereClause, }; pub struct HirFormatter<'a> { diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 6966d26e7..56a9365cb 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -22,8 +22,8 @@ use crate::{ to_chalk_trait_id, traits::{chalk::from_chalk, FnTrait}, utils::{generics, variant_data, Generics}, - AdtId, Binders, CallableDefId, FnPointer, FnSig, InEnvironment, Interner, Rawness, Scalar, - Substitution, TraitRef, Ty, TyBuilder, TyKind, + AdtId, Binders, CallableDefId, FnPointer, FnSig, InEnvironment, Interner, ProjectionTyExt, + Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyKind, }; use super::{ diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index a3addc8e9..928dd76e5 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -43,13 +43,13 @@ use crate::{db::HirDatabase, display::HirDisplay, utils::generics}; pub use autoderef::autoderef; pub use builder::TyBuilder; -pub use chalk_ext::TyExt; +pub use chalk_ext::{ProjectionTyExt, TyExt}; pub use infer::{could_unify, InferenceResult, InferenceVar}; pub use lower::{ associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode, TyDefId, TyLoweringContext, ValueTyDefId, }; -pub use traits::TraitEnvironment; +pub use traits::{chalk::Interner, TraitEnvironment}; pub use types::*; pub use walk::TypeWalk; @@ -57,8 +57,6 @@ pub use chalk_ir::{ cast::Cast, AdtId, BoundVar, DebruijnIndex, Mutability, Safety, Scalar, TyVariableKind, }; -pub use crate::traits::chalk::Interner; - pub type ForeignDefId = chalk_ir::ForeignDefId; pub type AssocTypeId = chalk_ir::AssocTypeId; pub type FnDefId = chalk_ir::FnDefId; @@ -76,26 +74,6 @@ pub type LifetimeOutlives = chalk_ir::LifetimeOutlives; pub type ChalkTraitId = chalk_ir::TraitId; -impl ProjectionTy { - pub fn trait_ref(&self, db: &dyn HirDatabase) -> TraitRef { - TraitRef { - trait_id: to_chalk_trait_id(self.trait_(db)), - substitution: self.substitution.clone(), - } - } - - pub fn self_type_parameter(&self, interner: &Interner) -> &Ty { - &self.substitution.interned()[0].assert_ty_ref(interner) - } - - fn trait_(&self, db: &dyn HirDatabase) -> TraitId { - match from_assoc_type_id(self.associated_ty_id).lookup(db.upcast()).container { - AssocContainerId::TraitId(it) => it, - _ => panic!("projection ty without parent trait"), - } - } -} - pub type FnSig = chalk_ir::FnSig; impl Substitution { diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index c3b148cab..9f10b889f 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::{ - db::HirDatabase, primitive::UintTy, AliasTy, CallableDefId, Canonical, DomainGoal, FnPointer, - GenericArg, InEnvironment, OpaqueTy, ProjectionTy, QuantifiedWhereClause, Scalar, Substitution, - TraitRef, Ty, TypeWalk, WhereClause, + chalk_ext::ProjectionTyExt, db::HirDatabase, primitive::UintTy, AliasTy, CallableDefId, + Canonical, DomainGoal, FnPointer, GenericArg, InEnvironment, OpaqueTy, ProjectionTy, + QuantifiedWhereClause, Scalar, Substitution, TraitRef, Ty, TypeWalk, WhereClause, }; use super::interner::*; diff --git a/crates/hir_ty/src/types.rs b/crates/hir_ty/src/types.rs index dc64e6e2b..bd89991dc 100644 --- a/crates/hir_ty/src/types.rs +++ b/crates/hir_ty/src/types.rs @@ -29,6 +29,12 @@ pub struct ProjectionTy { pub substitution: Substitution, } +impl ProjectionTy { + pub fn self_type_parameter(&self, interner: &Interner) -> &Ty { + &self.substitution.interned()[0].assert_ty_ref(interner) + } +} + #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct DynTy { /// The unknown self type. -- cgit v1.2.3