Skip to content

Commit 2fc137b

Browse files
bors[bot]Veykril
andauthored
Merge #7813
7813: Inline TypeCtor into Ty r=flodiebold a=Veykril This removes the `ApplicationTy` variant from `Ty` bringing the representation a lot closer to chalk's `TyKind`. Co-authored-by: Lukas Wirth <[email protected]>
2 parents 0a913fd + faf2dd4 commit 2fc137b

17 files changed

+709
-943
lines changed

crates/hir/src/code_model.rs

+56-95
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ use hir_ty::{
3131
display::{write_bounds_like_dyn_trait_with_prefix, HirDisplayError, HirFormatter},
3232
method_resolution,
3333
traits::{FnTrait, Solution, SolutionVariables},
34-
ApplicationTy, BoundVar, CallableDefId, Canonical, DebruijnIndex, FnSig, GenericPredicate,
35-
InEnvironment, Obligation, ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment,
36-
Ty, TyDefId, TyKind, TypeCtor,
34+
BoundVar, CallableDefId, Canonical, DebruijnIndex, FnSig, GenericPredicate, InEnvironment,
35+
Obligation, ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment, Ty, TyDefId,
36+
TyKind,
3737
};
3838
use rustc_hash::FxHashSet;
3939
use stdx::{format_to, impl_from};
@@ -1547,28 +1547,19 @@ impl Type {
15471547
}
15481548

15491549
pub fn is_unit(&self) -> bool {
1550-
matches!(
1551-
self.ty.value,
1552-
Ty::Apply(ApplicationTy { ctor: TypeCtor::Tuple { cardinality: 0 }, .. })
1553-
)
1550+
matches!(self.ty.value, Ty::Tuple { cardinality: 0, .. })
15541551
}
15551552
pub fn is_bool(&self) -> bool {
1556-
matches!(
1557-
self.ty.value,
1558-
Ty::Apply(ApplicationTy { ctor: TypeCtor::Scalar(Scalar::Bool), .. })
1559-
)
1553+
matches!(self.ty.value, Ty::Scalar(Scalar::Bool))
15601554
}
15611555

15621556
pub fn is_mutable_reference(&self) -> bool {
1563-
matches!(
1564-
self.ty.value,
1565-
Ty::Apply(ApplicationTy { ctor: TypeCtor::Ref(Mutability::Mut), .. })
1566-
)
1557+
matches!(self.ty.value, Ty::Ref(Mutability::Mut, ..))
15671558
}
15681559

