Upgrade Chalk to 0.36

Quite a few changes, because Chalk got rid of the `ApplicationTy` nesting.
This commit is contained in:
Florian Diebold 2020-10-30 18:57:16 +01:00
parent e7c8c600b7
commit bca51e3519
7 changed files with 188 additions and 329 deletions

16
Cargo.lock generated
View file

@ -168,9 +168,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chalk-derive"
version = "0.34.0"
version = "0.36.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e9f986750ecb4df889d0a95d4586bd921889497b33908cc75bb80eadb4c600a"
checksum = "9f88ce4deae1dace71e49b7611cfae2d5489de3530d6daba5758043c47ac3a10"
dependencies = [
"proc-macro2",
"quote",
@ -180,9 +180,9 @@ dependencies = [
[[package]]
name = "chalk-ir"
version = "0.34.0"
version = "0.36.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c352c4649f1408bb3de5d86a248fda78d3d9cd1cbbd9502e7eca1be1e7ac368"
checksum = "63362c629c2014ab639b04029070763fb8224df136d1363d30e9ece4c8877da3"
dependencies = [
"chalk-derive",
"lazy_static",
@ -190,9 +190,9 @@ dependencies = [
[[package]]
name = "chalk-recursive"
version = "0.34.0"
version = "0.36.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7294bb2ac5446fcb83ec9524b9113f59a8913f174a9c1dea6db60532f17a1579"
checksum = "5274a882b664121d5827d4f116f5bd8331e0eac3d501ab8038e9d5c5bc5a2689"
dependencies = [
"chalk-derive",
"chalk-ir",
@ -203,9 +203,9 @@ dependencies = [
[[package]]
name = "chalk-solve"
version = "0.34.0"
version = "0.36.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffbca06963ed6f3d22faed840847a685f02feefa3825c0b94f9b791d03a0ac6f"
checksum = "cac338a67af52a7f50bb2f8232e730a3518ce432dbe303246acfe525ddd838c7"
dependencies = [
"chalk-derive",
"chalk-ir",

View file

@ -22,7 +22,6 @@ debug = 0 # Set this to 1 or 2 to get more useful backtraces in debugger.
[patch.'crates-io']
# rowan = { path = "../rowan" }
[patch.'https://github.com/rust-lang/chalk.git']
# chalk-solve = { path = "../chalk/chalk-solve" }
# chalk-rust-ir = { path = "../chalk/chalk-rust-ir" }
# chalk-ir = { path = "../chalk/chalk-ir" }
# chalk-recursive = { path = "../chalk/chalk-recursive" }

View file

@ -17,9 +17,9 @@ ena = "0.14.0"
log = "0.4.8"
rustc-hash = "1.1.0"
scoped-tls = "1"
chalk-solve = { version = "0.34", default-features = false }
chalk-ir = "0.34"
chalk-recursive = "0.34"
chalk-solve = { version = "0.36", default-features = false }
chalk-ir = "0.36"
chalk-recursive = "0.36"
stdx = { path = "../stdx", version = "0.0.0" }
hir_def = { path = "../hir_def", version = "0.0.0" }

View file

@ -3,7 +3,7 @@ use std::sync::Arc;
use log::debug;
use chalk_ir::{fold::shift::Shift, CanonicalVarKinds, GenericArg, TypeName};
use chalk_ir::{fold::shift::Shift, CanonicalVarKinds, GenericArg};
use chalk_solve::rust_ir::{self, OpaqueTyDatumBound, WellKnownTrait};
use base_db::{salsa::InternKey, CrateId};
@ -81,7 +81,10 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
let ty: Ty = from_chalk(self.db, parameters[0].assert_ty_ref(&Interner).clone());
fn binder_kind(ty: &Ty, binders: &CanonicalVarKinds<Interner>) -> Option<chalk_ir::TyKind> {
fn binder_kind(
ty: &Ty,
binders: &CanonicalVarKinds<Interner>,
) -> Option<chalk_ir::TyVariableKind> {
if let Ty::Bound(bv) = ty {
let binders = binders.as_slice(&Interner);
if bv.debruijn == DebruijnIndex::INNERMOST {
@ -95,8 +98,8 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
let self_ty_fp = TyFingerprint::for_impl(&ty);
let fps: &[TyFingerprint] = match binder_kind(&ty, binders) {
Some(chalk_ir::TyKind::Integer) => &ALL_INT_FPS,
Some(chalk_ir::TyKind::Float) => &ALL_FLOAT_FPS,
Some(chalk_ir::TyVariableKind::Integer) => &ALL_INT_FPS,
Some(chalk_ir::TyVariableKind::Float) => &ALL_FLOAT_FPS,
_ => self_ty_fp.as_ref().map(std::slice::from_ref).unwrap_or(&[]),
};
@ -129,12 +132,8 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
debug!("impls_for_trait returned {} impls", result.len());
result
}
fn impl_provided_for(
&self,
auto_trait_id: TraitId,
application_ty: &chalk_ir::ApplicationTy<Interner>,
) -> bool {
debug!("impl_provided_for {:?}, {:?}", auto_trait_id, application_ty);
fn impl_provided_for(&self, auto_trait_id: TraitId, kind: &chalk_ir::TyKind<Interner>) -> bool {
debug!("impl_provided_for {:?}, {:?}", auto_trait_id, kind);
false // FIXME
}
fn associated_ty_value(&self, id: AssociatedTyValueId) -> Arc<AssociatedTyValue> {
@ -466,7 +465,7 @@ pub(crate) fn struct_datum_query(
struct_id: AdtId,
) -> Arc<StructDatum> {
debug!("struct_datum {:?}", struct_id);
let type_ctor: TypeCtor = from_chalk(db, TypeName::Adt(struct_id));
let type_ctor = TypeCtor::Adt(from_chalk(db, struct_id));
debug!("struct {:?} = {:?}", struct_id, type_ctor);
let num_params = type_ctor.num_ty_params(db);
let upstream = type_ctor.krate(db) != Some(krate);

View file

@ -122,13 +122,6 @@ impl chalk_ir::interner::Interner for Interner {
tls::with_current_program(|prog| Some(prog?.debug_program_clause_implication(pci, fmt)))
}
fn debug_application_ty(
application_ty: &chalk_ir::ApplicationTy<Interner>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
tls::with_current_program(|prog| Some(prog?.debug_application_ty(application_ty, fmt)))
}
fn debug_substitution(
substitution: &chalk_ir::Substitution<Interner>,
fmt: &mut fmt::Formatter<'_>,

View file

@ -5,7 +5,7 @@
use chalk_ir::{
cast::Cast, fold::shift::Shift, interner::HasInterner, LifetimeData, PlaceholderIndex, Scalar,
TypeName, UniverseIndex,
UniverseIndex,
};
use chalk_solve::rust_ir;
@ -32,7 +32,7 @@ impl ToChalk for Ty {
TypeCtor::Array => array_to_chalk(db, apply_ty.parameters),
TypeCtor::FnPtr { num_args: _, is_varargs } => {
let substitution = apply_ty.parameters.to_chalk(db).shifted_in(&Interner);
chalk_ir::TyData::Function(chalk_ir::FnPointer {
chalk_ir::TyKind::Function(chalk_ir::FnPointer {
num_binders: 0,
sig: chalk_ir::FnSig {
abi: (),
@ -43,10 +43,68 @@ impl ToChalk for Ty {
})
.intern(&Interner)
}
_ => {
let name = apply_ty.ctor.to_chalk(db);
TypeCtor::AssociatedType(type_alias) => {
let assoc_type = TypeAliasAsAssocType(type_alias);
let assoc_type_id = assoc_type.to_chalk(db);
let substitution = apply_ty.parameters.to_chalk(db);
chalk_ir::ApplicationTy { name, substitution }.cast(&Interner).intern(&Interner)
chalk_ir::TyKind::AssociatedType(assoc_type_id, substitution).intern(&Interner)
}
TypeCtor::OpaqueType(impl_trait_id) => {
let id = impl_trait_id.to_chalk(db);
let substitution = apply_ty.parameters.to_chalk(db);
chalk_ir::TyKind::OpaqueType(id, substitution).intern(&Interner)
}
TypeCtor::ForeignType(type_alias) => {
let foreign_type = TypeAliasAsForeignType(type_alias);
let foreign_type_id = foreign_type.to_chalk(db);
chalk_ir::TyKind::Foreign(foreign_type_id).intern(&Interner)
}
TypeCtor::Bool => chalk_ir::TyKind::Scalar(Scalar::Bool).intern(&Interner),
TypeCtor::Char => chalk_ir::TyKind::Scalar(Scalar::Char).intern(&Interner),
TypeCtor::Int(int_ty) => {
chalk_ir::TyKind::Scalar(int_ty_to_chalk(int_ty)).intern(&Interner)
}
TypeCtor::Float(FloatTy { bitness: FloatBitness::X32 }) => {
chalk_ir::TyKind::Scalar(Scalar::Float(chalk_ir::FloatTy::F32))
.intern(&Interner)
}
TypeCtor::Float(FloatTy { bitness: FloatBitness::X64 }) => {
chalk_ir::TyKind::Scalar(Scalar::Float(chalk_ir::FloatTy::F64))
.intern(&Interner)
}
TypeCtor::Tuple { cardinality } => {
let substitution = apply_ty.parameters.to_chalk(db);
chalk_ir::TyKind::Tuple(cardinality.into(), substitution).intern(&Interner)
}
TypeCtor::RawPtr(mutability) => {
let ty = apply_ty.parameters[0].clone().to_chalk(db);
chalk_ir::TyKind::Raw(mutability.to_chalk(db), ty).intern(&Interner)
}
TypeCtor::Slice => {
chalk_ir::TyKind::Slice(apply_ty.parameters[0].clone().to_chalk(db))
.intern(&Interner)
}
TypeCtor::Str => chalk_ir::TyKind::Str.intern(&Interner),
TypeCtor::FnDef(callable_def) => {
let id = callable_def.to_chalk(db);
let substitution = apply_ty.parameters.to_chalk(db);
chalk_ir::TyKind::FnDef(id, substitution).intern(&Interner)
}
TypeCtor::Never => chalk_ir::TyKind::Never.intern(&Interner),
TypeCtor::Closure { def, expr } => {
let closure_id = db.intern_closure((def, expr));
let substitution = apply_ty.parameters.to_chalk(db);
chalk_ir::TyKind::Closure(closure_id.into(), substitution).intern(&Interner)
}
TypeCtor::Adt(adt_id) => {
let substitution = apply_ty.parameters.to_chalk(db);
chalk_ir::TyKind::Adt(chalk_ir::AdtId(adt_id), substitution).intern(&Interner)
}
},
Ty::Projection(proj_ty) => {
@ -67,7 +125,7 @@ impl ToChalk for Ty {
}
.to_ty::<Interner>(&Interner)
}
Ty::Bound(idx) => chalk_ir::TyData::BoundVar(idx).intern(&Interner),
Ty::Bound(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner),
Ty::Infer(_infer_ty) => panic!("uncanonicalized infer ty"),
Ty::Dyn(predicates) => {
let where_clauses = chalk_ir::QuantifiedWhereClauses::from_iter(
@ -78,55 +136,45 @@ impl ToChalk for Ty {
bounds: make_binders(where_clauses, 1),
lifetime: LifetimeData::Static.intern(&Interner),
};
chalk_ir::TyData::Dyn(bounded_ty).intern(&Interner)
chalk_ir::TyKind::Dyn(bounded_ty).intern(&Interner)
}
Ty::Opaque(opaque_ty) => {
let opaque_ty_id = opaque_ty.opaque_ty_id.to_chalk(db);
let substitution = opaque_ty.parameters.to_chalk(db);
chalk_ir::TyData::Alias(chalk_ir::AliasTy::Opaque(chalk_ir::OpaqueTy {
chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(chalk_ir::OpaqueTy {
opaque_ty_id,
substitution,
}))
.intern(&Interner)
}
Ty::Unknown => {
let substitution = chalk_ir::Substitution::empty(&Interner);
let name = TypeName::Error;
chalk_ir::ApplicationTy { name, substitution }.cast(&Interner).intern(&Interner)
}
Ty::Unknown => chalk_ir::TyKind::Error.intern(&Interner),
}
}
fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self {
match chalk.data(&Interner).clone() {
chalk_ir::TyData::Apply(apply_ty) => match apply_ty.name {
TypeName::Error => Ty::Unknown,
TypeName::Ref(m) => ref_from_chalk(db, m, apply_ty.substitution),
TypeName::Array => array_from_chalk(db, apply_ty.substitution),
_ => {
let ctor = from_chalk(db, apply_ty.name);
let parameters = from_chalk(db, apply_ty.substitution);
Ty::Apply(ApplicationTy { ctor, parameters })
}
},
chalk_ir::TyData::Placeholder(idx) => {
match chalk.data(&Interner).kind.clone() {
chalk_ir::TyKind::Error => Ty::Unknown,
chalk_ir::TyKind::Array(ty, _size) => {
Ty::apply(TypeCtor::Array, Substs::single(from_chalk(db, ty)))
}
chalk_ir::TyKind::Placeholder(idx) => {
assert_eq!(idx.ui, UniverseIndex::ROOT);
let interned_id = crate::db::GlobalTypeParamId::from_intern_id(
crate::salsa::InternId::from(idx.idx),
);
Ty::Placeholder(db.lookup_intern_type_param_id(interned_id))
}
chalk_ir::TyData::Alias(chalk_ir::AliasTy::Projection(proj)) => {
chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => {
let associated_ty =
from_chalk::<TypeAliasAsAssocType, _>(db, proj.associated_ty_id).0;
let parameters = from_chalk(db, proj.substitution);
Ty::Projection(ProjectionTy { associated_ty, parameters })
}
chalk_ir::TyData::Alias(chalk_ir::AliasTy::Opaque(opaque_ty)) => {
chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(opaque_ty)) => {
let impl_trait_id = from_chalk(db, opaque_ty.opaque_ty_id);
let parameters = from_chalk(db, opaque_ty.substitution);
Ty::Opaque(OpaqueTy { opaque_ty_id: impl_trait_id, parameters })
}
chalk_ir::TyData::Function(chalk_ir::FnPointer {
chalk_ir::TyKind::Function(chalk_ir::FnPointer {
num_binders,
sig: chalk_ir::FnSig { variadic, .. },
substitution,
@ -145,9 +193,9 @@ impl ToChalk for Ty {
parameters,
})
}
chalk_ir::TyData::BoundVar(idx) => Ty::Bound(idx),
chalk_ir::TyData::InferenceVar(_iv, _kind) => Ty::Unknown,
chalk_ir::TyData::Dyn(where_clauses) => {
chalk_ir::TyKind::BoundVar(idx) => Ty::Bound(idx),
chalk_ir::TyKind::InferenceVar(_iv, _kind) => Ty::Unknown,
chalk_ir::TyKind::Dyn(where_clauses) => {
assert_eq!(where_clauses.bounds.binders.len(&Interner), 1);
let predicates = where_clauses
.bounds
@ -157,10 +205,76 @@ impl ToChalk for Ty {
.collect();
Ty::Dyn(predicates)
}
chalk_ir::TyKind::Adt(struct_id, subst) => {
apply_ty_from_chalk(db, TypeCtor::Adt(struct_id.0), subst)
}
chalk_ir::TyKind::AssociatedType(type_id, subst) => apply_ty_from_chalk(
db,
TypeCtor::AssociatedType(from_chalk::<TypeAliasAsAssocType, _>(db, type_id).0),
subst,
),
chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => {
apply_ty_from_chalk(db, TypeCtor::OpaqueType(from_chalk(db, opaque_type_id)), subst)
}
chalk_ir::TyKind::Scalar(Scalar::Bool) => Ty::simple(TypeCtor::Bool),
chalk_ir::TyKind::Scalar(Scalar::Char) => Ty::simple(TypeCtor::Char),
chalk_ir::TyKind::Scalar(Scalar::Int(int_ty)) => Ty::simple(TypeCtor::Int(IntTy {
signedness: Signedness::Signed,
bitness: bitness_from_chalk_int(int_ty),
})),
chalk_ir::TyKind::Scalar(Scalar::Uint(uint_ty)) => Ty::simple(TypeCtor::Int(IntTy {
signedness: Signedness::Unsigned,
bitness: bitness_from_chalk_uint(uint_ty),
})),
chalk_ir::TyKind::Scalar(Scalar::Float(chalk_ir::FloatTy::F32)) => {
Ty::simple(TypeCtor::Float(FloatTy { bitness: FloatBitness::X32 }))
}
chalk_ir::TyKind::Scalar(Scalar::Float(chalk_ir::FloatTy::F64)) => {
Ty::simple(TypeCtor::Float(FloatTy { bitness: FloatBitness::X64 }))
}
chalk_ir::TyKind::Tuple(cardinality, subst) => {
apply_ty_from_chalk(db, TypeCtor::Tuple { cardinality: cardinality as u16 }, subst)
}
chalk_ir::TyKind::Raw(mutability, ty) => {
Ty::apply_one(TypeCtor::RawPtr(from_chalk(db, mutability)), from_chalk(db, ty))
}
chalk_ir::TyKind::Slice(ty) => Ty::apply_one(TypeCtor::Slice, from_chalk(db, ty)),
chalk_ir::TyKind::Ref(mutability, _lifetime, ty) => {
Ty::apply_one(TypeCtor::Ref(from_chalk(db, mutability)), from_chalk(db, ty))
}
chalk_ir::TyKind::Str => Ty::simple(TypeCtor::Str),
chalk_ir::TyKind::Never => Ty::simple(TypeCtor::Never),
chalk_ir::TyKind::FnDef(fn_def_id, subst) => {
let callable_def = from_chalk(db, fn_def_id);
apply_ty_from_chalk(db, TypeCtor::FnDef(callable_def), subst)
}
chalk_ir::TyKind::Closure(id, subst) => {
let id: crate::db::ClosureId = id.into();
let (def, expr) = db.lookup_intern_closure(id);
apply_ty_from_chalk(db, TypeCtor::Closure { def, expr }, subst)
}
chalk_ir::TyKind::Foreign(foreign_def_id) => Ty::simple(TypeCtor::ForeignType(
from_chalk::<TypeAliasAsForeignType, _>(db, foreign_def_id).0,
)),
chalk_ir::TyKind::Generator(_, _) => unimplemented!(), // FIXME
chalk_ir::TyKind::GeneratorWitness(_, _) => unimplemented!(), // FIXME
}
}
}
fn apply_ty_from_chalk(
db: &dyn HirDatabase,
ctor: TypeCtor,
subst: chalk_ir::Substitution<Interner>,
) -> Ty {
Ty::Apply(ApplicationTy { ctor, parameters: from_chalk(db, subst) })
}
/// We currently don't model lifetimes, but Chalk does. So, we have to insert a
/// fake lifetime here, because Chalks built-in logic may expect it to be there.
fn ref_to_chalk(
@ -170,60 +284,21 @@ fn ref_to_chalk(
) -> chalk_ir::Ty<Interner> {
let arg = subst[0].clone().to_chalk(db);
let lifetime = LifetimeData::Static.intern(&Interner);
chalk_ir::ApplicationTy {
name: TypeName::Ref(mutability.to_chalk(db)),
substitution: chalk_ir::Substitution::from_iter(
&Interner,
vec![lifetime.cast(&Interner), arg.cast(&Interner)],
),
}
.intern(&Interner)
}
/// Here we remove the lifetime from the type we got from Chalk.
fn ref_from_chalk(
db: &dyn HirDatabase,
mutability: chalk_ir::Mutability,
subst: chalk_ir::Substitution<Interner>,
) -> Ty {
let tys = subst
.iter(&Interner)
.filter_map(|p| Some(from_chalk(db, p.ty(&Interner)?.clone())))
.collect();
Ty::apply(TypeCtor::Ref(from_chalk(db, mutability)), Substs(tys))
chalk_ir::TyKind::Ref(mutability.to_chalk(db), lifetime, arg).intern(&Interner)
}
/// We currently don't model constants, but Chalk does. So, we have to insert a
/// fake constant here, because Chalks built-in logic may expect it to be there.
fn array_to_chalk(db: &dyn HirDatabase, subst: Substs) -> chalk_ir::Ty<Interner> {
let arg = subst[0].clone().to_chalk(db);
let usize_ty = chalk_ir::ApplicationTy {
name: TypeName::Scalar(Scalar::Uint(chalk_ir::UintTy::Usize)),
substitution: chalk_ir::Substitution::empty(&Interner),
}
.intern(&Interner);
let usize_ty =
chalk_ir::TyKind::Scalar(Scalar::Uint(chalk_ir::UintTy::Usize)).intern(&Interner);
let const_ = chalk_ir::ConstData {
ty: usize_ty,
value: chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst { interned: () }),
}
.intern(&Interner);
chalk_ir::ApplicationTy {
name: TypeName::Array,
substitution: chalk_ir::Substitution::from_iter(
&Interner,
vec![arg.cast(&Interner), const_.cast(&Interner)],
),
}
.intern(&Interner)
}
/// Here we remove the const from the type we got from Chalk.
fn array_from_chalk(db: &dyn HirDatabase, subst: chalk_ir::Substitution<Interner>) -> Ty {
let tys = subst
.iter(&Interner)
.filter_map(|p| Some(from_chalk(db, p.ty(&Interner)?.clone())))
.collect();
Ty::apply(TypeCtor::Array, Substs(tys))
chalk_ir::TyKind::Array(arg, const_).intern(&Interner)
}
impl ToChalk for Substs {
@ -288,124 +363,6 @@ impl ToChalk for OpaqueTyId {
}
}
impl ToChalk for TypeCtor {
type Chalk = TypeName<Interner>;
fn to_chalk(self, db: &dyn HirDatabase) -> TypeName<Interner> {
match self {
TypeCtor::AssociatedType(type_alias) => {
let assoc_type = TypeAliasAsAssocType(type_alias);
let assoc_type_id = assoc_type.to_chalk(db);
TypeName::AssociatedType(assoc_type_id)
}
TypeCtor::OpaqueType(impl_trait_id) => {
let id = impl_trait_id.to_chalk(db);
TypeName::OpaqueType(id)
}
TypeCtor::ForeignType(type_alias) => {
let foreign_type = TypeAliasAsForeignType(type_alias);
let foreign_type_id = foreign_type.to_chalk(db);
TypeName::Foreign(foreign_type_id)
}
TypeCtor::Bool => TypeName::Scalar(Scalar::Bool),
TypeCtor::Char => TypeName::Scalar(Scalar::Char),
TypeCtor::Int(int_ty) => TypeName::Scalar(int_ty_to_chalk(int_ty)),
TypeCtor::Float(FloatTy { bitness: FloatBitness::X32 }) => {
TypeName::Scalar(Scalar::Float(chalk_ir::FloatTy::F32))
}
TypeCtor::Float(FloatTy { bitness: FloatBitness::X64 }) => {
TypeName::Scalar(Scalar::Float(chalk_ir::FloatTy::F64))
}
TypeCtor::Tuple { cardinality } => TypeName::Tuple(cardinality.into()),
TypeCtor::RawPtr(mutability) => TypeName::Raw(mutability.to_chalk(db)),
TypeCtor::Slice => TypeName::Slice,
TypeCtor::Array => TypeName::Array,
TypeCtor::Ref(mutability) => TypeName::Ref(mutability.to_chalk(db)),
TypeCtor::Str => TypeName::Str,
TypeCtor::FnDef(callable_def) => {
let id = callable_def.to_chalk(db);
TypeName::FnDef(id)
}
TypeCtor::Never => TypeName::Never,
TypeCtor::Closure { def, expr } => {
let closure_id = db.intern_closure((def, expr));
TypeName::Closure(closure_id.into())
}
TypeCtor::Adt(adt_id) => TypeName::Adt(chalk_ir::AdtId(adt_id)),
TypeCtor::FnPtr { .. } => {
// This should not be reached, since Chalk doesn't represent
// function pointers with TypeName
unreachable!()
}
}
}
fn from_chalk(db: &dyn HirDatabase, type_name: TypeName<Interner>) -> TypeCtor {
match type_name {
TypeName::Adt(struct_id) => TypeCtor::Adt(struct_id.0),
TypeName::AssociatedType(type_id) => {
TypeCtor::AssociatedType(from_chalk::<TypeAliasAsAssocType, _>(db, type_id).0)
}
TypeName::OpaqueType(opaque_type_id) => {
TypeCtor::OpaqueType(from_chalk(db, opaque_type_id))
}
TypeName::Scalar(Scalar::Bool) => TypeCtor::Bool,
TypeName::Scalar(Scalar::Char) => TypeCtor::Char,
TypeName::Scalar(Scalar::Int(int_ty)) => TypeCtor::Int(IntTy {
signedness: Signedness::Signed,
bitness: bitness_from_chalk_int(int_ty),
}),
TypeName::Scalar(Scalar::Uint(uint_ty)) => TypeCtor::Int(IntTy {
signedness: Signedness::Unsigned,
bitness: bitness_from_chalk_uint(uint_ty),
}),
TypeName::Scalar(Scalar::Float(chalk_ir::FloatTy::F32)) => {
TypeCtor::Float(FloatTy { bitness: FloatBitness::X32 })
}
TypeName::Scalar(Scalar::Float(chalk_ir::FloatTy::F64)) => {
TypeCtor::Float(FloatTy { bitness: FloatBitness::X64 })
}
TypeName::Tuple(cardinality) => TypeCtor::Tuple { cardinality: cardinality as u16 },
TypeName::Raw(mutability) => TypeCtor::RawPtr(from_chalk(db, mutability)),
TypeName::Slice => TypeCtor::Slice,
TypeName::Ref(mutability) => TypeCtor::Ref(from_chalk(db, mutability)),
TypeName::Str => TypeCtor::Str,
TypeName::Never => TypeCtor::Never,
TypeName::FnDef(fn_def_id) => {
let callable_def = from_chalk(db, fn_def_id);
TypeCtor::FnDef(callable_def)
}
TypeName::Array => TypeCtor::Array,
TypeName::Closure(id) => {
let id: crate::db::ClosureId = id.into();
let (def, expr) = db.lookup_intern_closure(id);
TypeCtor::Closure { def, expr }
}
TypeName::Foreign(foreign_def_id) => {
TypeCtor::ForeignType(from_chalk::<TypeAliasAsForeignType, _>(db, foreign_def_id).0)
}
TypeName::Error => {
// this should not be reached, since we don't represent TypeName::Error with TypeCtor
unreachable!()
}
TypeName::Generator(_) => unimplemented!(), // FIXME
TypeName::GeneratorWitness(_) => unimplemented!(), // FIXME
}
}
}
fn bitness_from_chalk_uint(uint_ty: chalk_ir::UintTy) -> IntBitness {
use chalk_ir::UintTy;
@ -677,9 +634,9 @@ where
.kinds
.iter()
.map(|k| match k {
TyKind::General => chalk_ir::TyKind::General,
TyKind::Integer => chalk_ir::TyKind::Integer,
TyKind::Float => chalk_ir::TyKind::Float,
TyKind::General => chalk_ir::TyVariableKind::General,
TyKind::Integer => chalk_ir::TyVariableKind::Integer,
TyKind::Float => chalk_ir::TyVariableKind::Float,
})
.map(|tk| {
chalk_ir::CanonicalVarKind::new(
@ -700,9 +657,9 @@ where
.iter(&Interner)
.map(|k| match k.kind {
chalk_ir::VariableKind::Ty(tk) => match tk {
chalk_ir::TyKind::General => TyKind::General,
chalk_ir::TyKind::Integer => TyKind::Integer,
chalk_ir::TyKind::Float => TyKind::Float,
chalk_ir::TyVariableKind::General => TyKind::General,
chalk_ir::TyVariableKind::Integer => TyKind::Integer,
chalk_ir::TyVariableKind::Float => TyKind::Float,
},
chalk_ir::VariableKind::Lifetime => panic!("unexpected lifetime from Chalk"),
chalk_ir::VariableKind::Const(_) => panic!("unexpected const from Chalk"),
@ -768,7 +725,8 @@ where
chalk_ir::Binders::new(
chalk_ir::VariableKinds::from_iter(
&Interner,
std::iter::repeat(chalk_ir::VariableKind::Ty(chalk_ir::TyKind::General)).take(num_vars),
std::iter::repeat(chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General))
.take(num_vars),
),
value,
)

View file

@ -1,12 +1,12 @@
//! Implementation of Chalk debug helper functions using TLS.
use std::fmt;
use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplication, TypeName};
use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplication};
use itertools::Itertools;
use super::{from_chalk, Interner, TypeAliasAsAssocType};
use crate::{db::HirDatabase, CallableDefId, TypeCtor};
use hir_def::{AdtId, AssocContainerId, DefWithBodyId, Lookup, TypeAliasId};
use crate::{db::HirDatabase, CallableDefId};
use hir_def::{AdtId, AssocContainerId, Lookup, TypeAliasId};
pub use unsafe_tls::{set_current_program, with_current_program};
@ -18,94 +18,12 @@ impl DebugContext<'_> {
id: super::AdtId,
f: &mut fmt::Formatter<'_>,
) -> Result<(), fmt::Error> {
let type_ctor: TypeCtor = from_chalk(self.0, TypeName::Adt(id));
match type_ctor {
TypeCtor::Bool => write!(f, "bool")?,
TypeCtor::Char => write!(f, "char")?,
TypeCtor::Int(t) => write!(f, "{}", t)?,
TypeCtor::Float(t) => write!(f, "{}", t)?,
TypeCtor::Str => write!(f, "str")?,
TypeCtor::Slice => write!(f, "slice")?,
TypeCtor::Array => write!(f, "array")?,
TypeCtor::RawPtr(m) => write!(f, "*{}", m.as_keyword_for_ptr())?,
TypeCtor::Ref(m) => write!(f, "&{}", m.as_keyword_for_ref())?,
TypeCtor::Never => write!(f, "!")?,
TypeCtor::Tuple { .. } => {
write!(f, "()")?;
}
TypeCtor::FnPtr { .. } => {
write!(f, "fn")?;
}
TypeCtor::FnDef(def) => {
let name = match def {
CallableDefId::FunctionId(ff) => self.0.function_data(ff).name.clone(),
CallableDefId::StructId(s) => self.0.struct_data(s).name.clone(),
CallableDefId::EnumVariantId(e) => {
let enum_data = self.0.enum_data(e.parent);
enum_data.variants[e.local_id].name.clone()
}
};
match def {
CallableDefId::FunctionId(_) => write!(f, "{{fn {}}}", name)?,
CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_) => {
write!(f, "{{ctor {}}}", name)?
}
}
}
TypeCtor::Adt(def_id) => {
let name = match def_id {
AdtId::StructId(it) => self.0.struct_data(it).name.clone(),
AdtId::UnionId(it) => self.0.union_data(it).name.clone(),
AdtId::EnumId(it) => self.0.enum_data(it).name.clone(),
};
write!(f, "{}", name)?;
}
TypeCtor::AssociatedType(type_alias) => {
let trait_ = match type_alias.lookup(self.0.upcast()).container {
AssocContainerId::TraitId(it) => it,
_ => panic!("not an associated type"),
};
let trait_name = self.0.trait_data(trait_).name.clone();
let name = self.0.type_alias_data(type_alias).name.clone();
write!(f, "{}::{}", trait_name, name)?;
}
TypeCtor::OpaqueType(opaque_ty_id) => match opaque_ty_id {
crate::OpaqueTyId::ReturnTypeImplTrait(func, idx) => {
write!(f, "{{impl trait {} of {:?}}}", idx, func)?;
}
crate::OpaqueTyId::AsyncBlockTypeImplTrait(def, idx) => {
write!(f, "{{impl trait of async block {} of {:?}}}", idx.into_raw(), def)?;
}
},
TypeCtor::ForeignType(type_alias) => {
let name = self.0.type_alias_data(type_alias).name.clone();
write!(f, "{}", name)?;
}
TypeCtor::Closure { def, expr } => {
write!(f, "{{closure {:?} in ", expr.into_raw())?;
match def {
DefWithBodyId::FunctionId(func) => {
write!(f, "fn {}", self.0.function_data(func).name)?
}
DefWithBodyId::StaticId(s) => {
if let Some(name) = self.0.static_data(s).name.as_ref() {
write!(f, "body of static {}", name)?;
} else {
write!(f, "body of unnamed static {:?}", s)?;
}
}
DefWithBodyId::ConstId(c) => {
if let Some(name) = self.0.const_data(c).name.as_ref() {
write!(f, "body of const {}", name)?;
} else {
write!(f, "body of unnamed const {:?}", c)?;
}
}
};
write!(f, "}}")?;
}
}
Ok(())
let name = match id.0 {
AdtId::StructId(it) => self.0.struct_data(it).name.clone(),
AdtId::UnionId(it) => self.0.union_data(it).name.clone(),
AdtId::EnumId(it) => self.0.enum_data(it).name.clone(),
};
write!(f, "{}", name)
}
pub fn debug_trait_id(
@ -234,14 +152,6 @@ impl DebugContext<'_> {
write!(fmt, "{:?}", pci.debug(&Interner))
}
pub fn debug_application_ty(
&self,
application_ty: &chalk_ir::ApplicationTy<Interner>,
fmt: &mut fmt::Formatter<'_>,
) -> Result<(), fmt::Error> {
write!(fmt, "{:?}", application_ty.debug(&Interner))
}
pub fn debug_substitution(
&self,
substitution: &chalk_ir::Substitution<Interner>,