Skip to content

Commit 9b4699a

Browse files
committed
Move Ty accessors to TyExt
1 parent 002e72a commit 9b4699a

File tree

14 files changed

+252
-226
lines changed

14 files changed

+252
-226
lines changed

crates/hir/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ use hir_ty::{
5959
traits::FnTrait,
6060
AliasEq, AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, CanonicalVarKinds, Cast,
6161
DebruijnIndex, InEnvironment, Interner, QuantifiedWhereClause, Scalar, Solution,
62-
SolutionVariables, Substitution, TraitEnvironment, Ty, TyBuilder, TyDefId, TyKind,
62+
SolutionVariables, Substitution, TraitEnvironment, Ty, TyBuilder, TyDefId, TyExt, TyKind,
6363
TyVariableKind, WhereClause,
6464
};
6565
use itertools::Itertools;

crates/hir_ty/src/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use smallvec::SmallVec;
1313
use crate::{
1414
db::HirDatabase, primitive, to_assoc_type_id, to_chalk_trait_id, utils::generics, Binders,
1515
CallableSig, FnPointer, FnSig, FnSubst, GenericArg, Interner, ProjectionTy, Substitution,
16-
TraitRef, Ty, TyDefId, TyKind, TypeWalk, ValueTyDefId,
16+
TraitRef, Ty, TyDefId, TyExt, TyKind, TypeWalk, ValueTyDefId,
1717
};
1818

1919
/// This is a builder for `Ty` or anything that needs a `Substitution`.

crates/hir_ty/src/chalk_ext.rs

Lines changed: 232 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,249 @@
11
//! Various extensions traits for Chalk types.
22
3-
use hir_def::{AssocContainerId, Lookup, TraitId};
3+
use chalk_ir::Mutability;
4+
use hir_def::{
5+
type_ref::Rawness, AssocContainerId, FunctionId, GenericDefId, HasModule, Lookup, TraitId,
6+
};
47

58
use crate::{
6-
db::HirDatabase, from_assoc_type_id, to_chalk_trait_id, Interner, ProjectionTy, TraitRef, Ty,
7-
TyKind,
9+
db::HirDatabase, from_assoc_type_id, from_chalk_trait_id, from_foreign_def_id,
10+
from_placeholder_idx, to_chalk_trait_id, AdtId, AliasEq, AliasTy, Binders, CallableDefId,
11+
CallableSig, ImplTraitId, Interner, Lifetime, ProjectionTy, QuantifiedWhereClause,
12+
Substitution, TraitRef, Ty, TyBuilder, TyKind, WhereClause,
813
};
914

1015
pub trait TyExt {
1116
fn is_unit(&self) -> bool;
17+
fn is_never(&self) -> bool;
18+
fn is_unknown(&self) -> bool;
19+
20+
fn as_adt(&self) -> Option<(hir_def::AdtId, &Substitution)>;
21+
fn as_tuple(&self) -> Option<&Substitution>;
22+
fn as_fn_def(&self, db: &dyn HirDatabase) -> Option<FunctionId>;
23+
fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)>;
24+
fn as_reference_or_ptr(&self) -> Option<(&Ty, Rawness, Mutability)>;
25+
fn as_generic_def(&self, db: &dyn HirDatabase) -> Option<GenericDefId>;
26+
27+
fn callable_def(&self, db: &dyn HirDatabase) -> Option<CallableDefId>;
28+
fn callable_sig(&self, db: &dyn HirDatabase) -> Option<CallableSig>;
29+
30+
fn strip_references(&self) -> &Ty;
31+
32+
/// If this is a `dyn Trait` type, this returns the `Trait` part.
33+
fn dyn_trait_ref(&self) -> Option<&TraitRef>;
34+
35+
/// If this is a `dyn Trait`, returns that trait.
36+
fn dyn_trait(&self) -> Option<TraitId>;
37+
38+
fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<QuantifiedWhereClause>>;
39+
fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<TraitId>;
1240
}
1341

