1use std::mem;
2use std::ops::ControlFlow;
34#[cfg(feature = "nightly")]
5use rustc_macros::HashStable_NoContext;
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::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, inspect,
34};
3536mod probe;
3738/// The kind of goal we're currently proving.
39///
40/// This has effects on cycle handling handling and on how we compute
41/// query responses, see the variant descriptions for more info.
42#[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)]
43enum CurrentGoalKind {
44 Misc,
45/// We're proving an trait goal for a coinductive trait, either an auto trait or `Sized`.
46 ///
47 /// These are currently the only goals whose impl where-clauses are considered to be
48 /// productive steps.
49CoinductiveTrait,
50/// Unlike other goals, `NormalizesTo` goals act like functions with the expected term
51 /// always being fully unconstrained. This would weaken inference however, as the nested
52 /// goals never get the inference constraints from the actual normalized-to type.
53 ///
54 /// Because of this we return any ambiguous nested goals from `NormalizesTo` to the
55 /// caller when then adds these to its own context. The caller is always an `AliasRelate`
56 /// goal so this never leaks out of the solver.
57NormalizesTo,
58}
5960impl CurrentGoalKind {
61fn from_query_input<I: Interner>(cx: I, input: QueryInput<I, I::Predicate>) -> CurrentGoalKind {
62match input.goal.predicate.kind().skip_binder() {
63 ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
64if cx.trait_is_coinductive(pred.trait_ref.def_id) {
65 CurrentGoalKind::CoinductiveTrait66 } else {
67 CurrentGoalKind::Misc68 }
69 }
70 ty::PredicateKind::NormalizesTo(_) => CurrentGoalKind::NormalizesTo,
71_ => CurrentGoalKind::Misc,
72 }
73 }
74}
7576pub struct EvalCtxt<'a, D, I = <D as SolverDelegate>::Interner>
77where
78D: SolverDelegate<Interner = I>,
79 I: Interner,
80{
81/// The inference context that backs (mostly) inference and placeholder terms
82 /// instantiated while solving goals.
83 ///
84 /// NOTE: The `InferCtxt` that backs the `EvalCtxt` is intentionally private,
85 /// because the `InferCtxt` is much more general than `EvalCtxt`. Methods such
86 /// as `take_registered_region_obligations` can mess up query responses,
87 /// using `At::normalize` is totally wrong, calling `evaluate_root_goal` can
88 /// cause coinductive unsoundness, etc.
89 ///
90 /// Methods that are generally of use for trait solving are *intentionally*
91 /// re-declared through the `EvalCtxt` below, often with cleaner signatures
92 /// since we don't care about things like `ObligationCause`s and `Span`s here.
93 /// If some `InferCtxt` method is missing, please first think defensively about
94 /// the method's compatibility with this solver, or if an existing one does
95 /// the job already.
96delegate: &'a D,
9798/// The variable info for the `var_values`, only used to make an ambiguous response
99 /// with no constraints.
100var_kinds: I::CanonicalVarKinds,
101102/// What kind of goal we're currently computing, see the enum definition
103 /// for more info.
104current_goal_kind: CurrentGoalKind,
105pub(super) var_values: CanonicalVarValues<I>,
106107/// The highest universe index nameable by the caller.
108 ///
109 /// When we enter a new binder inside of the query we create new universes
110 /// which the caller cannot name. We have to be careful with variables from
111 /// these new universes when creating the query response.
112 ///
113 /// Both because these new universes can prevent us from reaching a fixpoint
114 /// if we have a coinductive cycle and because that's the only way we can return
115 /// new placeholders to the caller.
116pub(super) max_input_universe: ty::UniverseIndex,
117/// The opaque types from the canonical input. We only need to return opaque types
118 /// which have been added to the storage while evaluating this goal.
119pub(super) initial_opaque_types_storage_num_entries:
120 <D::Infcx as InferCtxtLike>::OpaqueTypeStorageEntries,
121122pub(super) search_graph: &'a mut SearchGraph<D>,
123124 nested_goals: Vec<(GoalSource, Goal<I, I::Predicate>, Option<GoalStalledOn<I>>)>,
125126pub(super) origin_span: I::Span,
127128// Has this `EvalCtxt` errored out with `NoSolution` in `try_evaluate_added_goals`?
129 //
130 // If so, then it can no longer be used to make a canonical query response,
131 // since subsequent calls to `try_evaluate_added_goals` have possibly dropped
132 // ambiguous goals. Instead, a probe needs to be introduced somewhere in the
133 // evaluation code.
134tainted: Result<(), NoSolution>,
135136pub(super) inspect: inspect::EvaluationStepBuilder<D>,
137}
138139#[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_receiver_is_total_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)]
140#[cfg_attr(feature = "nightly", derive(const _: () =
{
impl<__CTX> ::rustc_data_structures::stable_hasher::HashStable<__CTX>
for GenerateProofTree {
#[inline]
fn hash_stable(&self, __hcx: &mut __CTX,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
match *self {
GenerateProofTree::Yes => {}
GenerateProofTree::No => {}
}
}
}
};HashStable_NoContext))]
141pub enum GenerateProofTree {
142 Yes,
143 No,
144}
145146pub trait SolverDelegateEvalExt: SolverDelegate {
147/// Evaluates a goal from **outside** of the trait solver.
148 ///
149 /// Using this while inside of the solver is wrong as it uses a new
150 /// search graph which would break cycle detection.
151fn evaluate_root_goal(
152&self,
153 goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
154 span: <Self::Interner as Interner>::Span,
155 stalled_on: Option<GoalStalledOn<Self::Interner>>,
156 ) -> Result<GoalEvaluation<Self::Interner>, NoSolution>;
157158/// Checks whether evaluating `goal` may hold while treating not-yet-defined
159 /// opaque types as being kind of rigid.
160 ///
161 /// See the comment on [OpaqueTypesJank] for more details.
162fn root_goal_may_hold_opaque_types_jank(
163&self,
164 goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
165 ) -> bool;
166167/// Check whether evaluating `goal` with a depth of `root_depth` may
168 /// succeed. This only returns `false` if the goal is guaranteed to
169 /// not hold. In case evaluation overflows and fails with ambiguity this
170 /// returns `true`.
171 ///
172 /// This is only intended to be used as a performance optimization
173 /// in coherence checking.
174fn root_goal_may_hold_with_depth(
175&self,
176 root_depth: usize,
177 goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
178 ) -> bool;
179180// FIXME: This is only exposed because we need to use it in `analyse.rs`
181 // which is not yet uplifted. Once that's done, we should remove this.
182fn evaluate_root_goal_for_proof_tree(
183&self,
184 goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
185 span: <Self::Interner as Interner>::Span,
186 ) -> (
187Result<NestedNormalizationGoals<Self::Interner>, NoSolution>,
188 inspect::GoalEvaluation<Self::Interner>,
189 );
190}
191192impl<D, I> SolverDelegateEvalExtfor D
193where
194D: SolverDelegate<Interner = I>,
195 I: Interner,
196{
197x;#[instrument(level = "debug", skip(self), ret)]198fn evaluate_root_goal(
199&self,
200 goal: Goal<I, I::Predicate>,
201 span: I::Span,
202 stalled_on: Option<GoalStalledOn<I>>,
203 ) -> Result<GoalEvaluation<I>, NoSolution> {
204 EvalCtxt::enter_root(self, self.cx().recursion_limit(), span, |ecx| {
205 ecx.evaluate_goal(GoalSource::Misc, goal, stalled_on)
206 })
207 }
208209x;#[instrument(level = "debug", skip(self), ret)]210fn root_goal_may_hold_opaque_types_jank(
211&self,
212 goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
213 ) -> bool {
214self.probe(|| {
215 EvalCtxt::enter_root(self, self.cx().recursion_limit(), I::Span::dummy(), |ecx| {
216 ecx.evaluate_goal(GoalSource::Misc, goal, None)
217 })
218 .is_ok_and(|r| match r.certainty {
219 Certainty::Yes => true,
220 Certainty::Maybe { cause: _, opaque_types_jank } => match opaque_types_jank {
221 OpaqueTypesJank::AllGood => true,
222 OpaqueTypesJank::ErrorIfRigidSelfTy => false,
223 },
224 })
225 })
226 }
227228fn root_goal_may_hold_with_depth(
229&self,
230 root_depth: usize,
231 goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
232 ) -> bool {
233self.probe(|| {
234EvalCtxt::enter_root(self, root_depth, I::Span::dummy(), |ecx| {
235ecx.evaluate_goal(GoalSource::Misc, goal, None)
236 })
237 })
238 .is_ok()
239 }
240241#[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(241u32),
::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))]242fn evaluate_root_goal_for_proof_tree(
243&self,
244 goal: Goal<I, I::Predicate>,
245 span: I::Span,
246 ) -> (Result<NestedNormalizationGoals<I>, NoSolution>, inspect::GoalEvaluation<I>) {
247 evaluate_root_goal_for_proof_tree(self, goal, span)
248 }
249}
250251impl<'a, D, I> EvalCtxt<'a, D>
252where
253D: SolverDelegate<Interner = I>,
254 I: Interner,
255{
256pub(super) fn typing_mode(&self) -> TypingMode<I> {
257self.delegate.typing_mode()
258 }
259260/// Computes the `PathKind` for the step from the current goal to the
261 /// nested goal required due to `source`.
262 ///
263 /// See #136824 for a more detailed reasoning for this behavior. We
264 /// consider cycles to be coinductive if they 'step into' a where-clause
265 /// of a coinductive trait. We will likely extend this function in the future
266 /// and will need to clearly document it in the rustc-dev-guide before
267 /// stabilization.
268pub(super) fn step_kind_for_source(&self, source: GoalSource) -> PathKind {
269match source {
270// We treat these goals as unknown for now. It is likely that most miscellaneous
271 // nested goals will be converted to an inductive variant in the future.
272 //
273 // Having unknown cycles is always the safer option, as changing that to either
274 // succeed or hard error is backwards compatible. If we incorrectly treat a cycle
275 // as inductive even though it should not be, it may be unsound during coherence and
276 // fixing it may cause inference breakage or introduce ambiguity.
277GoalSource::Misc => PathKind::Unknown,
278 GoalSource::NormalizeGoal(path_kind) => path_kind,
279 GoalSource::ImplWhereBound => match self.current_goal_kind {
280// We currently only consider a cycle coinductive if it steps
281 // into a where-clause of a coinductive trait.
282CurrentGoalKind::CoinductiveTrait => PathKind::Coinductive,
283// While normalizing via an impl does step into a where-clause of
284 // an impl, accessing the associated item immediately steps out of
285 // it again. This means cycles/recursive calls are not guarded
286 // by impls used for normalization.
287 //
288 // See tests/ui/traits/next-solver/cycles/normalizes-to-is-not-productive.rs
289 // for how this can go wrong.
290CurrentGoalKind::NormalizesTo => PathKind::Inductive,
291// We probably want to make all traits coinductive in the future,
292 // so we treat cycles involving where-clauses of not-yet coinductive
293 // traits as ambiguous for now.
294CurrentGoalKind::Misc => PathKind::Unknown,
295 },
296// Relating types is always unproductive. If we were to map proof trees to
297 // corecursive functions as explained in #136824, relating types never
298 // introduces a constructor which could cause the recursion to be guarded.
299GoalSource::TypeRelating => PathKind::Inductive,
300// These goal sources are likely unproductive and can be changed to
301 // `PathKind::Inductive`. Keeping them as unknown until we're confident
302 // about this and have an example where it is necessary.
303GoalSource::AliasBoundConstCondition | GoalSource::AliasWellFormed => PathKind::Unknown,
304 }
305 }
306307/// Creates a root evaluation context and search graph. This should only be
308 /// used from outside of any evaluation, and other methods should be preferred
309 /// over using this manually (such as [`SolverDelegateEvalExt::evaluate_root_goal`]).
310pub(super) fn enter_root<R>(
311 delegate: &D,
312 root_depth: usize,
313 origin_span: I::Span,
314 f: impl FnOnce(&mut EvalCtxt<'_, D>) -> R,
315 ) -> R {
316let mut search_graph = SearchGraph::new(root_depth);
317318let mut ecx = EvalCtxt {
319delegate,
320 search_graph: &mut search_graph,
321 nested_goals: Default::default(),
322 inspect: inspect::EvaluationStepBuilder::new_noop(),
323324// Only relevant when canonicalizing the response,
325 // which we don't do within this evaluation context.
326max_input_universe: ty::UniverseIndex::ROOT,
327 initial_opaque_types_storage_num_entries: Default::default(),
328 var_kinds: Default::default(),
329 var_values: CanonicalVarValues::dummy(),
330 current_goal_kind: CurrentGoalKind::Misc,
331origin_span,
332 tainted: Ok(()),
333 };
334let result = f(&mut ecx);
335if !ecx.nested_goals.is_empty() {
{
::core::panicking::panic_fmt(format_args!("root `EvalCtxt` should not have any goals added to it"));
}
};assert!(
336 ecx.nested_goals.is_empty(),
337"root `EvalCtxt` should not have any goals added to it"
338);
339if !search_graph.is_empty() {
::core::panicking::panic("assertion failed: search_graph.is_empty()")
};assert!(search_graph.is_empty());
340result341 }
342343/// Creates a nested evaluation context that shares the same search graph as the
344 /// one passed in. This is suitable for evaluation, granted that the search graph
345 /// has had the nested goal recorded on its stack. This method only be used by
346 /// `search_graph::Delegate::compute_goal`.
347 ///
348 /// This function takes care of setting up the inference context, setting the anchor,
349 /// and registering opaques from the canonicalized input.
350pub(super) fn enter_canonical<R>(
351 cx: I,
352 search_graph: &'a mut SearchGraph<D>,
353 canonical_input: CanonicalInput<I>,
354 proof_tree_builder: &mut inspect::ProofTreeBuilder<D>,
355 f: impl FnOnce(&mut EvalCtxt<'_, D>, Goal<I, I::Predicate>) -> R,
356 ) -> R {
357let (ref delegate, input, var_values) = D::build_with_canonical(cx, &canonical_input);
358for (key, ty) in input.predefined_opaques_in_body.iter() {
359let prev = delegate.register_hidden_type_in_storage(key, ty, I::Span::dummy());
360// It may be possible that two entries in the opaque type storage end up
361 // with the same key after resolving contained inference variables.
362 //
363 // We could put them in the duplicate list but don't have to. The opaques we
364 // encounter here are already tracked in the caller, so there's no need to
365 // also store them here. We'd take them out when computing the query response
366 // and then discard them, as they're already present in the input.
367 //
368 // Ideally we'd drop duplicate opaque type definitions when computing
369 // the canonical input. This is more annoying to implement and may cause a
370 // perf regression, so we do it inside of the query for now.
371if let Some(prev) = prev {
372{
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:372",
"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(372u32),
::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`");
373 }
374 }
375376let initial_opaque_types_storage_num_entries = delegate.opaque_types_storage_num_entries();
377let mut ecx = EvalCtxt {
378delegate,
379 var_kinds: canonical_input.canonical.var_kinds,
380var_values,
381 current_goal_kind: CurrentGoalKind::from_query_input(cx, input),
382 max_input_universe: canonical_input.canonical.max_universe,
383initial_opaque_types_storage_num_entries,
384search_graph,
385 nested_goals: Default::default(),
386 origin_span: I::Span::dummy(),
387 tainted: Ok(()),
388 inspect: proof_tree_builder.new_evaluation_step(var_values),
389 };
390391let result = f(&mut ecx, input.goal);
392ecx.inspect.probe_final_state(ecx.delegate, ecx.max_input_universe);
393proof_tree_builder.finish_evaluation_step(ecx.inspect);
394395// When creating a query response we clone the opaque type constraints
396 // instead of taking them. This would cause an ICE here, since we have
397 // assertions against dropping an `InferCtxt` without taking opaques.
398 // FIXME: Once we remove support for the old impl we can remove this.
399 // FIXME: Could we make `build_with_canonical` into `enter_with_canonical` and call this at the end?
400delegate.reset_opaque_types();
401402result403 }
404405pub(super) fn ignore_candidate_head_usages(&mut self, usages: CandidateHeadUsages) {
406self.search_graph.ignore_candidate_head_usages(usages);
407 }
408409/// Recursively evaluates `goal`, returning whether any inference vars have
410 /// been constrained and the certainty of the result.
411fn evaluate_goal(
412&mut self,
413 source: GoalSource,
414 goal: Goal<I, I::Predicate>,
415 stalled_on: Option<GoalStalledOn<I>>,
416 ) -> Result<GoalEvaluation<I>, NoSolution> {
417let (normalization_nested_goals, goal_evaluation) =
418self.evaluate_goal_raw(source, goal, stalled_on)?;
419if !normalization_nested_goals.is_empty() {
::core::panicking::panic("assertion failed: normalization_nested_goals.is_empty()")
};assert!(normalization_nested_goals.is_empty());
420Ok(goal_evaluation)
421 }
422423/// Recursively evaluates `goal`, returning the nested goals in case
424 /// the nested goal is a `NormalizesTo` goal.
425 ///
426 /// As all other goal kinds do not return any nested goals and
427 /// `NormalizesTo` is only used by `AliasRelate`, all other callsites
428 /// should use [`EvalCtxt::evaluate_goal`] which discards that empty
429 /// storage.
430pub(super) fn evaluate_goal_raw(
431&mut self,
432 source: GoalSource,
433 goal: Goal<I, I::Predicate>,
434 stalled_on: Option<GoalStalledOn<I>>,
435 ) -> Result<(NestedNormalizationGoals<I>, GoalEvaluation<I>), NoSolution> {
436// If we have run this goal before, and it was stalled, check that any of the goal's
437 // args have changed. Otherwise, we don't need to re-run the goal because it'll remain
438 // stalled, since it'll canonicalize the same way and evaluation is pure.
439if let Some(GoalStalledOn {
440 num_opaques,
441ref stalled_vars,
442ref sub_roots,
443 stalled_certainty,
444 }) = stalled_on445 && !stalled_vars.iter().any(|value| self.delegate.is_changed_arg(*value))
446 && !sub_roots447 .iter()
448 .any(|&vid| self.delegate.sub_unification_table_root_var(vid) != vid)
449 && !self.delegate.opaque_types_storage_num_entries().needs_reevaluation(num_opaques)
450 {
451return Ok((
452NestedNormalizationGoals::empty(),
453GoalEvaluation {
454goal,
455 certainty: stalled_certainty,
456 has_changed: HasChanged::No,
457stalled_on,
458 },
459 ));
460 }
461462// We only care about one entry per `OpaqueTypeKey` here,
463 // so we only canonicalize the lookup table and ignore
464 // duplicate entries.
465let opaque_types = self.delegate.clone_opaque_types_lookup_table();
466let (goal, opaque_types) = eager_resolve_vars(self.delegate, (goal, opaque_types));
467468let (orig_values, canonical_goal) = canonicalize_goal(self.delegate, goal, &opaque_types);
469let canonical_result = self.search_graph.evaluate_goal(
470self.cx(),
471canonical_goal,
472self.step_kind_for_source(source),
473&mut inspect::ProofTreeBuilder::new_noop(),
474 );
475let response = match canonical_result {
476Err(e) => return Err(e),
477Ok(response) => response,
478 };
479480let has_changed =
481if !has_only_region_constraints(response) { HasChanged::Yes } else { HasChanged::No };
482483let (normalization_nested_goals, certainty) = instantiate_and_apply_query_response(
484self.delegate,
485goal.param_env,
486&orig_values,
487response,
488self.origin_span,
489 );
490491// FIXME: We previously had an assert here that checked that recomputing
492 // a goal after applying its constraints did not change its response.
493 //
494 // This assert was removed as it did not hold for goals constraining
495 // an inference variable to a recursive alias, e.g. in
496 // tests/ui/traits/next-solver/overflow/recursive-self-normalization.rs.
497 //
498 // Once we have decided on how to handle trait-system-refactor-initiative#75,
499 // we should re-add an assert here.
500501let stalled_on = match certainty {
502 Certainty::Yes => None,
503 Certainty::Maybe { .. } => match has_changed {
504// FIXME: We could recompute a *new* set of stalled variables by walking
505 // through the orig values, resolving, and computing the root vars of anything
506 // that is not resolved. Only when *these* have changed is it meaningful
507 // to recompute this goal.
508HasChanged::Yes => None,
509 HasChanged::No => {
510let mut stalled_vars = orig_values;
511512// Remove the unconstrained RHS arg, which is expected to have changed.
513if let Some(normalizes_to) = goal.predicate.as_normalizes_to() {
514let normalizes_to = normalizes_to.skip_binder();
515let rhs_arg: I::GenericArg = normalizes_to.term.into();
516let idx = stalled_vars517 .iter()
518 .rposition(|arg| *arg == rhs_arg)
519 .expect("expected unconstrained arg");
520stalled_vars.swap_remove(idx);
521 }
522523// Remove the canonicalized universal vars, since we only care about stalled existentials.
524let mut sub_roots = Vec::new();
525stalled_vars.retain(|arg| match arg.kind() {
526// Lifetimes can never stall goals.
527ty::GenericArgKind::Lifetime(_) => false,
528 ty::GenericArgKind::Type(ty) => match ty.kind() {
529 ty::Infer(ty::TyVar(vid)) => {
530sub_roots.push(self.delegate.sub_unification_table_root_var(vid));
531true
532}
533 ty::Infer(_) => true,
534 ty::Param(_) | ty::Placeholder(_) => false,
535_ => {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("unexpected orig_value: {0:?}", ty)));
}unreachable!("unexpected orig_value: {ty:?}"),
536 },
537 ty::GenericArgKind::Const(ct) => match ct.kind() {
538 ty::ConstKind::Infer(_) => true,
539 ty::ConstKind::Param(_) | ty::ConstKind::Placeholder(_) => false,
540_ => {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("unexpected orig_value: {0:?}", ct)));
}unreachable!("unexpected orig_value: {ct:?}"),
541 },
542 });
543544Some(GoalStalledOn {
545 num_opaques: canonical_goal546 .canonical
547 .value
548 .predefined_opaques_in_body
549 .len(),
550stalled_vars,
551sub_roots,
552 stalled_certainty: certainty,
553 })
554 }
555 },
556 };
557558Ok((
559normalization_nested_goals,
560GoalEvaluation { goal, certainty, has_changed, stalled_on },
561 ))
562 }
563564pub(super) fn compute_goal(&mut self, goal: Goal<I, I::Predicate>) -> QueryResult<I> {
565let Goal { param_env, predicate } = goal;
566let kind = predicate.kind();
567self.enter_forall(kind, |ecx, kind| match kind {
568 ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)) => {
569ecx.compute_trait_goal(Goal { param_env, predicate }).map(|(r, _via)| r)
570 }
571 ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(predicate)) => {
572ecx.compute_host_effect_goal(Goal { param_env, predicate })
573 }
574 ty::PredicateKind::Clause(ty::ClauseKind::Projection(predicate)) => {
575ecx.compute_projection_goal(Goal { param_env, predicate })
576 }
577 ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(predicate)) => {
578ecx.compute_type_outlives_goal(Goal { param_env, predicate })
579 }
580 ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(predicate)) => {
581ecx.compute_region_outlives_goal(Goal { param_env, predicate })
582 }
583 ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ty)) => {
584ecx.compute_const_arg_has_type_goal(Goal { param_env, predicate: (ct, ty) })
585 }
586 ty::PredicateKind::Clause(ty::ClauseKind::UnstableFeature(symbol)) => {
587ecx.compute_unstable_feature_goal(param_env, symbol)
588 }
589 ty::PredicateKind::Subtype(predicate) => {
590ecx.compute_subtype_goal(Goal { param_env, predicate })
591 }
592 ty::PredicateKind::Coerce(predicate) => {
593ecx.compute_coerce_goal(Goal { param_env, predicate })
594 }
595 ty::PredicateKind::DynCompatible(trait_def_id) => {
596ecx.compute_dyn_compatible_goal(trait_def_id)
597 }
598 ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(term)) => {
599ecx.compute_well_formed_goal(Goal { param_env, predicate: term })
600 }
601 ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(ct)) => {
602ecx.compute_const_evaluatable_goal(Goal { param_env, predicate: ct })
603 }
604 ty::PredicateKind::ConstEquate(_, _) => {
605{
::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")606 }
607 ty::PredicateKind::NormalizesTo(predicate) => {
608ecx.compute_normalizes_to_goal(Goal { param_env, predicate })
609 }
610 ty::PredicateKind::AliasRelate(lhs, rhs, direction) => {
611ecx.compute_alias_relate_goal(Goal { param_env, predicate: (lhs, rhs, direction) })
612 }
613 ty::PredicateKind::Ambiguous => {
614ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
615 }
616 })
617 }
618619// Recursively evaluates all the goals added to this `EvalCtxt` to completion, returning
620 // the certainty of all the goals.
621#[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(621u32),
::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:634",
"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(634u32),
::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))]622pub(super) fn try_evaluate_added_goals(&mut self) -> Result<Certainty, NoSolution> {
623for _ in 0..FIXPOINT_STEP_LIMIT {
624match self.evaluate_added_goals_step() {
625Ok(None) => {}
626Ok(Some(cert)) => return Ok(cert),
627Err(NoSolution) => {
628self.tainted = Err(NoSolution);
629return Err(NoSolution);
630 }
631 }
632 }
633634debug!("try_evaluate_added_goals: encountered overflow");
635Ok(Certainty::overflow(false))
636 }
637638/// Iterate over all added goals: returning `Ok(Some(_))` in case we can stop rerunning.
639 ///
640 /// Goals for the next step get directly added to the nested goals of the `EvalCtxt`.
641fn evaluate_added_goals_step(&mut self) -> Result<Option<Certainty>, NoSolution> {
642let cx = self.cx();
643// If this loop did not result in any progress, what's our final certainty.
644let mut unchanged_certainty = Some(Certainty::Yes);
645for (source, goal, stalled_on) in mem::take(&mut self.nested_goals) {
646if let Some(certainty) = self.delegate.compute_goal_fast_path(goal, self.origin_span) {
647match certainty {
648 Certainty::Yes => {}
649 Certainty::Maybe { .. } => {
650self.nested_goals.push((source, goal, None));
651 unchanged_certainty = unchanged_certainty.map(|c| c.and(certainty));
652 }
653 }
654continue;
655 }
656657// We treat normalizes-to goals specially here. In each iteration we take the
658 // RHS of the projection, replace it with a fresh inference variable, and only
659 // after evaluating that goal do we equate the fresh inference variable with the
660 // actual RHS of the predicate.
661 //
662 // This is both to improve caching, and to avoid using the RHS of the
663 // projection predicate to influence the normalizes-to candidate we select.
664 //
665 // Forgetting to replace the RHS with a fresh inference variable when we evaluate
666 // this goal results in an ICE.
667if let Some(pred) = goal.predicate.as_normalizes_to() {
668// We should never encounter higher-ranked normalizes-to goals.
669let pred = pred.no_bound_vars().unwrap();
670// Replace the goal with an unconstrained infer var, so the
671 // RHS does not affect projection candidate assembly.
672let unconstrained_rhs = self.next_term_infer_of_kind(pred.term);
673let unconstrained_goal =
674 goal.with(cx, ty::NormalizesTo { alias: pred.alias, term: unconstrained_rhs });
675676let (
677 NestedNormalizationGoals(nested_goals),
678 GoalEvaluation { goal, certainty, stalled_on, has_changed: _ },
679 ) = self.evaluate_goal_raw(source, unconstrained_goal, stalled_on)?;
680// Add the nested goals from normalization to our own nested goals.
681{
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:681",
"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(681u32),
::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);
682self.nested_goals.extend(nested_goals.into_iter().map(|(s, g)| (s, g, None)));
683684// Finally, equate the goal's RHS with the unconstrained var.
685 //
686 // SUBTLE:
687 // We structurally relate aliases here. This is necessary
688 // as we otherwise emit a nested `AliasRelate` goal in case the
689 // returned term is a rigid alias, resulting in overflow.
690 //
691 // It is correct as both `goal.predicate.term` and `unconstrained_rhs`
692 // start out as an unconstrained inference variable so any aliases get
693 // fully normalized when instantiating it.
694 //
695 // FIXME: Strictly speaking this may be incomplete if the normalized-to
696 // type contains an ambiguous alias referencing bound regions. We should
697 // consider changing this to only use "shallow structural equality".
698self.eq_structurally_relating_aliases(
699 goal.param_env,
700 pred.term,
701 unconstrained_rhs,
702 )?;
703704// We only look at the `projection_ty` part here rather than
705 // looking at the "has changed" return from evaluate_goal,
706 // because we expect the `unconstrained_rhs` part of the predicate
707 // to have changed -- that means we actually normalized successfully!
708 // FIXME: Do we need to eagerly resolve here? Or should we check
709 // if the cache key has any changed vars?
710let with_resolved_vars = self.resolve_vars_if_possible(goal);
711if pred.alias
712 != with_resolved_vars
713 .predicate
714 .as_normalizes_to()
715 .unwrap()
716 .no_bound_vars()
717 .unwrap()
718 .alias
719 {
720 unchanged_certainty = None;
721 }
722723match certainty {
724 Certainty::Yes => {}
725 Certainty::Maybe { .. } => {
726self.nested_goals.push((source, with_resolved_vars, stalled_on));
727 unchanged_certainty = unchanged_certainty.map(|c| c.and(certainty));
728 }
729 }
730 } else {
731let GoalEvaluation { goal, certainty, has_changed, stalled_on } =
732self.evaluate_goal(source, goal, stalled_on)?;
733if has_changed == HasChanged::Yes {
734 unchanged_certainty = None;
735 }
736737match certainty {
738 Certainty::Yes => {}
739 Certainty::Maybe { .. } => {
740self.nested_goals.push((source, goal, stalled_on));
741 unchanged_certainty = unchanged_certainty.map(|c| c.and(certainty));
742 }
743 }
744 }
745 }
746747Ok(unchanged_certainty)
748 }
749750/// Record impl args in the proof tree for later access by `InspectCandidate`.
751pub(crate) fn record_impl_args(&mut self, impl_args: I::GenericArgs) {
752self.inspect.record_impl_args(self.delegate, self.max_input_universe, impl_args)
753 }
754755pub(super) fn cx(&self) -> I {
756self.delegate.cx()
757 }
758759#[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(759u32),
::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))]760pub(super) fn add_goal(&mut self, source: GoalSource, mut goal: Goal<I, I::Predicate>) {
761 goal.predicate =
762 goal.predicate.fold_with(&mut ReplaceAliasWithInfer::new(self, source, goal.param_env));
763self.inspect.add_goal(self.delegate, self.max_input_universe, source, goal);
764self.nested_goals.push((source, goal, None));
765 }
766767#[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(767u32),
::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))]768pub(super) fn add_goals(
769&mut self,
770 source: GoalSource,
771 goals: impl IntoIterator<Item = Goal<I, I::Predicate>>,
772 ) {
773for goal in goals {
774self.add_goal(source, goal);
775 }
776 }
777778pub(super) fn next_region_var(&mut self) -> I::Region {
779let region = self.delegate.next_region_infer();
780self.inspect.add_var_value(region);
781region782 }
783784pub(super) fn next_ty_infer(&mut self) -> I::Ty {
785let ty = self.delegate.next_ty_infer();
786self.inspect.add_var_value(ty);
787ty788 }
789790pub(super) fn next_const_infer(&mut self) -> I::Const {
791let ct = self.delegate.next_const_infer();
792self.inspect.add_var_value(ct);
793ct794 }
795796/// Returns a ty infer or a const infer depending on whether `kind` is a `Ty` or `Const`.
797 /// If `kind` is an integer inference variable this will still return a ty infer var.
798pub(super) fn next_term_infer_of_kind(&mut self, term: I::Term) -> I::Term {
799match term.kind() {
800 ty::TermKind::Ty(_) => self.next_ty_infer().into(),
801 ty::TermKind::Const(_) => self.next_const_infer().into(),
802 }
803 }
804805/// Is the projection predicate is of the form `exists<T> <Ty as Trait>::Assoc = T`.
806 ///
807 /// This is the case if the `term` does not occur in any other part of the predicate
808 /// and is able to name all other placeholder and inference variables.
809x;#[instrument(level = "trace", skip(self), ret)]810pub(super) fn term_is_fully_unconstrained(&self, goal: Goal<I, ty::NormalizesTo<I>>) -> bool {
811let universe_of_term = match goal.predicate.term.kind() {
812 ty::TermKind::Ty(ty) => {
813if let ty::Infer(ty::TyVar(vid)) = ty.kind() {
814self.delegate.universe_of_ty(vid).unwrap()
815 } else {
816return false;
817 }
818 }
819 ty::TermKind::Const(ct) => {
820if let ty::ConstKind::Infer(ty::InferConst::Var(vid)) = ct.kind() {
821self.delegate.universe_of_ct(vid).unwrap()
822 } else {
823return false;
824 }
825 }
826 };
827828struct ContainsTermOrNotNameable<'a, D: SolverDelegate<Interner = I>, I: Interner> {
829 term: I::Term,
830 universe_of_term: ty::UniverseIndex,
831 delegate: &'a D,
832 cache: HashSet<I::Ty>,
833 }
834835impl<D: SolverDelegate<Interner = I>, I: Interner> ContainsTermOrNotNameable<'_, D, I> {
836fn check_nameable(&self, universe: ty::UniverseIndex) -> ControlFlow<()> {
837if self.universe_of_term.can_name(universe) {
838 ControlFlow::Continue(())
839 } else {
840 ControlFlow::Break(())
841 }
842 }
843 }
844845impl<D: SolverDelegate<Interner = I>, I: Interner> TypeVisitor<I>
846for ContainsTermOrNotNameable<'_, D, I>
847 {
848type Result = ControlFlow<()>;
849fn visit_ty(&mut self, t: I::Ty) -> Self::Result {
850if self.cache.contains(&t) {
851return ControlFlow::Continue(());
852 }
853854match t.kind() {
855 ty::Infer(ty::TyVar(vid)) => {
856if let ty::TermKind::Ty(term) = self.term.kind()
857 && let ty::Infer(ty::TyVar(term_vid)) = term.kind()
858 && self.delegate.root_ty_var(vid) == self.delegate.root_ty_var(term_vid)
859 {
860return ControlFlow::Break(());
861 }
862863self.check_nameable(self.delegate.universe_of_ty(vid).unwrap())?;
864 }
865 ty::Placeholder(p) => self.check_nameable(p.universe())?,
866_ => {
867if t.has_non_region_infer() || t.has_placeholders() {
868 t.super_visit_with(self)?
869}
870 }
871 }
872873assert!(self.cache.insert(t));
874 ControlFlow::Continue(())
875 }
876877fn visit_const(&mut self, c: I::Const) -> Self::Result {
878match c.kind() {
879 ty::ConstKind::Infer(ty::InferConst::Var(vid)) => {
880if let ty::TermKind::Const(term) = self.term.kind()
881 && let ty::ConstKind::Infer(ty::InferConst::Var(term_vid)) = term.kind()
882 && self.delegate.root_const_var(vid)
883 == self.delegate.root_const_var(term_vid)
884 {
885return ControlFlow::Break(());
886 }
887888self.check_nameable(self.delegate.universe_of_ct(vid).unwrap())
889 }
890 ty::ConstKind::Placeholder(p) => self.check_nameable(p.universe()),
891_ => {
892if c.has_non_region_infer() || c.has_placeholders() {
893 c.super_visit_with(self)
894 } else {
895 ControlFlow::Continue(())
896 }
897 }
898 }
899 }
900901fn visit_predicate(&mut self, p: I::Predicate) -> Self::Result {
902if p.has_non_region_infer() || p.has_placeholders() {
903 p.super_visit_with(self)
904 } else {
905 ControlFlow::Continue(())
906 }
907 }
908909fn visit_clauses(&mut self, c: I::Clauses) -> Self::Result {
910if c.has_non_region_infer() || c.has_placeholders() {
911 c.super_visit_with(self)
912 } else {
913 ControlFlow::Continue(())
914 }
915 }
916 }
917918let mut visitor = ContainsTermOrNotNameable {
919 delegate: self.delegate,
920 universe_of_term,
921 term: goal.predicate.term,
922 cache: Default::default(),
923 };
924 goal.predicate.alias.visit_with(&mut visitor).is_continue()
925 && goal.param_env.visit_with(&mut visitor).is_continue()
926 }
927928pub(super) fn sub_unify_ty_vids_raw(&self, a: ty::TyVid, b: ty::TyVid) {
929self.delegate.sub_unify_ty_vids_raw(a, b)
930 }
931932x;#[instrument(level = "trace", skip(self, param_env), ret)]933pub(super) fn eq<T: Relate<I>>(
934&mut self,
935 param_env: I::ParamEnv,
936 lhs: T,
937 rhs: T,
938 ) -> Result<(), NoSolution> {
939self.relate(param_env, lhs, ty::Variance::Invariant, rhs)
940 }
941942/// This should be used when relating a rigid alias with another type.
943 ///
944 /// Normally we emit a nested `AliasRelate` when equating an inference
945 /// variable and an alias. This causes us to instead constrain the inference
946 /// variable to the alias without emitting a nested alias relate goals.
947x;#[instrument(level = "trace", skip(self, param_env), ret)]948pub(super) fn relate_rigid_alias_non_alias(
949&mut self,
950 param_env: I::ParamEnv,
951 alias: ty::AliasTerm<I>,
952 variance: ty::Variance,
953 term: I::Term,
954 ) -> Result<(), NoSolution> {
955// NOTE: this check is purely an optimization, the structural eq would
956 // always fail if the term is not an inference variable.
957if term.is_infer() {
958let cx = self.cx();
959// We need to relate `alias` to `term` treating only the outermost
960 // constructor as rigid, relating any contained generic arguments as
961 // normal. We do this by first structurally equating the `term`
962 // with the alias constructor instantiated with unconstrained infer vars,
963 // and then relate this with the whole `alias`.
964 //
965 // Alternatively we could modify `Equate` for this case by adding another
966 // variant to `StructurallyRelateAliases`.
967let identity_args = self.fresh_args_for_item(alias.def_id);
968let rigid_ctor = ty::AliasTerm::new_from_args(cx, alias.def_id, identity_args);
969let ctor_term = rigid_ctor.to_term(cx);
970let obligations = self.delegate.eq_structurally_relating_aliases(
971 param_env,
972 term,
973 ctor_term,
974self.origin_span,
975 )?;
976debug_assert!(obligations.is_empty());
977self.relate(param_env, alias, variance, rigid_ctor)
978 } else {
979Err(NoSolution)
980 }
981 }
982983/// This should only be used when we're either instantiating a previously
984 /// unconstrained "return value" or when we're sure that all aliases in
985 /// the types are rigid.
986x;#[instrument(level = "trace", skip(self, param_env), ret)]987pub(super) fn eq_structurally_relating_aliases<T: Relate<I>>(
988&mut self,
989 param_env: I::ParamEnv,
990 lhs: T,
991 rhs: T,
992 ) -> Result<(), NoSolution> {
993let result = self.delegate.eq_structurally_relating_aliases(
994 param_env,
995 lhs,
996 rhs,
997self.origin_span,
998 )?;
999assert_eq!(result, vec![]);
1000Ok(())
1001 }
10021003x;#[instrument(level = "trace", skip(self, param_env), ret)]1004pub(super) fn sub<T: Relate<I>>(
1005&mut self,
1006 param_env: I::ParamEnv,
1007 sub: T,
1008 sup: T,
1009 ) -> Result<(), NoSolution> {
1010self.relate(param_env, sub, ty::Variance::Covariant, sup)
1011 }
10121013x;#[instrument(level = "trace", skip(self, param_env), ret)]1014pub(super) fn relate<T: Relate<I>>(
1015&mut self,
1016 param_env: I::ParamEnv,
1017 lhs: T,
1018 variance: ty::Variance,
1019 rhs: T,
1020 ) -> Result<(), NoSolution> {
1021let goals = self.delegate.relate(param_env, lhs, variance, rhs, self.origin_span)?;
1022for &goal in goals.iter() {
1023let source = match goal.predicate.kind().skip_binder() {
1024 ty::PredicateKind::Subtype { .. } | ty::PredicateKind::AliasRelate(..) => {
1025 GoalSource::TypeRelating
1026 }
1027// FIXME(-Znext-solver=coinductive): should these WF goals also be unproductive?
1028ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_)) => GoalSource::Misc,
1029 p => unreachable!("unexpected nested goal in `relate`: {p:?}"),
1030 };
1031self.add_goal(source, goal);
1032 }
1033Ok(())
1034 }
10351036/// Equates two values returning the nested goals without adding them
1037 /// to the nested goals of the `EvalCtxt`.
1038 ///
1039 /// If possible, try using `eq` instead which automatically handles nested
1040 /// goals correctly.
1041x;#[instrument(level = "trace", skip(self, param_env), ret)]1042pub(super) fn eq_and_get_goals<T: Relate<I>>(
1043&self,
1044 param_env: I::ParamEnv,
1045 lhs: T,
1046 rhs: T,
1047 ) -> Result<Vec<Goal<I, I::Predicate>>, NoSolution> {
1048Ok(self.delegate.relate(param_env, lhs, ty::Variance::Invariant, rhs, self.origin_span)?)
1049 }
10501051pub(super) fn instantiate_binder_with_infer<T: TypeFoldable<I> + Copy>(
1052&self,
1053 value: ty::Binder<I, T>,
1054 ) -> T {
1055self.delegate.instantiate_binder_with_infer(value)
1056 }
10571058/// `enter_forall`, but takes `&mut self` and passes it back through the
1059 /// callback since it can't be aliased during the call.
1060pub(super) fn enter_forall<T: TypeFoldable<I>, U>(
1061&mut self,
1062 value: ty::Binder<I, T>,
1063 f: impl FnOnce(&mut Self, T) -> U,
1064 ) -> U {
1065self.delegate.enter_forall(value, |value| f(self, value))
1066 }
10671068pub(super) fn resolve_vars_if_possible<T>(&self, value: T) -> T
1069where
1070T: TypeFoldable<I>,
1071 {
1072self.delegate.resolve_vars_if_possible(value)
1073 }
10741075pub(super) fn shallow_resolve(&self, ty: I::Ty) -> I::Ty {
1076self.delegate.shallow_resolve(ty)
1077 }
10781079pub(super) fn eager_resolve_region(&self, r: I::Region) -> I::Region {
1080if let ty::ReVar(vid) = r.kind() {
1081self.delegate.opportunistic_resolve_lt_var(vid)
1082 } else {
1083r1084 }
1085 }
10861087pub(super) fn fresh_args_for_item(&mut self, def_id: I::DefId) -> I::GenericArgs {
1088let args = self.delegate.fresh_args_for_item(def_id);
1089for arg in args.iter() {
1090self.inspect.add_var_value(arg);
1091 }
1092args1093 }
10941095pub(super) fn register_ty_outlives(&self, ty: I::Ty, lt: I::Region) {
1096self.delegate.register_ty_outlives(ty, lt, self.origin_span);
1097 }
10981099pub(super) fn register_region_outlives(&self, a: I::Region, b: I::Region) {
1100// `'a: 'b` ==> `'b <= 'a`
1101self.delegate.sub_regions(b, a, self.origin_span);
1102 }
11031104/// Computes the list of goals required for `arg` to be well-formed
1105pub(super) fn well_formed_goals(
1106&self,
1107 param_env: I::ParamEnv,
1108 term: I::Term,
1109 ) -> Option<Vec<Goal<I, I::Predicate>>> {
1110self.delegate.well_formed_goals(param_env, term)
1111 }
11121113pub(super) fn trait_ref_is_knowable(
1114&mut self,
1115 param_env: I::ParamEnv,
1116 trait_ref: ty::TraitRef<I>,
1117 ) -> Result<bool, NoSolution> {
1118let delegate = self.delegate;
1119let lazily_normalize_ty = |ty| self.structurally_normalize_ty(param_env, ty);
1120 coherence::trait_ref_is_knowable(&**delegate, trait_ref, lazily_normalize_ty)
1121 .map(|is_knowable| is_knowable.is_ok())
1122 }
11231124pub(super) fn fetch_eligible_assoc_item(
1125&self,
1126 goal_trait_ref: ty::TraitRef<I>,
1127 trait_assoc_def_id: I::DefId,
1128 impl_def_id: I::ImplId,
1129 ) -> Result<Option<I::DefId>, I::ErrorGuaranteed> {
1130self.delegate.fetch_eligible_assoc_item(goal_trait_ref, trait_assoc_def_id, impl_def_id)
1131 }
11321133x;#[instrument(level = "debug", skip(self), ret)]1134pub(super) fn register_hidden_type_in_storage(
1135&mut self,
1136 opaque_type_key: ty::OpaqueTypeKey<I>,
1137 hidden_ty: I::Ty,
1138 ) -> Option<I::Ty> {
1139self.delegate.register_hidden_type_in_storage(opaque_type_key, hidden_ty, self.origin_span)
1140 }
11411142pub(super) fn add_item_bounds_for_hidden_type(
1143&mut self,
1144 opaque_def_id: I::DefId,
1145 opaque_args: I::GenericArgs,
1146 param_env: I::ParamEnv,
1147 hidden_ty: I::Ty,
1148 ) {
1149let mut goals = Vec::new();
1150self.delegate.add_item_bounds_for_hidden_type(
1151opaque_def_id,
1152opaque_args,
1153param_env,
1154hidden_ty,
1155&mut goals,
1156 );
1157self.add_goals(GoalSource::AliasWellFormed, goals);
1158 }
11591160// Try to evaluate a const, or return `None` if the const is too generic.
1161 // This doesn't mean the const isn't evaluatable, though, and should be treated
1162 // as an ambiguity rather than no-solution.
1163pub(super) fn evaluate_const(
1164&self,
1165 param_env: I::ParamEnv,
1166 uv: ty::UnevaluatedConst<I>,
1167 ) -> Option<I::Const> {
1168self.delegate.evaluate_const(param_env, uv)
1169 }
11701171pub(super) fn is_transmutable(
1172&mut self,
1173 src: I::Ty,
1174 dst: I::Ty,
1175 assume: I::Const,
1176 ) -> Result<Certainty, NoSolution> {
1177self.delegate.is_transmutable(dst, src, assume)
1178 }
11791180pub(super) fn replace_bound_vars<T: TypeFoldable<I>>(
1181&self,
1182 t: T,
1183 universes: &mut Vec<Option<ty::UniverseIndex>>,
1184 ) -> T {
1185BoundVarReplacer::replace_bound_vars(&**self.delegate, universes, t).0
1186}
11871188pub(super) fn may_use_unstable_feature(
1189&self,
1190 param_env: I::ParamEnv,
1191 symbol: I::Symbol,
1192 ) -> bool {
1193may_use_unstable_feature(&**self.delegate, param_env, symbol)
1194 }
11951196pub(crate) fn opaques_with_sub_unified_hidden_type(
1197&self,
1198 self_ty: I::Ty,
1199 ) -> Vec<ty::AliasTy<I>> {
1200if let ty::Infer(ty::TyVar(vid)) = self_ty.kind() {
1201self.delegate.opaques_with_sub_unified_hidden_type(vid)
1202 } else {
1203::alloc::vec::Vec::new()vec![]1204 }
1205 }
12061207/// To return the constraints of a canonical query to the caller, we canonicalize:
1208 ///
1209 /// - `var_values`: a map from bound variables in the canonical goal to
1210 /// the values inferred while solving the instantiated goal.
1211 /// - `external_constraints`: additional constraints which aren't expressible
1212 /// using simple unification of inference variables.
1213 ///
1214 /// This takes the `shallow_certainty` which represents whether we're confident
1215 /// that the final result of the current goal only depends on the nested goals.
1216 ///
1217 /// In case this is `Certainty::Maybe`, there may still be additional nested goals
1218 /// or inference constraints required for this candidate to be hold. The candidate
1219 /// always requires all already added constraints and nested goals.
1220x;#[instrument(level = "trace", skip(self), ret)]1221pub(in crate::solve) fn evaluate_added_goals_and_make_canonical_response(
1222&mut self,
1223 shallow_certainty: Certainty,
1224 ) -> QueryResult<I> {
1225self.inspect.make_canonical_response(shallow_certainty);
12261227let goals_certainty = self.try_evaluate_added_goals()?;
1228assert_eq!(
1229self.tainted,
1230Ok(()),
1231"EvalCtxt is tainted -- nested goals may have been dropped in a \
1232 previous call to `try_evaluate_added_goals!`"
1233);
12341235// We only check for leaks from universes which were entered inside
1236 // of the query.
1237self.delegate.leak_check(self.max_input_universe).map_err(|NoSolution| {
1238trace!("failed the leak check");
1239 NoSolution
1240 })?;
12411242let (certainty, normalization_nested_goals) =
1243match (self.current_goal_kind, shallow_certainty) {
1244// When normalizing, we've replaced the expected term with an unconstrained
1245 // inference variable. This means that we dropped information which could
1246 // have been important. We handle this by instead returning the nested goals
1247 // to the caller, where they are then handled. We only do so if we do not
1248 // need to recompute the `NormalizesTo` goal afterwards to avoid repeatedly
1249 // uplifting its nested goals. This is the case if the `shallow_certainty` is
1250 // `Certainty::Yes`.
1251(CurrentGoalKind::NormalizesTo, Certainty::Yes) => {
1252let goals = std::mem::take(&mut self.nested_goals);
1253// As we return all ambiguous nested goals, we can ignore the certainty
1254 // returned by `self.try_evaluate_added_goals()`.
1255if goals.is_empty() {
1256assert!(matches!(goals_certainty, Certainty::Yes));
1257 }
1258 (
1259 Certainty::Yes,
1260 NestedNormalizationGoals(
1261 goals.into_iter().map(|(s, g, _)| (s, g)).collect(),
1262 ),
1263 )
1264 }
1265_ => {
1266let certainty = shallow_certainty.and(goals_certainty);
1267 (certainty, NestedNormalizationGoals::empty())
1268 }
1269 };
12701271if let Certainty::Maybe {
1272 cause: cause @ MaybeCause::Overflow { keep_constraints: false, .. },
1273 opaque_types_jank,
1274 } = certainty
1275 {
1276// If we have overflow, it's probable that we're substituting a type
1277 // into itself infinitely and any partial substitutions in the query
1278 // response are probably not useful anyways, so just return an empty
1279 // query response.
1280 //
1281 // This may prevent us from potentially useful inference, e.g.
1282 // 2 candidates, one ambiguous and one overflow, which both
1283 // have the same inference constraints.
1284 //
1285 // Changing this to retain some constraints in the future
1286 // won't be a breaking change, so this is good enough for now.
1287return Ok(self.make_ambiguous_response_no_constraints(cause, opaque_types_jank));
1288 }
12891290let external_constraints =
1291self.compute_external_query_constraints(certainty, normalization_nested_goals);
1292let (var_values, mut external_constraints) =
1293 eager_resolve_vars(self.delegate, (self.var_values, external_constraints));
12941295// Remove any trivial or duplicated region constraints once we've resolved regions
1296let mut unique = HashSet::default();
1297 external_constraints.region_constraints.retain(|outlives| {
1298 outlives.0.as_region().is_none_or(|re| re != outlives.1) && unique.insert(*outlives)
1299 });
13001301let canonical = canonicalize_response(
1302self.delegate,
1303self.max_input_universe,
1304 Response {
1305 var_values,
1306 certainty,
1307 external_constraints: self.cx().mk_external_constraints(external_constraints),
1308 },
1309 );
13101311Ok(canonical)
1312 }
13131314/// Constructs a totally unconstrained, ambiguous response to a goal.
1315 ///
1316 /// Take care when using this, since often it's useful to respond with
1317 /// ambiguity but return constrained variables to guide inference.
1318pub(in crate::solve) fn make_ambiguous_response_no_constraints(
1319&self,
1320 cause: MaybeCause,
1321 opaque_types_jank: OpaqueTypesJank,
1322 ) -> CanonicalResponse<I> {
1323response_no_constraints_raw(
1324self.cx(),
1325self.max_input_universe,
1326self.var_kinds,
1327 Certainty::Maybe { cause, opaque_types_jank },
1328 )
1329 }
13301331/// Computes the region constraints and *new* opaque types registered when
1332 /// proving a goal.
1333 ///
1334 /// If an opaque was already constrained before proving this goal, then the
1335 /// external constraints do not need to record that opaque, since if it is
1336 /// further constrained by inference, that will be passed back in the var
1337 /// values.
1338x;#[instrument(level = "trace", skip(self), ret)]1339fn compute_external_query_constraints(
1340&self,
1341 certainty: Certainty,
1342 normalization_nested_goals: NestedNormalizationGoals<I>,
1343 ) -> ExternalConstraintsData<I> {
1344// We only return region constraints once the certainty is `Yes`. This
1345 // is necessary as we may drop nested goals on ambiguity, which may result
1346 // in unconstrained inference variables in the region constraints. It also
1347 // prevents us from emitting duplicate region constraints, avoiding some
1348 // unnecessary work. This slightly weakens the leak check in case it uses
1349 // region constraints from an ambiguous nested goal. This is tested in both
1350 // `tests/ui/higher-ranked/leak-check/leak-check-in-selection-5-ambig.rs` and
1351 // `tests/ui/higher-ranked/leak-check/leak-check-in-selection-6-ambig-unify.rs`.
1352let region_constraints = if certainty == Certainty::Yes {
1353self.delegate.make_deduplicated_outlives_constraints()
1354 } else {
1355 Default::default()
1356 };
13571358// We only return *newly defined* opaque types from canonical queries.
1359 //
1360 // Constraints for any existing opaque types are already tracked by changes
1361 // to the `var_values`.
1362let opaque_types = self
1363.delegate
1364 .clone_opaque_types_added_since(self.initial_opaque_types_storage_num_entries);
13651366 ExternalConstraintsData { region_constraints, opaque_types, normalization_nested_goals }
1367 }
1368}
13691370/// Eagerly replace aliases with inference variables, emitting `AliasRelate`
1371/// goals, used when adding goals to the `EvalCtxt`. We compute the
1372/// `AliasRelate` goals before evaluating the actual goal to get all the
1373/// constraints we can.
1374///
1375/// This is a performance optimization to more eagerly detect cycles during trait
1376/// solving. See tests/ui/traits/next-solver/cycles/cycle-modulo-ambig-aliases.rs.
1377///
1378/// The emitted goals get evaluated in the context of the parent goal; by
1379/// replacing aliases in nested goals we essentially pull the normalization out of
1380/// the nested goal. We want to treat the goal as if the normalization still happens
1381/// inside of the nested goal by inheriting the `step_kind` of the nested goal and
1382/// storing it in the `GoalSource` of the emitted `AliasRelate` goals.
1383/// This is necessary for tests/ui/sized/coinductive-1.rs to compile.
1384struct ReplaceAliasWithInfer<'me, 'a, D, I>
1385where
1386D: SolverDelegate<Interner = I>,
1387 I: Interner,
1388{
1389 ecx: &'me mut EvalCtxt<'a, D>,
1390 param_env: I::ParamEnv,
1391 normalization_goal_source: GoalSource,
1392 cache: HashMap<I::Ty, I::Ty>,
1393}
13941395impl<'me, 'a, D, I> ReplaceAliasWithInfer<'me, 'a, D, I>
1396where
1397D: SolverDelegate<Interner = I>,
1398 I: Interner,
1399{
1400fn new(
1401 ecx: &'me mut EvalCtxt<'a, D>,
1402 for_goal_source: GoalSource,
1403 param_env: I::ParamEnv,
1404 ) -> Self {
1405let step_kind = ecx.step_kind_for_source(for_goal_source);
1406ReplaceAliasWithInfer {
1407ecx,
1408param_env,
1409 normalization_goal_source: GoalSource::NormalizeGoal(step_kind),
1410 cache: Default::default(),
1411 }
1412 }
1413}
14141415impl<D, I> TypeFolder<I> for ReplaceAliasWithInfer<'_, '_, D, I>
1416where
1417D: SolverDelegate<Interner = I>,
1418 I: Interner,
1419{
1420fn cx(&self) -> I {
1421self.ecx.cx()
1422 }
14231424fn fold_ty(&mut self, ty: I::Ty) -> I::Ty {
1425match ty.kind() {
1426 ty::Alias(..) if !ty.has_escaping_bound_vars() => {
1427let infer_ty = self.ecx.next_ty_infer();
1428let normalizes_to = ty::PredicateKind::AliasRelate(
1429ty.into(),
1430infer_ty.into(),
1431 ty::AliasRelationDirection::Equate,
1432 );
1433self.ecx.add_goal(
1434self.normalization_goal_source,
1435Goal::new(self.cx(), self.param_env, normalizes_to),
1436 );
1437infer_ty1438 }
1439_ => {
1440if !ty.has_aliases() {
1441ty1442 } else if let Some(&entry) = self.cache.get(&ty) {
1443return entry;
1444 } else {
1445let res = ty.super_fold_with(self);
1446if !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());
1447res1448 }
1449 }
1450 }
1451 }
14521453fn fold_const(&mut self, ct: I::Const) -> I::Const {
1454match ct.kind() {
1455 ty::ConstKind::Unevaluated(..) if !ct.has_escaping_bound_vars() => {
1456let infer_ct = self.ecx.next_const_infer();
1457let normalizes_to = ty::PredicateKind::AliasRelate(
1458ct.into(),
1459infer_ct.into(),
1460 ty::AliasRelationDirection::Equate,
1461 );
1462self.ecx.add_goal(
1463self.normalization_goal_source,
1464Goal::new(self.cx(), self.param_env, normalizes_to),
1465 );
1466infer_ct1467 }
1468_ => ct.super_fold_with(self),
1469 }
1470 }
14711472fn fold_predicate(&mut self, predicate: I::Predicate) -> I::Predicate {
1473if predicate.allow_normalization() { predicate.super_fold_with(self) } else { predicate }
1474 }
1475}
14761477/// Do not call this directly, use the `tcx` query instead.
1478pub fn evaluate_root_goal_for_proof_tree_raw_provider<
1479 D: SolverDelegate<Interner = I>,
1480 I: Interner,
1481>(
1482 cx: I,
1483 canonical_goal: CanonicalInput<I>,
1484) -> (QueryResult<I>, I::Probe) {
1485let mut inspect = inspect::ProofTreeBuilder::new();
1486let canonical_result = SearchGraph::<D>::evaluate_root_goal_for_proof_tree(
1487cx,
1488cx.recursion_limit(),
1489canonical_goal,
1490&mut inspect,
1491 );
1492let final_revision = inspect.unwrap();
1493 (canonical_result, cx.mk_probe(final_revision))
1494}
14951496/// Evaluate a goal to build a proof tree.
1497///
1498/// This is a copy of [EvalCtxt::evaluate_goal_raw] which avoids relying on the
1499/// [EvalCtxt] and uses a separate cache.
1500pub(super) fn evaluate_root_goal_for_proof_tree<D: SolverDelegate<Interner = I>, I: Interner>(
1501 delegate: &D,
1502 goal: Goal<I, I::Predicate>,
1503 origin_span: I::Span,
1504) -> (Result<NestedNormalizationGoals<I>, NoSolution>, inspect::GoalEvaluation<I>) {
1505let opaque_types = delegate.clone_opaque_types_lookup_table();
1506let (goal, opaque_types) = eager_resolve_vars(delegate, (goal, opaque_types));
15071508let (orig_values, canonical_goal) = canonicalize_goal(delegate, goal, &opaque_types);
15091510let (canonical_result, final_revision) =
1511delegate.cx().evaluate_root_goal_for_proof_tree_raw(canonical_goal);
15121513let proof_tree = inspect::GoalEvaluation {
1514 uncanonicalized_goal: goal,
1515orig_values,
1516final_revision,
1517 result: canonical_result,
1518 };
15191520let response = match canonical_result {
1521Err(e) => return (Err(e), proof_tree),
1522Ok(response) => response,
1523 };
15241525let (normalization_nested_goals, _certainty) = instantiate_and_apply_query_response(
1526delegate,
1527goal.param_env,
1528&proof_tree.orig_values,
1529response,
1530origin_span,
1531 );
15321533 (Ok(normalization_nested_goals), proof_tree)
1534}