1use std::mem;
2use std::ops::ControlFlow;
34#[cfg(feature = "nightly")]
5use rustc_macros::StableHash;
6use rustc_type_ir::data_structures::{HashMap, HashSet};
7use rustc_type_ir::inherent::*;
8use rustc_type_ir::relate::Relate;
9use rustc_type_ir::relate::solver_relating::RelateExt;
10use rustc_type_ir::search_graph::{CandidateHeadUsages, PathKind};
11use rustc_type_ir::solve::{MaybeInfo, OpaqueTypesJank};
12use rustc_type_ir::{
13selfas ty, CanonicalVarValues, InferCtxtLike, Interner, TypeFoldable, TypeFolder,
14TypeSuperFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
15TypingMode,
16};
17use tracing::{debug, instrument, trace};
1819use super::has_only_region_constraints;
20use crate::canonical::{
21canonicalize_goal, canonicalize_response, instantiate_and_apply_query_response,
22response_no_constraints_raw,
23};
24use crate::coherence;
25use crate::delegate::SolverDelegate;
26use crate::placeholder::BoundVarReplacer;
27use crate::resolve::eager_resolve_vars;
28use crate::solve::search_graph::SearchGraph;
29use crate::solve::ty::may_use_unstable_feature;
30use crate::solve::{
31CanonicalInput, CanonicalResponse, Certainty, ExternalConstraintsData, FIXPOINT_STEP_LIMIT,
32Goal, GoalEvaluation, GoalSource, GoalStalledOn, HasChanged, MaybeCause,
33NestedNormalizationGoals, NoSolution, QueryInput, QueryResult, Response, VisibleForLeakCheck,
34inspect,
35};
3637mod probe;
3839/// The kind of goal we're currently proving.
40///
41/// This has effects on cycle handling handling and on how we compute
42/// query responses, see the variant descriptions for more info.
43#[derive(#[automatically_derived]
impl ::core::fmt::Debug for CurrentGoalKind {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
CurrentGoalKind::Misc => "Misc",
CurrentGoalKind::CoinductiveTrait => "CoinductiveTrait",
CurrentGoalKind::NormalizesTo => "NormalizesTo",
})
}
}Debug, #[automatically_derived]
impl ::core::marker::Copy for CurrentGoalKind { }Copy, #[automatically_derived]
impl ::core::clone::Clone for CurrentGoalKind {
#[inline]
fn clone(&self) -> CurrentGoalKind { *self }
}Clone)]
44enum CurrentGoalKind {
45 Misc,
46/// We're proving an trait goal for a coinductive trait, either an auto trait or `Sized`.
47 ///
48 /// These are currently the only goals whose impl where-clauses are considered to be
49 /// productive steps.
50CoinductiveTrait,
51/// Unlike other goals, `NormalizesTo` goals act like functions with the expected term
52 /// always being fully unconstrained. This would weaken inference however, as the nested
53 /// goals never get the inference constraints from the actual normalized-to type.
54 ///
55 /// Because of this we return any ambiguous nested goals from `NormalizesTo` to the
56 /// caller when then adds these to its own context. The caller is always an `AliasRelate`
57 /// goal so this never leaks out of the solver.
58NormalizesTo,
59}
6061impl CurrentGoalKind {
62fn from_query_input<I: Interner>(cx: I, input: QueryInput<I, I::Predicate>) -> CurrentGoalKind {
63match input.goal.predicate.kind().skip_binder() {
64 ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
65if cx.trait_is_coinductive(pred.trait_ref.def_id) {
66 CurrentGoalKind::CoinductiveTrait67 } else {
68 CurrentGoalKind::Misc69 }
70 }
71 ty::PredicateKind::NormalizesTo(_) => CurrentGoalKind::NormalizesTo,
72_ => CurrentGoalKind::Misc,
73 }
74 }
75}
7677pub struct EvalCtxt<'a, D, I = <D as SolverDelegate>::Interner>
78where
79D: SolverDelegate<Interner = I>,
80 I: Interner,
81{
82/// The inference context that backs (mostly) inference and placeholder terms
83 /// instantiated while solving goals.
84 ///
85 /// NOTE: The `InferCtxt` that backs the `EvalCtxt` is intentionally private,
86 /// because the `InferCtxt` is much more general than `EvalCtxt`. Methods such
87 /// as `take_registered_region_obligations` can mess up query responses,
88 /// using `At::normalize` is totally wrong, calling `evaluate_root_goal` can
89 /// cause coinductive unsoundness, etc.
90 ///
91 /// Methods that are generally of use for trait solving are *intentionally*
92 /// re-declared through the `EvalCtxt` below, often with cleaner signatures
93 /// since we don't care about things like `ObligationCause`s and `Span`s here.
94 /// If some `InferCtxt` method is missing, please first think defensively about
95 /// the method's compatibility with this solver, or if an existing one does
96 /// the job already.
97delegate: &'a D,
9899/// The variable info for the `var_values`, only used to make an ambiguous response
100 /// with no constraints.
101var_kinds: I::CanonicalVarKinds,
102103/// What kind of goal we're currently computing, see the enum definition
104 /// for more info.
105current_goal_kind: CurrentGoalKind,
106pub(super) var_values: CanonicalVarValues<I>,
107108/// The highest universe index nameable by the caller.
109 ///
110 /// When we enter a new binder inside of the query we create new universes
111 /// which the caller cannot name. We have to be careful with variables from
112 /// these new universes when creating the query response.
113 ///
114 /// Both because these new universes can prevent us from reaching a fixpoint
115 /// if we have a coinductive cycle and because that's the only way we can return
116 /// new placeholders to the caller.
117pub(super) max_input_universe: ty::UniverseIndex,
118/// The opaque types from the canonical input. We only need to return opaque types
119 /// which have been added to the storage while evaluating this goal.
120pub(super) initial_opaque_types_storage_num_entries:
121 <D::Infcx as InferCtxtLike>::OpaqueTypeStorageEntries,
122123pub(super) search_graph: &'a mut SearchGraph<D>,
124125 nested_goals: Vec<(GoalSource, Goal<I, I::Predicate>, Option<GoalStalledOn<I>>)>,
126127pub(super) origin_span: I::Span,
128129// Has this `EvalCtxt` errored out with `NoSolution` in `try_evaluate_added_goals`?
130 //
131 // If so, then it can no longer be used to make a canonical query response,
132 // since subsequent calls to `try_evaluate_added_goals` have possibly dropped
133 // ambiguous goals. Instead, a probe needs to be introduced somewhere in the
134 // evaluation code.
135tainted: Result<(), NoSolution>,
136137pub(super) inspect: inspect::EvaluationStepBuilder<D>,
138}
139140#[derive(#[automatically_derived]
impl ::core::cmp::PartialEq for GenerateProofTree {
#[inline]
fn eq(&self, other: &GenerateProofTree) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for GenerateProofTree {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::fmt::Debug for GenerateProofTree {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
GenerateProofTree::Yes => "Yes",
GenerateProofTree::No => "No",
})
}
}Debug, #[automatically_derived]
impl ::core::hash::Hash for GenerateProofTree {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
let __self_discr = ::core::intrinsics::discriminant_value(self);
::core::hash::Hash::hash(&__self_discr, state)
}
}Hash, #[automatically_derived]
impl ::core::clone::Clone for GenerateProofTree {
#[inline]
fn clone(&self) -> GenerateProofTree { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for GenerateProofTree { }Copy)]
141#[cfg_attr(feature = "nightly", derive(const _: () =
{
impl ::rustc_data_structures::stable_hasher::StableHash for
GenerateProofTree {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hasher::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
::std::mem::discriminant(self).stable_hash(__hcx, __hasher);
match *self {
GenerateProofTree::Yes => {}
GenerateProofTree::No => {}
}
}
}
};StableHash))]
142pub enum GenerateProofTree {
143 Yes,
144 No,
145}
146147pub trait SolverDelegateEvalExt: SolverDelegate {
148/// Evaluates a goal from **outside** of the trait solver.
149 ///
150 /// Using this while inside of the solver is wrong as it uses a new
151 /// search graph which would break cycle detection.
152fn evaluate_root_goal(
153&self,
154 goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
155 span: <Self::Interner as Interner>::Span,
156 stalled_on: Option<GoalStalledOn<Self::Interner>>,
157 ) -> Result<GoalEvaluation<Self::Interner>, NoSolution>;
158159/// Checks whether evaluating `goal` may hold while treating not-yet-defined
160 /// opaque types as being kind of rigid.
161 ///
162 /// See the comment on [OpaqueTypesJank] for more details.
163fn root_goal_may_hold_opaque_types_jank(
164&self,
165 goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
166 ) -> bool;
167168/// Check whether evaluating `goal` with a depth of `root_depth` may
169 /// succeed. This only returns `false` if the goal is guaranteed to
170 /// not hold. In case evaluation overflows and fails with ambiguity this
171 /// returns `true`.
172 ///
173 /// This is only intended to be used as a performance optimization
174 /// in coherence checking.
175fn root_goal_may_hold_with_depth(
176&self,
177 root_depth: usize,
178 goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
179 ) -> bool;
180181// FIXME: This is only exposed because we need to use it in `analyse.rs`
182 // which is not yet uplifted. Once that's done, we should remove this.
183fn evaluate_root_goal_for_proof_tree(
184&self,
185 goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
186 span: <Self::Interner as Interner>::Span,
187 ) -> (
188Result<NestedNormalizationGoals<Self::Interner>, NoSolution>,
189 inspect::GoalEvaluation<Self::Interner>,
190 );
191}
192193impl<D, I> SolverDelegateEvalExtfor D
194where
195D: SolverDelegate<Interner = I>,
196 I: Interner,
197{
198x;#[instrument(level = "debug", skip(self), ret)]199fn evaluate_root_goal(
200&self,
201 goal: Goal<I, I::Predicate>,
202 span: I::Span,
203 stalled_on: Option<GoalStalledOn<I>>,
204 ) -> Result<GoalEvaluation<I>, NoSolution> {
205 EvalCtxt::enter_root(self, self.cx().recursion_limit(), span, |ecx| {
206 ecx.evaluate_goal(GoalSource::Misc, goal, stalled_on)
207 })
208 }
209210x;#[instrument(level = "debug", skip(self), ret)]211fn root_goal_may_hold_opaque_types_jank(
212&self,
213 goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
214 ) -> bool {
215self.probe(|| {
216 EvalCtxt::enter_root(self, self.cx().recursion_limit(), I::Span::dummy(), |ecx| {
217 ecx.evaluate_goal(GoalSource::Misc, goal, None)
218 })
219 .is_ok_and(|r| match r.certainty {
220 Certainty::Yes => true,
221 Certainty::Maybe(MaybeInfo {
222 cause: _,
223 opaque_types_jank,
224 stalled_on_coroutines: _,
225 }) => match opaque_types_jank {
226 OpaqueTypesJank::AllGood => true,
227 OpaqueTypesJank::ErrorIfRigidSelfTy => false,
228 },
229 })
230 })
231 }
232233fn root_goal_may_hold_with_depth(
234&self,
235 root_depth: usize,
236 goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
237 ) -> bool {
238self.probe(|| {
239EvalCtxt::enter_root(self, root_depth, I::Span::dummy(), |ecx| {
240ecx.evaluate_goal(GoalSource::Misc, goal, None)
241 })
242 })
243 .is_ok()
244 }
245246#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("evaluate_root_goal_for_proof_tree",
"rustc_next_trait_solver::solve::eval_ctxt",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs"),
::tracing_core::__macro_support::Option::Some(246u32),
::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::eval_ctxt"),
::tracing_core::field::FieldSet::new(&["goal", "span"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&goal)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&span)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return:
(Result<NestedNormalizationGoals<I>, NoSolution>,
inspect::GoalEvaluation<I>) = loop {};
return __tracing_attr_fake_return;
}
{ evaluate_root_goal_for_proof_tree(self, goal, span) }
}
}#[instrument(level = "debug", skip(self))]247fn evaluate_root_goal_for_proof_tree(
248&self,
249 goal: Goal<I, I::Predicate>,
250 span: I::Span,
251 ) -> (Result<NestedNormalizationGoals<I>, NoSolution>, inspect::GoalEvaluation<I>) {
252 evaluate_root_goal_for_proof_tree(self, goal, span)
253 }
254}
255256impl<'a, D, I> EvalCtxt<'a, D>
257where
258D: SolverDelegate<Interner = I>,
259 I: Interner,
260{
261pub(super) fn typing_mode(&self) -> TypingMode<I> {
262self.delegate.typing_mode()
263 }
264265/// Computes the `PathKind` for the step from the current goal to the
266 /// nested goal required due to `source`.
267 ///
268 /// See #136824 for a more detailed reasoning for this behavior. We
269 /// consider cycles to be coinductive if they 'step into' a where-clause
270 /// of a coinductive trait. We will likely extend this function in the future
271 /// and will need to clearly document it in the rustc-dev-guide before
272 /// stabilization.
273pub(super) fn step_kind_for_source(&self, source: GoalSource) -> PathKind {
274match source {
275// We treat these goals as unknown for now. It is likely that most miscellaneous
276 // nested goals will be converted to an inductive variant in the future.
277 //
278 // Having unknown cycles is always the safer option, as changing that to either
279 // succeed or hard error is backwards compatible. If we incorrectly treat a cycle
280 // as inductive even though it should not be, it may be unsound during coherence and
281 // fixing it may cause inference breakage or introduce ambiguity.
282GoalSource::Misc => PathKind::Unknown,
283 GoalSource::NormalizeGoal(path_kind) => path_kind,
284 GoalSource::ImplWhereBound => match self.current_goal_kind {
285// We currently only consider a cycle coinductive if it steps
286 // into a where-clause of a coinductive trait.
287CurrentGoalKind::CoinductiveTrait => PathKind::Coinductive,
288// While normalizing via an impl does step into a where-clause of
289 // an impl, accessing the associated item immediately steps out of
290 // it again. This means cycles/recursive calls are not guarded
291 // by impls used for normalization.
292 //
293 // See tests/ui/traits/next-solver/cycles/normalizes-to-is-not-productive.rs
294 // for how this can go wrong.
295CurrentGoalKind::NormalizesTo => PathKind::Inductive,
296// We probably want to make all traits coinductive in the future,
297 // so we treat cycles involving where-clauses of not-yet coinductive
298 // traits as ambiguous for now.
299CurrentGoalKind::Misc => PathKind::Unknown,
300 },
301// Relating types is always unproductive. If we were to map proof trees to
302 // corecursive functions as explained in #136824, relating types never
303 // introduces a constructor which could cause the recursion to be guarded.
304GoalSource::TypeRelating => PathKind::Inductive,
305// These goal sources are likely unproductive and can be changed to
306 // `PathKind::Inductive`. Keeping them as unknown until we're confident
307 // about this and have an example where it is necessary.
308GoalSource::AliasBoundConstCondition | GoalSource::AliasWellFormed => PathKind::Unknown,
309 }
310 }
311312/// Creates a root evaluation context and search graph. This should only be
313 /// used from outside of any evaluation, and other methods should be preferred
314 /// over using this manually (such as [`SolverDelegateEvalExt::evaluate_root_goal`]).
315pub(super) fn enter_root<R>(
316 delegate: &D,
317 root_depth: usize,
318 origin_span: I::Span,
319 f: impl FnOnce(&mut EvalCtxt<'_, D>) -> R,
320 ) -> R {
321let mut search_graph = SearchGraph::new(root_depth);
322323let mut ecx = EvalCtxt {
324delegate,
325 search_graph: &mut search_graph,
326 nested_goals: Default::default(),
327 inspect: inspect::EvaluationStepBuilder::new_noop(),
328329// Only relevant when canonicalizing the response,
330 // which we don't do within this evaluation context.
331max_input_universe: ty::UniverseIndex::ROOT,
332 initial_opaque_types_storage_num_entries: Default::default(),
333 var_kinds: Default::default(),
334 var_values: CanonicalVarValues::dummy(),
335 current_goal_kind: CurrentGoalKind::Misc,
336origin_span,
337 tainted: Ok(()),
338 };
339let result = f(&mut ecx);
340if !ecx.nested_goals.is_empty() {
{
::core::panicking::panic_fmt(format_args!("root `EvalCtxt` should not have any goals added to it"));
}
};assert!(
341 ecx.nested_goals.is_empty(),
342"root `EvalCtxt` should not have any goals added to it"
343);
344if !search_graph.is_empty() {
::core::panicking::panic("assertion failed: search_graph.is_empty()")
};assert!(search_graph.is_empty());
345result346 }
347348/// Creates a nested evaluation context that shares the same search graph as the
349 /// one passed in. This is suitable for evaluation, granted that the search graph
350 /// has had the nested goal recorded on its stack. This method only be used by
351 /// `search_graph::Delegate::compute_goal`.
352 ///
353 /// This function takes care of setting up the inference context, setting the anchor,
354 /// and registering opaques from the canonicalized input.
355pub(super) fn enter_canonical<R>(
356 cx: I,
357 search_graph: &'a mut SearchGraph<D>,
358 canonical_input: CanonicalInput<I>,
359 proof_tree_builder: &mut inspect::ProofTreeBuilder<D>,
360 f: impl FnOnce(&mut EvalCtxt<'_, D>, Goal<I, I::Predicate>) -> R,
361 ) -> R {
362let (ref delegate, input, var_values) = D::build_with_canonical(cx, &canonical_input);
363for (key, ty) in input.predefined_opaques_in_body.iter() {
364let prev = delegate.register_hidden_type_in_storage(key, ty, I::Span::dummy());
365// It may be possible that two entries in the opaque type storage end up
366 // with the same key after resolving contained inference variables.
367 //
368 // We could put them in the duplicate list but don't have to. The opaques we
369 // encounter here are already tracked in the caller, so there's no need to
370 // also store them here. We'd take them out when computing the query response
371 // and then discard them, as they're already present in the input.
372 //
373 // Ideally we'd drop duplicate opaque type definitions when computing
374 // the canonical input. This is more annoying to implement and may cause a
375 // perf regression, so we do it inside of the query for now.
376if let Some(prev) = prev {
377{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs:377",
"rustc_next_trait_solver::solve::eval_ctxt",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs"),
::tracing_core::__macro_support::Option::Some(377u32),
::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::eval_ctxt"),
::tracing_core::field::FieldSet::new(&["message", "key",
"ty", "prev"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("ignore duplicate in `opaque_types_storage`")
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&key) as
&dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&ty) as
&dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&prev) as
&dyn Value))])
});
} else { ; }
};debug!(?key, ?ty, ?prev, "ignore duplicate in `opaque_types_storage`");
378 }
379 }
380381let initial_opaque_types_storage_num_entries = delegate.opaque_types_storage_num_entries();
382let mut ecx = EvalCtxt {
383delegate,
384 var_kinds: canonical_input.canonical.var_kinds,
385var_values,
386 current_goal_kind: CurrentGoalKind::from_query_input(cx, input),
387 max_input_universe: canonical_input.canonical.max_universe,
388initial_opaque_types_storage_num_entries,
389search_graph,
390 nested_goals: Default::default(),
391 origin_span: I::Span::dummy(),
392 tainted: Ok(()),
393 inspect: proof_tree_builder.new_evaluation_step(var_values),
394 };
395396let result = f(&mut ecx, input.goal);
397ecx.inspect.probe_final_state(ecx.delegate, ecx.max_input_universe);
398proof_tree_builder.finish_evaluation_step(ecx.inspect);
399400// When creating a query response we clone the opaque type constraints
401 // instead of taking them. This would cause an ICE here, since we have
402 // assertions against dropping an `InferCtxt` without taking opaques.
403 // FIXME: Once we remove support for the old impl we can remove this.
404 // FIXME: Could we make `build_with_canonical` into `enter_with_canonical` and call this at the end?
405delegate.reset_opaque_types();
406407result408 }
409410pub(super) fn ignore_candidate_head_usages(&mut self, usages: CandidateHeadUsages) {
411self.search_graph.ignore_candidate_head_usages(usages);
412 }
413414/// Recursively evaluates `goal`, returning whether any inference vars have
415 /// been constrained and the certainty of the result.
416fn evaluate_goal(
417&mut self,
418 source: GoalSource,
419 goal: Goal<I, I::Predicate>,
420 stalled_on: Option<GoalStalledOn<I>>,
421 ) -> Result<GoalEvaluation<I>, NoSolution> {
422let (normalization_nested_goals, goal_evaluation) =
423self.evaluate_goal_raw(source, goal, stalled_on)?;
424if !normalization_nested_goals.is_empty() {
::core::panicking::panic("assertion failed: normalization_nested_goals.is_empty()")
};assert!(normalization_nested_goals.is_empty());
425Ok(goal_evaluation)
426 }
427428/// Recursively evaluates `goal`, returning the nested goals in case
429 /// the nested goal is a `NormalizesTo` goal.
430 ///
431 /// As all other goal kinds do not return any nested goals and
432 /// `NormalizesTo` is only used by `AliasRelate`, all other callsites
433 /// should use [`EvalCtxt::evaluate_goal`] which discards that empty
434 /// storage.
435pub(super) fn evaluate_goal_raw(
436&mut self,
437 source: GoalSource,
438 goal: Goal<I, I::Predicate>,
439 stalled_on: Option<GoalStalledOn<I>>,
440 ) -> Result<(NestedNormalizationGoals<I>, GoalEvaluation<I>), NoSolution> {
441// If we have run this goal before, and it was stalled, check that any of the goal's
442 // args have changed. Otherwise, we don't need to re-run the goal because it'll remain
443 // stalled, since it'll canonicalize the same way and evaluation is pure.
444if let Some(GoalStalledOn {
445 num_opaques,
446ref stalled_vars,
447ref sub_roots,
448 stalled_certainty,
449 }) = stalled_on450 && !stalled_vars.iter().any(|value| self.delegate.is_changed_arg(*value))
451 && !sub_roots452 .iter()
453 .any(|&vid| self.delegate.sub_unification_table_root_var(vid) != vid)
454 && !self.delegate.opaque_types_storage_num_entries().needs_reevaluation(num_opaques)
455 {
456return Ok((
457NestedNormalizationGoals::empty(),
458GoalEvaluation {
459goal,
460 certainty: stalled_certainty,
461 has_changed: HasChanged::No,
462stalled_on,
463 },
464 ));
465 }
466467// We only care about one entry per `OpaqueTypeKey` here,
468 // so we only canonicalize the lookup table and ignore
469 // duplicate entries.
470let opaque_types = self.delegate.clone_opaque_types_lookup_table();
471let (goal, opaque_types) = eager_resolve_vars(self.delegate, (goal, opaque_types));
472473let (orig_values, canonical_goal) = canonicalize_goal(self.delegate, goal, &opaque_types);
474let canonical_result = self.search_graph.evaluate_goal(
475self.cx(),
476canonical_goal,
477self.step_kind_for_source(source),
478&mut inspect::ProofTreeBuilder::new_noop(),
479 );
480let response = match canonical_result {
481Err(e) => return Err(e),
482Ok(response) => response,
483 };
484485let has_changed =
486if !has_only_region_constraints(response) { HasChanged::Yes } else { HasChanged::No };
487488// FIXME: We should revisit and consider removing this after
489 // *assumptions on binders* is available, like once we had done in the
490 // stabilization of `-Znext-solver=coherence`(#121848).
491 // We ignore constraints from the nested goals in leak check. This is to match
492 // with the old solver's behavior, which has separated evaluation and fulfillment,
493 // and the former doesn't consider outlives obligations from the later.
494let vis = match goal.predicate.kind().skip_binder() {
495 ty::PredicateKind::Clause(_)
496 | ty::PredicateKind::DynCompatible(_)
497 | ty::PredicateKind::Subtype(_)
498 | ty::PredicateKind::Coerce(_)
499 | ty::PredicateKind::ConstEquate(_, _)
500 | ty::PredicateKind::Ambiguous501 | ty::PredicateKind::NormalizesTo(_) => VisibleForLeakCheck::No,
502 ty::PredicateKind::AliasRelate(_, _, _) => VisibleForLeakCheck::Yes,
503 };
504505let (normalization_nested_goals, certainty) = instantiate_and_apply_query_response(
506self.delegate,
507goal.param_env,
508&orig_values,
509response,
510vis,
511self.origin_span,
512 );
513514// FIXME: We previously had an assert here that checked that recomputing
515 // a goal after applying its constraints did not change its response.
516 //
517 // This assert was removed as it did not hold for goals constraining
518 // an inference variable to a recursive alias, e.g. in
519 // tests/ui/traits/next-solver/overflow/recursive-self-normalization.rs.
520 //
521 // Once we have decided on how to handle trait-system-refactor-initiative#75,
522 // we should re-add an assert here.
523524let stalled_on = match certainty {
525 Certainty::Yes => None,
526 Certainty::Maybe { .. } => match has_changed {
527// FIXME: We could recompute a *new* set of stalled variables by walking
528 // through the orig values, resolving, and computing the root vars of anything
529 // that is not resolved. Only when *these* have changed is it meaningful
530 // to recompute this goal.
531HasChanged::Yes => None,
532 HasChanged::No => {
533let mut stalled_vars = orig_values;
534535// Remove the unconstrained RHS arg, which is expected to have changed.
536if let Some(normalizes_to) = goal.predicate.as_normalizes_to() {
537let normalizes_to = normalizes_to.skip_binder();
538let rhs_arg: I::GenericArg = normalizes_to.term.into();
539let idx = stalled_vars540 .iter()
541 .rposition(|arg| *arg == rhs_arg)
542 .expect("expected unconstrained arg");
543stalled_vars.swap_remove(idx);
544 }
545546// Remove the canonicalized universal vars, since we only care about stalled existentials.
547let mut sub_roots = Vec::new();
548stalled_vars.retain(|arg| match arg.kind() {
549// Lifetimes can never stall goals.
550ty::GenericArgKind::Lifetime(_) => false,
551 ty::GenericArgKind::Type(ty) => match ty.kind() {
552 ty::Infer(ty::TyVar(vid)) => {
553sub_roots.push(self.delegate.sub_unification_table_root_var(vid));
554true
555}
556 ty::Infer(_) => true,
557 ty::Param(_) | ty::Placeholder(_) => false,
558_ => {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("unexpected orig_value: {0:?}", ty)));
}unreachable!("unexpected orig_value: {ty:?}"),
559 },
560 ty::GenericArgKind::Const(ct) => match ct.kind() {
561 ty::ConstKind::Infer(_) => true,
562 ty::ConstKind::Param(_) | ty::ConstKind::Placeholder(_) => false,
563_ => {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("unexpected orig_value: {0:?}", ct)));
}unreachable!("unexpected orig_value: {ct:?}"),
564 },
565 });
566567Some(GoalStalledOn {
568 num_opaques: canonical_goal569 .canonical
570 .value
571 .predefined_opaques_in_body
572 .len(),
573stalled_vars,
574sub_roots,
575 stalled_certainty: certainty,
576 })
577 }
578 },
579 };
580581Ok((
582normalization_nested_goals,
583GoalEvaluation { goal, certainty, has_changed, stalled_on },
584 ))
585 }
586587pub(super) fn compute_goal(&mut self, goal: Goal<I, I::Predicate>) -> QueryResult<I> {
588let Goal { param_env, predicate } = goal;
589let kind = predicate.kind();
590self.enter_forall(kind, |ecx, kind| match kind {
591 ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)) => {
592ecx.compute_trait_goal(Goal { param_env, predicate }).map(|(r, _via)| r)
593 }
594 ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(predicate)) => {
595ecx.compute_host_effect_goal(Goal { param_env, predicate })
596 }
597 ty::PredicateKind::Clause(ty::ClauseKind::Projection(predicate)) => {
598ecx.compute_projection_goal(Goal { param_env, predicate })
599 }
600 ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(predicate)) => {
601ecx.compute_type_outlives_goal(Goal { param_env, predicate })
602 }
603 ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(predicate)) => {
604ecx.compute_region_outlives_goal(Goal { param_env, predicate })
605 }
606 ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ty)) => {
607ecx.compute_const_arg_has_type_goal(Goal { param_env, predicate: (ct, ty) })
608 }
609 ty::PredicateKind::Clause(ty::ClauseKind::UnstableFeature(symbol)) => {
610ecx.compute_unstable_feature_goal(param_env, symbol)
611 }
612 ty::PredicateKind::Subtype(predicate) => {
613ecx.compute_subtype_goal(Goal { param_env, predicate })
614 }
615 ty::PredicateKind::Coerce(predicate) => {
616ecx.compute_coerce_goal(Goal { param_env, predicate })
617 }
618 ty::PredicateKind::DynCompatible(trait_def_id) => {
619ecx.compute_dyn_compatible_goal(trait_def_id)
620 }
621 ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(term)) => {
622ecx.compute_well_formed_goal(Goal { param_env, predicate: term })
623 }
624 ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(ct)) => {
625ecx.compute_const_evaluatable_goal(Goal { param_env, predicate: ct })
626 }
627 ty::PredicateKind::ConstEquate(_, _) => {
628{
::core::panicking::panic_fmt(format_args!("ConstEquate should not be emitted when `-Znext-solver` is active"));
}panic!("ConstEquate should not be emitted when `-Znext-solver` is active")629 }
630 ty::PredicateKind::NormalizesTo(predicate) => {
631ecx.compute_normalizes_to_goal(Goal { param_env, predicate })
632 }
633 ty::PredicateKind::AliasRelate(lhs, rhs, direction) => {
634ecx.compute_alias_relate_goal(Goal { param_env, predicate: (lhs, rhs, direction) })
635 }
636 ty::PredicateKind::Ambiguous => {
637ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
638 }
639 })
640 }
641642// Recursively evaluates all the goals added to this `EvalCtxt` to completion, returning
643 // the certainty of all the goals.
644#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("try_evaluate_added_goals",
"rustc_next_trait_solver::solve::eval_ctxt",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs"),
::tracing_core::__macro_support::Option::Some(644u32),
::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::eval_ctxt"),
::tracing_core::field::FieldSet::new(&[],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{ meta.fields().value_set(&[]) })
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Result<Certainty, NoSolution> =
loop {};
return __tracing_attr_fake_return;
}
{
for _ in 0..FIXPOINT_STEP_LIMIT {
match self.evaluate_added_goals_step() {
Ok(None) => {}
Ok(Some(cert)) => return Ok(cert),
Err(NoSolution) => {
self.tainted = Err(NoSolution);
return Err(NoSolution);
}
}
}
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs:657",
"rustc_next_trait_solver::solve::eval_ctxt",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs"),
::tracing_core::__macro_support::Option::Some(657u32),
::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::eval_ctxt"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("try_evaluate_added_goals: encountered overflow")
as &dyn Value))])
});
} else { ; }
};
Ok(Certainty::overflow(false))
}
}
}#[instrument(level = "trace", skip(self))]645pub(super) fn try_evaluate_added_goals(&mut self) -> Result<Certainty, NoSolution> {
646for _ in 0..FIXPOINT_STEP_LIMIT {
647match self.evaluate_added_goals_step() {
648Ok(None) => {}
649Ok(Some(cert)) => return Ok(cert),
650Err(NoSolution) => {
651self.tainted = Err(NoSolution);
652return Err(NoSolution);
653 }
654 }
655 }
656657debug!("try_evaluate_added_goals: encountered overflow");
658Ok(Certainty::overflow(false))
659 }
660661/// Iterate over all added goals: returning `Ok(Some(_))` in case we can stop rerunning.
662 ///
663 /// Goals for the next step get directly added to the nested goals of the `EvalCtxt`.
664fn evaluate_added_goals_step(&mut self) -> Result<Option<Certainty>, NoSolution> {
665let cx = self.cx();
666// If this loop did not result in any progress, what's our final certainty.
667let mut unchanged_certainty = Some(Certainty::Yes);
668for (source, goal, stalled_on) in mem::take(&mut self.nested_goals) {
669if let Some(certainty) = self.delegate.compute_goal_fast_path(goal, self.origin_span) {
670match certainty {
671 Certainty::Yes => {}
672 Certainty::Maybe { .. } => {
673self.nested_goals.push((source, goal, None));
674 unchanged_certainty = unchanged_certainty.map(|c| c.and(certainty));
675 }
676 }
677continue;
678 }
679680// We treat normalizes-to goals specially here. In each iteration we take the
681 // RHS of the projection, replace it with a fresh inference variable, and only
682 // after evaluating that goal do we equate the fresh inference variable with the
683 // actual RHS of the predicate.
684 //
685 // This is both to improve caching, and to avoid using the RHS of the
686 // projection predicate to influence the normalizes-to candidate we select.
687 //
688 // Forgetting to replace the RHS with a fresh inference variable when we evaluate
689 // this goal results in an ICE.
690if let Some(pred) = goal.predicate.as_normalizes_to() {
691// We should never encounter higher-ranked normalizes-to goals.
692let pred = pred.no_bound_vars().unwrap();
693// Replace the goal with an unconstrained infer var, so the
694 // RHS does not affect projection candidate assembly.
695let unconstrained_rhs = self.next_term_infer_of_kind(pred.term);
696let unconstrained_goal =
697 goal.with(cx, ty::NormalizesTo { alias: pred.alias, term: unconstrained_rhs });
698699let (
700 NestedNormalizationGoals(nested_goals),
701 GoalEvaluation { goal, certainty, stalled_on, has_changed: _ },
702 ) = self.evaluate_goal_raw(source, unconstrained_goal, stalled_on)?;
703// Add the nested goals from normalization to our own nested goals.
704{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs:704",
"rustc_next_trait_solver::solve::eval_ctxt",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs"),
::tracing_core::__macro_support::Option::Some(704u32),
::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::eval_ctxt"),
::tracing_core::field::FieldSet::new(&["nested_goals"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&nested_goals)
as &dyn Value))])
});
} else { ; }
};trace!(?nested_goals);
705self.nested_goals.extend(nested_goals.into_iter().map(|(s, g)| (s, g, None)));
706707// Finally, equate the goal's RHS with the unconstrained var.
708 //
709 // SUBTLE:
710 // We structurally relate aliases here. This is necessary
711 // as we otherwise emit a nested `AliasRelate` goal in case the
712 // returned term is a rigid alias, resulting in overflow.
713 //
714 // It is correct as both `goal.predicate.term` and `unconstrained_rhs`
715 // start out as an unconstrained inference variable so any aliases get
716 // fully normalized when instantiating it.
717 //
718 // FIXME: Strictly speaking this may be incomplete if the normalized-to
719 // type contains an ambiguous alias referencing bound regions. We should
720 // consider changing this to only use "shallow structural equality".
721self.eq_structurally_relating_aliases(
722 goal.param_env,
723 pred.term,
724 unconstrained_rhs,
725 )?;
726727// We only look at the `projection_ty` part here rather than
728 // looking at the "has changed" return from evaluate_goal,
729 // because we expect the `unconstrained_rhs` part of the predicate
730 // to have changed -- that means we actually normalized successfully!
731 // FIXME: Do we need to eagerly resolve here? Or should we check
732 // if the cache key has any changed vars?
733let with_resolved_vars = self.resolve_vars_if_possible(goal);
734if pred.alias
735 != with_resolved_vars
736 .predicate
737 .as_normalizes_to()
738 .unwrap()
739 .no_bound_vars()
740 .unwrap()
741 .alias
742 {
743 unchanged_certainty = None;
744 }
745746match certainty {
747 Certainty::Yes => {}
748 Certainty::Maybe { .. } => {
749self.nested_goals.push((source, with_resolved_vars, stalled_on));
750 unchanged_certainty = unchanged_certainty.map(|c| c.and(certainty));
751 }
752 }
753 } else {
754let GoalEvaluation { goal, certainty, has_changed, stalled_on } =
755self.evaluate_goal(source, goal, stalled_on)?;
756if has_changed == HasChanged::Yes {
757 unchanged_certainty = None;
758 }
759760match certainty {
761 Certainty::Yes => {}
762 Certainty::Maybe { .. } => {
763self.nested_goals.push((source, goal, stalled_on));
764 unchanged_certainty = unchanged_certainty.map(|c| c.and(certainty));
765 }
766 }
767 }
768 }
769770Ok(unchanged_certainty)
771 }
772773/// Record impl args in the proof tree for later access by `InspectCandidate`.
774pub(crate) fn record_impl_args(&mut self, impl_args: I::GenericArgs) {
775self.inspect.record_impl_args(self.delegate, self.max_input_universe, impl_args)
776 }
777778pub(super) fn cx(&self) -> I {
779self.delegate.cx()
780 }
781782#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("add_goal",
"rustc_next_trait_solver::solve::eval_ctxt",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs"),
::tracing_core::__macro_support::Option::Some(782u32),
::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::eval_ctxt"),
::tracing_core::field::FieldSet::new(&["source", "goal"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&source)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&goal)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: () = loop {};
return __tracing_attr_fake_return;
}
{
goal.predicate =
goal.predicate.fold_with(&mut ReplaceAliasWithInfer::new(self,
source, goal.param_env));
self.inspect.add_goal(self.delegate, self.max_input_universe,
source, goal);
self.nested_goals.push((source, goal, None));
}
}
}#[instrument(level = "debug", skip(self))]783pub(super) fn add_goal(&mut self, source: GoalSource, mut goal: Goal<I, I::Predicate>) {
784 goal.predicate =
785 goal.predicate.fold_with(&mut ReplaceAliasWithInfer::new(self, source, goal.param_env));
786self.inspect.add_goal(self.delegate, self.max_input_universe, source, goal);
787self.nested_goals.push((source, goal, None));
788 }
789790#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("add_goals",
"rustc_next_trait_solver::solve::eval_ctxt",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs"),
::tracing_core::__macro_support::Option::Some(790u32),
::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::eval_ctxt"),
::tracing_core::field::FieldSet::new(&["source"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&source)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: () = loop {};
return __tracing_attr_fake_return;
}
{ for goal in goals { self.add_goal(source, goal); } }
}
}#[instrument(level = "trace", skip(self, goals))]791pub(super) fn add_goals(
792&mut self,
793 source: GoalSource,
794 goals: impl IntoIterator<Item = Goal<I, I::Predicate>>,
795 ) {
796for goal in goals {
797self.add_goal(source, goal);
798 }
799 }
800801pub(super) fn next_region_var(&mut self) -> I::Region {
802let region = self.delegate.next_region_infer();
803self.inspect.add_var_value(region);
804region805 }
806807pub(super) fn next_ty_infer(&mut self) -> I::Ty {
808let ty = self.delegate.next_ty_infer();
809self.inspect.add_var_value(ty);
810ty811 }
812813pub(super) fn next_const_infer(&mut self) -> I::Const {
814let ct = self.delegate.next_const_infer();
815self.inspect.add_var_value(ct);
816ct817 }
818819/// Returns a ty infer or a const infer depending on whether `kind` is a `Ty` or `Const`.
820 /// If `kind` is an integer inference variable this will still return a ty infer var.
821pub(super) fn next_term_infer_of_kind(&mut self, term: I::Term) -> I::Term {
822match term.kind() {
823 ty::TermKind::Ty(_) => self.next_ty_infer().into(),
824 ty::TermKind::Const(_) => self.next_const_infer().into(),
825 }
826 }
827828/// Is the projection predicate is of the form `exists<T> <Ty as Trait>::Assoc = T`.
829 ///
830 /// This is the case if the `term` does not occur in any other part of the predicate
831 /// and is able to name all other placeholder and inference variables.
832x;#[instrument(level = "trace", skip(self), ret)]833pub(super) fn term_is_fully_unconstrained(&self, goal: Goal<I, ty::NormalizesTo<I>>) -> bool {
834let universe_of_term = match goal.predicate.term.kind() {
835 ty::TermKind::Ty(ty) => {
836if let ty::Infer(ty::TyVar(vid)) = ty.kind() {
837self.delegate.universe_of_ty(vid).unwrap()
838 } else {
839return false;
840 }
841 }
842 ty::TermKind::Const(ct) => {
843if let ty::ConstKind::Infer(ty::InferConst::Var(vid)) = ct.kind() {
844self.delegate.universe_of_ct(vid).unwrap()
845 } else {
846return false;
847 }
848 }
849 };
850851struct ContainsTermOrNotNameable<'a, D: SolverDelegate<Interner = I>, I: Interner> {
852 term: I::Term,
853 universe_of_term: ty::UniverseIndex,
854 delegate: &'a D,
855 cache: HashSet<I::Ty>,
856 }
857858impl<D: SolverDelegate<Interner = I>, I: Interner> ContainsTermOrNotNameable<'_, D, I> {
859fn check_nameable(&self, universe: ty::UniverseIndex) -> ControlFlow<()> {
860if self.universe_of_term.can_name(universe) {
861 ControlFlow::Continue(())
862 } else {
863 ControlFlow::Break(())
864 }
865 }
866 }
867868impl<D: SolverDelegate<Interner = I>, I: Interner> TypeVisitor<I>
869for ContainsTermOrNotNameable<'_, D, I>
870 {
871type Result = ControlFlow<()>;
872fn visit_ty(&mut self, t: I::Ty) -> Self::Result {
873if self.cache.contains(&t) {
874return ControlFlow::Continue(());
875 }
876877match t.kind() {
878 ty::Infer(ty::TyVar(vid)) => {
879if let ty::TermKind::Ty(term) = self.term.kind()
880 && let ty::Infer(ty::TyVar(term_vid)) = term.kind()
881 && self.delegate.root_ty_var(vid) == self.delegate.root_ty_var(term_vid)
882 {
883return ControlFlow::Break(());
884 }
885886self.check_nameable(self.delegate.universe_of_ty(vid).unwrap())?;
887 }
888 ty::Placeholder(p) => self.check_nameable(p.universe())?,
889_ => {
890if t.has_non_region_infer() || t.has_placeholders() {
891 t.super_visit_with(self)?
892}
893 }
894 }
895896assert!(self.cache.insert(t));
897 ControlFlow::Continue(())
898 }
899900fn visit_const(&mut self, c: I::Const) -> Self::Result {
901match c.kind() {
902 ty::ConstKind::Infer(ty::InferConst::Var(vid)) => {
903if let ty::TermKind::Const(term) = self.term.kind()
904 && let ty::ConstKind::Infer(ty::InferConst::Var(term_vid)) = term.kind()
905 && self.delegate.root_const_var(vid)
906 == self.delegate.root_const_var(term_vid)
907 {
908return ControlFlow::Break(());
909 }
910911self.check_nameable(self.delegate.universe_of_ct(vid).unwrap())
912 }
913 ty::ConstKind::Placeholder(p) => self.check_nameable(p.universe()),
914_ => {
915if c.has_non_region_infer() || c.has_placeholders() {
916 c.super_visit_with(self)
917 } else {
918 ControlFlow::Continue(())
919 }
920 }
921 }
922 }
923924fn visit_predicate(&mut self, p: I::Predicate) -> Self::Result {
925if p.has_non_region_infer() || p.has_placeholders() {
926 p.super_visit_with(self)
927 } else {
928 ControlFlow::Continue(())
929 }
930 }
931932fn visit_clauses(&mut self, c: I::Clauses) -> Self::Result {
933if c.has_non_region_infer() || c.has_placeholders() {
934 c.super_visit_with(self)
935 } else {
936 ControlFlow::Continue(())
937 }
938 }
939 }
940941let mut visitor = ContainsTermOrNotNameable {
942 delegate: self.delegate,
943 universe_of_term,
944 term: goal.predicate.term,
945 cache: Default::default(),
946 };
947 goal.predicate.alias.visit_with(&mut visitor).is_continue()
948 && goal.param_env.visit_with(&mut visitor).is_continue()
949 }
950951pub(super) fn sub_unify_ty_vids_raw(&self, a: ty::TyVid, b: ty::TyVid) {
952self.delegate.sub_unify_ty_vids_raw(a, b)
953 }
954955x;#[instrument(level = "trace", skip(self, param_env), ret)]956pub(super) fn eq<T: Relate<I>>(
957&mut self,
958 param_env: I::ParamEnv,
959 lhs: T,
960 rhs: T,
961 ) -> Result<(), NoSolution> {
962self.relate(param_env, lhs, ty::Variance::Invariant, rhs)
963 }
964965/// This should be used when relating a rigid alias with another type.
966 ///
967 /// Normally we emit a nested `AliasRelate` when equating an inference
968 /// variable and an alias. This causes us to instead constrain the inference
969 /// variable to the alias without emitting a nested alias relate goals.
970x;#[instrument(level = "trace", skip(self, param_env), ret)]971pub(super) fn relate_rigid_alias_non_alias(
972&mut self,
973 param_env: I::ParamEnv,
974 alias: ty::AliasTerm<I>,
975 variance: ty::Variance,
976 term: I::Term,
977 ) -> Result<(), NoSolution> {
978// NOTE: this check is purely an optimization, the structural eq would
979 // always fail if the term is not an inference variable.
980if term.is_infer() {
981let cx = self.cx();
982// We need to relate `alias` to `term` treating only the outermost
983 // constructor as rigid, relating any contained generic arguments as
984 // normal. We do this by first structurally equating the `term`
985 // with the alias constructor instantiated with unconstrained infer vars,
986 // and then relate this with the whole `alias`.
987 //
988 // Alternatively we could modify `Equate` for this case by adding another
989 // variant to `StructurallyRelateAliases`.
990let identity_args = self.fresh_args_for_item(alias.def_id());
991let rigid_ctor = alias.with_args(cx, identity_args);
992let ctor_term = rigid_ctor.to_term(cx);
993let obligations = self.delegate.eq_structurally_relating_aliases(
994 param_env,
995 term,
996 ctor_term,
997self.origin_span,
998 )?;
999debug_assert!(obligations.is_empty());
1000self.relate(param_env, alias, variance, rigid_ctor)
1001 } else {
1002Err(NoSolution)
1003 }
1004 }
10051006/// This should only be used when we're either instantiating a previously
1007 /// unconstrained "return value" or when we're sure that all aliases in
1008 /// the types are rigid.
1009x;#[instrument(level = "trace", skip(self, param_env), ret)]1010pub(super) fn eq_structurally_relating_aliases<T: Relate<I>>(
1011&mut self,
1012 param_env: I::ParamEnv,
1013 lhs: T,
1014 rhs: T,
1015 ) -> Result<(), NoSolution> {
1016let result = self.delegate.eq_structurally_relating_aliases(
1017 param_env,
1018 lhs,
1019 rhs,
1020self.origin_span,
1021 )?;
1022assert_eq!(result, vec![]);
1023Ok(())
1024 }
10251026x;#[instrument(level = "trace", skip(self, param_env), ret)]1027pub(super) fn sub<T: Relate<I>>(
1028&mut self,
1029 param_env: I::ParamEnv,
1030 sub: T,
1031 sup: T,
1032 ) -> Result<(), NoSolution> {
1033self.relate(param_env, sub, ty::Variance::Covariant, sup)
1034 }
10351036x;#[instrument(level = "trace", skip(self, param_env), ret)]1037pub(super) fn relate<T: Relate<I>>(
1038&mut self,
1039 param_env: I::ParamEnv,
1040 lhs: T,
1041 variance: ty::Variance,
1042 rhs: T,
1043 ) -> Result<(), NoSolution> {
1044let goals = self.delegate.relate(param_env, lhs, variance, rhs, self.origin_span)?;
1045for &goal in goals.iter() {
1046let source = match goal.predicate.kind().skip_binder() {
1047 ty::PredicateKind::Subtype { .. } | ty::PredicateKind::AliasRelate(..) => {
1048 GoalSource::TypeRelating
1049 }
1050// FIXME(-Znext-solver=coinductive): should these WF goals also be unproductive?
1051ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_)) => GoalSource::Misc,
1052 p => unreachable!("unexpected nested goal in `relate`: {p:?}"),
1053 };
1054self.add_goal(source, goal);
1055 }
1056Ok(())
1057 }
10581059/// Equates two values returning the nested goals without adding them
1060 /// to the nested goals of the `EvalCtxt`.
1061 ///
1062 /// If possible, try using `eq` instead which automatically handles nested
1063 /// goals correctly.
1064x;#[instrument(level = "trace", skip(self, param_env), ret)]1065pub(super) fn eq_and_get_goals<T: Relate<I>>(
1066&self,
1067 param_env: I::ParamEnv,
1068 lhs: T,
1069 rhs: T,
1070 ) -> Result<Vec<Goal<I, I::Predicate>>, NoSolution> {
1071Ok(self.delegate.relate(param_env, lhs, ty::Variance::Invariant, rhs, self.origin_span)?)
1072 }
10731074pub(super) fn instantiate_binder_with_infer<T: TypeFoldable<I> + Copy>(
1075&self,
1076 value: ty::Binder<I, T>,
1077 ) -> T {
1078self.delegate.instantiate_binder_with_infer(value)
1079 }
10801081/// `enter_forall`, but takes `&mut self` and passes it back through the
1082 /// callback since it can't be aliased during the call.
1083pub(super) fn enter_forall<T: TypeFoldable<I>, U>(
1084&mut self,
1085 value: ty::Binder<I, T>,
1086 f: impl FnOnce(&mut Self, T) -> U,
1087 ) -> U {
1088self.delegate.enter_forall(value, |value| f(self, value))
1089 }
10901091pub(super) fn resolve_vars_if_possible<T>(&self, value: T) -> T
1092where
1093T: TypeFoldable<I>,
1094 {
1095self.delegate.resolve_vars_if_possible(value)
1096 }
10971098pub(super) fn shallow_resolve(&self, ty: I::Ty) -> I::Ty {
1099self.delegate.shallow_resolve(ty)
1100 }
11011102pub(super) fn eager_resolve_region(&self, r: I::Region) -> I::Region {
1103if let ty::ReVar(vid) = r.kind() {
1104self.delegate.opportunistic_resolve_lt_var(vid)
1105 } else {
1106r1107 }
1108 }
11091110pub(super) fn fresh_args_for_item(&mut self, def_id: I::DefId) -> I::GenericArgs {
1111let args = self.delegate.fresh_args_for_item(def_id);
1112for arg in args.iter() {
1113self.inspect.add_var_value(arg);
1114 }
1115args1116 }
11171118pub(super) fn register_ty_outlives(&self, ty: I::Ty, lt: I::Region) {
1119self.delegate.register_ty_outlives(ty, lt, self.origin_span);
1120 }
11211122pub(super) fn register_region_outlives(
1123&self,
1124 a: I::Region,
1125 b: I::Region,
1126 vis: VisibleForLeakCheck,
1127 ) {
1128// `'a: 'b` ==> `'b <= 'a`
1129self.delegate.sub_regions(b, a, vis, self.origin_span);
1130 }
11311132/// Computes the list of goals required for `arg` to be well-formed
1133pub(super) fn well_formed_goals(
1134&self,
1135 param_env: I::ParamEnv,
1136 term: I::Term,
1137 ) -> Option<Vec<Goal<I, I::Predicate>>> {
1138self.delegate.well_formed_goals(param_env, term)
1139 }
11401141pub(super) fn trait_ref_is_knowable(
1142&mut self,
1143 param_env: I::ParamEnv,
1144 trait_ref: ty::TraitRef<I>,
1145 ) -> Result<bool, NoSolution> {
1146let delegate = self.delegate;
1147let lazily_normalize_ty = |ty| self.structurally_normalize_ty(param_env, ty);
1148 coherence::trait_ref_is_knowable(&**delegate, trait_ref, lazily_normalize_ty)
1149 .map(|is_knowable| is_knowable.is_ok())
1150 }
11511152pub(super) fn fetch_eligible_assoc_item(
1153&self,
1154 goal_trait_ref: ty::TraitRef<I>,
1155 trait_assoc_def_id: I::DefId,
1156 impl_def_id: I::ImplId,
1157 ) -> Result<Option<I::DefId>, I::ErrorGuaranteed> {
1158self.delegate.fetch_eligible_assoc_item(goal_trait_ref, trait_assoc_def_id, impl_def_id)
1159 }
11601161x;#[instrument(level = "debug", skip(self), ret)]1162pub(super) fn register_hidden_type_in_storage(
1163&mut self,
1164 opaque_type_key: ty::OpaqueTypeKey<I>,
1165 hidden_ty: I::Ty,
1166 ) -> Option<I::Ty> {
1167self.delegate.register_hidden_type_in_storage(opaque_type_key, hidden_ty, self.origin_span)
1168 }
11691170pub(super) fn add_item_bounds_for_hidden_type(
1171&mut self,
1172 opaque_def_id: I::DefId,
1173 opaque_args: I::GenericArgs,
1174 param_env: I::ParamEnv,
1175 hidden_ty: I::Ty,
1176 ) {
1177let mut goals = Vec::new();
1178self.delegate.add_item_bounds_for_hidden_type(
1179opaque_def_id,
1180opaque_args,
1181param_env,
1182hidden_ty,
1183&mut goals,
1184 );
1185self.add_goals(GoalSource::AliasWellFormed, goals);
1186 }
11871188// Try to evaluate a const, or return `None` if the const is too generic.
1189 // This doesn't mean the const isn't evaluatable, though, and should be treated
1190 // as an ambiguity rather than no-solution.
1191pub(super) fn evaluate_const(
1192&self,
1193 param_env: I::ParamEnv,
1194 uv: ty::UnevaluatedConst<I>,
1195 ) -> Option<I::Const> {
1196self.delegate.evaluate_const(param_env, uv)
1197 }
11981199pub(super) fn is_transmutable(
1200&mut self,
1201 src: I::Ty,
1202 dst: I::Ty,
1203 assume: I::Const,
1204 ) -> Result<Certainty, NoSolution> {
1205self.delegate.is_transmutable(dst, src, assume)
1206 }
12071208pub(super) fn replace_bound_vars<T: TypeFoldable<I>>(
1209&self,
1210 t: T,
1211 universes: &mut Vec<Option<ty::UniverseIndex>>,
1212 ) -> T {
1213BoundVarReplacer::replace_bound_vars(&**self.delegate, universes, t).0
1214}
12151216pub(super) fn may_use_unstable_feature(
1217&self,
1218 param_env: I::ParamEnv,
1219 symbol: I::Symbol,
1220 ) -> bool {
1221may_use_unstable_feature(&**self.delegate, param_env, symbol)
1222 }
12231224pub(crate) fn opaques_with_sub_unified_hidden_type(
1225&self,
1226 self_ty: I::Ty,
1227 ) -> Vec<ty::AliasTy<I>> {
1228if let ty::Infer(ty::TyVar(vid)) = self_ty.kind() {
1229self.delegate.opaques_with_sub_unified_hidden_type(vid)
1230 } else {
1231::alloc::vec::Vec::new()vec![]1232 }
1233 }
12341235/// To return the constraints of a canonical query to the caller, we canonicalize:
1236 ///
1237 /// - `var_values`: a map from bound variables in the canonical goal to
1238 /// the values inferred while solving the instantiated goal.
1239 /// - `external_constraints`: additional constraints which aren't expressible
1240 /// using simple unification of inference variables.
1241 ///
1242 /// This takes the `shallow_certainty` which represents whether we're confident
1243 /// that the final result of the current goal only depends on the nested goals.
1244 ///
1245 /// In case this is `Certainty::Maybe`, there may still be additional nested goals
1246 /// or inference constraints required for this candidate to be hold. The candidate
1247 /// always requires all already added constraints and nested goals.
1248x;#[instrument(level = "trace", skip(self), ret)]1249pub(in crate::solve) fn evaluate_added_goals_and_make_canonical_response(
1250&mut self,
1251 shallow_certainty: Certainty,
1252 ) -> QueryResult<I> {
1253self.inspect.make_canonical_response(shallow_certainty);
12541255let goals_certainty = self.try_evaluate_added_goals()?;
1256assert_eq!(
1257self.tainted,
1258Ok(()),
1259"EvalCtxt is tainted -- nested goals may have been dropped in a \
1260 previous call to `try_evaluate_added_goals!`"
1261);
12621263// We only check for leaks from universes which were entered inside
1264 // of the query.
1265self.delegate.leak_check(self.max_input_universe).map_err(|NoSolution| {
1266trace!("failed the leak check");
1267 NoSolution
1268 })?;
12691270let (certainty, normalization_nested_goals) =
1271match (self.current_goal_kind, shallow_certainty) {
1272// When normalizing, we've replaced the expected term with an unconstrained
1273 // inference variable. This means that we dropped information which could
1274 // have been important. We handle this by instead returning the nested goals
1275 // to the caller, where they are then handled. We only do so if we do not
1276 // need to recompute the `NormalizesTo` goal afterwards to avoid repeatedly
1277 // uplifting its nested goals. This is the case if the `shallow_certainty` is
1278 // `Certainty::Yes`.
1279(CurrentGoalKind::NormalizesTo, Certainty::Yes) => {
1280let goals = std::mem::take(&mut self.nested_goals);
1281// As we return all ambiguous nested goals, we can ignore the certainty
1282 // returned by `self.try_evaluate_added_goals()`.
1283if goals.is_empty() {
1284assert!(matches!(goals_certainty, Certainty::Yes));
1285 }
1286 (
1287 Certainty::Yes,
1288 NestedNormalizationGoals(
1289 goals.into_iter().map(|(s, g, _)| (s, g)).collect(),
1290 ),
1291 )
1292 }
1293_ => {
1294let certainty = shallow_certainty.and(goals_certainty);
1295 (certainty, NestedNormalizationGoals::empty())
1296 }
1297 };
12981299if let Certainty::Maybe(
1300 maybe_info @ MaybeInfo {
1301 cause: MaybeCause::Overflow { keep_constraints: false, .. },
1302 opaque_types_jank: _,
1303 stalled_on_coroutines: _,
1304 },
1305 ) = certainty
1306 {
1307// If we have overflow, it's probable that we're substituting a type
1308 // into itself infinitely and any partial substitutions in the query
1309 // response are probably not useful anyways, so just return an empty
1310 // query response.
1311 //
1312 // This may prevent us from potentially useful inference, e.g.
1313 // 2 candidates, one ambiguous and one overflow, which both
1314 // have the same inference constraints.
1315 //
1316 // Changing this to retain some constraints in the future
1317 // won't be a breaking change, so this is good enough for now.
1318return Ok(self.make_ambiguous_response_no_constraints(maybe_info));
1319 }
13201321let external_constraints =
1322self.compute_external_query_constraints(certainty, normalization_nested_goals);
1323let (var_values, mut external_constraints) =
1324 eager_resolve_vars(self.delegate, (self.var_values, external_constraints));
13251326// Remove any trivial or duplicated region constraints once we've resolved regions
1327let mut unique = HashSet::default();
1328 external_constraints
1329 .region_constraints
1330 .retain(|(outlives, _)| !outlives.is_trivial() && unique.insert(*outlives));
13311332let canonical = canonicalize_response(
1333self.delegate,
1334self.max_input_universe,
1335 Response {
1336 var_values,
1337 certainty,
1338 external_constraints: self.cx().mk_external_constraints(external_constraints),
1339 },
1340 );
13411342Ok(canonical)
1343 }
13441345/// Constructs a totally unconstrained, ambiguous response to a goal.
1346 ///
1347 /// Take care when using this, since often it's useful to respond with
1348 /// ambiguity but return constrained variables to guide inference.
1349pub(in crate::solve) fn make_ambiguous_response_no_constraints(
1350&self,
1351 maybe: MaybeInfo,
1352 ) -> CanonicalResponse<I> {
1353response_no_constraints_raw(
1354self.cx(),
1355self.max_input_universe,
1356self.var_kinds,
1357 Certainty::Maybe(maybe),
1358 )
1359 }
13601361/// Computes the region constraints and *new* opaque types registered when
1362 /// proving a goal.
1363 ///
1364 /// If an opaque was already constrained before proving this goal, then the
1365 /// external constraints do not need to record that opaque, since if it is
1366 /// further constrained by inference, that will be passed back in the var
1367 /// values.
1368x;#[instrument(level = "trace", skip(self), ret)]1369fn compute_external_query_constraints(
1370&self,
1371 certainty: Certainty,
1372 normalization_nested_goals: NestedNormalizationGoals<I>,
1373 ) -> ExternalConstraintsData<I> {
1374// We only return region constraints once the certainty is `Yes`. This
1375 // is necessary as we may drop nested goals on ambiguity, which may result
1376 // in unconstrained inference variables in the region constraints. It also
1377 // prevents us from emitting duplicate region constraints, avoiding some
1378 // unnecessary work. This slightly weakens the leak check in case it uses
1379 // region constraints from an ambiguous nested goal. This is tested in both
1380 // `tests/ui/higher-ranked/leak-check/leak-check-in-selection-5-ambig.rs` and
1381 // `tests/ui/higher-ranked/leak-check/leak-check-in-selection-6-ambig-unify.rs`.
1382let region_constraints = if certainty == Certainty::Yes {
1383self.delegate.make_deduplicated_region_constraints()
1384 } else {
1385 Default::default()
1386 };
13871388// We only return *newly defined* opaque types from canonical queries.
1389 //
1390 // Constraints for any existing opaque types are already tracked by changes
1391 // to the `var_values`.
1392let opaque_types = self
1393.delegate
1394 .clone_opaque_types_added_since(self.initial_opaque_types_storage_num_entries);
13951396 ExternalConstraintsData { region_constraints, opaque_types, normalization_nested_goals }
1397 }
1398}
13991400/// Eagerly replace aliases with inference variables, emitting `AliasRelate`
1401/// goals, used when adding goals to the `EvalCtxt`. We compute the
1402/// `AliasRelate` goals before evaluating the actual goal to get all the
1403/// constraints we can.
1404///
1405/// This is a performance optimization to more eagerly detect cycles during trait
1406/// solving. See tests/ui/traits/next-solver/cycles/cycle-modulo-ambig-aliases.rs.
1407///
1408/// The emitted goals get evaluated in the context of the parent goal; by
1409/// replacing aliases in nested goals we essentially pull the normalization out of
1410/// the nested goal. We want to treat the goal as if the normalization still happens
1411/// inside of the nested goal by inheriting the `step_kind` of the nested goal and
1412/// storing it in the `GoalSource` of the emitted `AliasRelate` goals.
1413/// This is necessary for tests/ui/sized/coinductive-1.rs to compile.
1414struct ReplaceAliasWithInfer<'me, 'a, D, I>
1415where
1416D: SolverDelegate<Interner = I>,
1417 I: Interner,
1418{
1419 ecx: &'me mut EvalCtxt<'a, D>,
1420 param_env: I::ParamEnv,
1421 normalization_goal_source: GoalSource,
1422 cache: HashMap<I::Ty, I::Ty>,
1423}
14241425impl<'me, 'a, D, I> ReplaceAliasWithInfer<'me, 'a, D, I>
1426where
1427D: SolverDelegate<Interner = I>,
1428 I: Interner,
1429{
1430fn new(
1431 ecx: &'me mut EvalCtxt<'a, D>,
1432 for_goal_source: GoalSource,
1433 param_env: I::ParamEnv,
1434 ) -> Self {
1435let step_kind = ecx.step_kind_for_source(for_goal_source);
1436ReplaceAliasWithInfer {
1437ecx,
1438param_env,
1439 normalization_goal_source: GoalSource::NormalizeGoal(step_kind),
1440 cache: Default::default(),
1441 }
1442 }
1443}
14441445impl<D, I> TypeFolder<I> for ReplaceAliasWithInfer<'_, '_, D, I>
1446where
1447D: SolverDelegate<Interner = I>,
1448 I: Interner,
1449{
1450fn cx(&self) -> I {
1451self.ecx.cx()
1452 }
14531454fn fold_ty(&mut self, ty: I::Ty) -> I::Ty {
1455match ty.kind() {
1456 ty::Alias(..) if !ty.has_escaping_bound_vars() => {
1457let infer_ty = self.ecx.next_ty_infer();
1458let normalizes_to = ty::PredicateKind::AliasRelate(
1459ty.into(),
1460infer_ty.into(),
1461 ty::AliasRelationDirection::Equate,
1462 );
1463self.ecx.add_goal(
1464self.normalization_goal_source,
1465Goal::new(self.cx(), self.param_env, normalizes_to),
1466 );
1467infer_ty1468 }
1469_ => {
1470if !ty.has_aliases() {
1471ty1472 } else if let Some(&entry) = self.cache.get(&ty) {
1473return entry;
1474 } else {
1475let res = ty.super_fold_with(self);
1476if !self.cache.insert(ty, res).is_none() {
::core::panicking::panic("assertion failed: self.cache.insert(ty, res).is_none()")
};assert!(self.cache.insert(ty, res).is_none());
1477res1478 }
1479 }
1480 }
1481 }
14821483fn fold_const(&mut self, ct: I::Const) -> I::Const {
1484match ct.kind() {
1485 ty::ConstKind::Unevaluated(..) if !ct.has_escaping_bound_vars() => {
1486let infer_ct = self.ecx.next_const_infer();
1487let normalizes_to = ty::PredicateKind::AliasRelate(
1488ct.into(),
1489infer_ct.into(),
1490 ty::AliasRelationDirection::Equate,
1491 );
1492self.ecx.add_goal(
1493self.normalization_goal_source,
1494Goal::new(self.cx(), self.param_env, normalizes_to),
1495 );
1496infer_ct1497 }
1498_ => ct.super_fold_with(self),
1499 }
1500 }
15011502fn fold_predicate(&mut self, predicate: I::Predicate) -> I::Predicate {
1503if predicate.allow_normalization() { predicate.super_fold_with(self) } else { predicate }
1504 }
1505}
15061507/// Do not call this directly, use the `tcx` query instead.
1508pub fn evaluate_root_goal_for_proof_tree_raw_provider<
1509 D: SolverDelegate<Interner = I>,
1510 I: Interner,
1511>(
1512 cx: I,
1513 canonical_goal: CanonicalInput<I>,
1514) -> (QueryResult<I>, I::Probe) {
1515let mut inspect = inspect::ProofTreeBuilder::new();
1516let canonical_result = SearchGraph::<D>::evaluate_root_goal_for_proof_tree(
1517cx,
1518cx.recursion_limit(),
1519canonical_goal,
1520&mut inspect,
1521 );
1522let final_revision = inspect.unwrap();
1523 (canonical_result, cx.mk_probe(final_revision))
1524}
15251526/// Evaluate a goal to build a proof tree.
1527///
1528/// This is a copy of [EvalCtxt::evaluate_goal_raw] which avoids relying on the
1529/// [EvalCtxt] and uses a separate cache.
1530pub(super) fn evaluate_root_goal_for_proof_tree<D: SolverDelegate<Interner = I>, I: Interner>(
1531 delegate: &D,
1532 goal: Goal<I, I::Predicate>,
1533 origin_span: I::Span,
1534) -> (Result<NestedNormalizationGoals<I>, NoSolution>, inspect::GoalEvaluation<I>) {
1535let opaque_types = delegate.clone_opaque_types_lookup_table();
1536let (goal, opaque_types) = eager_resolve_vars(delegate, (goal, opaque_types));
15371538let (orig_values, canonical_goal) = canonicalize_goal(delegate, goal, &opaque_types);
15391540let (canonical_result, final_revision) =
1541delegate.cx().evaluate_root_goal_for_proof_tree_raw(canonical_goal);
15421543let proof_tree = inspect::GoalEvaluation {
1544 uncanonicalized_goal: goal,
1545orig_values,
1546final_revision,
1547 result: canonical_result,
1548 };
15491550let response = match canonical_result {
1551Err(e) => return (Err(e), proof_tree),
1552Ok(response) => response,
1553 };
15541555let (normalization_nested_goals, _certainty) = instantiate_and_apply_query_response(
1556delegate,
1557goal.param_env,
1558&proof_tree.orig_values,
1559response,
1560 VisibleForLeakCheck::Yes,
1561origin_span,
1562 );
15631564 (Ok(normalization_nested_goals), proof_tree)
1565}