8016: More Chalk adaptations r=flodiebold a=flodiebold

 - rename a bunch of fields
 - use `chalk_ir::FnSig`

Co-authored-by: Florian Diebold <flodiebold@gmail.com>
This commit is contained in:
bors[bot] 2021-03-14 15:34:37 +00:00 committed by GitHub
commit 406e4be04c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 89 additions and 84 deletions

View File

@ -1686,8 +1686,8 @@ impl Type {
.build();
let predicate = ProjectionPredicate {
projection_ty: ProjectionTy {
associated_ty: to_assoc_type_id(alias.id),
parameters: subst,
associated_ty_id: to_assoc_type_id(alias.id),
substitution: subst,
},
ty: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)).intern(&Interner),
};
@ -1979,7 +1979,7 @@ impl Type {
walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb);
}
walk_substs(db, type_, &opaque_ty.parameters, cb);
walk_substs(db, type_, &opaque_ty.substitution, cb);
}
TyKind::Placeholder(_) => {
if let Some(bounds) = ty.impl_trait_bounds(db) {

View File

@ -84,7 +84,10 @@ 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: to_assoc_type_id(target), parameters },
projection_ty: super::ProjectionTy {
associated_ty_id: to_assoc_type_id(target),
substitution: parameters,
},
};
let obligation = super::Obligation::Projection(projection);

View File

