1
Fork 0

Stop using a placeholder for empty regions in Chalk

This commit is contained in:
Matthew Jasper 2022-01-17 22:06:02 +00:00
parent 6499c5e7fc
commit cb3cff3761
3 changed files with 14 additions and 60 deletions

View file

@ -20,11 +20,10 @@ use rustc_span::symbol::sym;
use std::fmt;
use std::sync::Arc;
use crate::chalk::lowering::{self, LowerInto};
use crate::chalk::lowering::LowerInto;
pub struct RustIrDatabase<'tcx> {
pub(crate) interner: RustInterner<'tcx>,
pub(crate) reempty_placeholder: ty::Region<'tcx>,
}
impl fmt::Debug for RustIrDatabase<'_> {
@ -40,12 +39,9 @@ impl<'tcx> RustIrDatabase<'tcx> {
bound_vars: SubstsRef<'tcx>,
) -> Vec<chalk_ir::QuantifiedWhereClause<RustInterner<'tcx>>> {
let predicates = self.interner.tcx.predicates_defined_on(def_id).predicates;
let mut regions_substitutor =
lowering::RegionsSubstitutor::new(self.interner.tcx, self.reempty_placeholder);
predicates
.iter()
.map(|(wc, _)| wc.subst(self.interner.tcx, bound_vars))
.map(|wc| wc.fold_with(&mut regions_substitutor))
.filter_map(|wc| LowerInto::<
Option<chalk_ir::QuantifiedWhereClause<RustInterner<'tcx>>>
>::lower_into(wc, self.interner)).collect()
@ -287,9 +283,6 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
let trait_ref = self.interner.tcx.impl_trait_ref(def_id).expect("not an impl");
let trait_ref = trait_ref.subst(self.interner.tcx, bound_vars);
let mut regions_substitutor =
lowering::RegionsSubstitutor::new(self.interner.tcx, self.reempty_placeholder);
let trait_ref = trait_ref.fold_with(&mut regions_substitutor);
let where_clauses = self.where_clauses_for(def_id, bound_vars);
@ -335,9 +328,6 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
let self_ty = trait_ref.self_ty();
let self_ty = self_ty.subst(self.interner.tcx, bound_vars);
let mut regions_substitutor =
lowering::RegionsSubstitutor::new(self.interner.tcx, self.reempty_placeholder);
let self_ty = self_ty.fold_with(&mut regions_substitutor);
let lowered_ty = self_ty.lower_into(self.interner);
parameters[0].assert_ty_ref(self.interner).could_match(

View file

@ -464,9 +464,11 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::Lifetime<RustInterner<'tcx>>> for Region<'t
})
.intern(interner)
}
ReEmpty(_) => unimplemented!(),
// FIXME(chalk): need to handle ReErased
ReErased => unimplemented!(),
ReEmpty(ui) => {
chalk_ir::LifetimeData::Empty(chalk_ir::UniverseIndex { counter: ui.index() })
.intern(interner)
}
ReErased => chalk_ir::LifetimeData::Erased.intern(interner),
}
}
}
@ -488,12 +490,12 @@ impl<'tcx> LowerInto<'tcx, Region<'tcx>> for &chalk_ir::Lifetime<RustInterner<'t
name: ty::BoundRegionKind::BrAnon(p.idx as u32),
})
}
chalk_ir::LifetimeData::Static => ty::RegionKind::ReStatic,
chalk_ir::LifetimeData::Phantom(_, _) => unimplemented!(),
chalk_ir::LifetimeData::Static => return interner.tcx.lifetimes.re_static,
chalk_ir::LifetimeData::Empty(ui) => {
ty::RegionKind::ReEmpty(ty::UniverseIndex::from_usize(ui.counter))
ty::ReEmpty(ty::UniverseIndex::from_usize(ui.counter))
}
chalk_ir::LifetimeData::Erased => ty::RegionKind::ReErased,
chalk_ir::LifetimeData::Erased => return interner.tcx.lifetimes.re_erased,
chalk_ir::LifetimeData::Phantom(void, _) => match *void {},
};
interner.tcx.mk_region(kind)
}
@ -1110,32 +1112,3 @@ impl<'tcx> TypeVisitor<'tcx> for PlaceholdersCollector {
r.super_visit_with(self)
}
}
/// Used to substitute specific `Regions`s with placeholders.
crate struct RegionsSubstitutor<'tcx> {
tcx: TyCtxt<'tcx>,
reempty_placeholder: ty::Region<'tcx>,
}
impl<'tcx> RegionsSubstitutor<'tcx> {
crate fn new(tcx: TyCtxt<'tcx>, reempty_placeholder: ty::Region<'tcx>) -> Self {
RegionsSubstitutor { tcx, reempty_placeholder }
}
}
impl<'tcx> TypeFolder<'tcx> for RegionsSubstitutor<'tcx> {
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
self.tcx
}
fn fold_region(&mut self, r: Region<'tcx>) -> Region<'tcx> {
match r {
ty::ReEmpty(ui) => {
assert_eq!(ui.as_usize(), 0);
self.reempty_placeholder
}
_ => r.super_fold_with(self),
}
}
}

View file

@ -22,9 +22,8 @@ use rustc_infer::infer::canonical::{
use rustc_infer::traits::{self, CanonicalChalkEnvironmentAndGoal};
use crate::chalk::db::RustIrDatabase as ChalkRustIrDatabase;
use crate::chalk::lowering::{
LowerInto, ParamsSubstitutor, PlaceholdersCollector, RegionsSubstitutor,
};
use crate::chalk::lowering::LowerInto;
use crate::chalk::lowering::{ParamsSubstitutor, PlaceholdersCollector};
use chalk_solve::Solution;
@ -42,20 +41,11 @@ crate fn evaluate_goal<'tcx>(
let mut placeholders_collector = PlaceholdersCollector::new();
obligation.visit_with(&mut placeholders_collector);
let reempty_placeholder = tcx.mk_region(ty::RegionKind::RePlaceholder(ty::Placeholder {
universe: ty::UniverseIndex::ROOT,
name: ty::BoundRegionKind::BrAnon(placeholders_collector.next_anon_region_placeholder + 1),
}));
let mut params_substitutor =
ParamsSubstitutor::new(tcx, placeholders_collector.next_ty_placeholder);
let obligation = obligation.fold_with(&mut params_substitutor);
// FIXME(chalk): we really should be substituting these back in the solution
let _params: FxHashMap<usize, ParamTy> = params_substitutor.params;
let mut regions_substitutor = RegionsSubstitutor::new(tcx, reempty_placeholder);
let obligation = obligation.fold_with(&mut regions_substitutor);
let max_universe = obligation.max_universe.index();
let lowered_goal: chalk_ir::UCanonical<
@ -96,7 +86,8 @@ crate fn evaluate_goal<'tcx>(
use chalk_solve::Solver;
let mut solver = chalk_engine::solve::SLGSolver::new(32, None);
let db = ChalkRustIrDatabase { interner, reempty_placeholder };
let db = ChalkRustIrDatabase { interner };
debug!(?lowered_goal);
let solution = solver.solve(&db, &lowered_goal);
debug!(?obligation, ?solution, "evaluate goal");