Skip to content

Commit 788533d

Browse files
committed
Move ProjectionTy methods to extension trait
1 parent 8c96a7d commit 788533d

File tree

7 files changed

+43
-32
lines changed

7 files changed

+43
-32
lines changed

crates/hir_ty/src/autoderef.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use log::{info, warn};
1313

1414
use crate::{
1515
db::HirDatabase, AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex,
16-
InEnvironment, Interner, Solution, Ty, TyBuilder, TyKind,
16+
InEnvironment, Interner, ProjectionTyExt, Solution, Ty, TyBuilder, TyKind,
1717
};
1818

1919
const AUTODEREF_RECURSION_LIMIT: usize = 10;

crates/hir_ty/src/chalk_ext.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
//! Various extensions traits for Chalk types.
22
3-
use crate::{Interner, Ty, TyKind};
3+
use hir_def::{AssocContainerId, Lookup, TraitId};
4+
5+
use crate::{
6+
db::HirDatabase, from_assoc_type_id, to_chalk_trait_id, Interner, ProjectionTy, TraitRef, Ty,
7+
TyKind,
8+
};
49

510
pub trait TyExt {
611
fn is_unit(&self) -> bool;
@@ -11,3 +16,24 @@ impl TyExt for Ty {
1116
matches!(self.kind(&Interner), TyKind::Tuple(0, _))
1217
}
1318
}
19+
20+
pub trait ProjectionTyExt {
21+
fn trait_ref(&self, db: &dyn HirDatabase) -> TraitRef;
22+
fn trait_(&self, db: &dyn HirDatabase) -> TraitId;
23+
}
24+
25+
impl ProjectionTyExt for ProjectionTy {
26+
fn trait_ref(&self, db: &dyn HirDatabase) -> TraitRef {
27+
TraitRef {
28+
trait_id: to_chalk_trait_id(self.trait_(db)),
29+
substitution: self.substitution.clone(),
30+
}
31+
}
32+
33+
fn trait_(&self, db: &dyn HirDatabase) -> TraitId {
34+
match from_assoc_type_id(self.associated_ty_id).lookup(db.upcast()).container {
35+
AssocContainerId::TraitId(it) => it,
36+
_ => panic!("projection ty without parent trait"),
37+
}
38+
}
39+
}

crates/hir_ty/src/display.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ use crate::{
2222
lt_from_placeholder_idx, primitive, to_assoc_type_id, traits::chalk::from_chalk,
2323
utils::generics, AdtId, AliasEq, AliasTy, CallableDefId, CallableSig, DomainGoal, GenericArg,
2424
ImplTraitId, Interner, Lifetime, LifetimeData, LifetimeOutlives, Mutability, OpaqueTy,
25-
ProjectionTy, QuantifiedWhereClause, Scalar, TraitRef, Ty, TyExt, TyKind, WhereClause,
25+
ProjectionTy, ProjectionTyExt, QuantifiedWhereClause, Scalar, TraitRef, Ty, TyExt, TyKind,
26+
WhereClause,
2627
};
2728

2829
pub struct HirFormatter<'a> {

crates/hir_ty/src/infer/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use crate::{
2222
to_chalk_trait_id,
2323
traits::{chalk::from_chalk, FnTrait},
2424
utils::{generics, variant_data, Generics},
25-
AdtId, Binders, CallableDefId, FnPointer, FnSig, InEnvironment, Interner, Rawness, Scalar,
26-
Substitution, TraitRef, Ty, TyBuilder, TyKind,
25+
AdtId, Binders, CallableDefId, FnPointer, FnSig, InEnvironment, Interner, ProjectionTyExt,
26+
Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyKind,
2727
};
2828

2929
use super::{

crates/hir_ty/src/lib.rs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,20 @@ use crate::{db::HirDatabase, display::HirDisplay, utils::generics};
4343

4444
pub use autoderef::autoderef;
4545
pub use builder::TyBuilder;
46-
pub use chalk_ext::TyExt;
46+
pub use chalk_ext::{ProjectionTyExt, TyExt};
4747
pub use infer::{could_unify, InferenceResult, InferenceVar};
4848
pub use lower::{
4949
associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode,
5050
TyDefId, TyLoweringContext, ValueTyDefId,
5151
};
52-
pub use traits::TraitEnvironment;
52+
pub use traits::{chalk::Interner, TraitEnvironment};
5353
pub use types::*;
5454
pub use walk::TypeWalk;
5555

5656
pub use chalk_ir::{
5757
cast::Cast, AdtId, BoundVar, DebruijnIndex, Mutability, Safety, Scalar, TyVariableKind,
5858
};
5959

60-
pub use crate::traits::chalk::Interner;
61-
6260
pub type ForeignDefId = chalk_ir::ForeignDefId<Interner>;
6361
pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>;
6462
pub type FnDefId = chalk_ir::FnDefId<Interner>;
@@ -76,26 +74,6 @@ pub type LifetimeOutlives = chalk_ir::LifetimeOutlives<Interner>;
7674

7775
pub type ChalkTraitId = chalk_ir::TraitId<Interner>;
7876

79-
impl ProjectionTy {
80-
pub fn trait_ref(&self, db: &dyn HirDatabase) -> TraitRef {
81-
TraitRef {
82-
trait_id: to_chalk_trait_id(self.trait_(db)),
83-
substitution: self.substitution.clone(),
84-
}
85-
}
86-
87-
pub fn self_type_parameter(&self, interner: &Interner) -> &Ty {
88-
&self.substitution.interned()[0].assert_ty_ref(interner)
89-
}
90-
91-
fn trait_(&self, db: &dyn HirDatabase) -> TraitId {
92-
match from_assoc_type_id(self.associated_ty_id).lookup(db.upcast()).container {
93-
AssocContainerId::TraitId(it) => it,
94-
_ => panic!("projection ty without parent trait"),
95-
}
96-
}
97-
}
98-
9977
pub type FnSig = chalk_ir::FnSig<Interner>;
10078

10179
impl Substitution {

crates/hir_ty/src/traits/chalk/mapping.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ use base_db::salsa::InternKey;
1010
use hir_def::{GenericDefId, TypeAliasId};
1111

1212
use crate::{
13-
db::HirDatabase, primitive::UintTy, AliasTy, CallableDefId, Canonical, DomainGoal, FnPointer,
14-
GenericArg, InEnvironment, OpaqueTy, ProjectionTy, QuantifiedWhereClause, Scalar, Substitution,
15-
TraitRef, Ty, TypeWalk, WhereClause,
13+
chalk_ext::ProjectionTyExt, db::HirDatabase, primitive::UintTy, AliasTy, CallableDefId,
14+
Canonical, DomainGoal, FnPointer, GenericArg, InEnvironment, OpaqueTy, ProjectionTy,
15+
QuantifiedWhereClause, Scalar, Substitution, TraitRef, Ty, TypeWalk, WhereClause,
1616
};
1717

1818
use super::interner::*;

crates/hir_ty/src/types.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ pub struct ProjectionTy {
2929
pub substitution: Substitution,
3030
}
3131

32+
impl ProjectionTy {
33+
pub fn self_type_parameter(&self, interner: &Interner) -> &Ty {
34+
&self.substitution.interned()[0].assert_ty_ref(interner)
35+
}
36+
}
37+
3238
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
3339
pub struct DynTy {
3440
/// The unknown self type.

0 commit comments

Comments
 (0)