Merge #7998
7998: Use more Chalk IDs r=flodiebold a=flodiebold Co-authored-by: Florian Diebold <flodiebold@gmail.com>
This commit is contained in:
commit
6ab405f2e9
@ -51,7 +51,7 @@ use hir_expand::{diagnostics::DiagnosticSink, name::name, MacroDefKind};
|
||||
use hir_ty::{
|
||||
autoderef,
|
||||
display::{write_bounds_like_dyn_trait_with_prefix, HirDisplayError, HirFormatter},
|
||||
method_resolution,
|
||||
method_resolution, to_assoc_type_id,
|
||||
traits::{FnTrait, Solution, SolutionVariables},
|
||||
AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, DebruijnIndex, GenericPredicate,
|
||||
InEnvironment, Interner, Obligation, ProjectionPredicate, ProjectionTy, Scalar, Substs,
|
||||
@ -1683,7 +1683,10 @@ impl Type {
|
||||
.fill(args.iter().map(|t| t.ty.value.clone()))
|
||||
.build();
|
||||
let predicate = ProjectionPredicate {
|
||||
projection_ty: ProjectionTy { associated_ty: alias.id, parameters: subst },
|
||||
projection_ty: ProjectionTy {
|
||||
associated_ty: to_assoc_type_id(alias.id),
|
||||
parameters: subst,
|
||||
},
|
||||
ty: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)).intern(&Interner),
|
||||
};
|
||||
let goal = Canonical {
|
||||
@ -1712,10 +1715,7 @@ impl Type {
|
||||
}
|
||||
|
||||
pub fn as_callable(&self, db: &dyn HirDatabase) -> Option<Callable> {
|
||||
let def = match self.ty.value.interned(&Interner) {
|
||||
&TyKind::FnDef(def, _) => Some(def),
|
||||
_ => None,
|
||||
};
|
||||
let def = self.ty.value.callable_def(db);
|
||||
|
||||
let sig = self.ty.value.callable_sig(db)?;
|
||||
Some(Callable { ty: self.clone(), sig, def, is_bound_method: false })
|
||||
|
@ -12,6 +12,7 @@ use log::{info, warn};
|
||||
|
||||
use crate::{
|
||||
db::HirDatabase,
|
||||
to_assoc_type_id,
|
||||
traits::{InEnvironment, Solution},
|
||||
utils::generics,
|
||||
BoundVar, Canonical, DebruijnIndex, Interner, Obligation, Substs, TraitRef, Ty, TyKind,
|
||||
@ -83,7 +84,7 @@ fn deref_by_trait(
|
||||
let projection = super::traits::ProjectionPredicate {
|
||||
ty: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, ty.value.kinds.len()))
|
||||
.intern(&Interner),
|
||||
projection_ty: super::ProjectionTy { associated_ty: target, parameters },
|
||||
projection_ty: super::ProjectionTy { associated_ty: to_assoc_type_id(target), parameters },
|
||||
};
|
||||
|
||||
let obligation = super::Obligation::Projection(projection);
|
||||
|
@ -12,7 +12,7 @@ use la_arena::ArenaMap;
|
||||
use crate::{
|
||||
method_resolution::{InherentImpls, TraitImpls},
|
||||
traits::chalk,
|
||||
Binders, CallableDefId, GenericPredicate, InferenceResult, OpaqueTyId, PolyFnSig,
|
||||
Binders, CallableDefId, FnDefId, GenericPredicate, InferenceResult, OpaqueTyId, PolyFnSig,
|
||||
ReturnTypeImplTraits, TraitRef, Ty, TyDefId, ValueTyDefId,
|
||||
};
|
||||
use hir_expand::name::Name;
|
||||
@ -100,10 +100,10 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
|
||||
fn impl_datum(&self, krate: CrateId, impl_id: chalk::ImplId) -> Arc<chalk::ImplDatum>;
|
||||
|
||||
#[salsa::invoke(crate::traits::chalk::fn_def_datum_query)]
|
||||
fn fn_def_datum(&self, krate: CrateId, fn_def_id: chalk::FnDefId) -> Arc<chalk::FnDefDatum>;
|
||||
fn fn_def_datum(&self, krate: CrateId, fn_def_id: FnDefId) -> Arc<chalk::FnDefDatum>;
|
||||
|
||||
#[salsa::invoke(crate::traits::chalk::fn_def_variance_query)]
|
||||
fn fn_def_variance(&self, krate: CrateId, fn_def_id: chalk::FnDefId) -> chalk::Variances;
|
||||
fn fn_def_variance(&self, krate: CrateId, fn_def_id: FnDefId) -> chalk::Variances;
|
||||
|
||||
#[salsa::invoke(crate::traits::chalk::adt_variance_query)]
|
||||
fn adt_variance(&self, krate: CrateId, adt_id: chalk::AdtId) -> chalk::Variances;
|
||||
|
@ -85,7 +85,7 @@ fn walk_unsafe(
|
||||
let expr = &body.exprs[current];
|
||||
match expr {
|
||||
&Expr::Call { callee, .. } => {
|
||||
if let Some(func) = infer[callee].as_fn_def() {
|
||||
if let Some(func) = infer[callee].as_fn_def(db) {
|
||||
if db.function_data(func).is_unsafe {
|
||||
unsafe_exprs.push(UnsafeExpr { expr: current, inside_unsafe_block });
|
||||
}
|
||||
|
@ -11,7 +11,8 @@ use hir_def::{
|
||||
use hir_expand::name::Name;
|
||||
|
||||
use crate::{
|
||||
db::HirDatabase, primitive, utils::generics, AdtId, AliasTy, CallableDefId, CallableSig,
|
||||
db::HirDatabase, from_assoc_type_id, from_foreign_def_id, primitive, to_assoc_type_id,
|
||||
traits::chalk::from_chalk, utils::generics, AdtId, AliasTy, CallableDefId, CallableSig,
|
||||
GenericPredicate, Interner, Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar,
|
||||
Substs, TraitRef, Ty, TyKind,
|
||||
};
|
||||
@ -256,7 +257,7 @@ impl HirDisplay for ProjectionTy {
|
||||
f.write_joined(&self.parameters[1..], ", ")?;
|
||||
write!(f, ">")?;
|
||||
}
|
||||
write!(f, ">::{}", f.db.type_alias_data(self.associated_ty).name)?;
|
||||
write!(f, ">::{}", f.db.type_alias_data(from_assoc_type_id(self.associated_ty)).name)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@ -363,7 +364,7 @@ impl HirDisplay for Ty {
|
||||
sig.hir_fmt(f)?;
|
||||
}
|
||||
TyKind::FnDef(def, parameters) => {
|
||||
let def = *def;
|
||||
let def = from_chalk(f.db, *def);
|
||||
let sig = f.db.callable_item_signature(def).subst(parameters);
|
||||
match def {
|
||||
CallableDefId::FunctionId(ff) => {
|
||||
@ -431,7 +432,7 @@ impl HirDisplay for Ty {
|
||||
|| f.omit_verbose_types()
|
||||
{
|
||||
match self
|
||||
.as_generic_def()
|
||||
.as_generic_def(f.db)
|
||||
.map(|generic_def_id| f.db.generic_defaults(generic_def_id))
|
||||
.filter(|defaults| !defaults.is_empty())
|
||||
{
|
||||
@ -467,13 +468,14 @@ impl HirDisplay for Ty {
|
||||
}
|
||||
}
|
||||
}
|
||||
TyKind::AssociatedType(type_alias, parameters) => {
|
||||
TyKind::AssociatedType(assoc_type_id, parameters) => {
|
||||
let type_alias = from_assoc_type_id(*assoc_type_id);
|
||||
let trait_ = match type_alias.lookup(f.db.upcast()).container {
|
||||
AssocContainerId::TraitId(it) => it,
|
||||
_ => panic!("not an associated type"),
|
||||
};
|
||||
let trait_ = f.db.trait_data(trait_);
|
||||
let type_alias_data = f.db.type_alias_data(*type_alias);
|
||||
let type_alias_data = f.db.type_alias_data(type_alias);
|
||||
|
||||
// Use placeholder associated types when the target is test (https://rust-lang.github.io/chalk/book/clauses/type_equality.html#placeholder-associated-types)
|
||||
if f.display_target.is_test() {
|
||||
@ -484,14 +486,16 @@ impl HirDisplay for Ty {
|
||||
write!(f, ">")?;
|
||||
}
|
||||
} else {
|
||||
let projection_ty =
|
||||
ProjectionTy { associated_ty: *type_alias, parameters: parameters.clone() };
|
||||
let projection_ty = ProjectionTy {
|
||||
associated_ty: to_assoc_type_id(type_alias),
|
||||
parameters: parameters.clone(),
|
||||
};
|
||||
|
||||
projection_ty.hir_fmt(f)?;
|
||||
}
|
||||
}
|
||||
TyKind::ForeignType(type_alias) => {
|
||||
let type_alias = f.db.type_alias_data(*type_alias);
|
||||
let type_alias = f.db.type_alias_data(from_foreign_def_id(*type_alias));
|
||||
write!(f, "{}", type_alias.name)?;
|
||||
}
|
||||
TyKind::OpaqueType(opaque_ty_id, parameters) => {
|
||||
@ -697,7 +701,9 @@ fn write_bounds_like_dyn_trait(
|
||||
write!(f, "<")?;
|
||||
angle_open = true;
|
||||
}
|
||||
let type_alias = f.db.type_alias_data(projection_pred.projection_ty.associated_ty);
|
||||
let type_alias = f.db.type_alias_data(from_assoc_type_id(
|
||||
projection_pred.projection_ty.associated_ty,
|
||||
));
|
||||
write!(f, "{} = ", type_alias.name)?;
|
||||
projection_pred.ty.hir_fmt(f)?;
|
||||
}
|
||||
@ -768,7 +774,10 @@ impl HirDisplay for GenericPredicate {
|
||||
write!(
|
||||
f,
|
||||
">::{} = ",
|
||||
f.db.type_alias_data(projection_pred.projection_ty.associated_ty).name,
|
||||
f.db.type_alias_data(from_assoc_type_id(
|
||||
projection_pred.projection_ty.associated_ty
|
||||
))
|
||||
.name,
|
||||
)?;
|
||||
projection_pred.ty.hir_fmt(f)?;
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ use super::{
|
||||
};
|
||||
use crate::{
|
||||
db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode,
|
||||
AliasTy, Interner, TyKind,
|
||||
to_assoc_type_id, AliasTy, Interner, TyKind,
|
||||
};
|
||||
|
||||
pub(crate) use unify::unify;
|
||||
@ -382,7 +382,10 @@ impl<'a> InferenceContext<'a> {
|
||||
let trait_ref = TraitRef { trait_, substs: substs.clone() };
|
||||
let projection = ProjectionPredicate {
|
||||
ty: ty.clone(),
|
||||
projection_ty: ProjectionTy { associated_ty: res_assoc_ty, parameters: substs },
|
||||
projection_ty: ProjectionTy {
|
||||
associated_ty: to_assoc_type_id(res_assoc_ty),
|
||||
parameters: substs,
|
||||
},
|
||||
};
|
||||
self.obligations.push(Obligation::Trait(trait_ref));
|
||||
self.obligations.push(Obligation::Projection(projection));
|
||||
|
@ -18,7 +18,8 @@ use crate::{
|
||||
lower::lower_to_chalk_mutability,
|
||||
method_resolution, op,
|
||||
primitive::{self, UintTy},
|
||||
traits::{FnTrait, InEnvironment},
|
||||
to_assoc_type_id,
|
||||
traits::{chalk::from_chalk, FnTrait, InEnvironment},
|
||||
utils::{generics, variant_data, Generics},
|
||||
AdtId, Binders, CallableDefId, FnPointer, FnSig, Interner, Obligation, OpaqueTyId, Rawness,
|
||||
Scalar, Substs, TraitRef, Ty, TyKind,
|
||||
@ -97,8 +98,10 @@ impl<'a> InferenceContext<'a> {
|
||||
});
|
||||
if self.db.trait_solve(krate, goal.value).is_some() {
|
||||
self.obligations.push(implements_fn_trait);
|
||||
let output_proj_ty =
|
||||
crate::ProjectionTy { associated_ty: output_assoc_type, parameters: substs };
|
||||
let output_proj_ty = crate::ProjectionTy {
|
||||
associated_ty: to_assoc_type_id(output_assoc_type),
|
||||
parameters: substs,
|
||||
};
|
||||
let return_ty = self.normalize_projection_ty(output_proj_ty);
|
||||
Some((arg_tys, return_ty))
|
||||
} else {
|
||||
@ -929,8 +932,9 @@ impl<'a> InferenceContext<'a> {
|
||||
}
|
||||
|
||||
fn register_obligations_for_call(&mut self, callable_ty: &Ty) {
|
||||
if let TyKind::FnDef(def, parameters) = callable_ty.interned(&Interner) {
|
||||
let generic_predicates = self.db.generic_predicates((*def).into());
|
||||
if let TyKind::FnDef(fn_def, parameters) = callable_ty.interned(&Interner) {
|
||||
let def: CallableDefId = from_chalk(self.db, *fn_def);
|
||||
let generic_predicates = self.db.generic_predicates(def.into());
|
||||
for predicate in generic_predicates.iter() {
|
||||
let predicate = predicate.clone().subst(parameters);
|
||||
if let Some(obligation) = Obligation::from_predicate(predicate) {
|
||||
|
@ -51,6 +51,10 @@ pub use chalk_ir::{AdtId, BoundVar, DebruijnIndex, Mutability, Scalar, TyVariabl
|
||||
|
||||
pub use crate::traits::chalk::Interner;
|
||||
|
||||
pub type ForeignDefId = chalk_ir::ForeignDefId<Interner>;
|
||||
pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>;
|
||||
pub(crate) type FnDefId = chalk_ir::FnDefId<Interner>;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
||||
pub enum Lifetime {
|
||||
Parameter(LifetimeParamId),
|
||||
@ -68,7 +72,7 @@ pub struct OpaqueTy {
|
||||
/// trait and all its parameters are fully known.
|
||||
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
||||
pub struct ProjectionTy {
|
||||
pub associated_ty: TypeAliasId,
|
||||
pub associated_ty: AssocTypeId,
|
||||
pub parameters: Substs,
|
||||
}
|
||||
|
||||
@ -78,7 +82,7 @@ impl ProjectionTy {
|
||||
}
|
||||
|
||||
fn trait_(&self, db: &dyn HirDatabase) -> TraitId {
|
||||
match self.associated_ty.lookup(db.upcast()).container {
|
||||
match from_assoc_type_id(self.associated_ty).lookup(db.upcast()).container {
|
||||
AssocContainerId::TraitId(it) => it,
|
||||
_ => panic!("projection ty without parent trait"),
|
||||
}
|
||||
@ -139,7 +143,7 @@ pub enum TyKind {
|
||||
/// when we have tried to normalize a projection like `T::Item` but
|
||||
/// couldn't find a better representation. In that case, we generate
|
||||
/// an **application type** like `(Iterator::Item)<T>`.
|
||||
AssociatedType(TypeAliasId, Substs),
|
||||
AssociatedType(AssocTypeId, Substs),
|
||||
|
||||
/// a scalar type like `bool` or `u32`
|
||||
Scalar(Scalar),
|
||||
@ -179,7 +183,7 @@ pub enum TyKind {
|
||||
/// fn foo() -> i32 { 1 }
|
||||
/// let bar = foo; // bar: fn() -> i32 {foo}
|
||||
/// ```
|
||||
FnDef(CallableDefId, Substs),
|
||||
FnDef(FnDefId, Substs),
|
||||
|
||||
/// The pointee of a string slice. Written as `str`.
|
||||
Str,
|
||||
@ -194,7 +198,7 @@ pub enum TyKind {
|
||||
Closure(DefWithBodyId, ExprId, Substs),
|
||||
|
||||
/// Represents a foreign type declared in external blocks.
|
||||
ForeignType(TypeAliasId),
|
||||
ForeignType(ForeignDefId),
|
||||
|
||||
/// A pointer to a function. Written as `fn() -> i32`.
|
||||
///
|
||||
@ -700,12 +704,14 @@ impl Ty {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_generic_def(&self) -> Option<GenericDefId> {
|
||||
pub fn as_generic_def(&self, db: &dyn HirDatabase) -> Option<GenericDefId> {
|
||||
match *self.interned(&Interner) {
|
||||
TyKind::Adt(AdtId(adt), ..) => Some(adt.into()),
|
||||
TyKind::FnDef(callable, ..) => Some(callable.into()),
|
||||
TyKind::AssociatedType(type_alias, ..) => Some(type_alias.into()),
|
||||
TyKind::ForeignType(type_alias, ..) => Some(type_alias.into()),
|
||||
TyKind::FnDef(callable, ..) => {
|
||||
Some(db.lookup_intern_callable_def(callable.into()).into())
|
||||
}
|
||||
TyKind::AssociatedType(type_alias, ..) => Some(from_assoc_type_id(type_alias).into()),
|
||||
TyKind::ForeignType(type_alias, ..) => Some(from_foreign_def_id(type_alias).into()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@ -724,8 +730,10 @@ impl Ty {
|
||||
(TyKind::Slice(_), TyKind::Slice(_)) | (TyKind::Array(_), TyKind::Array(_)) => true,
|
||||
(TyKind::FnDef(def_id, ..), TyKind::FnDef(def_id2, ..)) => def_id == def_id2,
|
||||
(TyKind::OpaqueType(ty_id, ..), TyKind::OpaqueType(ty_id2, ..)) => ty_id == ty_id2,
|
||||
(TyKind::AssociatedType(ty_id, ..), TyKind::AssociatedType(ty_id2, ..))
|
||||
| (TyKind::ForeignType(ty_id, ..), TyKind::ForeignType(ty_id2, ..)) => ty_id == ty_id2,
|
||||
(TyKind::AssociatedType(ty_id, ..), TyKind::AssociatedType(ty_id2, ..)) => {
|
||||
ty_id == ty_id2
|
||||
}
|
||||
(TyKind::ForeignType(ty_id, ..), TyKind::ForeignType(ty_id2, ..)) => ty_id == ty_id2,
|
||||
(TyKind::Closure(def, expr, _), TyKind::Closure(def2, expr2, _)) => {
|
||||
expr == expr2 && def == def2
|
||||
}
|
||||
@ -770,18 +778,27 @@ impl Ty {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_fn_def(&self) -> Option<FunctionId> {
|
||||
pub fn callable_def(&self, db: &dyn HirDatabase) -> Option<CallableDefId> {
|
||||
match self.interned(&Interner) {
|
||||
&TyKind::FnDef(CallableDefId::FunctionId(func), ..) => Some(func),
|
||||
&TyKind::FnDef(def, ..) => Some(db.lookup_intern_callable_def(def.into())),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_fn_def(&self, db: &dyn HirDatabase) -> Option<FunctionId> {
|
||||
if let Some(CallableDefId::FunctionId(func)) = self.callable_def(db) {
|
||||
Some(func)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn callable_sig(&self, db: &dyn HirDatabase) -> Option<CallableSig> {
|
||||
match self.interned(&Interner) {
|
||||
TyKind::Function(fn_ptr) => Some(CallableSig::from_fn_ptr(fn_ptr)),
|
||||
TyKind::FnDef(def, parameters) => {
|
||||
let sig = db.callable_item_signature(*def);
|
||||
let callable_def = db.lookup_intern_callable_def((*def).into());
|
||||
let sig = db.callable_item_signature(callable_def);
|
||||
Some(sig.subst(¶meters))
|
||||
}
|
||||
TyKind::Closure(.., substs) => {
|
||||
@ -916,14 +933,15 @@ impl Ty {
|
||||
|
||||
pub fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<TraitId> {
|
||||
match self.interned(&Interner) {
|
||||
TyKind::AssociatedType(type_alias_id, ..) => {
|
||||
match type_alias_id.lookup(db.upcast()).container {
|
||||
TyKind::AssociatedType(id, ..) => {
|
||||
match from_assoc_type_id(*id).lookup(db.upcast()).container {
|
||||
AssocContainerId::TraitId(trait_id) => Some(trait_id),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
TyKind::Alias(AliasTy::Projection(projection_ty)) => {
|
||||
match projection_ty.associated_ty.lookup(db.upcast()).container {
|
||||
match from_assoc_type_id(projection_ty.associated_ty).lookup(db.upcast()).container
|
||||
{
|
||||
AssocContainerId::TraitId(trait_id) => Some(trait_id),
|
||||
_ => None,
|
||||
}
|
||||
@ -1116,3 +1134,19 @@ pub struct ReturnTypeImplTraits {
|
||||
pub(crate) struct ReturnTypeImplTrait {
|
||||
pub(crate) bounds: Binders<Vec<GenericPredicate>>,
|
||||
}
|
||||
|
||||
pub fn to_foreign_def_id(id: TypeAliasId) -> ForeignDefId {
|
||||
chalk_ir::ForeignDefId(salsa::InternKey::as_intern_id(&id))
|
||||
}
|
||||
|
||||
pub fn from_foreign_def_id(id: ForeignDefId) -> TypeAliasId {
|
||||
salsa::InternKey::from_intern_id(id.0)
|
||||
}
|
||||
|
||||
pub fn to_assoc_type_id(id: TypeAliasId) -> AssocTypeId {
|
||||
chalk_ir::AssocTypeId(salsa::InternKey::as_intern_id(&id))
|
||||
}
|
||||
|
||||
pub fn from_assoc_type_id(id: AssocTypeId) -> TypeAliasId {
|
||||
salsa::InternKey::from_intern_id(id.0)
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ use stdx::impl_from;
|
||||
|
||||
use crate::{
|
||||
db::HirDatabase,
|
||||
to_assoc_type_id,
|
||||
traits::chalk::{Interner, ToChalk},
|
||||
utils::{
|
||||
all_super_trait_refs, associated_type_by_name_including_super_traits, generics,
|
||||
@ -358,7 +359,7 @@ impl Ty {
|
||||
Some((super_trait_ref, associated_ty)) => {
|
||||
// FIXME handle type parameters on the segment
|
||||
TyKind::Alias(AliasTy::Projection(ProjectionTy {
|
||||
associated_ty,
|
||||
associated_ty: to_assoc_type_id(associated_ty),
|
||||
parameters: super_trait_ref.substs,
|
||||
}))
|
||||
.intern(&Interner)
|
||||
@ -487,7 +488,7 @@ impl Ty {
|
||||
// FIXME handle type parameters on the segment
|
||||
return Some(
|
||||
TyKind::Alias(AliasTy::Projection(ProjectionTy {
|
||||
associated_ty,
|
||||
associated_ty: to_assoc_type_id(associated_ty),
|
||||
parameters: substs,
|
||||
}))
|
||||
.intern(&Interner),
|
||||
@ -753,7 +754,10 @@ fn assoc_type_bindings_from_type_bound<'a>(
|
||||
None => return SmallVec::<[GenericPredicate; 1]>::new(),
|
||||
Some(t) => t,
|
||||
};
|
||||
let projection_ty = ProjectionTy { associated_ty, parameters: super_trait_ref.substs };
|
||||
let projection_ty = ProjectionTy {
|
||||
associated_ty: to_assoc_type_id(associated_ty),
|
||||
parameters: super_trait_ref.substs,
|
||||
};
|
||||
let mut preds = SmallVec::with_capacity(
|
||||
binding.type_ref.as_ref().map_or(0, |_| 1) + binding.bounds.len(),
|
||||
);
|
||||
@ -1060,7 +1064,10 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig {
|
||||
fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> {
|
||||
let generics = generics(db.upcast(), def.into());
|
||||
let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST);
|
||||
Binders::new(substs.len(), TyKind::FnDef(def.into(), substs).intern(&Interner))
|
||||
Binders::new(
|
||||
substs.len(),
|
||||
TyKind::FnDef(CallableDefId::FunctionId(def).to_chalk(db), substs).intern(&Interner),
|
||||
)
|
||||
}
|
||||
|
||||
/// Build the declared type of a const.
|
||||
@ -1103,7 +1110,10 @@ fn type_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> Binders<T
|
||||
}
|
||||
let generics = generics(db.upcast(), def.into());
|
||||
let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST);
|
||||
Binders::new(substs.len(), TyKind::FnDef(def.into(), substs).intern(&Interner))
|
||||
Binders::new(
|
||||
substs.len(),
|
||||
TyKind::FnDef(CallableDefId::StructId(def).to_chalk(db), substs).intern(&Interner),
|
||||
)
|
||||
}
|
||||
|
||||
fn fn_sig_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -> PolyFnSig {
|
||||
@ -1128,7 +1138,10 @@ fn type_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -
|
||||
}
|
||||
let generics = generics(db.upcast(), def.parent.into());
|
||||
let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST);
|
||||
Binders::new(substs.len(), TyKind::FnDef(def.into(), substs).intern(&Interner))
|
||||
Binders::new(
|
||||
substs.len(),
|
||||
TyKind::FnDef(CallableDefId::EnumVariantId(def).to_chalk(db), substs).intern(&Interner),
|
||||
)
|
||||
}
|
||||
|
||||
fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> {
|
||||
@ -1143,7 +1156,7 @@ fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> {
|
||||
let ctx =
|
||||
TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable);
|
||||
if db.type_alias_data(t).is_extern {
|
||||
Binders::new(0, TyKind::ForeignType(t).intern(&Interner))
|
||||
Binders::new(0, TyKind::ForeignType(crate::to_foreign_def_id(t)).intern(&Interner))
|
||||
} else {
|
||||
let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST);
|
||||
let type_ref = &db.type_alias_data(t).type_ref;
|
||||
|
@ -9,7 +9,7 @@ use base_db::CrateId;
|
||||
use chalk_ir::Mutability;
|
||||
use hir_def::{
|
||||
lang_item::LangItemTarget, AssocContainerId, AssocItemId, FunctionId, GenericDefId, HasModule,
|
||||
ImplId, Lookup, ModuleId, TraitId, TypeAliasId,
|
||||
ImplId, Lookup, ModuleId, TraitId,
|
||||
};
|
||||
use hir_expand::name::Name;
|
||||
use rustc_hash::{FxHashMap, FxHashSet};
|
||||
@ -17,10 +17,11 @@ use rustc_hash::{FxHashMap, FxHashSet};
|
||||
use crate::{
|
||||
autoderef,
|
||||
db::HirDatabase,
|
||||
from_foreign_def_id,
|
||||
primitive::{self, FloatTy, IntTy, UintTy},
|
||||
utils::all_super_traits,
|
||||
AdtId, Canonical, DebruijnIndex, FnPointer, FnSig, InEnvironment, Interner, Scalar, Substs,
|
||||
TraitEnvironment, TraitRef, Ty, TyKind, TypeWalk,
|
||||
AdtId, Canonical, DebruijnIndex, FnPointer, FnSig, ForeignDefId, InEnvironment, Interner,
|
||||
Scalar, Substs, TraitEnvironment, TraitRef, Ty, TyKind, TypeWalk,
|
||||
};
|
||||
|
||||
/// This is used as a key for indexing impls.
|
||||
@ -35,7 +36,7 @@ pub enum TyFingerprint {
|
||||
Adt(hir_def::AdtId),
|
||||
Dyn(TraitId),
|
||||
Tuple(usize),
|
||||
ForeignType(TypeAliasId),
|
||||
ForeignType(ForeignDefId),
|
||||
FnPtr(usize, FnSig),
|
||||
}
|
||||
|
||||
@ -236,8 +237,10 @@ impl Ty {
|
||||
TyKind::Adt(AdtId(def_id), _) => {
|
||||
return mod_to_crate_ids(def_id.module(db.upcast()));
|
||||
}
|
||||
TyKind::ForeignType(type_alias_id) => {
|
||||
return mod_to_crate_ids(type_alias_id.lookup(db.upcast()).module(db.upcast()));
|
||||
TyKind::ForeignType(id) => {
|
||||
return mod_to_crate_ids(
|
||||
from_foreign_def_id(*id).lookup(db.upcast()).module(db.upcast()),
|
||||
);
|
||||
}
|
||||
TyKind::Scalar(Scalar::Bool) => lang_item_crate!("bool"),
|
||||
TyKind::Scalar(Scalar::Char) => lang_item_crate!("char"),
|
||||
|
@ -17,14 +17,15 @@ use super::ChalkContext;
|
||||
use crate::{
|
||||
db::HirDatabase,
|
||||
display::HirDisplay,
|
||||
from_assoc_type_id,
|
||||
method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS},
|
||||
to_assoc_type_id,
|
||||
utils::generics,
|
||||
BoundVar, CallableDefId, CallableSig, DebruijnIndex, GenericPredicate, ProjectionPredicate,
|
||||
ProjectionTy, Substs, TraitRef, Ty, TyKind,
|
||||
BoundVar, CallableDefId, CallableSig, DebruijnIndex, FnDefId, GenericPredicate,
|
||||
ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TyKind,
|
||||
};
|
||||
use mapping::{
|
||||
convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsAssocType,
|
||||
TypeAliasAsValue,
|
||||
convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue,
|
||||
};
|
||||
|
||||
pub use self::interner::Interner;
|
||||
@ -234,7 +235,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
|
||||
ty: TyKind::BoundVar(BoundVar { debruijn: DebruijnIndex::ONE, index: 0 })
|
||||
.intern(&Interner),
|
||||
projection_ty: ProjectionTy {
|
||||
associated_ty: future_output,
|
||||
associated_ty: to_assoc_type_id(future_output),
|
||||
// Self type as the first parameter.
|
||||
parameters: Substs::single(
|
||||
TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0))
|
||||
@ -383,7 +384,7 @@ pub(crate) fn associated_ty_data_query(
|
||||
id: AssocTypeId,
|
||||
) -> Arc<AssociatedTyDatum> {
|
||||
debug!("associated_ty_data {:?}", id);
|
||||
let type_alias: TypeAliasId = from_chalk::<TypeAliasAsAssocType, _>(db, id).0;
|
||||
let type_alias: TypeAliasId = from_assoc_type_id(id);
|
||||
let trait_ = match type_alias.lookup(db.upcast()).container {
|
||||
AssocContainerId::TraitId(t) => t,
|
||||
_ => panic!("associated type not in trait"),
|
||||
@ -438,10 +439,8 @@ pub(crate) fn trait_datum_query(
|
||||
fundamental: false,
|
||||
};
|
||||
let where_clauses = convert_where_clauses(db, trait_.into(), &bound_vars);
|
||||
let associated_ty_ids = trait_data
|
||||
.associated_types()
|
||||
.map(|type_alias| TypeAliasAsAssocType(type_alias).to_chalk(db))
|
||||
.collect();
|
||||
let associated_ty_ids =
|
||||
trait_data.associated_types().map(|type_alias| to_assoc_type_id(type_alias)).collect();
|
||||
let trait_datum_bound = rust_ir::TraitDatumBound { where_clauses };
|
||||
let well_known =
|
||||
lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name));
|
||||
@ -623,7 +622,7 @@ fn type_alias_associated_ty_value(
|
||||
let value_bound = rust_ir::AssociatedTyValueBound { ty: ty.value.to_chalk(db) };
|
||||
let value = rust_ir::AssociatedTyValue {
|
||||
impl_id: impl_id.to_chalk(db),
|
||||
associated_ty_id: TypeAliasAsAssocType(assoc_ty).to_chalk(db),
|
||||
associated_ty_id: to_assoc_type_id(assoc_ty),
|
||||
value: make_binders(value_bound, ty.num_binders),
|
||||
};
|
||||
Arc::new(value)
|
||||
|
@ -12,7 +12,6 @@ pub struct Interner;
|
||||
|
||||
pub(crate) type AssocTypeId = chalk_ir::AssocTypeId<Interner>;
|
||||
pub(crate) type AssociatedTyDatum = chalk_solve::rust_ir::AssociatedTyDatum<Interner>;
|
||||
pub(crate) type ForeignDefId = chalk_ir::ForeignDefId<Interner>;
|
||||
pub(crate) type TraitId = chalk_ir::TraitId<Interner>;
|
||||
pub(crate) type TraitDatum = chalk_solve::rust_ir::TraitDatum<Interner>;
|
||||
pub(crate) type AdtId = chalk_ir::AdtId<Interner>;
|
||||
@ -21,7 +20,6 @@ pub(crate) type ImplId = chalk_ir::ImplId<Interner>;
|
||||
pub(crate) type ImplDatum = chalk_solve::rust_ir::ImplDatum<Interner>;
|
||||
pub(crate) type AssociatedTyValueId = chalk_solve::rust_ir::AssociatedTyValueId<Interner>;
|
||||
pub(crate) type AssociatedTyValue = chalk_solve::rust_ir::AssociatedTyValue<Interner>;
|
||||
pub(crate) type FnDefId = chalk_ir::FnDefId<Interner>;
|
||||
pub(crate) type FnDefDatum = chalk_solve::rust_ir::FnDefDatum<Interner>;
|
||||
pub(crate) type OpaqueTyId = chalk_ir::OpaqueTyId<Interner>;
|
||||
pub(crate) type OpaqueTyDatum = chalk_solve::rust_ir::OpaqueTyDatum<Interner>;
|
||||
|
@ -14,6 +14,7 @@ use hir_def::{AssocContainerId, GenericDefId, Lookup, TypeAliasId};
|
||||
|
||||
use crate::{
|
||||
db::HirDatabase,
|
||||
from_assoc_type_id,
|
||||
primitive::UintTy,
|
||||
traits::{Canonical, Obligation},
|
||||
AliasTy, CallableDefId, FnPointer, FnSig, GenericPredicate, InEnvironment, OpaqueTy,
|
||||
@ -38,9 +39,7 @@ impl ToChalk for Ty {
|
||||
})
|
||||
.intern(&Interner)
|
||||
}
|
||||
TyKind::AssociatedType(type_alias, substs) => {
|
||||
let assoc_type = TypeAliasAsAssocType(type_alias);
|
||||
let assoc_type_id = assoc_type.to_chalk(db);
|
||||
TyKind::AssociatedType(assoc_type_id, substs) => {
|
||||
let substitution = substs.to_chalk(db);
|
||||
chalk_ir::TyKind::AssociatedType(assoc_type_id, substitution).intern(&Interner)
|
||||
}
|
||||
@ -51,11 +50,7 @@ impl ToChalk for Ty {
|
||||
chalk_ir::TyKind::OpaqueType(id, substitution).intern(&Interner)
|
||||
}
|
||||
|
||||
TyKind::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)
|
||||
}
|
||||
TyKind::ForeignType(id) => chalk_ir::TyKind::Foreign(id).intern(&Interner),
|
||||
|
||||
TyKind::Scalar(scalar) => chalk_ir::TyKind::Scalar(scalar).intern(&Interner),
|
||||
|
||||
@ -71,8 +66,7 @@ impl ToChalk for Ty {
|
||||
chalk_ir::TyKind::Slice(substs[0].clone().to_chalk(db)).intern(&Interner)
|
||||
}
|
||||
TyKind::Str => chalk_ir::TyKind::Str.intern(&Interner),
|
||||
TyKind::FnDef(callable_def, substs) => {
|
||||
let id = callable_def.to_chalk(db);
|
||||
TyKind::FnDef(id, substs) => {
|
||||
let substitution = substs.to_chalk(db);
|
||||
chalk_ir::TyKind::FnDef(id, substitution).intern(&Interner)
|
||||
}
|
||||
@ -89,7 +83,7 @@ impl ToChalk for Ty {
|
||||
chalk_ir::TyKind::Adt(adt_id, substitution).intern(&Interner)
|
||||
}
|
||||
TyKind::Alias(AliasTy::Projection(proj_ty)) => {
|
||||
let associated_ty_id = TypeAliasAsAssocType(proj_ty.associated_ty).to_chalk(db);
|
||||
let associated_ty_id = proj_ty.associated_ty;
|
||||
let substitution = proj_ty.parameters.to_chalk(db);
|
||||
chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy {
|
||||
associated_ty_id,
|
||||
@ -143,8 +137,7 @@ impl ToChalk for Ty {
|
||||
TyKind::Placeholder(db.lookup_intern_type_param_id(interned_id))
|
||||
}
|
||||
chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => {
|
||||
let associated_ty =
|
||||
from_chalk::<TypeAliasAsAssocType, _>(db, proj.associated_ty_id).0;
|
||||
let associated_ty = proj.associated_ty_id;
|
||||
let parameters = from_chalk(db, proj.substitution);
|
||||
TyKind::Alias(AliasTy::Projection(ProjectionTy { associated_ty, parameters }))
|
||||
}
|
||||
@ -184,10 +177,9 @@ impl ToChalk for Ty {
|
||||
}
|
||||
|
||||
chalk_ir::TyKind::Adt(adt_id, subst) => TyKind::Adt(adt_id, from_chalk(db, subst)),
|
||||
chalk_ir::TyKind::AssociatedType(type_id, subst) => TyKind::AssociatedType(
|
||||
from_chalk::<TypeAliasAsAssocType, _>(db, type_id).0,
|
||||
from_chalk(db, subst),
|
||||
),
|
||||
chalk_ir::TyKind::AssociatedType(type_id, subst) => {
|
||||
TyKind::AssociatedType(type_id, from_chalk(db, subst))
|
||||
}
|
||||
|
||||
chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => {
|
||||
TyKind::OpaqueType(from_chalk(db, opaque_type_id), from_chalk(db, subst))
|
||||
@ -208,7 +200,7 @@ impl ToChalk for Ty {
|
||||
chalk_ir::TyKind::Never => TyKind::Never,
|
||||
|
||||
chalk_ir::TyKind::FnDef(fn_def_id, subst) => {
|
||||
TyKind::FnDef(from_chalk(db, fn_def_id), from_chalk(db, subst))
|
||||
TyKind::FnDef(fn_def_id, from_chalk(db, subst))
|
||||
}
|
||||
|
||||
chalk_ir::TyKind::Closure(id, subst) => {
|
||||
@ -217,9 +209,7 @@ impl ToChalk for Ty {
|
||||
TyKind::Closure(def, expr, from_chalk(db, subst))
|
||||
}
|
||||
|
||||
chalk_ir::TyKind::Foreign(foreign_def_id) => {
|
||||
TyKind::ForeignType(from_chalk::<TypeAliasAsForeignType, _>(db, foreign_def_id).0)
|
||||
}
|
||||
chalk_ir::TyKind::Foreign(foreign_def_id) => TyKind::ForeignType(foreign_def_id),
|
||||
chalk_ir::TyKind::Generator(_, _) => unimplemented!(), // FIXME
|
||||
chalk_ir::TyKind::GeneratorWitness(_, _) => unimplemented!(), // FIXME
|
||||
}
|
||||
@ -338,34 +328,6 @@ impl ToChalk for CallableDefId {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct TypeAliasAsAssocType(pub(crate) TypeAliasId);
|
||||
|
||||
impl ToChalk for TypeAliasAsAssocType {
|
||||
type Chalk = AssocTypeId;
|
||||
|
||||
fn to_chalk(self, _db: &dyn HirDatabase) -> AssocTypeId {
|
||||
chalk_ir::AssocTypeId(self.0.as_intern_id())
|
||||
}
|
||||
|
||||
fn from_chalk(_db: &dyn HirDatabase, assoc_type_id: AssocTypeId) -> TypeAliasAsAssocType {
|
||||
TypeAliasAsAssocType(InternKey::from_intern_id(assoc_type_id.0))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct TypeAliasAsForeignType(pub(crate) TypeAliasId);
|
||||
|
||||
impl ToChalk for TypeAliasAsForeignType {
|
||||
type Chalk = ForeignDefId;
|
||||
|
||||
fn to_chalk(self, _db: &dyn HirDatabase) -> ForeignDefId {
|
||||
chalk_ir::ForeignDefId(self.0.as_intern_id())
|
||||
}
|
||||
|
||||
fn from_chalk(_db: &dyn HirDatabase, foreign_def_id: ForeignDefId) -> TypeAliasAsForeignType {
|
||||
TypeAliasAsForeignType(InternKey::from_intern_id(foreign_def_id.0))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct TypeAliasAsValue(pub(crate) TypeAliasId);
|
||||
|
||||
impl ToChalk for TypeAliasAsValue {
|
||||
@ -447,7 +409,7 @@ impl ToChalk for ProjectionTy {
|
||||
|
||||
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::ProjectionTy<Interner> {
|
||||
chalk_ir::ProjectionTy {
|
||||
associated_ty_id: TypeAliasAsAssocType(self.associated_ty).to_chalk(db),
|
||||
associated_ty_id: self.associated_ty,
|
||||
substitution: self.parameters.to_chalk(db),
|
||||
}
|
||||
}
|
||||
@ -457,11 +419,7 @@ impl ToChalk for ProjectionTy {
|
||||
projection_ty: chalk_ir::ProjectionTy<Interner>,
|
||||
) -> ProjectionTy {
|
||||
ProjectionTy {
|
||||
associated_ty: from_chalk::<TypeAliasAsAssocType, _>(
|
||||
db,
|
||||
projection_ty.associated_ty_id,
|
||||
)
|
||||
.0,
|
||||
associated_ty: projection_ty.associated_ty_id,
|
||||
parameters: from_chalk(db, projection_ty.substitution),
|
||||
}
|
||||
}
|
||||
@ -615,7 +573,10 @@ pub(super) fn generic_predicate_to_inline_bound(
|
||||
if &proj.projection_ty.parameters[0] != self_ty {
|
||||
return None;
|
||||
}
|
||||
let trait_ = match proj.projection_ty.associated_ty.lookup(db.upcast()).container {
|
||||
let trait_ = match from_assoc_type_id(proj.projection_ty.associated_ty)
|
||||
.lookup(db.upcast())
|
||||
.container
|
||||
{
|
||||
AssocContainerId::TraitId(t) => t,
|
||||
_ => panic!("associated type not in trait"),
|
||||
};
|
||||
@ -626,8 +587,7 @@ pub(super) fn generic_predicate_to_inline_bound(
|
||||
let alias_eq_bound = rust_ir::AliasEqBound {
|
||||
value: proj.ty.clone().to_chalk(db),
|
||||
trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self },
|
||||
associated_ty_id: TypeAliasAsAssocType(proj.projection_ty.associated_ty)
|
||||
.to_chalk(db),
|
||||
associated_ty_id: proj.projection_ty.associated_ty,
|
||||
parameters: Vec::new(), // FIXME we don't support generic associated types yet
|
||||
};
|
||||
Some(rust_ir::InlineBound::AliasEqBound(alias_eq_bound))
|
||||
|
@ -4,8 +4,8 @@ use std::fmt;
|
||||
use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplication};
|
||||
use itertools::Itertools;
|
||||
|
||||
use super::{from_chalk, Interner, TypeAliasAsAssocType};
|
||||
use crate::{db::HirDatabase, CallableDefId};
|
||||
use super::{from_chalk, Interner};
|
||||
use crate::{db::HirDatabase, from_assoc_type_id, CallableDefId};
|
||||
use hir_def::{AdtId, AssocContainerId, Lookup, TypeAliasId};
|
||||
|
||||
pub(crate) use unsafe_tls::{set_current_program, with_current_program};
|
||||
@ -41,7 +41,7 @@ impl DebugContext<'_> {
|
||||
id: super::AssocTypeId,
|
||||
fmt: &mut fmt::Formatter<'_>,
|
||||
) -> Result<(), fmt::Error> {
|
||||
let type_alias: TypeAliasId = from_chalk::<TypeAliasAsAssocType, _>(self.0, id).0;
|
||||
let type_alias: TypeAliasId = from_assoc_type_id(id);
|
||||
let type_alias_data = self.0.type_alias_data(type_alias);
|
||||
let trait_ = match type_alias.lookup(self.0.upcast()).container {
|
||||
AssocContainerId::TraitId(t) => t,
|
||||
@ -75,8 +75,7 @@ impl DebugContext<'_> {
|
||||
projection_ty: &chalk_ir::ProjectionTy<Interner>,
|
||||
fmt: &mut fmt::Formatter<'_>,
|
||||
) -> Result<(), fmt::Error> {
|
||||
let type_alias: TypeAliasId =
|
||||
from_chalk::<TypeAliasAsAssocType, _>(self.0, projection_ty.associated_ty_id).0;
|
||||
let type_alias = from_assoc_type_id(projection_ty.associated_ty_id);
|
||||
let type_alias_data = self.0.type_alias_data(type_alias);
|
||||
let trait_ = match type_alias.lookup(self.0.upcast()).container {
|
||||
AssocContainerId::TraitId(t) => t,
|
||||
|
Loading…
x
Reference in New Issue
Block a user