Skip to content

Commit f8f5a5e

Browse files
committed
refactor: use cast() instead of interning GenericArgData
1 parent 5c28ad1 commit f8f5a5e

File tree

5 files changed

+34
-61
lines changed

5 files changed

+34
-61
lines changed

crates/hir-ty/src/builder.rs

+15-24
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use chalk_ir::{
66
cast::{Cast, CastTo, Caster},
77
fold::TypeFoldable,
88
interner::HasInterner,
9-
AdtId, BoundVar, DebruijnIndex, Scalar,
9+
AdtId, DebruijnIndex, Scalar,
1010
};
1111
use hir_def::{
1212
builtin_type::BuiltinType, generics::TypeOrConstParamData, ConstParamId, DefWithBodyId,
@@ -16,9 +16,9 @@ use smallvec::SmallVec;
1616

1717
use crate::{
1818
consteval::unknown_const_as_generic, db::HirDatabase, infer::unify::InferenceTable, primitive,
19-
to_assoc_type_id, to_chalk_trait_id, utils::generics, Binders, CallableSig, ConstData,
20-
ConstValue, GenericArg, GenericArgData, Interner, ProjectionTy, Substitution, TraitRef, Ty,
21-
TyDefId, TyExt, TyKind, ValueTyDefId,
19+
to_assoc_type_id, to_chalk_trait_id, utils::generics, Binders, BoundVar, CallableSig,
20+
GenericArg, Interner, ProjectionTy, Substitution, TraitRef, Ty, TyDefId, TyExt, TyKind,
21+
ValueTyDefId,
2222
};
2323

2424
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -79,20 +79,12 @@ impl<D> TyBuilder<D> {
7979
pub fn fill_with_bound_vars(self, debruijn: DebruijnIndex, starting_from: usize) -> Self {
8080
// self.fill is inlined to make borrow checker happy
8181
let mut this = self;
82-
let other = this.param_kinds.iter().skip(this.vec.len());
82+
let other = &this.param_kinds[this.vec.len()..];
8383
let filler = (starting_from..).zip(other).map(|(idx, kind)| match kind {
84-
ParamKind::Type => {
85-
GenericArgData::Ty(TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(Interner))
86-
.intern(Interner)
84+
ParamKind::Type => BoundVar::new(debruijn, idx).to_ty(Interner).cast(Interner),
85+
ParamKind::Const(ty) => {
86+
BoundVar::new(debruijn, idx).to_const(Interner, ty.clone()).cast(Interner)
8787
}
88-
ParamKind::Const(ty) => GenericArgData::Const(
89-
ConstData {
90-
value: ConstValue::BoundVar(BoundVar::new(debruijn, idx)),
91-
ty: ty.clone(),
92-
}
93-
.intern(Interner),
94-
)
95-
.intern(Interner),
9688
});
9789
this.vec.extend(filler.take(this.remaining()).casted(Interner));
9890
assert_eq!(this.remaining(), 0);
@@ -102,8 +94,8 @@ impl<D> TyBuilder<D> {
10294
pub fn fill_with_unknown(self) -> Self {
10395
// self.fill is inlined to make borrow checker happy
10496
let mut this = self;
105-
let filler = this.param_kinds.iter().skip(this.vec.len()).map(|x| match x {
106-
ParamKind::Type => GenericArgData::Ty(TyKind::Error.intern(Interner)).intern(Interner),
97+
let filler = this.param_kinds[this.vec.len()..].iter().map(|x| match x {
98+
ParamKind::Type => TyKind::Error.intern(Interner).cast(Interner),
10799
ParamKind::Const(ty) => unknown_const_as_generic(ty.clone()),
108100
});
109101
this.vec.extend(filler.casted(Interner));
@@ -113,15 +105,13 @@ impl<D> TyBuilder<D> {
113105

114106
pub(crate) fn fill_with_inference_vars(self, table: &mut InferenceTable<'_>) -> Self {
115107
self.fill(|x| match x {
116-
ParamKind::Type => GenericArgData::Ty(table.new_type_var()).intern(Interner),
117-
ParamKind::Const(ty) => {
118-
GenericArgData::Const(table.new_const_var(ty.clone())).intern(Interner)
119-
}
108+
ParamKind::Type => table.new_type_var().cast(Interner),
109+
ParamKind::Const(ty) => table.new_const_var(ty.clone()).cast(Interner),
120110
})
121111
}
122112

123113
pub fn fill(mut self, filler: impl FnMut(&ParamKind) -> GenericArg) -> Self {
124-
self.vec.extend(self.param_kinds.iter().skip(self.vec.len()).map(filler));
114+
self.vec.extend(self.param_kinds[self.vec.len()..].iter().map(filler));
125115
assert_eq!(self.remaining(), 0);
126116
self
127117
}
@@ -255,7 +245,8 @@ impl TyBuilder<hir_def::AdtId> {
255245
) -> Self {
256246
let defaults = db.generic_defaults(self.data.into());
257247
for default_ty in defaults.iter().skip(self.vec.len()) {
258-
if let GenericArgData::Ty(x) = default_ty.skip_binders().data(Interner) {
248+
// NOTE(skip_binders): we only check if the arg type is error type.
249+
if let Some(x) = default_ty.skip_binders().ty(Interner) {
259250
if x.is_unknown() {
260251
self.vec.push(fallback().cast(Interner));
261252
continue;

crates/hir-ty/src/chalk_ext.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl TyExt for Ty {
152152
TyKind::FnDef(def, parameters) => {
153153
let callable_def = db.lookup_intern_callable_def((*def).into());
154154
let sig = db.callable_item_signature(callable_def);
155-
Some(sig.substitute(Interner, &parameters))
155+
Some(sig.substitute(Interner, parameters))
156156
}
157157
TyKind::Closure(.., substs) => {
158158
let sig_param = substs.at(Interner, 0).assert_ty_ref(Interner);

crates/hir-ty/src/infer/path.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ use crate::{
1212
builder::ParamKind,
1313
consteval,
1414
method_resolution::{self, VisibleFromModule},
15-
GenericArgData, Interner, Substitution, TraitRefExt, Ty, TyBuilder, TyExt, TyKind,
16-
ValueTyDefId,
15+
Interner, Substitution, TraitRefExt, Ty, TyBuilder, TyExt, TyKind, ValueTyDefId,
1716
};
1817

1918
use super::{ExprOrPatId, InferenceContext, TraitRef};
@@ -104,9 +103,7 @@ impl<'a> InferenceContext<'a> {
104103
.use_parent_substs(&parent_substs)
105104
.fill(|x| {
106105
it.next().unwrap_or_else(|| match x {
107-
ParamKind::Type => {
108-
GenericArgData::Ty(TyKind::Error.intern(Interner)).intern(Interner)
109-
}
106+
ParamKind::Type => TyKind::Error.intern(Interner).cast(Interner),
110107
ParamKind::Const(ty) => consteval::unknown_const_as_generic(ty.clone()),
111108
})
112109
})

crates/hir-ty/src/lower.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ impl<'a> TyLoweringContext<'a> {
678678
let total_len =
679679
parent_params + self_params + type_params + const_params + impl_trait_params;
680680

681-
let ty_error = GenericArgData::Ty(TyKind::Error.intern(Interner)).intern(Interner);
681+
let ty_error = TyKind::Error.intern(Interner).cast(Interner);
682682

683683
let mut def_generic_iter = def_generics.iter_id();
684684

@@ -696,7 +696,7 @@ impl<'a> TyLoweringContext<'a> {
696696
let fill_self_params = || {
697697
for x in explicit_self_ty
698698
.into_iter()
699-
.map(|x| GenericArgData::Ty(x).intern(Interner))
699+
.map(|x| x.cast(Interner))
700700
.chain(iter::repeat(ty_error.clone()))
701701
.take(self_params)
702702
{

crates/hir-ty/src/utils.rs

+14-29
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use std::iter;
55

66
use base_db::CrateId;
7-
use chalk_ir::{fold::Shift, BoundVar, DebruijnIndex};
7+
use chalk_ir::{cast::Cast, fold::Shift, BoundVar, DebruijnIndex};
88
use hir_def::{
99
db::DefDatabase,
1010
generics::{
@@ -24,8 +24,7 @@ use smallvec::{smallvec, SmallVec};
2424
use syntax::SmolStr;
2525

2626
use crate::{
27-
db::HirDatabase, ChalkTraitId, ConstData, ConstValue, GenericArgData, Interner, Substitution,
28-
TraitRef, TraitRefExt, TyKind, WhereClause,
27+
db::HirDatabase, ChalkTraitId, Interner, Substitution, TraitRef, TraitRefExt, WhereClause,
2928
};
3029

3130
pub(crate) fn fn_traits(db: &dyn DefDatabase, krate: CrateId) -> impl Iterator<Item = TraitId> {
@@ -282,8 +281,8 @@ impl Generics {
282281
}
283282
}
284283

285-
fn parent_generics(&self) -> Option<&Generics> {
286-
self.parent_generics.as_ref().map(|it| &**it)
284+
pub(crate) fn parent_generics(&self) -> Option<&Generics> {
285+
self.parent_generics.as_deref()
287286
}
288287

289288
/// Returns a Substitution that replaces each parameter by a bound variable.
@@ -295,18 +294,10 @@ impl Generics {
295294
Substitution::from_iter(
296295
Interner,
297296
self.iter_id().enumerate().map(|(idx, id)| match id {
298-
Either::Left(_) => GenericArgData::Ty(
299-
TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(Interner),
300-
)
301-
.intern(Interner),
302-
Either::Right(id) => GenericArgData::Const(
303-
ConstData {
304-
value: ConstValue::BoundVar(BoundVar::new(debruijn, idx)),
305-
ty: db.const_param_ty(id),
306-
}
307-
.intern(Interner),
308-
)
309-
.intern(Interner),
297+
Either::Left(_) => BoundVar::new(debruijn, idx).to_ty(Interner).cast(Interner),
298+
Either::Right(id) => BoundVar::new(debruijn, idx)
299+
.to_const(Interner, db.const_param_ty(id))
300+
.cast(Interner),
310301
}),
311302
)
312303
}
@@ -316,18 +307,12 @@ impl Generics {
316307
Substitution::from_iter(
317308
Interner,
318309
self.iter_id().map(|id| match id {
319-
Either::Left(id) => GenericArgData::Ty(
320-
TyKind::Placeholder(crate::to_placeholder_idx(db, id.into())).intern(Interner),
321-
)
322-
.intern(Interner),
323-
Either::Right(id) => GenericArgData::Const(
324-
ConstData {
325-
value: ConstValue::Placeholder(crate::to_placeholder_idx(db, id.into())),
326-
ty: db.const_param_ty(id),
327-
}
328-
.intern(Interner),
329-
)
330-
.intern(Interner),
310+
Either::Left(id) => {
311+
crate::to_placeholder_idx(db, id.into()).to_ty(Interner).cast(Interner)
312+
}
313+
Either::Right(id) => crate::to_placeholder_idx(db, id.into())
314+
.to_const(Interner, db.const_param_ty(id))
315+
.cast(Interner),
331316
}),
332317
)
333318
}

0 commit comments

Comments
 (0)