1442
impl TyExt for Ty {
1543
fn is_unit(&self) -> bool {
1644
matches!(self.kind(&Interner), TyKind::Tuple(0, _))
1745
}
46+
47+
fn is_never(&self) -> bool {
48+
matches!(self.kind(&Interner), TyKind::Never)
49+
}
50+
51+
fn is_unknown(&self) -> bool {
52+
matches!(self.kind(&Interner), TyKind::Error)
53+
}
54+
55+
fn as_adt(&self) -> Option<(hir_def::AdtId, &Substitution)> {
56+
match self.kind(&Interner) {
57+
TyKind::Adt(AdtId(adt), parameters) => Some((*adt, parameters)),
58+
_ => None,
59+
}
60+
}
61+
62+
fn as_tuple(&self) -> Option<&Substitution> {
63+
match self.kind(&Interner) {
64+
TyKind::Tuple(_, substs) => Some(substs),
65+
_ => None,
66+
}
67+
}
68+
69+
fn as_fn_def(&self, db: &dyn HirDatabase) -> Option<FunctionId> {
70+
if let Some(CallableDefId::FunctionId(func)) = self.callable_def(db) {
71+
Some(func)
72+
} else {
73+
None
74+
}
75+
}
76+
fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)> {
77+
match self.kind(&Interner) {
78+
TyKind::Ref(mutability, lifetime, ty) => Some((ty, *lifetime, *mutability)),
79+
_ => None,
80+
}
81+
}
82+
83+
fn as_reference_or_ptr(&self) -> Option<(&Ty, Rawness, Mutability)> {
84+
match self.kind(&Interner) {
85+
TyKind::Ref(mutability, _, ty) => Some((ty, Rawness::Ref, *mutability)),
86+
TyKind::Raw(mutability, ty) => Some((ty, Rawness::RawPtr, *mutability)),
87+
_ => None,
88+
}
89+
}
90+
91+
fn as_generic_def(&self, db: &dyn HirDatabase) -> Option<GenericDefId> {
92+
match *self.kind(&Interner) {
93+
TyKind::Adt(AdtId(adt), ..) => Some(adt.into()),
94+
TyKind::FnDef(callable, ..) => {
95+
Some(db.lookup_intern_callable_def(callable.into()).into())
96+
}
97+
TyKind::AssociatedType(type_alias, ..) => Some(from_assoc_type_id(type_alias).into()),
98+
TyKind::Foreign(type_alias, ..) => Some(from_foreign_def_id(type_alias).into()),
99+
_ => None,
100+
}
101+
}
102+
103+
fn callable_def(&self, db: &dyn HirDatabase) -> Option<CallableDefId> {
104+
match self.kind(&Interner) {
105+
&TyKind::FnDef(def, ..) => Some(db.lookup_intern_callable_def(def.into())),
106+
_ => None,
107+
}
108+
}
109+
110+
fn callable_sig(&self, db: &dyn HirDatabase) -> Option<CallableSig> {
111+
match self.kind(&Interner) {
112+
TyKind::Function(fn_ptr) => Some(CallableSig::from_fn_ptr(fn_ptr)),
113+
TyKind::FnDef(def, parameters) => {
114+
let callable_def = db.lookup_intern_callable_def((*def).into());
115+
let sig = db.callable_item_signature(callable_def);
116+
Some(sig.substitute(&Interner, &parameters))
117+
}
118+
TyKind::Closure(.., substs) => {
119+
let sig_param = substs.at(&Interner, 0).assert_ty_ref(&Interner);
120+
sig_param.callable_sig(db)
121+
}
122+
_ => None,
123+
}
124+
}
125+
126+
fn dyn_trait_ref(&self) -> Option<&TraitRef> {
127+
match self.kind(&Interner) {
128+
TyKind::Dyn(dyn_ty) => dyn_ty.bounds.skip_binders().interned().get(0).and_then(|b| {
129+
match b.skip_binders() {
130+
WhereClause::Implemented(trait_ref) => Some(trait_ref),
131+
_ => None,
132+
}
133+
}),
134+
_ => None,
135+
}
136+
}
137+
138+
fn dyn_trait(&self) -> Option<TraitId> {
139+
self.dyn_trait_ref().map(|it| it.trait_id).map(from_chalk_trait_id)
140+
}
141+
142+
fn strip_references(&self) -> &Ty {
143+
let mut t: &Ty = self;
144+
while let TyKind::Ref(_mutability, _lifetime, ty) = t.kind(&Interner) {
145+
t = ty;
146+
}
147+
t
148+
}
149+
150+
fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<QuantifiedWhereClause>> {
151+
match self.kind(&Interner) {
152+
TyKind::OpaqueType(opaque_ty_id, ..) => {
153+
match db.lookup_intern_impl_trait_id((*opaque_ty_id).into()) {
154+
ImplTraitId::AsyncBlockTypeImplTrait(def, _expr) => {
155+
let krate = def.module(db.upcast()).krate();
156+
if let Some(future_trait) = db
157+
.lang_item(krate, "future_trait".into())
158+
.and_then(|item| item.as_trait())
159+
{
160+
// This is only used by type walking.
161+
// Parameters will be walked outside, and projection predicate is not used.
162+
// So just provide the Future trait.
163+
let impl_bound = Binders::empty(
164+
&Interner,
165+
WhereClause::Implemented(TraitRef {
166+
trait_id: to_chalk_trait_id(future_trait),
167+
substitution: Substitution::empty(&Interner),
168+
}),
169+
);
170+
Some(vec![impl_bound])
171+
} else {
172+
None
173+
}
174+
}
175+
ImplTraitId::ReturnTypeImplTrait(..) => None,
176+
}
177+
}
178+
TyKind::Alias(AliasTy::Opaque(opaque_ty)) => {
179+
let predicates = match db.lookup_intern_impl_trait_id(opaque_ty.opaque_ty_id.into())
180+
{
181+
ImplTraitId::ReturnTypeImplTrait(func, idx) => {
182+
db.return_type_impl_traits(func).map(|it| {
183+
let data = (*it)
184+
.as_ref()
185+
.map(|rpit| rpit.impl_traits[idx as usize].bounds.clone());
186+
data.substitute(&Interner, &opaque_ty.substitution)
187+
})
188+
}
189+
// It always has an parameter for Future::Output type.
190+
ImplTraitId::AsyncBlockTypeImplTrait(..) => unreachable!(),
191+
};
192+
193+
predicates.map(|it| it.into_value_and_skipped_binders().0)
194+
}
195+
TyKind::Placeholder(idx) => {
196+
let id = from_placeholder_idx(db, *idx);
197+
let generic_params = db.generic_params(id.parent);
198+
let param_data = &generic_params.types[id.local_id];
199+
match param_data.provenance {
200+
hir_def::generics::TypeParamProvenance::ArgumentImplTrait => {
201+
let substs = TyBuilder::type_params_subst(db, id.parent);
202+
let predicates = db
203+
.generic_predicates(id.parent)
204+
.into_iter()
205+
.map(|pred| pred.clone().substitute(&Interner, &substs))
206+
.filter(|wc| match &wc.skip_binders() {
207+
WhereClause::Implemented(tr) => {
208+
tr.self_type_parameter(&Interner) == self
209+
}
210+
WhereClause::AliasEq(AliasEq {
211+
alias: AliasTy::Projection(proj),
212+
ty: _,
213+
}) => proj.self_type_parameter(&Interner) == self,
214+
_ => false,
215+
})
216+
.collect::<Vec<_>>();
217+
218+
Some(predicates)
219+
}
220+
_ => None,
221+
}
222+
}
223+
_ => None,
224+
}
225+
}
226+
227+
fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<TraitId> {
228+
match self.kind(&Interner) {
229+
TyKind::AssociatedType(id, ..) => {
230+
match from_assoc_type_id(*id).lookup(db.upcast()).container {
231+
AssocContainerId::TraitId(trait_id) => Some(trait_id),
232+
_ => None,
233+
}
234+
}
235+
TyKind::Alias(AliasTy::Projection(projection_ty)) => {
236+
match from_assoc_type_id(projection_ty.associated_ty_id)
237+
.lookup(db.upcast())
238+
.container
239+
{
240+
AssocContainerId::TraitId(trait_id) => Some(trait_id),
241+
_ => None,
242+
}
243+
}
244+
_ => None,
245+
}
246+
}
18247
}
19248