@ -245,19 +245,19 @@ impl HirDisplay for ProjectionTy {
}
let trait_ = f.db.trait_data(self.trait_(f.db));
let first_parameter = self.parameters[0].into_displayable(
let first_parameter = self.substitution[0].into_displayable(
f.db,
f.max_size,
f.omit_verbose_types,
f.display_target,
);
write!(f, "<{} as {}", first_parameter, trait_.name)?;
if self.parameters.len() > 1 {
if self.substitution.len() > 1 {
write!(f, "<")?;
f.write_joined(&self.parameters[1..], ", ")?;
f.write_joined(&self.substitution[1..], ", ")?;
write!(f, ">")?;
}
write!(f, ">::{}", f.db.type_alias_data(from_assoc_type_id(self.associated_ty)).name)?;
write!(f, ">::{}", f.db.type_alias_data(from_assoc_type_id(self.associated_ty_id)).name)?;
Ok(())
}
}
@ -319,7 +319,10 @@ impl HirDisplay for Ty {
TyKind::Dyn(predicates) if predicates.len() > 1 => {
Cow::Borrowed(predicates.as_ref())
}
&TyKind::Alias(AliasTy::Opaque(OpaqueTy { opaque_ty_id, ref parameters })) => {
&TyKind::Alias(AliasTy::Opaque(OpaqueTy {
opaque_ty_id,
substitution: ref parameters,
})) => {
let impl_trait_id = f.db.lookup_intern_impl_trait_id(opaque_ty_id.into());
if let ImplTraitId::ReturnTypeImplTrait(func, idx) = impl_trait_id {
datas =
@ -491,8 +494,8 @@ impl HirDisplay for Ty {
}
} else {
let projection_ty = ProjectionTy {
associated_ty: to_assoc_type_id(type_alias),
parameters: parameters.clone(),
associated_ty_id: to_assoc_type_id(type_alias),
substitution: parameters.clone(),
};
projection_ty.hir_fmt(f)?;
@ -579,7 +582,7 @@ impl HirDisplay for Ty {
let data = (*datas)
.as_ref()
.map(|rpit| rpit.impl_traits[idx as usize].bounds.clone());
let bounds = data.subst(&opaque_ty.parameters);
let bounds = data.subst(&opaque_ty.substitution);
write_bounds_like_dyn_trait_with_prefix("impl", &bounds.value, f)?;
}
ImplTraitId::AsyncBlockTypeImplTrait(..) => {
@ -709,7 +712,7 @@ fn write_bounds_like_dyn_trait(
angle_open = true;
}
let type_alias = f.db.type_alias_data(from_assoc_type_id(
projection_pred.projection_ty.associated_ty,
projection_pred.projection_ty.associated_ty_id,
));
write!(f, "{} = ", type_alias.name)?;
projection_pred.ty.hir_fmt(f)?;
@ -782,7 +785,7 @@ impl HirDisplay for GenericPredicate {
f,
">::{} = ",
f.db.type_alias_data(from_assoc_type_id(
projection_pred.projection_ty.associated_ty
projection_pred.projection_ty.associated_ty_id
))
.name,
)?;

View File

@ -385,8 +385,8 @@ impl<'a> InferenceContext<'a> {
let projection = ProjectionPredicate {
ty: ty.clone(),
projection_ty: ProjectionTy {
associated_ty: to_assoc_type_id(res_assoc_ty),
parameters: substs,
associated_ty_id: to_assoc_type_id(res_assoc_ty),
substitution: substs,
},
};
self.obligations.push(Obligation::Trait(trait_ref));

View File

@ -99,8 +99,8 @@ 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: to_assoc_type_id(output_assoc_type),
parameters: substs,
associated_ty_id: to_assoc_type_id(output_assoc_type),
substitution: substs,
};
let return_ty = self.normalize_projection_ty(output_proj_ty);
Some((arg_tys, return_ty))
@ -261,7 +261,7 @@ impl<'a> InferenceContext<'a> {
sig_tys.push(ret_ty.clone());
let sig_ty = TyKind::Function(FnPointer {
num_args: sig_tys.len() - 1,
sig: FnSig { variadic: false },
sig: FnSig { abi: (), safety: chalk_ir::Safety::Safe, variadic: false },
substs: Substs(sig_tys.clone().into()),
})
.intern(&Interner);

View File

@ -381,11 +381,11 @@ impl InferenceTable {
self.unify_substs(&tr1.substs, &tr2.substs, depth + 1)
}
(GenericPredicate::Projection(proj1), GenericPredicate::Projection(proj2))
if proj1.projection_ty.associated_ty == proj2.projection_ty.associated_ty =>
if proj1.projection_ty.associated_ty_id == proj2.projection_ty.associated_ty_id =>
{
self.unify_substs(
&proj1.projection_ty.parameters,
&proj2.projection_ty.parameters,
&proj1.projection_ty.substitution,
&proj2.projection_ty.substitution,
depth + 1,
) && self.unify_inner(&proj1.ty, &proj2.ty, depth + 1)
}

View File

@ -46,7 +46,7 @@ pub use lower::{
};
pub use traits::{InEnvironment, Obligation, ProjectionPredicate, TraitEnvironment};
pub use chalk_ir::{AdtId, BoundVar, DebruijnIndex, Mutability, Scalar, TyVariableKind};
pub use chalk_ir::{AdtId, BoundVar, DebruijnIndex, Mutability, Safety, Scalar, TyVariableKind};
pub use crate::traits::chalk::Interner;
@ -66,7 +66,7 @@ pub enum Lifetime {
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub struct OpaqueTy {
pub opaque_ty_id: OpaqueTyId,
pub parameters: Substs,
pub substitution: Substs,
}
/// A "projection" type corresponds to an (unnormalized)
@ -74,17 +74,17 @@ pub struct OpaqueTy {
/// trait and all its parameters are fully known.
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub struct ProjectionTy {
pub associated_ty: AssocTypeId,
pub parameters: Substs,
pub associated_ty_id: AssocTypeId,
pub substitution: Substs,
}
impl ProjectionTy {
pub fn trait_ref(&self, db: &dyn HirDatabase) -> TraitRef {
TraitRef { trait_: self.trait_(db), substs: self.parameters.clone() }
TraitRef { trait_: self.trait_(db), substs: self.substitution.clone() }
}
fn trait_(&self, db: &dyn HirDatabase) -> TraitId {
match from_assoc_type_id(self.associated_ty).lookup(db.upcast()).container {
match from_assoc_type_id(self.associated_ty_id).lookup(db.upcast()).container {
AssocContainerId::TraitId(it) => it,
_ => panic!("projection ty without parent trait"),
}
@ -93,7 +93,7 @@ impl ProjectionTy {
impl TypeWalk for ProjectionTy {
fn walk(&self, f: &mut impl FnMut(&Ty)) {
self.parameters.walk(f);
self.substitution.walk(f);
}
fn walk_mut_binders(
@ -101,14 +101,11 @@ impl TypeWalk for ProjectionTy {
f: &mut impl FnMut(&mut Ty, DebruijnIndex),
binders: DebruijnIndex,
) {
self.parameters.walk_mut_binders(f, binders);
self.substitution.walk_mut_binders(f, binders);
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub struct FnSig {
pub variadic: bool,
}
pub type FnSig = chalk_ir::FnSig<Interner>;
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub struct FnPointer {
@ -643,7 +640,7 @@ impl Ty {
pub fn fn_ptr(sig: CallableSig) -> Self {
TyKind::Function(FnPointer {
num_args: sig.params().len(),
sig: FnSig { variadic: sig.is_varargs },
sig: FnSig { abi: (), safety: Safety::Safe, variadic: sig.is_varargs },
substs: Substs(sig.params_and_return),
})
.intern(&Interner)
@ -906,7 +903,7 @@ impl Ty {
let data = (*it)
.as_ref()
.map(|rpit| rpit.impl_traits[idx as usize].bounds.clone());
data.subst(&opaque_ty.parameters)
data.subst(&opaque_ty.substitution)
})
}
// It always has an parameter for Future::Output type.
@ -945,7 +942,9 @@ impl Ty {
}
}
TyKind::Alias(AliasTy::Projection(projection_ty)) => {
match from_assoc_type_id(projection_ty.associated_ty).lookup(db.upcast()).container
match from_assoc_type_id(projection_ty.associated_ty_id)
.lookup(db.upcast())
.container
{
AssocContainerId::TraitId(trait_id) => Some(trait_id),
_ => None,
@ -1055,12 +1054,12 @@ impl TypeWalk for Ty {
fn walk(&self, f: &mut impl FnMut(&Ty)) {
match self.interned(&Interner) {
TyKind::Alias(AliasTy::Projection(p_ty)) => {
for t in p_ty.parameters.iter() {
for t in p_ty.substitution.iter() {
t.walk(f);
}
}
TyKind::Alias(AliasTy::Opaque(o_ty)) => {
for t in o_ty.parameters.iter() {
for t in o_ty.substitution.iter() {
t.walk(f);
}
}
@ -1087,7 +1086,7 @@ impl TypeWalk for Ty {
) {
match &mut self.0 {
TyKind::Alias(AliasTy::Projection(p_ty)) => {
p_ty.parameters.walk_mut_binders(f, binders);
p_ty.substitution.walk_mut_binders(f, binders);
}
TyKind::Dyn(predicates) => {
for p in make_mut_slice(predicates) {
@ -1095,7 +1094,7 @@ impl TypeWalk for Ty {
}
}
TyKind::Alias(AliasTy::Opaque(o_ty)) => {
o_ty.parameters.walk_mut_binders(f, binders);
o_ty.substitution.walk_mut_binders(f, binders);
}
_ => {
if let Some(substs) = self.substs_mut() {

View File

@ -8,7 +8,7 @@
use std::{iter, sync::Arc};
use base_db::CrateId;
use chalk_ir::{cast::Cast, Mutability};
use chalk_ir::{cast::Cast, Mutability, Safety};
use hir_def::{
adt::StructKind,
builtin_type::BuiltinType,
@ -181,7 +181,7 @@ impl<'a> TyLoweringContext<'a> {
let substs = Substs(params.iter().map(|tr| self.lower_ty(tr)).collect());
TyKind::Function(FnPointer {
num_args: substs.len() - 1,
sig: FnSig { variadic: *is_varargs },
sig: FnSig { abi: (), safety: Safety::Safe, variadic: *is_varargs },
substs,
})
.intern(&Interner)
@ -230,8 +230,11 @@ impl<'a> TyLoweringContext<'a> {
let opaque_ty_id = self.db.intern_impl_trait_id(impl_trait_id).into();
let generics = generics(self.db.upcast(), func.into());
let parameters = Substs::bound_vars(&generics, self.in_binders);
TyKind::Alias(AliasTy::Opaque(OpaqueTy { opaque_ty_id, parameters }))
.intern(&Interner)
TyKind::Alias(AliasTy::Opaque(OpaqueTy {
opaque_ty_id,
substitution: parameters,
}))
.intern(&Interner)
}
ImplTraitLoweringMode::Param => {
let idx = self.impl_trait_counter.get();
@ -357,8 +360,8 @@ impl<'a> TyLoweringContext<'a> {
Some((super_trait_ref, associated_ty)) => {
// FIXME handle type parameters on the segment
TyKind::Alias(AliasTy::Projection(ProjectionTy {
associated_ty: to_assoc_type_id(associated_ty),
parameters: super_trait_ref.substs,
associated_ty_id: to_assoc_type_id(associated_ty),
substitution: super_trait_ref.substs,
}))
.intern(&Interner)
}
@ -478,8 +481,8 @@ impl<'a> TyLoweringContext<'a> {
// FIXME handle type parameters on the segment
return Some(
TyKind::Alias(AliasTy::Projection(ProjectionTy {
associated_ty: to_assoc_type_id(associated_ty),
parameters: substs,
associated_ty_id: to_assoc_type_id(associated_ty),
substitution: substs,
}))
.intern(&Interner),
);
@ -736,8 +739,8 @@ impl<'a> TyLoweringContext<'a> {
Some(t) => t,
};
let projection_ty = ProjectionTy {
associated_ty: to_assoc_type_id(associated_ty),
parameters: super_trait_ref.substs,
associated_ty_id: to_assoc_type_id(associated_ty),
substitution: super_trait_ref.substs,
};
let mut preds = SmallVec::with_capacity(
binding.type_ref.as_ref().map_or(0, |_| 1) + binding.bounds.len(),

View File

@ -143,7 +143,7 @@ pub(crate) fn trait_solve_query(
log::info!("trait_solve_query({})", goal.value.value.display(db));
if let Obligation::Projection(pred) = &goal.value.value {
if let TyKind::BoundVar(_) = &pred.projection_ty.parameters[0].interned(&Interner) {
if let TyKind::BoundVar(_) = &pred.projection_ty.substitution[0].interned(&Interner) {
// Hack: don't ask Chalk to normalize with an unknown self type, it'll say that's impossible
return Some(Solution::Ambig(Guidance::Unknown));
}

View File

@ -234,9 +234,9 @@ 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: to_assoc_type_id(future_output),
associated_ty_id: to_assoc_type_id(future_output),
// Self type as the first parameter.
parameters: Substs::single(
substitution: Substs::single(
TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0))
.intern(&Interner),
),

View File

@ -14,7 +14,7 @@ use crate::{
from_assoc_type_id,
primitive::UintTy,
traits::{Canonical, Obligation},
AliasTy, CallableDefId, FnPointer, FnSig, GenericPredicate, InEnvironment, OpaqueTy,
AliasTy, CallableDefId, FnPointer, GenericPredicate, InEnvironment, OpaqueTy,
ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitRef, Ty,
};
@ -27,11 +27,11 @@ impl ToChalk for Ty {
match self.0 {
TyKind::Ref(m, parameters) => ref_to_chalk(db, m, parameters),
TyKind::Array(parameters) => array_to_chalk(db, parameters),
TyKind::Function(FnPointer { sig: FnSig { variadic }, substs, .. }) => {
TyKind::Function(FnPointer { sig, substs, .. }) => {
let substitution = chalk_ir::FnSubst(substs.to_chalk(db).shifted_in(&Interner));
chalk_ir::TyKind::Function(chalk_ir::FnPointer {
num_binders: 0,
sig: chalk_ir::FnSig { abi: (), safety: chalk_ir::Safety::Safe, variadic },
sig,
substitution,
})
.intern(&Interner)
@ -78,8 +78,8 @@ impl ToChalk for Ty {
chalk_ir::TyKind::Adt(adt_id, substitution).intern(&Interner)
}
TyKind::Alias(AliasTy::Projection(proj_ty)) => {
let associated_ty_id = proj_ty.associated_ty;
let substitution = proj_ty.parameters.to_chalk(db);
let associated_ty_id = proj_ty.associated_ty_id;
let substitution = proj_ty.substitution.to_chalk(db);
chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy {
associated_ty_id,
substitution,
@ -87,6 +87,13 @@ impl ToChalk for Ty {
.cast(&Interner)
.intern(&Interner)
}
TyKind::Alias(AliasTy::Opaque(opaque_ty)) => {
let opaque_ty_id = opaque_ty.opaque_ty_id;
let substitution = opaque_ty.substitution.to_chalk(db);
chalk_ir::AliasTy::Opaque(chalk_ir::OpaqueTy { opaque_ty_id, substitution })
.cast(&Interner)
.intern(&Interner)
}
TyKind::Placeholder(idx) => idx.to_ty::<Interner>(&Interner),
TyKind::BoundVar(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner),
TyKind::InferenceVar(..) => panic!("uncanonicalized infer ty"),
@ -101,15 +108,6 @@ impl ToChalk for Ty {
};
chalk_ir::TyKind::Dyn(bounded_ty).intern(&Interner)
}
TyKind::Alias(AliasTy::Opaque(opaque_ty)) => {
let opaque_ty_id = opaque_ty.opaque_ty_id;
let substitution = opaque_ty.parameters.to_chalk(db);
chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(chalk_ir::OpaqueTy {
opaque_ty_id,
substitution,
}))
.intern(&Interner)
}
TyKind::Unknown => chalk_ir::TyKind::Error.intern(&Interner),
}
}
@ -121,16 +119,19 @@ impl ToChalk for Ty {
chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => {
let associated_ty = proj.associated_ty_id;
let parameters = from_chalk(db, proj.substitution);
TyKind::Alias(AliasTy::Projection(ProjectionTy { associated_ty, parameters }))
TyKind::Alias(AliasTy::Projection(ProjectionTy {
associated_ty_id: associated_ty,
substitution: parameters,
}))
}
chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(opaque_ty)) => {
let opaque_ty_id = opaque_ty.opaque_ty_id;
let parameters = from_chalk(db, opaque_ty.substitution);
TyKind::Alias(AliasTy::Opaque(OpaqueTy { opaque_ty_id, parameters }))
TyKind::Alias(AliasTy::Opaque(OpaqueTy { opaque_ty_id, substitution: parameters }))
}
chalk_ir::TyKind::Function(chalk_ir::FnPointer {
num_binders,
sig: chalk_ir::FnSig { variadic, .. },
sig,
substitution,
..
}) => {
@ -139,11 +140,7 @@ impl ToChalk for Ty {
db,
substitution.0.shifted_out(&Interner).expect("fn ptr should have no binders"),
);
TyKind::Function(FnPointer {
num_args: (substs.len() - 1),
sig: FnSig { variadic },
substs,
})
TyKind::Function(FnPointer { num_args: (substs.len() - 1), sig, substs })
}
chalk_ir::TyKind::BoundVar(idx) => TyKind::BoundVar(idx),
chalk_ir::TyKind::InferenceVar(_iv, _kind) => TyKind::Unknown,
@ -372,8 +369,8 @@ impl ToChalk for ProjectionTy {
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::ProjectionTy<Interner> {
chalk_ir::ProjectionTy {
associated_ty_id: self.associated_ty,
substitution: self.parameters.to_chalk(db),
associated_ty_id: self.associated_ty_id,
substitution: self.substitution.to_chalk(db),
}
}
@ -382,8 +379,8 @@ impl ToChalk for ProjectionTy {
projection_ty: chalk_ir::ProjectionTy<Interner>,
) -> ProjectionTy {
ProjectionTy {
associated_ty: projection_ty.associated_ty_id,
parameters: from_chalk(db, projection_ty.substitution),
associated_ty_id: projection_ty.associated_ty_id,
substitution: from_chalk(db, projection_ty.substitution),
}
}
}
@ -533,24 +530,24 @@ pub(super) fn generic_predicate_to_inline_bound(
Some(rust_ir::InlineBound::TraitBound(trait_bound))
}
GenericPredicate::Projection(proj) => {
if &proj.projection_ty.parameters[0] != self_ty {
if &proj.projection_ty.substitution[0] != self_ty {
return None;
}
let trait_ = match from_assoc_type_id(proj.projection_ty.associated_ty)
let trait_ = match from_assoc_type_id(proj.projection_ty.associated_ty_id)
.lookup(db.upcast())
.container
{
AssocContainerId::TraitId(t) => t,
_ => panic!("associated type not in trait"),
};
let args_no_self = proj.projection_ty.parameters[1..]
let args_no_self = proj.projection_ty.substitution[1..]
.iter()
.map(|ty| ty.clone().to_chalk(db).cast(&Interner))
.collect();
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: proj.projection_ty.associated_ty,
associated_ty_id: proj.projection_ty.associated_ty_id,
parameters: Vec::new(), // FIXME we don't support generic associated types yet
};
Some(rust_ir::InlineBound::AliasEqBound(alias_eq_bound))