refactor: use cast() instead of interning GenericArgData

This commit is contained in:
Ryo Yoshida 2022-10-02 18:39:42 +09:00
parent 5c28ad1932
commit f8f5a5ea57
No known key found for this signature in database
GPG key ID: E25698A930586171
5 changed files with 34 additions and 61 deletions

View file

@ -6,7 +6,7 @@ use chalk_ir::{
cast::{Cast, CastTo, Caster}, cast::{Cast, CastTo, Caster},
fold::TypeFoldable, fold::TypeFoldable,
interner::HasInterner, interner::HasInterner,
AdtId, BoundVar, DebruijnIndex, Scalar, AdtId, DebruijnIndex, Scalar,
}; };
use hir_def::{ use hir_def::{
builtin_type::BuiltinType, generics::TypeOrConstParamData, ConstParamId, DefWithBodyId, builtin_type::BuiltinType, generics::TypeOrConstParamData, ConstParamId, DefWithBodyId,
@ -16,9 +16,9 @@ use smallvec::SmallVec;
use crate::{ use crate::{
consteval::unknown_const_as_generic, db::HirDatabase, infer::unify::InferenceTable, primitive, consteval::unknown_const_as_generic, db::HirDatabase, infer::unify::InferenceTable, primitive,
to_assoc_type_id, to_chalk_trait_id, utils::generics, Binders, CallableSig, ConstData, to_assoc_type_id, to_chalk_trait_id, utils::generics, Binders, BoundVar, CallableSig,
ConstValue, GenericArg, GenericArgData, Interner, ProjectionTy, Substitution, TraitRef, Ty, GenericArg, Interner, ProjectionTy, Substitution, TraitRef, Ty, TyDefId, TyExt, TyKind,
TyDefId, TyExt, TyKind, ValueTyDefId, ValueTyDefId,
}; };
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@ -79,20 +79,12 @@ impl<D> TyBuilder<D> {
pub fn fill_with_bound_vars(self, debruijn: DebruijnIndex, starting_from: usize) -> Self { pub fn fill_with_bound_vars(self, debruijn: DebruijnIndex, starting_from: usize) -> Self {
// self.fill is inlined to make borrow checker happy // self.fill is inlined to make borrow checker happy
let mut this = self; let mut this = self;
let other = this.param_kinds.iter().skip(this.vec.len()); let other = &this.param_kinds[this.vec.len()..];
let filler = (starting_from..).zip(other).map(|(idx, kind)| match kind { let filler = (starting_from..).zip(other).map(|(idx, kind)| match kind {
ParamKind::Type => { ParamKind::Type => BoundVar::new(debruijn, idx).to_ty(Interner).cast(Interner),
GenericArgData::Ty(TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(Interner)) ParamKind::Const(ty) => {
.intern(Interner) BoundVar::new(debruijn, idx).to_const(Interner, ty.clone()).cast(Interner)
} }
ParamKind::Const(ty) => GenericArgData::Const(
ConstData {
value: ConstValue::BoundVar(BoundVar::new(debruijn, idx)),
ty: ty.clone(),
}
.intern(Interner),
)
.intern(Interner),
}); });
this.vec.extend(filler.take(this.remaining()).casted(Interner)); this.vec.extend(filler.take(this.remaining()).casted(Interner));
assert_eq!(this.remaining(), 0); assert_eq!(this.remaining(), 0);
@ -102,8 +94,8 @@ impl<D> TyBuilder<D> {
pub fn fill_with_unknown(self) -> Self { pub fn fill_with_unknown(self) -> Self {
// self.fill is inlined to make borrow checker happy // self.fill is inlined to make borrow checker happy
let mut this = self; let mut this = self;
let filler = this.param_kinds.iter().skip(this.vec.len()).map(|x| match x { let filler = this.param_kinds[this.vec.len()..].iter().map(|x| match x {
ParamKind::Type => GenericArgData::Ty(TyKind::Error.intern(Interner)).intern(Interner), ParamKind::Type => TyKind::Error.intern(Interner).cast(Interner),
ParamKind::Const(ty) => unknown_const_as_generic(ty.clone()), ParamKind::Const(ty) => unknown_const_as_generic(ty.clone()),
}); });
this.vec.extend(filler.casted(Interner)); this.vec.extend(filler.casted(Interner));
@ -113,15 +105,13 @@ impl<D> TyBuilder<D> {
pub(crate) fn fill_with_inference_vars(self, table: &mut InferenceTable<'_>) -> Self { pub(crate) fn fill_with_inference_vars(self, table: &mut InferenceTable<'_>) -> Self {
self.fill(|x| match x { self.fill(|x| match x {
ParamKind::Type => GenericArgData::Ty(table.new_type_var()).intern(Interner), ParamKind::Type => table.new_type_var().cast(Interner),
ParamKind::Const(ty) => { ParamKind::Const(ty) => table.new_const_var(ty.clone()).cast(Interner),
GenericArgData::Const(table.new_const_var(ty.clone())).intern(Interner)
}
}) })
} }
pub fn fill(mut self, filler: impl FnMut(&ParamKind) -> GenericArg) -> Self { pub fn fill(mut self, filler: impl FnMut(&ParamKind) -> GenericArg) -> Self {
self.vec.extend(self.param_kinds.iter().skip(self.vec.len()).map(filler)); self.vec.extend(self.param_kinds[self.vec.len()..].iter().map(filler));
assert_eq!(self.remaining(), 0); assert_eq!(self.remaining(), 0);
self self
} }
@ -255,7 +245,8 @@ impl TyBuilder<hir_def::AdtId> {
) -> Self { ) -> Self {
let defaults = db.generic_defaults(self.data.into()); let defaults = db.generic_defaults(self.data.into());
for default_ty in defaults.iter().skip(self.vec.len()) { for default_ty in defaults.iter().skip(self.vec.len()) {
if let GenericArgData::Ty(x) = default_ty.skip_binders().data(Interner) { // NOTE(skip_binders): we only check if the arg type is error type.
if let Some(x) = default_ty.skip_binders().ty(Interner) {
if x.is_unknown() { if x.is_unknown() {
self.vec.push(fallback().cast(Interner)); self.vec.push(fallback().cast(Interner));
continue; continue;

View file

@ -152,7 +152,7 @@ impl TyExt for Ty {
TyKind::FnDef(def, parameters) => { TyKind::FnDef(def, parameters) => {
let callable_def = db.lookup_intern_callable_def((*def).into()); let callable_def = db.lookup_intern_callable_def((*def).into());
let sig = db.callable_item_signature(callable_def); let sig = db.callable_item_signature(callable_def);
Some(sig.substitute(Interner, &parameters)) Some(sig.substitute(Interner, parameters))
} }
TyKind::Closure(.., substs) => { TyKind::Closure(.., substs) => {
let sig_param = substs.at(Interner, 0).assert_ty_ref(Interner); let sig_param = substs.at(Interner, 0).assert_ty_ref(Interner);

View file

@ -12,8 +12,7 @@ use crate::{
builder::ParamKind, builder::ParamKind,
consteval, consteval,
method_resolution::{self, VisibleFromModule}, method_resolution::{self, VisibleFromModule},
GenericArgData, Interner, Substitution, TraitRefExt, Ty, TyBuilder, TyExt, TyKind, Interner, Substitution, TraitRefExt, Ty, TyBuilder, TyExt, TyKind, ValueTyDefId,
ValueTyDefId,
}; };
use super::{ExprOrPatId, InferenceContext, TraitRef}; use super::{ExprOrPatId, InferenceContext, TraitRef};
@ -104,9 +103,7 @@ impl<'a> InferenceContext<'a> {
.use_parent_substs(&parent_substs) .use_parent_substs(&parent_substs)
.fill(|x| { .fill(|x| {
it.next().unwrap_or_else(|| match x { it.next().unwrap_or_else(|| match x {
ParamKind::Type => { ParamKind::Type => TyKind::Error.intern(Interner).cast(Interner),
GenericArgData::Ty(TyKind::Error.intern(Interner)).intern(Interner)
}
ParamKind::Const(ty) => consteval::unknown_const_as_generic(ty.clone()), ParamKind::Const(ty) => consteval::unknown_const_as_generic(ty.clone()),
}) })
}) })

View file

@ -678,7 +678,7 @@ impl<'a> TyLoweringContext<'a> {
let total_len = let total_len =
parent_params + self_params + type_params + const_params + impl_trait_params; parent_params + self_params + type_params + const_params + impl_trait_params;
let ty_error = GenericArgData::Ty(TyKind::Error.intern(Interner)).intern(Interner); let ty_error = TyKind::Error.intern(Interner).cast(Interner);
let mut def_generic_iter = def_generics.iter_id(); let mut def_generic_iter = def_generics.iter_id();
@ -696,7 +696,7 @@ impl<'a> TyLoweringContext<'a> {
let fill_self_params = || { let fill_self_params = || {
for x in explicit_self_ty for x in explicit_self_ty
.into_iter() .into_iter()
.map(|x| GenericArgData::Ty(x).intern(Interner)) .map(|x| x.cast(Interner))
.chain(iter::repeat(ty_error.clone())) .chain(iter::repeat(ty_error.clone()))
.take(self_params) .take(self_params)
{ {

View file

@ -4,7 +4,7 @@
use std::iter; use std::iter;
use base_db::CrateId; use base_db::CrateId;
use chalk_ir::{fold::Shift, BoundVar, DebruijnIndex}; use chalk_ir::{cast::Cast, fold::Shift, BoundVar, DebruijnIndex};
use hir_def::{ use hir_def::{
db::DefDatabase, db::DefDatabase,
generics::{ generics::{
@ -24,8 +24,7 @@ use smallvec::{smallvec, SmallVec};
use syntax::SmolStr; use syntax::SmolStr;
use crate::{ use crate::{
db::HirDatabase, ChalkTraitId, ConstData, ConstValue, GenericArgData, Interner, Substitution, db::HirDatabase, ChalkTraitId, Interner, Substitution, TraitRef, TraitRefExt, WhereClause,
TraitRef, TraitRefExt, TyKind, WhereClause,
}; };
pub(crate) fn fn_traits(db: &dyn DefDatabase, krate: CrateId) -> impl Iterator<Item = TraitId> { pub(crate) fn fn_traits(db: &dyn DefDatabase, krate: CrateId) -> impl Iterator<Item = TraitId> {
@ -282,8 +281,8 @@ impl Generics {
} }
} }
fn parent_generics(&self) -> Option<&Generics> { pub(crate) fn parent_generics(&self) -> Option<&Generics> {
self.parent_generics.as_ref().map(|it| &**it) self.parent_generics.as_deref()
} }
/// Returns a Substitution that replaces each parameter by a bound variable. /// Returns a Substitution that replaces each parameter by a bound variable.
@ -295,18 +294,10 @@ impl Generics {
Substitution::from_iter( Substitution::from_iter(
Interner, Interner,
self.iter_id().enumerate().map(|(idx, id)| match id { self.iter_id().enumerate().map(|(idx, id)| match id {
Either::Left(_) => GenericArgData::Ty( Either::Left(_) => BoundVar::new(debruijn, idx).to_ty(Interner).cast(Interner),
TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(Interner), Either::Right(id) => BoundVar::new(debruijn, idx)
) .to_const(Interner, db.const_param_ty(id))
.intern(Interner), .cast(Interner),
Either::Right(id) => GenericArgData::Const(
ConstData {
value: ConstValue::BoundVar(BoundVar::new(debruijn, idx)),
ty: db.const_param_ty(id),
}
.intern(Interner),
)
.intern(Interner),
}), }),
) )
} }
@ -316,18 +307,12 @@ impl Generics {
Substitution::from_iter( Substitution::from_iter(
Interner, Interner,
self.iter_id().map(|id| match id { self.iter_id().map(|id| match id {
Either::Left(id) => GenericArgData::Ty( Either::Left(id) => {
TyKind::Placeholder(crate::to_placeholder_idx(db, id.into())).intern(Interner), crate::to_placeholder_idx(db, id.into()).to_ty(Interner).cast(Interner)
)
.intern(Interner),
Either::Right(id) => GenericArgData::Const(
ConstData {
value: ConstValue::Placeholder(crate::to_placeholder_idx(db, id.into())),
ty: db.const_param_ty(id),
} }
.intern(Interner), Either::Right(id) => crate::to_placeholder_idx(db, id.into())
) .to_const(Interner, db.const_param_ty(id))
.intern(Interner), .cast(Interner),
}), }),
) )
} }