20249
pub trait ProjectionTyExt {

crates/hir_ty/src/diagnostics/match_check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ use hir_def::{
227227
use la_arena::Idx;
228228
use smallvec::{smallvec, SmallVec};
229229

230-
use crate::{db::HirDatabase, AdtId, InferenceResult, Interner, TyKind};
230+
use crate::{db::HirDatabase, AdtId, InferenceResult, Interner, TyExt, TyKind};
231231

232232
#[derive(Debug, Clone, Copy)]
233233
/// Either a pattern from the source code being analyzed, represented as

crates/hir_ty/src/diagnostics/unsafe_check.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use hir_def::{
1111
};
1212
use hir_expand::diagnostics::DiagnosticSink;
1313

14-
use crate::{db::HirDatabase, diagnostics::MissingUnsafe, InferenceResult, Interner, TyKind};
14+
use crate::{
15+
db::HirDatabase, diagnostics::MissingUnsafe, InferenceResult, Interner, TyExt, TyKind,
16+
};
1517

1618
pub(super) struct UnsafeValidator<'a, 'b: 'a> {
1719
owner: DefWithBodyId,

crates/hir_ty/src/infer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use super::{
4242
};
4343
use crate::{
4444
db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode,
45-
to_assoc_type_id, AliasEq, AliasTy, Interner, TyBuilder, TyKind,
45+
to_assoc_type_id, AliasEq, AliasTy, Interner, TyBuilder, TyExt, TyKind,
4646
};
4747

4848
// This lint has a false positive here. See the link below for details.

crates/hir_ty/src/infer/coerce.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use chalk_ir::{cast::Cast, Mutability, TyVariableKind};
88
use hir_def::lang_item::LangItemTarget;
99

10-
use crate::{autoderef, Interner, Solution, Ty, TyBuilder, TyKind};
10+
use crate::{autoderef, Interner, Solution, Ty, TyBuilder, TyExt, TyKind};
1111

1212
use super::{InEnvironment, InferenceContext};
1313

crates/hir_ty/src/infer/expr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ use crate::{
2323
traits::{chalk::from_chalk, FnTrait},
2424
utils::{generics, variant_data, Generics},
2525
AdtId, Binders, CallableDefId, FnPointer, FnSig, FnSubst, InEnvironment, Interner,
26-
ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyKind, TypeWalk,
26+
ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyExt, TyKind,
27+
TypeWalk,
2728
};
2829

2930
use super::{

crates/hir_ty/src/infer/pat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use hir_expand::name::Name;
1414
use super::{BindingMode, Expectation, InferenceContext};
1515
use crate::{
1616
lower::lower_to_chalk_mutability, static_lifetime, utils::variant_data, Interner, Substitution,
17-
Ty, TyBuilder, TyKind,
17+
Ty, TyBuilder, TyExt, TyKind,
1818
};
1919

2020
impl<'a> InferenceContext<'a> {

crates/hir_ty/src/infer/path.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use hir_def::{
1010
};
1111
use hir_expand::name::Name;
1212

13-
use crate::{method_resolution, Interner, Substitution, Ty, TyBuilder, TyKind, ValueTyDefId};
13+
use crate::{
14+
method_resolution, Interner, Substitution, Ty, TyBuilder, TyExt, TyKind, ValueTyDefId,
15+
};
1416

1517
use super::{ExprOrPatId, InferenceContext, TraitRef};
1618

0 commit comments

Comments
 (0)