1
Fork 0

Fix impl for SolverDelegate

This commit is contained in:
Michael Goulet 2024-06-17 19:09:46 -04:00
parent 532149eb88
commit 7d2be888b6
11 changed files with 385 additions and 85 deletions

View file

@ -9,6 +9,8 @@ pub trait SolverDelegate: Sized {
type Interner: Interner; type Interner: Interner;
fn interner(&self) -> Self::Interner; fn interner(&self) -> Self::Interner;
type Span: Copy;
fn solver_mode(&self) -> SolverMode; fn solver_mode(&self) -> SolverMode;
fn build_with_canonical<V>( fn build_with_canonical<V>(
@ -57,6 +59,12 @@ pub trait SolverDelegate: Sized {
def_id: <Self::Interner as Interner>::DefId, def_id: <Self::Interner as Interner>::DefId,
) -> <Self::Interner as Interner>::GenericArgs; ) -> <Self::Interner as Interner>::GenericArgs;
fn fresh_var_for_kind_with_span(
&self,
arg: <Self::Interner as Interner>::GenericArg,
span: Self::Span,
) -> <Self::Interner as Interner>::GenericArg;
fn instantiate_binder_with_infer<T: TypeFoldable<Self::Interner> + Copy>( fn instantiate_binder_with_infer<T: TypeFoldable<Self::Interner> + Copy>(
&self, &self,
value: ty::Binder<Self::Interner, T>, value: ty::Binder<Self::Interner, T>,

View file

@ -394,3 +394,35 @@ where
state, state,
) )
} }
// FIXME: needs to be pub to be accessed by downstream
// `rustc_trait_selection::solve::inspect::analyse`.
pub fn instantiate_canonical_state<Infcx, I, T: TypeFoldable<I>>(
infcx: &Infcx,
span: Infcx::Span,
param_env: I::ParamEnv,
orig_values: &mut Vec<I::GenericArg>,
state: inspect::CanonicalState<I, T>,
) -> T
where
Infcx: SolverDelegate<Interner = I>,
I: Interner,
{
// In case any fresh inference variables have been created between `state`
// and the previous instantiation, extend `orig_values` for it.
assert!(orig_values.len() <= state.value.var_values.len());
for &arg in &state.value.var_values.var_values[orig_values.len()..state.value.var_values.len()]
{
// FIXME: This is so ugly.
let unconstrained = infcx.fresh_var_for_kind_with_span(arg, span);
orig_values.push(unconstrained);
}
let instantiation =
EvalCtxt::compute_query_response_instantiation_values(infcx, orig_values, &state);
let inspect::State { var_values, data } = infcx.instantiate_canonical(state, instantiation);
EvalCtxt::unify_query_var_values(infcx, param_env, orig_values, var_values);
data
}

View file

@ -127,6 +127,17 @@ pub trait SolverDelegateEvalExt: SolverDelegate {
goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>, goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
generate_proof_tree: GenerateProofTree, generate_proof_tree: GenerateProofTree,
) -> (Result<(bool, Certainty), NoSolution>, Option<inspect::GoalEvaluation<Self::Interner>>); ) -> (Result<(bool, Certainty), NoSolution>, Option<inspect::GoalEvaluation<Self::Interner>>);
// FIXME: This is only exposed because we need to use it in `analyse.rs`
// which is not yet uplifted. Once that's done, we should remove this.
fn evaluate_root_goal_raw(
&self,
goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
generate_proof_tree: GenerateProofTree,
) -> (
Result<(NestedNormalizationGoals<Self::Interner>, bool, Certainty), NoSolution>,
Option<inspect::GoalEvaluation<Self::Interner>>,
);
} }
impl<Infcx, I> SolverDelegateEvalExt for Infcx impl<Infcx, I> SolverDelegateEvalExt for Infcx
@ -148,6 +159,20 @@ where
ecx.evaluate_goal(GoalEvaluationKind::Root, GoalSource::Misc, goal) ecx.evaluate_goal(GoalEvaluationKind::Root, GoalSource::Misc, goal)
}) })
} }
#[instrument(level = "debug", skip(self))]
fn evaluate_root_goal_raw(
&self,
goal: Goal<I, I::Predicate>,
generate_proof_tree: GenerateProofTree,
) -> (
Result<(NestedNormalizationGoals<I>, bool, Certainty), NoSolution>,
Option<inspect::GoalEvaluation<I>>,
) {
EvalCtxt::enter_root(self, generate_proof_tree, |ecx| {
ecx.evaluate_goal_raw(GoalEvaluationKind::Root, GoalSource::Misc, goal)
})
}
} }
impl<'a, Infcx, I> EvalCtxt<'a, Infcx> impl<'a, Infcx, I> EvalCtxt<'a, Infcx>

View file

@ -2,3 +2,5 @@ pub use rustc_type_ir::solve::inspect::*;
mod build; mod build;
pub(in crate::solve) use build::*; pub(in crate::solve) use build::*;
pub use crate::solve::eval_ctxt::canonical::instantiate_canonical_state;

View file