15691560
pub fn remove_ref(&self) -> Option<Type> {
1570-
if let Ty::Apply(ApplicationTy { ctor: TypeCtor::Ref(_), .. }) = self.ty.value {
1571-
self.ty.value.substs().map(|substs| self.derived(substs[0].clone()))
1561+
if let Ty::Ref(.., substs) = &self.ty.value {
1562+
Some(self.derived(substs[0].clone()))
15721563
} else {
15731564
None
15741565
}
@@ -1688,7 +1679,7 @@ impl Type {
16881679

16891680
pub fn as_callable(&self, db: &dyn HirDatabase) -> Option<Callable> {
16901681
let def = match self.ty.value {
1691-
Ty::Apply(ApplicationTy { ctor: TypeCtor::FnDef(def), parameters: _ }) => Some(def),
1682+
Ty::FnDef(def, _) => Some(def),
16921683
_ => None,
16931684
};
16941685

@@ -1697,20 +1688,16 @@ impl Type {
16971688
}
16981689

16991690
pub fn is_closure(&self) -> bool {
1700-
matches!(&self.ty.value, Ty::Apply(ApplicationTy { ctor: TypeCtor::Closure { .. }, .. }))
1691+
matches!(&self.ty.value, Ty::Closure { .. })
17011692
}
17021693

17031694
pub fn is_fn(&self) -> bool {
1704-
matches!(
1705-
&self.ty.value,
1706-
Ty::Apply(ApplicationTy { ctor: TypeCtor::FnDef(..), .. })
1707-
| Ty::Apply(ApplicationTy { ctor: TypeCtor::FnPtr { .. }, .. })
1708-
)
1695+
matches!(&self.ty.value, Ty::FnDef(..) | Ty::FnPtr { .. })
17091696
}
17101697

17111698
pub fn is_packed(&self, db: &dyn HirDatabase) -> bool {
17121699
let adt_id = match self.ty.value {
1713-
Ty::Apply(ApplicationTy { ctor: TypeCtor::Adt(adt_id), .. }) => adt_id,
1700+
Ty::Adt(adt_id, ..) => adt_id,
17141701
_ => return false,
17151702
};
17161703

@@ -1722,7 +1709,7 @@ impl Type {
17221709
}
17231710

17241711
pub fn is_raw_ptr(&self) -> bool {
1725-
matches!(&self.ty.value, Ty::Apply(ApplicationTy { ctor: TypeCtor::RawPtr(..), .. }))
1712+
matches!(&self.ty.value, Ty::RawPtr(..))
17261713
}
17271714

17281715
pub fn contains_unknown(&self) -> bool {
@@ -1731,44 +1718,34 @@ impl Type {
17311718
fn go(ty: &Ty) -> bool {
17321719
match ty {
17331720
Ty::Unknown => true,
1734-
Ty::Apply(a_ty) => a_ty.parameters.iter().any(go),
1735-
_ => false,
1721+
_ => ty.substs().map_or(false, |substs| substs.iter().any(go)),
17361722
}
17371723
}
17381724
}
17391725

17401726
pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> {
1741-
if let Ty::Apply(a_ty) = &self.ty.value {
1742-
let variant_id = match a_ty.ctor {
1743-
TypeCtor::Adt(AdtId::StructId(s)) => s.into(),
1744-
TypeCtor::Adt(AdtId::UnionId(u)) => u.into(),
1745-
_ => return Vec::new(),
1746-
};
1747-
1748-
return db
1749-
.field_types(variant_id)
1750-
.iter()
1751-
.map(|(local_id, ty)| {
1752-
let def = Field { parent: variant_id.into(), id: local_id };
1753-
let ty = ty.clone().subst(&a_ty.parameters);
1754-
(def, self.derived(ty))
1755-
})
1756-
.collect();
1727+
let (variant_id, substs) = match self.ty.value {
1728+
Ty::Adt(AdtId::StructId(s), ref substs) => (s.into(), substs),
1729+
Ty::Adt(AdtId::UnionId(u), ref substs) => (u.into(), substs),
1730+
_ => return Vec::new(),
17571731
};
1758-
Vec::new()
1732+
1733+
db.field_types(variant_id)
1734+
.iter()
1735+
.map(|(local_id, ty)| {
1736+
let def = Field { parent: variant_id.into(), id: local_id };
1737+
let ty = ty.clone().subst(substs);
1738+
(def, self.derived(ty))
1739+
})
1740+
.collect()
17591741
}
17601742

17611743
pub fn tuple_fields(&self, _db: &dyn HirDatabase) -> Vec<Type> {
1762-
let mut res = Vec::new();
1763-
if let Ty::Apply(a_ty) = &self.ty.value {
1764-
if let TypeCtor::Tuple { .. } = a_ty.ctor {
1765-
for ty in a_ty.parameters.iter() {
1766-
let ty = ty.clone();
1767-
res.push(self.derived(ty));
1768-
}
1769-
}
1770-
};
1771-
res
1744+
if let Ty::Tuple { substs, .. } = &self.ty.value {
1745+
substs.iter().map(|ty| self.derived(ty.clone())).collect()
1746+
} else {
1747+
Vec::new()
1748+
}
17721749
}
17731750

17741751
pub fn autoderef<'a>(&'a self, db: &'a dyn HirDatabase) -> impl Iterator<Item = Type> + 'a {
@@ -1805,15 +1782,13 @@ impl Type {
18051782
}
18061783

18071784
pub fn type_parameters(&self) -> impl Iterator<Item = Type> + '_ {
1808-
let ty = self.ty.value.strip_references();
1809-
let substs = match ty {
1810-
Ty::Apply(apply_ty) => &apply_ty.parameters,
1811-
Ty::Opaque(opaque_ty) => &opaque_ty.parameters,
1812-
_ => return Either::Left(iter::empty()),
1813-
};
1814-
1815-
let iter = substs.iter().map(move |ty| self.derived(ty.clone()));
1816-
Either::Right(iter)
1785+
self.ty
1786+
.value
1787+
.strip_references()
1788+
.substs()
1789+
.into_iter()
1790+
.flat_map(|substs| substs.iter())
1791+
.map(move |ty| self.derived(ty.clone()))
18171792
}
18181793

18191794
pub fn iterate_method_candidates<T>(
@@ -1903,17 +1878,8 @@ impl Type {
19031878

19041879
// FIXME: provide required accessors such that it becomes implementable from outside.
19051880
pub fn is_equal_for_find_impls(&self, other: &Type) -> bool {
1906-
match (&self.ty.value, &other.ty.value) {
1907-
(Ty::Apply(a_original_ty), Ty::Apply(ApplicationTy { ctor, parameters })) => match ctor
1908-
{
1909-
TypeCtor::Ref(..) => match parameters.as_single() {
1910-
Ty::Apply(a_ty) => a_original_ty.ctor == a_ty.ctor,
1911-
_ => false,
1912-
},
1913-
_ => a_original_ty.ctor == *ctor,
1914-
},
1915-
_ => false,
1916-
}
1881+
let rref = other.remove_ref();
1882+
self.ty.value.equals_ctor(rref.as_ref().map_or(&other.ty.value, |it| &it.ty.value))
19171883
}
19181884

19191885
fn derived(&self, ty: Ty) -> Type {
@@ -1958,26 +1924,18 @@ impl Type {
19581924
fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) {
19591925
let ty = type_.ty.value.strip_references();
19601926
match ty {
1961-
Ty::Apply(ApplicationTy { ctor, parameters }) => {
1962-
match ctor {
1963-
TypeCtor::Adt(_) => {
1964-
cb(type_.derived(ty.clone()));
1965-
}
1966-
TypeCtor::AssociatedType(_) => {
1967-
if let Some(_) = ty.associated_type_parent_trait(db) {
1968-
cb(type_.derived(ty.clone()));
1969-
}
1970-
}
1971-
TypeCtor::OpaqueType(..) => {
1972-
if let Some(bounds) = ty.impl_trait_bounds(db) {
1973-
walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb);
1974-
}
1975-
}
1976-
_ => (),
1927+
Ty::Adt(..) => {
1928+
cb(type_.derived(ty.clone()));
1929+
}
1930+
Ty::AssociatedType(..) => {
1931+
if let Some(_) = ty.associated_type_parent_trait(db) {
1932+
cb(type_.derived(ty.clone()));
1933+
}
1934+
}
1935+
Ty::OpaqueType(..) => {
1936+
if let Some(bounds) = ty.impl_trait_bounds(db) {
1937+
walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb);
19771938
}
1978-
1979-
// adt params, tuples, etc...
1980-
walk_substs(db, type_, parameters, cb);
19811939
}
19821940
Ty::Opaque(opaque_ty) => {
19831941
if let Some(bounds) = ty.impl_trait_bounds(db) {
@@ -1995,7 +1953,10 @@ impl Type {
19951953
walk_bounds(db, &type_.derived(ty.clone()), bounds.as_ref(), cb);
19961954
}
19971955

1998-
_ => (),
1956+
_ => {}
1957+
}
1958+
if let Some(substs) = ty.substs() {
1959+
walk_substs(db, type_, &substs, cb);
19991960
}
20001961
}
20011962

crates/hir/src/source_analyzer.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use hir_def::{
2020
use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile};
2121
use hir_ty::{
2222
diagnostics::{record_literal_missing_fields, record_pattern_missing_fields},
23-
InferenceResult, Substs, Ty,
23+
InferenceResult, Substs,
2424
};
2525
use syntax::{
2626
ast::{self, AstNode},
@@ -299,14 +299,11 @@ impl SourceAnalyzer {
299299
let infer = self.infer.as_ref()?;
300300

301301
let expr_id = self.expr_id(db, &literal.clone().into())?;
302-
let substs = match &infer.type_of_expr[expr_id] {
303-
Ty::Apply(a_ty) => &a_ty.parameters,
304-
_ => return None,
305-
};
302+
let substs = infer.type_of_expr[expr_id].substs()?;
306303

307304
let (variant, missing_fields, _exhaustive) =
308305
record_literal_missing_fields(db, infer, expr_id, &body[expr_id])?;
309-
let res = self.missing_fields(db, krate, substs, variant, missing_fields);
306+
let res = self.missing_fields(db, krate, &substs, variant, missing_fields);
310307
Some(res)
311308
}
312309

@@ -320,14 +317,11 @@ impl SourceAnalyzer {
320317
let infer = self.infer.as_ref()?;
321318

322319
let pat_id = self.pat_id(&pattern.clone().into())?;
323-
let substs = match &infer.type_of_pat[pat_id] {
324-
Ty::Apply(a_ty) => &a_ty.parameters,
325-
_ => return None,
326-
};
320+
let substs = infer.type_of_pat[pat_id].substs()?;
327321

328322
let (variant, missing_fields, _exhaustive) =
329323
record_pattern_missing_fields(db, infer, pat_id, &body[pat_id])?;
330-
let res = self.missing_fields(db, krate, substs, variant, missing_fields);
324+
let res = self.missing_fields(db, krate, &substs, variant, missing_fields);
331325
Some(res)
332326
}
333327

crates/hir_ty/src/diagnostics/expr.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717
MissingPatFields, RemoveThisSemicolon,
1818
},
1919
utils::variant_data,
20-
ApplicationTy, InferenceResult, Ty, TypeCtor,
20+
InferenceResult, Ty,
2121
};
2222

2323
pub(crate) use hir_def::{
@@ -381,14 +381,11 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
381381
_ => return,
382382
};
383383

384-
let core_result_ctor = TypeCtor::Adt(AdtId::EnumId(core_result_enum));
385-
let core_option_ctor = TypeCtor::Adt(AdtId::EnumId(core_option_enum));
386-
387-
let (params, required) = match &mismatch.expected {
388-
Ty::Apply(ApplicationTy { ctor, parameters }) if ctor == &core_result_ctor => {
384+
let (params, required) = match mismatch.expected {
385+
Ty::Adt(AdtId::EnumId(enum_id), ref parameters) if enum_id == core_result_enum => {
389386
(parameters, "Ok".to_string())
390387
}
391-
Ty::Apply(ApplicationTy { ctor, parameters }) if ctor == &core_option_ctor => {
388+
Ty::Adt(AdtId::EnumId(enum_id), ref parameters) if enum_id == core_option_enum => {
392389
(parameters, "Some".to_string())
393390
}
394391
_ => return,

crates/hir_ty/src/diagnostics/match_check.rs

+3-5
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, ApplicationTy, InferenceResult, Ty, TypeCtor};
230+
use crate::{db::HirDatabase, InferenceResult, Ty};
231231

232232
#[derive(Debug, Clone, Copy)]
233233
/// Either a pattern from the source code being analyzed, represented as
@@ -627,14 +627,12 @@ pub(super) fn is_useful(
627627
// - `!` type
628628
// In those cases, no match arm is useful.
629629
match cx.infer[cx.match_expr].strip_references() {
630-
Ty::Apply(ApplicationTy { ctor: TypeCtor::Adt(AdtId::EnumId(enum_id)), .. }) => {
630+
Ty::Adt(AdtId::EnumId(enum_id), ..) => {
631631
if cx.db.enum_data(*enum_id).variants.is_empty() {
632632
return Ok(Usefulness::NotUseful);
633633
}
634634
}
635-
Ty::Apply(ApplicationTy { ctor: TypeCtor::Never, .. }) => {
636-
return Ok(Usefulness::NotUseful);
637-
}
635+
Ty::Never => return Ok(Usefulness::NotUseful),
638636
_ => (),
639637
}
640638

crates/hir_ty/src/diagnostics/unsafe_check.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ use hir_def::{
1111
};
1212
use hir_expand::diagnostics::DiagnosticSink;
1313

14-
use crate::{
15-
db::HirDatabase, diagnostics::MissingUnsafe, ApplicationTy, InferenceResult, Ty, TypeCtor,
16-
};
14+
use crate::{db::HirDatabase, diagnostics::MissingUnsafe, InferenceResult, Ty};
1715

1816
pub(super) struct UnsafeValidator<'a, 'b: 'a> {
1917
owner: DefWithBodyId,
@@ -112,7 +110,7 @@ fn walk_unsafe(
112110
}
113111
}
114112
Expr::UnaryOp { expr, op: UnaryOp::Deref } => {
115-
if let Ty::Apply(ApplicationTy { ctor: TypeCtor::RawPtr(..), .. }) = &infer[*expr] {
113+
if let Ty::RawPtr(..) = &infer[*expr] {
116114
unsafe_exprs.push(UnsafeExpr { expr: current, inside_unsafe_block });
117115
}
118116
}

0 commit comments

Comments
 (0)