Use Cast::cast() instead of interning GenericArgData

This commit is contained in:
Ryo Yoshida 2023-08-09 00:47:29 +09:00
parent 783130bd26
commit 6aa03c5d15
No known key found for this signature in database
GPG Key ID: E25698A930586171
7 changed files with 45 additions and 73 deletions

View File

@ -17,7 +17,8 @@ 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, BoundVar, CallableSig, to_assoc_type_id, to_chalk_trait_id, utils::generics, Binders, BoundVar, CallableSig,
GenericArg, Interner, ProjectionTy, Substitution, TraitRef, Ty, TyDefId, TyExt, TyKind, GenericArg, GenericArgData, Interner, ProjectionTy, Substitution, TraitRef, Ty, TyDefId, TyExt,
TyKind,
}; };
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@ -79,9 +80,9 @@ impl<D> TyBuilder<D> {
let expected_kind = &self.param_kinds[self.vec.len()]; let expected_kind = &self.param_kinds[self.vec.len()];
let arg_kind = match arg.data(Interner) { let arg_kind = match arg.data(Interner) {
chalk_ir::GenericArgData::Ty(_) => ParamKind::Type, GenericArgData::Ty(_) => ParamKind::Type,
chalk_ir::GenericArgData::Lifetime(_) => panic!("Got lifetime in TyBuilder::push"), GenericArgData::Lifetime(_) => panic!("Got lifetime in TyBuilder::push"),
chalk_ir::GenericArgData::Const(c) => { GenericArgData::Const(c) => {
let c = c.data(Interner); let c = c.data(Interner);
ParamKind::Const(c.ty.clone()) ParamKind::Const(c.ty.clone())
} }
@ -139,8 +140,8 @@ impl<D> TyBuilder<D> {
fn assert_match_kind(&self, a: &chalk_ir::GenericArg<Interner>, e: &ParamKind) { fn assert_match_kind(&self, a: &chalk_ir::GenericArg<Interner>, e: &ParamKind) {
match (a.data(Interner), e) { match (a.data(Interner), e) {
(chalk_ir::GenericArgData::Ty(_), ParamKind::Type) (GenericArgData::Ty(_), ParamKind::Type)
| (chalk_ir::GenericArgData::Const(_), ParamKind::Const(_)) => (), | (GenericArgData::Const(_), ParamKind::Const(_)) => (),
_ => panic!("Mismatched kinds: {a:?}, {:?}, {:?}", self.vec, self.param_kinds), _ => panic!("Mismatched kinds: {a:?}, {:?}, {:?}", self.vec, self.param_kinds),
} }
} }

View File

@ -1,7 +1,7 @@
//! Constant evaluation details //! Constant evaluation details
use base_db::CrateId; use base_db::CrateId;
use chalk_ir::{BoundVar, DebruijnIndex, GenericArgData}; use chalk_ir::{cast::Cast, BoundVar, DebruijnIndex};
use hir_def::{ use hir_def::{
hir::Expr, hir::Expr,
path::Path, path::Path,
@ -120,7 +120,7 @@ pub fn unknown_const(ty: Ty) -> Const {
} }
pub fn unknown_const_as_generic(ty: Ty) -> GenericArg { pub fn unknown_const_as_generic(ty: Ty) -> GenericArg {
GenericArgData::Const(unknown_const(ty)).intern(Interner) unknown_const(ty).cast(Interner)
} }
/// Interns a constant scalar with the given type /// Interns a constant scalar with the given type

View File

@ -5,9 +5,7 @@ use std::{
mem, mem,
}; };
use chalk_ir::{ use chalk_ir::{cast::Cast, fold::Shift, DebruijnIndex, Mutability, TyVariableKind};
cast::Cast, fold::Shift, DebruijnIndex, GenericArgData, Mutability, TyVariableKind,
};
use hir_def::{ use hir_def::{
generics::TypeOrConstParamData, generics::TypeOrConstParamData,
hir::{ hir::{
@ -750,7 +748,7 @@ impl InferenceContext<'_> {
self.resolve_associated_type_with_params( self.resolve_associated_type_with_params(
self_ty, self_ty,
self.resolve_ops_index_output(), self.resolve_ops_index_output(),
&[GenericArgData::Ty(index_ty).intern(Interner)], &[index_ty.cast(Interner)],
) )
} else { } else {
self.err_ty() self.err_ty()
@ -1721,16 +1719,13 @@ impl InferenceContext<'_> {
for (id, data) in def_generics.iter().skip(substs.len()) { for (id, data) in def_generics.iter().skip(substs.len()) {
match data { match data {
TypeOrConstParamData::TypeParamData(_) => { TypeOrConstParamData::TypeParamData(_) => {
substs.push(GenericArgData::Ty(self.table.new_type_var()).intern(Interner)) substs.push(self.table.new_type_var().cast(Interner))
}
TypeOrConstParamData::ConstParamData(_) => {
substs.push(
GenericArgData::Const(self.table.new_const_var(
self.db.const_param_ty(ConstParamId::from_unchecked(id)),
))
.intern(Interner),
)
} }
TypeOrConstParamData::ConstParamData(_) => substs.push(
self.table
.new_const_var(self.db.const_param_ty(ConstParamId::from_unchecked(id)))
.cast(Interner),
),
} }
} }
assert_eq!(substs.len(), total_len); assert_eq!(substs.len(), total_len);

View File

@ -10,7 +10,6 @@ use chalk_solve::infer::ParameterEnaVariableExt;
use either::Either; use either::Either;
use ena::unify::UnifyKey; use ena::unify::UnifyKey;
use hir_expand::name; use hir_expand::name;
use stdx::never;
use triomphe::Arc; use triomphe::Arc;
use super::{InferOk, InferResult, InferenceContext, TypeError}; use super::{InferOk, InferResult, InferenceContext, TypeError};
@ -92,15 +91,10 @@ pub(crate) fn unify(
let vars = Substitution::from_iter( let vars = Substitution::from_iter(
Interner, Interner,
tys.binders.iter(Interner).map(|it| match &it.kind { tys.binders.iter(Interner).map(|it| match &it.kind {
chalk_ir::VariableKind::Ty(_) => { chalk_ir::VariableKind::Ty(_) => table.new_type_var().cast(Interner),
GenericArgData::Ty(table.new_type_var()).intern(Interner) // FIXME: maybe wrong?
} chalk_ir::VariableKind::Lifetime => table.new_type_var().cast(Interner),
chalk_ir::VariableKind::Lifetime => { chalk_ir::VariableKind::Const(ty) => table.new_const_var(ty.clone()).cast(Interner),
GenericArgData::Ty(table.new_type_var()).intern(Interner)
} // FIXME: maybe wrong?
chalk_ir::VariableKind::Const(ty) => {
GenericArgData::Const(table.new_const_var(ty.clone())).intern(Interner)
}
}), }),
); );
let ty1_with_vars = vars.apply(tys.value.0.clone(), Interner); let ty1_with_vars = vars.apply(tys.value.0.clone(), Interner);
@ -111,10 +105,10 @@ pub(crate) fn unify(
// default any type vars that weren't unified back to their original bound vars // default any type vars that weren't unified back to their original bound vars
// (kind of hacky) // (kind of hacky)
let find_var = |iv| { let find_var = |iv| {
vars.iter(Interner).position(|v| match v.interned() { vars.iter(Interner).position(|v| match v.data(Interner) {
chalk_ir::GenericArgData::Ty(ty) => ty.inference_var(Interner), GenericArgData::Ty(ty) => ty.inference_var(Interner),
chalk_ir::GenericArgData::Lifetime(lt) => lt.inference_var(Interner), GenericArgData::Lifetime(lt) => lt.inference_var(Interner),
chalk_ir::GenericArgData::Const(c) => c.inference_var(Interner), GenericArgData::Const(c) => c.inference_var(Interner),
} == Some(iv)) } == Some(iv))
}; };
let fallback = |iv, kind, default, binder| match kind { let fallback = |iv, kind, default, binder| match kind {
@ -611,9 +605,9 @@ impl<'a> InferenceTable<'a> {
fn check_changed(&mut self, canonicalized: &Canonicalized<InEnvironment<Goal>>) -> bool { fn check_changed(&mut self, canonicalized: &Canonicalized<InEnvironment<Goal>>) -> bool {
canonicalized.free_vars.iter().any(|var| { canonicalized.free_vars.iter().any(|var| {
let iv = match var.data(Interner) { let iv = match var.data(Interner) {
chalk_ir::GenericArgData::Ty(ty) => ty.inference_var(Interner), GenericArgData::Ty(ty) => ty.inference_var(Interner),
chalk_ir::GenericArgData::Lifetime(lt) => lt.inference_var(Interner), GenericArgData::Lifetime(lt) => lt.inference_var(Interner),
chalk_ir::GenericArgData::Const(c) => c.inference_var(Interner), GenericArgData::Const(c) => c.inference_var(Interner),
} }
.expect("free var is not inference var"); .expect("free var is not inference var");
if self.var_unification_table.probe_var(iv).is_some() { if self.var_unification_table.probe_var(iv).is_some() {
@ -690,14 +684,10 @@ impl<'a> InferenceTable<'a> {
.fill(|it| { .fill(|it| {
let arg = match it { let arg = match it {
ParamKind::Type => self.new_type_var(), ParamKind::Type => self.new_type_var(),
ParamKind::Const(ty) => { ParamKind::Const(_) => unreachable!("Tuple with const parameter"),
never!("Tuple with const parameter");
return GenericArgData::Const(self.new_const_var(ty.clone()))
.intern(Interner);
}
}; };
arg_tys.push(arg.clone()); arg_tys.push(arg.clone());
GenericArgData::Ty(arg).intern(Interner) arg.cast(Interner)
}) })
.build(); .build();

View File

@ -58,10 +58,9 @@ use crate::{
InTypeConstIdMetadata, InTypeConstIdMetadata,
}, },
AliasEq, AliasTy, Binders, BoundVar, CallableSig, Const, ConstScalar, DebruijnIndex, DynTy, AliasEq, AliasTy, Binders, BoundVar, CallableSig, Const, ConstScalar, DebruijnIndex, DynTy,
FnPointer, FnSig, FnSubst, GenericArgData, ImplTraitId, Interner, ParamKind, PolyFnSig, FnPointer, FnSig, FnSubst, ImplTraitId, Interner, ParamKind, PolyFnSig, ProjectionTy,
ProjectionTy, QuantifiedWhereClause, QuantifiedWhereClauses, ReturnTypeImplTrait, QuantifiedWhereClause, QuantifiedWhereClauses, ReturnTypeImplTrait, ReturnTypeImplTraits,
ReturnTypeImplTraits, Substitution, TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, Substitution, TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyKind, WhereClause,
TyKind, WhereClause,
}; };
#[derive(Debug)] #[derive(Debug)]
@ -1643,9 +1642,7 @@ pub(crate) fn generic_defaults_recover(
.iter_id() .iter_id()
.map(|id| { .map(|id| {
let val = match id { let val = match id {
Either::Left(_) => { Either::Left(_) => TyKind::Error.intern(Interner).cast(Interner),
GenericArgData::Ty(TyKind::Error.intern(Interner)).intern(Interner)
}
Either::Right(id) => unknown_const_as_generic(db.const_param_ty(id)), Either::Right(id) => unknown_const_as_generic(db.const_param_ty(id)),
}; };
crate::make_binders(db, &generic_params, val) crate::make_binders(db, &generic_params, val)
@ -1991,16 +1988,9 @@ pub(crate) fn generic_arg_to_chalk<'a, T>(
} }
}; };
Some(match (arg, kind) { Some(match (arg, kind) {
(GenericArg::Type(type_ref), ParamKind::Type) => { (GenericArg::Type(type_ref), ParamKind::Type) => for_type(this, type_ref).cast(Interner),
let ty = for_type(this, type_ref); (GenericArg::Const(c), ParamKind::Const(c_ty)) => for_const(this, c, c_ty).cast(Interner),
GenericArgData::Ty(ty).intern(Interner) (GenericArg::Const(_), ParamKind::Type) => TyKind::Error.intern(Interner).cast(Interner),
}
(GenericArg::Const(c), ParamKind::Const(c_ty)) => {
GenericArgData::Const(for_const(this, c, c_ty)).intern(Interner)
}
(GenericArg::Const(_), ParamKind::Type) => {
GenericArgData::Ty(TyKind::Error.intern(Interner)).intern(Interner)
}
(GenericArg::Type(t), ParamKind::Const(c_ty)) => { (GenericArg::Type(t), ParamKind::Const(c_ty)) => {
// We want to recover simple idents, which parser detects them // We want to recover simple idents, which parser detects them
// as types. Maybe here is not the best place to do it, but // as types. Maybe here is not the best place to do it, but
@ -2010,9 +2000,7 @@ pub(crate) fn generic_arg_to_chalk<'a, T>(
if p.kind == PathKind::Plain { if p.kind == PathKind::Plain {
if let [n] = p.segments() { if let [n] = p.segments() {
let c = ConstRef::Path(n.clone()); let c = ConstRef::Path(n.clone());
return Some( return Some(for_const(this, &c, c_ty).cast(Interner));
GenericArgData::Const(for_const(this, &c, c_ty)).intern(Interner),
);
} }
} }
} }

View File

@ -10,7 +10,7 @@ use std::{
}; };
use base_db::{CrateId, FileId}; use base_db::{CrateId, FileId};
use chalk_ir::Mutability; use chalk_ir::{cast::Cast, Mutability};
use either::Either; use either::Either;
use hir_def::{ use hir_def::{
builtin_type::BuiltinType, builtin_type::BuiltinType,
@ -40,8 +40,8 @@ use crate::{
name, static_lifetime, name, static_lifetime,
traits::FnTrait, traits::FnTrait,
utils::{detect_variant_from_bytes, ClosureSubst}, utils::{detect_variant_from_bytes, ClosureSubst},
CallableDefId, ClosureId, Const, ConstScalar, FnDefId, GenericArgData, Interner, MemoryMap, CallableDefId, ClosureId, Const, ConstScalar, FnDefId, Interner, MemoryMap, Substitution,
Substitution, TraitEnvironment, Ty, TyBuilder, TyExt, TyKind, TraitEnvironment, Ty, TyBuilder, TyExt, TyKind,
}; };
use super::{ use super::{
@ -2248,7 +2248,7 @@ impl Evaluator<'_> {
interval: args_for_target[0].interval.slice(0..self.ptr_size()), interval: args_for_target[0].interval.slice(0..self.ptr_size()),
ty: ty.clone(), ty: ty.clone(),
}; };
let ty = GenericArgData::Ty(ty.clone()).intern(Interner); let ty = ty.clone().cast(Interner);
let generics_for_target = Substitution::from_iter( let generics_for_target = Substitution::from_iter(
Interner, Interner,
generic_args.iter(Interner).enumerate().map(|(i, it)| { generic_args.iter(Interner).enumerate().map(|(i, it)| {

View File

@ -1273,7 +1273,7 @@ impl Adt {
.fill(|x| { .fill(|x| {
let r = it.next().unwrap_or_else(|| TyKind::Error.intern(Interner)); let r = it.next().unwrap_or_else(|| TyKind::Error.intern(Interner));
match x { match x {
ParamKind::Type => GenericArgData::Ty(r).intern(Interner), ParamKind::Type => r.cast(Interner),
ParamKind::Const(ty) => unknown_const_as_generic(ty.clone()), ParamKind::Const(ty) => unknown_const_as_generic(ty.clone()),
} }
}) })
@ -3716,7 +3716,7 @@ impl Type {
.fill(|x| { .fill(|x| {
let r = it.next().unwrap(); let r = it.next().unwrap();
match x { match x {
ParamKind::Type => GenericArgData::Ty(r).intern(Interner), ParamKind::Type => r.cast(Interner),
ParamKind::Const(ty) => { ParamKind::Const(ty) => {
// FIXME: this code is not covered in tests. // FIXME: this code is not covered in tests.
unknown_const_as_generic(ty.clone()) unknown_const_as_generic(ty.clone())
@ -3749,9 +3749,7 @@ impl Type {
.fill(|it| { .fill(|it| {
// FIXME: this code is not covered in tests. // FIXME: this code is not covered in tests.
match it { match it {
ParamKind::Type => { ParamKind::Type => args.next().unwrap().ty.clone().cast(Interner),
GenericArgData::Ty(args.next().unwrap().ty.clone()).intern(Interner)
}
ParamKind::Const(ty) => unknown_const_as_generic(ty.clone()), ParamKind::Const(ty) => unknown_const_as_generic(ty.clone()),
} }
}) })