@ -12,7 +12,7 @@ use crate::solve::{
}; };
#[derive(Copy, Clone, PartialEq, Eq, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Limit(usize); pub struct SolverLimit(usize);
rustc_index::newtype_index! { rustc_index::newtype_index! {
#[orderable] #[orderable]
@ -35,7 +35,7 @@ bitflags::bitflags! {
struct StackEntry<I: Interner> { struct StackEntry<I: Interner> {
input: CanonicalInput<I>, input: CanonicalInput<I>,
available_depth: Limit, available_depth: SolverLimit,
/// The maximum depth reached by this stack entry, only up-to date /// The maximum depth reached by this stack entry, only up-to date
/// for the top of the stack and lazily updated for the rest. /// for the top of the stack and lazily updated for the rest.
@ -164,19 +164,19 @@ impl<I: Interner> SearchGraph<I> {
fn allowed_depth_for_nested( fn allowed_depth_for_nested(
tcx: I, tcx: I,
stack: &IndexVec<StackDepth, StackEntry<I>>, stack: &IndexVec<StackDepth, StackEntry<I>>,
) -> Option<Limit> { ) -> Option<SolverLimit> {
if let Some(last) = stack.raw.last() { if let Some(last) = stack.raw.last() {
if last.available_depth.0 == 0 { if last.available_depth.0 == 0 {
return None; return None;
} }
Some(if last.encountered_overflow { Some(if last.encountered_overflow {
Limit(last.available_depth.0 / 4) SolverLimit(last.available_depth.0 / 4)
} else { } else {
Limit(last.available_depth.0 - 1) SolverLimit(last.available_depth.0 - 1)
}) })
} else { } else {
Some(Limit(tcx.recursion_limit())) Some(SolverLimit(tcx.recursion_limit()))
} }
} }
@ -414,12 +414,12 @@ impl<I: Interner> SearchGraph<I> {
&mut self, &mut self,
tcx: I, tcx: I,
input: CanonicalInput<I>, input: CanonicalInput<I>,
available_depth: Limit, available_depth: SolverLimit,
inspect: &mut ProofTreeBuilder<Infcx>, inspect: &mut ProofTreeBuilder<Infcx>,
) -> Option<QueryResult<I>> { ) -> Option<QueryResult<I>> {
let CacheData { result, proof_tree, additional_depth, encountered_overflow } = self let CacheData { result, proof_tree, additional_depth, encountered_overflow } = self
.global_cache(tcx) .global_cache(tcx)
// TODO: Awkward `Limit -> usize -> Limit`. // FIXME: Awkward `Limit -> usize -> Limit`.
.get(tcx, input, self.stack.iter().map(|e| e.input), available_depth.0)?; .get(tcx, input, self.stack.iter().map(|e| e.input), available_depth.0)?;
// If we're building a proof tree and the current cache entry does not // If we're building a proof tree and the current cache entry does not

View file

@ -0,0 +1,12 @@
pub use rustc_next_trait_solver::solve::*;
mod fulfill;
mod infcx;
pub mod inspect;
mod normalize;
mod select;
pub use fulfill::{FulfillmentCtxt, NextSolverError};
pub(crate) use normalize::deeply_normalize_for_diagnostics;
pub use normalize::{deeply_normalize, deeply_normalize_with_skipped_universes};
pub use select::InferCtxtSelectExt;

View file

@ -12,13 +12,14 @@ use rustc_infer::traits::{
use rustc_middle::bug; use rustc_middle::bug;
use rustc_middle::ty::error::{ExpectedFound, TypeError}; use rustc_middle::ty::error::{ExpectedFound, TypeError};
use rustc_middle::ty::{self, TyCtxt}; use rustc_middle::ty::{self, TyCtxt};
use rustc_next_trait_solver::solve::{GenerateProofTree, SolverDelegateEvalExt as _};
use rustc_span::symbol::sym; use rustc_span::symbol::sym;
use crate::traits::{FulfillmentError, FulfillmentErrorCode, ScrubbedTraitError}; use crate::traits::{FulfillmentError, FulfillmentErrorCode, ScrubbedTraitError};
use super::eval_ctxt::GenerateProofTree; use super::infcx::SolverDelegate;
use super::inspect::{self, ProofTreeInferCtxtExt, ProofTreeVisitor}; use super::inspect::{self, ProofTreeInferCtxtExt, ProofTreeVisitor};
use super::{Certainty, InferCtxtEvalExt}; use super::Certainty;
/// A trait engine using the new trait solver. /// A trait engine using the new trait solver.
/// ///
@ -83,7 +84,9 @@ impl<'tcx> ObligationStorage<'tcx> {
// change. // change.
self.overflowed.extend(self.pending.extract_if(|o| { self.overflowed.extend(self.pending.extract_if(|o| {
let goal = o.clone().into(); let goal = o.clone().into();
let result = infcx.evaluate_root_goal(goal, GenerateProofTree::No).0; let result = <&SolverDelegate<'tcx>>::from(infcx)
.evaluate_root_goal(goal, GenerateProofTree::No)
.0;
match result { match result {
Ok((has_changed, _)) => has_changed, Ok((has_changed, _)) => has_changed,
_ => false, _ => false,
@ -165,7 +168,9 @@ where
let mut has_changed = false; let mut has_changed = false;
for obligation in self.obligations.unstalled_for_select() { for obligation in self.obligations.unstalled_for_select() {
let goal = obligation.clone().into(); let goal = obligation.clone().into();
let result = infcx.evaluate_root_goal(goal, GenerateProofTree::No).0; let result = <&SolverDelegate<'tcx>>::from(infcx)
.evaluate_root_goal(goal, GenerateProofTree::No)
.0;
self.inspect_evaluated_obligation(infcx, &obligation, &result); self.inspect_evaluated_obligation(infcx, &obligation, &result);
let (changed, certainty) = match result { let (changed, certainty) = match result {
Ok(result) => result, Ok(result) => result,
@ -288,7 +293,10 @@ fn fulfillment_error_for_stalled<'tcx>(
root_obligation: PredicateObligation<'tcx>, root_obligation: PredicateObligation<'tcx>,
) -> FulfillmentError<'tcx> { ) -> FulfillmentError<'tcx> {
let (code, refine_obligation) = infcx.probe(|_| { let (code, refine_obligation) = infcx.probe(|_| {
match infcx.evaluate_root_goal(root_obligation.clone().into(), GenerateProofTree::No).0 { match <&SolverDelegate<'tcx>>::from(infcx)
.evaluate_root_goal(root_obligation.clone().into(), GenerateProofTree::No)
.0
{
Ok((_, Certainty::Maybe(MaybeCause::Ambiguity))) => { Ok((_, Certainty::Maybe(MaybeCause::Ambiguity))) => {
(FulfillmentErrorCode::Ambiguity { overflow: None }, true) (FulfillmentErrorCode::Ambiguity { overflow: None }, true)
} }

View file

@ -1,14 +1,25 @@
use std::ops::Deref; use std::ops::Deref;
use rustc_data_structures::fx::FxHashSet;
use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_infer::infer::{BoundRegionConversionTime, InferCtxt}; use rustc_infer::infer::canonical::query_response::make_query_region_constraints;
use rustc_infer::infer::canonical::{
Canonical, CanonicalExt as _, CanonicalVarInfo, CanonicalVarValues,
};
use rustc_infer::infer::{
BoundRegionConversionTime, InferCtxt, RegionVariableOrigin, SubregionOrigin, TyCtxtInferExt,
};
use rustc_infer::traits::solve::Goal; use rustc_infer::traits::solve::Goal;
use rustc_infer::traits::ObligationCause; use rustc_infer::traits::util::supertraits;
use rustc_infer::traits::{ObligationCause, Reveal};
use rustc_middle::ty::fold::TypeFoldable; use rustc_middle::ty::fold::TypeFoldable;
use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt as _};
use rustc_span::DUMMY_SP; use rustc_span::{ErrorGuaranteed, Span, DUMMY_SP};
use rustc_type_ir::relate::Relate; use rustc_type_ir::relate::Relate;
use rustc_type_ir::solve::NoSolution; use rustc_type_ir::solve::{NoSolution, SolverMode};
use crate::traits::coherence::trait_ref_is_knowable;
use crate::traits::specialization_graph;
#[repr(transparent)] #[repr(transparent)]
pub struct SolverDelegate<'tcx>(InferCtxt<'tcx>); pub struct SolverDelegate<'tcx>(InferCtxt<'tcx>);
@ -32,7 +43,43 @@ impl<'tcx> rustc_next_trait_solver::infcx::SolverDelegate for SolverDelegate<'tc
type Interner = TyCtxt<'tcx>; type Interner = TyCtxt<'tcx>;
fn interner(&self) -> TyCtxt<'tcx> { fn interner(&self) -> TyCtxt<'tcx> {
(**self).tcx self.0.tcx
}
type Span = Span;
fn solver_mode(&self) -> ty::solve::SolverMode {
match self.intercrate {
true => SolverMode::Coherence,
false => SolverMode::Normal,
}
}
fn build_with_canonical<V>(
interner: TyCtxt<'tcx>,
solver_mode: SolverMode,
canonical: &Canonical<'tcx, V>,
) -> (Self, V, CanonicalVarValues<'tcx>)
where
V: TypeFoldable<TyCtxt<'tcx>>,
{
let (infcx, value, vars) = interner
.infer_ctxt()
.with_next_trait_solver(true)
.intercrate(match solver_mode {
SolverMode::Normal => false,
SolverMode::Coherence => true,
})
.build_with_canonical(DUMMY_SP, canonical);
(SolverDelegate(infcx), value, vars)
}
fn universe(&self) -> ty::UniverseIndex {
self.0.universe()
}
fn create_next_universe(&self) -> ty::UniverseIndex {
self.0.create_next_universe()
} }
fn universe_of_ty(&self, vid: ty::TyVid) -> Option<ty::UniverseIndex> { fn universe_of_ty(&self, vid: ty::TyVid) -> Option<ty::UniverseIndex> {
@ -40,14 +87,14 @@ impl<'tcx> rustc_next_trait_solver::infcx::SolverDelegate for SolverDelegate<'tc
// ty infers will give you the universe of the var it resolved to not the universe // ty infers will give you the universe of the var it resolved to not the universe
// it actually had. It also means that if you have a `?0.1` and infer it to `u8` then // it actually had. It also means that if you have a `?0.1` and infer it to `u8` then
// try to print out `?0.1` it will just print `?0`. // try to print out `?0.1` it will just print `?0`.
match (**self).probe_ty_var(vid) { match self.0.probe_ty_var(vid) {
Err(universe) => Some(universe), Err(universe) => Some(universe),
Ok(_) => None, Ok(_) => None,
} }
} }
fn universe_of_lt(&self, lt: ty::RegionVid) -> Option<ty::UniverseIndex> { fn universe_of_lt(&self, lt: ty::RegionVid) -> Option<ty::UniverseIndex> {
match (**self).inner.borrow_mut().unwrap_region_constraints().probe_value(lt) { match self.0.inner.borrow_mut().unwrap_region_constraints().probe_value(lt) {
Err(universe) => Some(universe), Err(universe) => Some(universe),
Ok(_) => None, Ok(_) => None,
} }
@ -55,81 +102,95 @@ impl<'tcx> rustc_next_trait_solver::infcx::SolverDelegate for SolverDelegate<'tc
fn universe_of_ct(&self, ct: ty::ConstVid) -> Option<ty::UniverseIndex> { fn universe_of_ct(&self, ct: ty::ConstVid) -> Option<ty::UniverseIndex> {
// Same issue as with `universe_of_ty` // Same issue as with `universe_of_ty`
match (**self).probe_const_var(ct) { match self.0.probe_const_var(ct) {
Err(universe) => Some(universe), Err(universe) => Some(universe),
Ok(_) => None, Ok(_) => None,
} }
} }
fn root_ty_var(&self, var: ty::TyVid) -> ty::TyVid { fn root_ty_var(&self, var: ty::TyVid) -> ty::TyVid {
(**self).root_var(var) self.0.root_var(var)
} }
fn root_const_var(&self, var: ty::ConstVid) -> ty::ConstVid { fn root_const_var(&self, var: ty::ConstVid) -> ty::ConstVid {
(**self).root_const_var(var) self.0.root_const_var(var)
} }
fn opportunistic_resolve_ty_var(&self, vid: ty::TyVid) -> Ty<'tcx> { fn opportunistic_resolve_ty_var(&self, vid: ty::TyVid) -> Ty<'tcx> {
match (**self).probe_ty_var(vid) { match self.0.probe_ty_var(vid) {
Ok(ty) => ty, Ok(ty) => ty,
Err(_) => Ty::new_var((**self).tcx, (**self).root_var(vid)), Err(_) => Ty::new_var(self.0.tcx, self.0.root_var(vid)),
} }
} }
fn opportunistic_resolve_int_var(&self, vid: ty::IntVid) -> Ty<'tcx> { fn opportunistic_resolve_int_var(&self, vid: ty::IntVid) -> Ty<'tcx> {
(**self).opportunistic_resolve_int_var(vid) self.0.opportunistic_resolve_int_var(vid)
} }
fn opportunistic_resolve_float_var(&self, vid: ty::FloatVid) -> Ty<'tcx> { fn opportunistic_resolve_float_var(&self, vid: ty::FloatVid) -> Ty<'tcx> {
(**self).opportunistic_resolve_float_var(vid) self.0.opportunistic_resolve_float_var(vid)
} }
fn opportunistic_resolve_ct_var(&self, vid: ty::ConstVid) -> ty::Const<'tcx> { fn opportunistic_resolve_ct_var(&self, vid: ty::ConstVid) -> ty::Const<'tcx> {
match (**self).probe_const_var(vid) { match self.0.probe_const_var(vid) {
Ok(ct) => ct, Ok(ct) => ct,
Err(_) => ty::Const::new_var((**self).tcx, (**self).root_const_var(vid)), Err(_) => ty::Const::new_var(self.0.tcx, self.0.root_const_var(vid)),
} }
} }
fn opportunistic_resolve_effect_var(&self, vid: ty::EffectVid) -> ty::Const<'tcx> { fn opportunistic_resolve_effect_var(&self, vid: ty::EffectVid) -> ty::Const<'tcx> {
match (**self).probe_effect_var(vid) { match self.0.probe_effect_var(vid) {
Some(ct) => ct, Some(ct) => ct,
None => ty::Const::new_infer( None => ty::Const::new_infer(
(**self).tcx, self.0.tcx,
ty::InferConst::EffectVar((**self).root_effect_var(vid)), ty::InferConst::EffectVar(self.0.root_effect_var(vid)),
), ),
} }
} }
fn opportunistic_resolve_lt_var(&self, vid: ty::RegionVid) -> ty::Region<'tcx> { fn opportunistic_resolve_lt_var(&self, vid: ty::RegionVid) -> ty::Region<'tcx> {
(**self) self.0
.inner .inner
.borrow_mut() .borrow_mut()
.unwrap_region_constraints() .unwrap_region_constraints()
.opportunistic_resolve_var((**self).tcx, vid) .opportunistic_resolve_var(self.0.tcx, vid)
} }
fn defining_opaque_types(&self) -> &'tcx ty::List<LocalDefId> { fn defining_opaque_types(&self) -> &'tcx ty::List<LocalDefId> {
(**self).defining_opaque_types() self.0.defining_opaque_types()
} }
fn next_ty_infer(&self) -> Ty<'tcx> { fn next_ty_infer(&self) -> Ty<'tcx> {
(**self).next_ty_var(DUMMY_SP) self.0.next_ty_var(DUMMY_SP)
} }
fn next_const_infer(&self) -> ty::Const<'tcx> { fn next_const_infer(&self) -> ty::Const<'tcx> {
(**self).next_const_var(DUMMY_SP) self.0.next_const_var(DUMMY_SP)
} }
fn fresh_args_for_item(&self, def_id: DefId) -> ty::GenericArgsRef<'tcx> { fn fresh_args_for_item(&self, def_id: DefId) -> ty::GenericArgsRef<'tcx> {
(**self).fresh_args_for_item(DUMMY_SP, def_id) self.0.fresh_args_for_item(DUMMY_SP, def_id)
} }
fn instantiate_binder_with_infer<T: TypeFoldable<Self::Interner> + Copy>( fn fresh_var_for_kind_with_span(
&self,
arg: ty::GenericArg<'tcx>,
span: Span,
) -> ty::GenericArg<'tcx> {
match arg.unpack() {
ty::GenericArgKind::Lifetime(_) => {
self.next_region_var(RegionVariableOrigin::MiscVariable(span)).into()
}
ty::GenericArgKind::Type(_) => self.next_ty_var(span).into(),
ty::GenericArgKind::Const(_) => self.next_const_var(span).into(),
}
}
fn instantiate_binder_with_infer<T: TypeFoldable<TyCtxt<'tcx>> + Copy>(
&self, &self,
value: ty::Binder<'tcx, T>, value: ty::Binder<'tcx, T>,
) -> T { ) -> T {
(**self).instantiate_binder_with_fresh_vars( self.0.instantiate_binder_with_fresh_vars(
DUMMY_SP, DUMMY_SP,
BoundRegionConversionTime::HigherRankedType, BoundRegionConversionTime::HigherRankedType,
value, value,
@ -141,7 +202,7 @@ impl<'tcx> rustc_next_trait_solver::infcx::SolverDelegate for SolverDelegate<'tc
value: ty::Binder<'tcx, T>, value: ty::Binder<'tcx, T>,
f: impl FnOnce(T) -> U, f: impl FnOnce(T) -> U,
) -> U { ) -> U {
(**self).enter_forall(value, f) self.0.enter_forall(value, f)
} }
fn relate<T: Relate<TyCtxt<'tcx>>>( fn relate<T: Relate<TyCtxt<'tcx>>>(
@ -151,7 +212,7 @@ impl<'tcx> rustc_next_trait_solver::infcx::SolverDelegate for SolverDelegate<'tc
variance: ty::Variance, variance: ty::Variance,
rhs: T, rhs: T,
) -> Result<Vec<Goal<'tcx, ty::Predicate<'tcx>>>, NoSolution> { ) -> Result<Vec<Goal<'tcx, ty::Predicate<'tcx>>>, NoSolution> {
(**self).at(&ObligationCause::dummy(), param_env).relate_no_trace(lhs, variance, rhs) self.0.at(&ObligationCause::dummy(), param_env).relate_no_trace(lhs, variance, rhs)
} }
fn eq_structurally_relating_aliases<T: Relate<TyCtxt<'tcx>>>( fn eq_structurally_relating_aliases<T: Relate<TyCtxt<'tcx>>>(
@ -160,7 +221,7 @@ impl<'tcx> rustc_next_trait_solver::infcx::SolverDelegate for SolverDelegate<'tc
lhs: T, lhs: T,
rhs: T, rhs: T,
) -> Result<Vec<Goal<'tcx, ty::Predicate<'tcx>>>, NoSolution> { ) -> Result<Vec<Goal<'tcx, ty::Predicate<'tcx>>>, NoSolution> {
(**self) self.0
.at(&ObligationCause::dummy(), param_env) .at(&ObligationCause::dummy(), param_env)
.eq_structurally_relating_aliases_no_trace(lhs, rhs) .eq_structurally_relating_aliases_no_trace(lhs, rhs)
} }
@ -169,10 +230,180 @@ impl<'tcx> rustc_next_trait_solver::infcx::SolverDelegate for SolverDelegate<'tc
where where
T: TypeFoldable<TyCtxt<'tcx>>, T: TypeFoldable<TyCtxt<'tcx>>,
{ {
(**self).resolve_vars_if_possible(value) self.0.resolve_vars_if_possible(value)
} }
fn probe<T>(&self, probe: impl FnOnce() -> T) -> T { fn probe<T>(&self, probe: impl FnOnce() -> T) -> T {
(**self).probe(|_| probe()) self.0.probe(|_| probe())
}
fn leak_check(&self, max_input_universe: ty::UniverseIndex) -> Result<(), NoSolution> {
self.0.leak_check(max_input_universe, None).map_err(|_| NoSolution)
}
fn elaborate_supertraits(
interner: TyCtxt<'tcx>,
trait_ref: ty::Binder<'tcx, ty::TraitRef<'tcx>>,
) -> impl Iterator<Item = ty::Binder<'tcx, ty::TraitRef<'tcx>>> {
supertraits(interner, trait_ref)
}
fn try_const_eval_resolve(
&self,
param_env: ty::ParamEnv<'tcx>,
unevaluated: ty::UnevaluatedConst<'tcx>,
) -> Option<ty::Const<'tcx>> {
use rustc_middle::mir::interpret::ErrorHandled;
match self.const_eval_resolve(param_env, unevaluated, DUMMY_SP) {
Ok(Some(val)) => Some(ty::Const::new_value(
self.tcx,
val,
self.tcx.type_of(unevaluated.def).instantiate(self.tcx, unevaluated.args),
)),
Ok(None) | Err(ErrorHandled::TooGeneric(_)) => None,
Err(ErrorHandled::Reported(e, _)) => Some(ty::Const::new_error(self.tcx, e.into())),
}
}
fn sub_regions(&self, sub: ty::Region<'tcx>, sup: ty::Region<'tcx>) {
self.0.sub_regions(SubregionOrigin::RelateRegionParamBound(DUMMY_SP), sub, sup)
}
fn register_ty_outlives(&self, ty: Ty<'tcx>, r: ty::Region<'tcx>) {
self.0.register_region_obligation_with_cause(ty, r, &ObligationCause::dummy());
}
fn well_formed_goals(
&self,
param_env: ty::ParamEnv<'tcx>,
arg: ty::GenericArg<'tcx>,
) -> Option<Vec<Goal<'tcx, ty::Predicate<'tcx>>>> {
crate::traits::wf::unnormalized_obligations(&self.0, param_env, arg).map(|obligations| {
obligations.into_iter().map(|obligation| obligation.into()).collect()
})
}
fn clone_opaque_types_for_query_response(&self) -> Vec<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)> {
self.0.clone_opaque_types_for_query_response()
}
fn make_deduplicated_outlives_constraints(
&self,
) -> Vec<ty::OutlivesPredicate<'tcx, ty::GenericArg<'tcx>>> {
// Cannot use `take_registered_region_obligations` as we may compute the response
// inside of a `probe` whenever we have multiple choices inside of the solver.
let region_obligations = self.0.inner.borrow().region_obligations().to_owned();
let region_constraints = self.0.with_region_constraints(|region_constraints| {
make_query_region_constraints(
self.tcx,
region_obligations
.iter()
.map(|r_o| (r_o.sup_type, r_o.sub_region, r_o.origin.to_constraint_category())),
region_constraints,
)
});
assert_eq!(region_constraints.member_constraints, vec![]);
let mut seen = FxHashSet::default();
region_constraints
.outlives
.into_iter()
.filter(|&(outlives, _)| seen.insert(outlives))
.map(|(outlives, _)| outlives)
.collect()
}
fn instantiate_canonical<V>(
&self,
canonical: Canonical<'tcx, V>,
values: CanonicalVarValues<'tcx>,
) -> V
where
V: TypeFoldable<TyCtxt<'tcx>>,
{
canonical.instantiate(self.tcx, &values)
}
fn instantiate_canonical_var_with_infer(
&self,
cv_info: CanonicalVarInfo<'tcx>,
universe_map: impl Fn(ty::UniverseIndex) -> ty::UniverseIndex,
) -> ty::GenericArg<'tcx> {
self.0.instantiate_canonical_var(DUMMY_SP, cv_info, universe_map)
}
fn insert_hidden_type(
&self,
opaque_type_key: ty::OpaqueTypeKey<'tcx>,
param_env: ty::ParamEnv<'tcx>,
hidden_ty: Ty<'tcx>,
goals: &mut Vec<Goal<'tcx, ty::Predicate<'tcx>>>,
) -> Result<(), NoSolution> {
self.0
.insert_hidden_type(opaque_type_key, DUMMY_SP, param_env, hidden_ty, goals)
.map_err(|_| NoSolution)
}
fn add_item_bounds_for_hidden_type(
&self,
def_id: DefId,
args: ty::GenericArgsRef<'tcx>,
param_env: ty::ParamEnv<'tcx>,
hidden_ty: Ty<'tcx>,
goals: &mut Vec<Goal<'tcx, ty::Predicate<'tcx>>>,
) {
self.0.add_item_bounds_for_hidden_type(def_id, args, param_env, hidden_ty, goals);
}
fn inject_new_hidden_type_unchecked(&self, key: ty::OpaqueTypeKey<'tcx>, hidden_ty: Ty<'tcx>) {
self.0.inject_new_hidden_type_unchecked(
key,
ty::OpaqueHiddenType { ty: hidden_ty, span: DUMMY_SP },
)
}
fn reset_opaque_types(&self) {
let _ = self.take_opaque_types();
}
fn trait_ref_is_knowable<E: std::fmt::Debug>(
&self,
trait_ref: ty::TraitRef<'tcx>,
lazily_normalize_ty: impl FnMut(Ty<'tcx>) -> Result<Ty<'tcx>, E>,
) -> Result<bool, E> {
trait_ref_is_knowable(&self.0, trait_ref, lazily_normalize_ty)
.map(|is_knowable| is_knowable.is_ok())
}
fn fetch_eligible_assoc_item(
&self,
param_env: ty::ParamEnv<'tcx>,
goal_trait_ref: ty::TraitRef<'tcx>,
trait_assoc_def_id: DefId,
impl_def_id: DefId,
) -> Result<Option<DefId>, NoSolution> {
let node_item = specialization_graph::assoc_def(self.tcx, impl_def_id, trait_assoc_def_id)
.map_err(|ErrorGuaranteed { .. }| NoSolution)?;
let eligible = if node_item.is_final() {
// Non-specializable items are always projectable.
true
} else {
// Only reveal a specializable default if we're past type-checking
// and the obligation is monomorphic, otherwise passes such as
// transmute checking and polymorphic MIR optimizations could
// get a result which isn't correct for all monomorphizations.
if param_env.reveal() == Reveal::All {
let poly_trait_ref = self.resolve_vars_if_possible(goal_trait_ref);
!poly_trait_ref.still_further_specializable()
} else {
trace!(?node_item.item.def_id, "not eligible due to default");
false
}
};
// FIXME: Check for defaultness here may cause diagnostics problems.
if eligible { Ok(Some(node_item.item.def_id)) } else { Ok(None) }
} }
} }

View file

@ -0,0 +1,4 @@
pub use rustc_next_trait_solver::solve::inspect::*;
mod analyse;
pub use analyse::*;

View file

@ -13,19 +13,16 @@ use rustc_ast_ir::try_visit;
use rustc_ast_ir::visit::VisitorResult; use rustc_ast_ir::visit::VisitorResult;
use rustc_infer::infer::{DefineOpaqueTypes, InferCtxt, InferOk}; use rustc_infer::infer::{DefineOpaqueTypes, InferCtxt, InferOk};
use rustc_macros::extension; use rustc_macros::extension;
use rustc_middle::traits::query::NoSolution; use rustc_middle::traits::solve::{Certainty, Goal, GoalSource, NoSolution, QueryResult};
use rustc_middle::traits::solve::{inspect, QueryResult};
use rustc_middle::traits::solve::{Certainty, Goal, MaybeCause};
use rustc_middle::traits::ObligationCause; use rustc_middle::traits::ObligationCause;
use rustc_middle::ty::{TyCtxt, TypeFoldable}; use rustc_middle::ty::{TyCtxt, TypeFoldable};
use rustc_middle::{bug, ty}; use rustc_middle::{bug, ty};
use rustc_next_trait_solver::resolve::EagerResolver; use rustc_next_trait_solver::resolve::EagerResolver;
use rustc_next_trait_solver::solve::inspect::{self, instantiate_canonical_state};
use rustc_next_trait_solver::solve::{GenerateProofTree, MaybeCause, SolverDelegateEvalExt as _};
use rustc_span::{Span, DUMMY_SP}; use rustc_span::{Span, DUMMY_SP};
use crate::solve::eval_ctxt::canonical;
use crate::solve::infcx::SolverDelegate; use crate::solve::infcx::SolverDelegate;
use crate::solve::{EvalCtxt, GoalEvaluationKind, GoalSource};
use crate::solve::{GenerateProofTree, InferCtxtEvalExt};
use crate::traits::ObligationCtxt; use crate::traits::ObligationCtxt;
pub struct InspectConfig { pub struct InspectConfig {
@ -33,7 +30,7 @@ pub struct InspectConfig {
} }
pub struct InspectGoal<'a, 'tcx> { pub struct InspectGoal<'a, 'tcx> {
infcx: &'a InferCtxt<'tcx>, infcx: &'a SolverDelegate<'tcx>,
depth: usize, depth: usize,
orig_values: Vec<ty::GenericArg<'tcx>>, orig_values: Vec<ty::GenericArg<'tcx>>,
goal: Goal<'tcx, ty::Predicate<'tcx>>, goal: Goal<'tcx, ty::Predicate<'tcx>>,
@ -163,16 +160,10 @@ impl<'a, 'tcx> InspectCandidate<'a, 'tcx> {
match **step { match **step {
inspect::ProbeStep::AddGoal(source, goal) => instantiated_goals.push(( inspect::ProbeStep::AddGoal(source, goal) => instantiated_goals.push((
source, source,
canonical::instantiate_canonical_state( instantiate_canonical_state(infcx, span, param_env, &mut orig_values, goal),
infcx,
span,
param_env,
&mut orig_values,
goal,
),
)), )),
inspect::ProbeStep::RecordImplArgs { impl_args } => { inspect::ProbeStep::RecordImplArgs { impl_args } => {
opt_impl_args = Some(canonical::instantiate_canonical_state( opt_impl_args = Some(instantiate_canonical_state(
infcx, infcx,
span, span,
param_env, param_env,
@ -185,13 +176,8 @@ impl<'a, 'tcx> InspectCandidate<'a, 'tcx> {
} }
} }
let () = canonical::instantiate_canonical_state( let () =
infcx, instantiate_canonical_state(infcx, span, param_env, &mut orig_values, self.final_state);
span,
param_env,
&mut orig_values,
self.final_state,
);
if let Some(term_hack) = self.goal.normalizes_to_term_hack { if let Some(term_hack) = self.goal.normalizes_to_term_hack {
// FIXME: We ignore the expected term of `NormalizesTo` goals // FIXME: We ignore the expected term of `NormalizesTo` goals
@ -200,9 +186,8 @@ impl<'a, 'tcx> InspectCandidate<'a, 'tcx> {
let _ = term_hack.constrain(infcx, span, param_env); let _ = term_hack.constrain(infcx, span, param_env);
} }
let opt_impl_args = opt_impl_args.map(|impl_args| { let opt_impl_args =
impl_args.fold_with(&mut EagerResolver::new(<&SolverDelegate<'tcx>>::from(infcx))) opt_impl_args.map(|impl_args| impl_args.fold_with(&mut EagerResolver::new(infcx)));
});
let goals = instantiated_goals let goals = instantiated_goals
.into_iter() .into_iter()
@ -221,16 +206,7 @@ impl<'a, 'tcx> InspectCandidate<'a, 'tcx> {
// instantiating the candidate it is already constrained to the result of another // instantiating the candidate it is already constrained to the result of another
// candidate. // candidate.
let proof_tree = infcx let proof_tree = infcx
.probe(|_| { .probe(|_| infcx.evaluate_root_goal_raw(goal, GenerateProofTree::Yes).1);
EvalCtxt::enter_root(infcx, GenerateProofTree::Yes, |ecx| {
ecx.evaluate_goal_raw(
GoalEvaluationKind::Root,
GoalSource::Misc,
goal,
)
})
})
.1;
InspectGoal::new( InspectGoal::new(
infcx, infcx,
self.goal.depth + 1, self.goal.depth + 1,
@ -390,6 +366,8 @@ impl<'a, 'tcx> InspectGoal<'a, 'tcx> {
normalizes_to_term_hack: Option<NormalizesToTermHack<'tcx>>, normalizes_to_term_hack: Option<NormalizesToTermHack<'tcx>>,
source: GoalSource, source: GoalSource,
) -> Self { ) -> Self {
let infcx = <&SolverDelegate<'tcx>>::from(infcx);
let inspect::GoalEvaluation { uncanonicalized_goal, orig_values, evaluation } = root; let inspect::GoalEvaluation { uncanonicalized_goal, orig_values, evaluation } = root;
let result = evaluation.result.and_then(|ok| { let result = evaluation.result.and_then(|ok| {
if let Some(term_hack) = normalizes_to_term_hack { if let Some(term_hack) = normalizes_to_term_hack {
@ -405,8 +383,7 @@ impl<'a, 'tcx> InspectGoal<'a, 'tcx> {
infcx, infcx,
depth, depth,
orig_values, orig_values,
goal: uncanonicalized_goal goal: uncanonicalized_goal.fold_with(&mut EagerResolver::new(infcx)),
.fold_with(&mut EagerResolver::new(<&SolverDelegate<'tcx>>::from(infcx))),
result, result,
evaluation_kind: evaluation.kind, evaluation_kind: evaluation.kind,
normalizes_to_term_hack, normalizes_to_term_hack,
@ -452,7 +429,8 @@ impl<'tcx> InferCtxt<'tcx> {
depth: usize, depth: usize,
visitor: &mut V, visitor: &mut V,
) -> V::Result { ) -> V::Result {
let (_, proof_tree) = self.evaluate_root_goal(goal, GenerateProofTree::Yes); let (_, proof_tree) =
<&SolverDelegate<'tcx>>::from(self).evaluate_root_goal(goal, GenerateProofTree::Yes);
let proof_tree = proof_tree.unwrap(); let proof_tree = proof_tree.unwrap();
visitor.visit_goal(&InspectGoal::new(self, depth, proof_tree, None, GoalSource::Misc)) visitor.visit_goal(&InspectGoal::new(self, depth, proof_tree, None, GoalSource::Misc))
} }

View file

@ -19,7 +19,7 @@ use super::{
}; };
use crate::infer::{InferCtxt, InferOk, TypeFreshener}; use crate::infer::{InferCtxt, InferOk, TypeFreshener};
use crate::solve::InferCtxtSelectExt; use crate::solve::InferCtxtSelectExt as _;
use crate::traits::error_reporting::TypeErrCtxtExt; use crate::traits::error_reporting::TypeErrCtxtExt;
use crate::traits::normalize::normalize_with_depth; use crate::traits::normalize::normalize_with_depth;
use crate::traits::normalize::normalize_with_depth_to; use crate::traits::normalize::normalize_with_depth_to;