Move ProjectionTy methods to extension trait

This commit is contained in:
Florian Diebold 2021-04-05 21:14:49 +02:00
parent 8c96a7d81e
commit 788533d380
7 changed files with 43 additions and 32 deletions

View file

@ -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;

View file

@ -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"),
}
}
}

View file

@ -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> {

View file

@ -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::{

View file

@ -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<Interner>;
pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>;
pub type FnDefId = chalk_ir::FnDefId<Interner>;
@ -76,26 +74,6 @@ pub type LifetimeOutlives = chalk_ir::LifetimeOutlives<Interner>;
pub type ChalkTraitId = chalk_ir::TraitId<Interner>;
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<Interner>;
impl Substitution {

View file

@ -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::*;

View file

@ -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.