1use std::mem;
2use std::ops::ControlFlow;
34#[cfg(feature = "nightly")]
5use rustc_macros::HashStable;
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, 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_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)]
140#[cfg_attr(feature = "nightly", derive(const _: () =
{
impl ::rustc_data_structures::stable_hasher::HashStable for
GenerateProofTree {
#[inline]
fn hash_stable<__Hcx: ::rustc_data_structures::stable_hasher::HashStableContext>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
match *self {
GenerateProofTree::Yes => {}
GenerateProofTree::No => {}
}
}
}
};HashStable))]
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(MaybeInfo {
221 cause: _,
222 opaque_types_jank,
223 stalled_on_coroutines: _,
224 }) => match opaque_types_jank {
225 OpaqueTypesJank::AllGood => true,
226 OpaqueTypesJank::ErrorIfRigidSelfTy => false,
227 },
228 })
229 })
230 }
231232fn root_goal_may_hold_with_depth(
233&self,
234 root_depth: usize,
235 goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
236 ) -> bool {
237self.probe(|| {
238EvalCtxt::enter_root(self, root_depth, I::Span::dummy(), |ecx| {
239ecx.evaluate_goal(GoalSource::Misc, goal, None)
240 })
241 })
242 .is_ok()
243 }
244245#[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(245u32),
::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))]246fn evaluate_root_goal_for_proof_tree(
247&self,
248 goal: Goal<I, I::Predicate>,
249 span: I::Span,
250 ) -> (Result<NestedNormalizationGoals<I>, NoSolution>, inspect::GoalEvaluation<I>) {
251 evaluate_root_goal_for_proof_tree(self, goal, span)
252 }
253}
254255impl<'a, D, I> EvalCtxt<'a, D>
256where
257D: SolverDelegate<Interner = I>,
258 I: Interner,
259{
260pub(super) fn typing_mode(&self) -> TypingMode<I> {
261self.delegate.typing_mode()
262 }
263264/// Computes the `PathKind` for the step from the current goal to the
265 /// nested goal required due to `source`.
266 ///
267 /// See #136824 for a more detailed reasoning for this behavior. We
268 /// consider cycles to be coinductive if they 'step into' a where-clause
269 /// of a coinductive trait. We will likely extend this function in the future
270 /// and will need to clearly document it in the rustc-dev-guide before
271 /// stabilization.
272pub(super) fn step_kind_for_source(&self, source: GoalSource) -> PathKind {
273match source {
274// We treat these goals as unknown for now. It is likely that most miscellaneous
275 // nested goals will be converted to an inductive variant in the future.
276 //
277 // Having unknown cycles is always the safer option, as changing that to either
278 // succeed or hard error is backwards compatible. If we incorrectly treat a cycle
279 // as inductive even though it should not be, it may be unsound during coherence and
280 // fixing it may cause inference breakage or introduce ambiguity.
281GoalSource::Misc => PathKind::Unknown,
282 GoalSource::NormalizeGoal(path_kind) => path_kind,
283 GoalSource::ImplWhereBound => match self.current_goal_kind {
284// We currently only consider a cycle coinductive if it steps
285 // into a where-clause of a coinductive trait.
286CurrentGoalKind::CoinductiveTrait => PathKind::Coinductive,
287// While normalizing via an impl does step into a where-clause of
288 // an impl, accessing the associated item immediately steps out of
289 // it again. This means cycles/recursive calls are not guarded
290 // by impls used for normalization.
291 //
292 // See tests/ui/traits/next-solver/cycles/normalizes-to-is-not-productive.rs
293 // for how this can go wrong.
294CurrentGoalKind::NormalizesTo => PathKind::Inductive,
295// We probably want to make all traits coinductive in the future,
296 // so we treat cycles involving where-clauses of not-yet coinductive
297 // traits as ambiguous for now.
298CurrentGoalKind::Misc => PathKind::Unknown,
299 },
300// Relating types is always unproductive. If we were to map proof trees to
301 // corecursive functions as explained in #136824, relating types never
302 // introduces a constructor which could cause the recursion to be guarded.
303GoalSource::TypeRelating => PathKind::Inductive,
304// These goal sources are likely unproductive and can be changed to
305 // `PathKind::Inductive`. Keeping them as unknown until we're confident
306 // about this and have an example where it is necessary.
307GoalSource::AliasBoundConstCondition | GoalSource::AliasWellFormed => PathKind::Unknown,
308 }
309 }
310311/// Creates a root evaluation context and search graph. This should only be
312 /// used from outside of any evaluation, and other methods should be preferred
313 /// over using this manually (such as [`SolverDelegateEvalExt::evaluate_root_goal`]).
314pub(super) fn enter_root<R>(
315 delegate: &D,
316 root_depth: usize,
317 origin_span: I::Span,
318 f: impl FnOnce(&mut EvalCtxt<'_, D>) -> R,
319 ) -> R {
320let mut search_graph = SearchGraph::new(root_depth);
321322let mut ecx = EvalCtxt {
323delegate,
324 search_graph: &mut search_graph,
325 nested_goals: Default::default(),
326 inspect: inspect::EvaluationStepBuilder::new_noop(),
327328// Only relevant when canonicalizing the response,
329 // which we don't do within this evaluation context.
330max_input_universe: ty::UniverseIndex::ROOT,
331 initial_opaque_types_storage_num_entries: Default::default(),
332 var_kinds: Default::default(),
333 var_values: CanonicalVarValues::dummy(),
334 current_goal_kind: CurrentGoalKind::Misc,
335origin_span,
336 tainted: Ok(()),
337 };
338let result = f(&mut ecx);
339if !ecx.nested_goals.is_empty() {
{
::core::panicking::panic_fmt(format_args!("root `EvalCtxt` should not have any goals added to it"));
}
};assert!(
340 ecx.nested_goals.is_empty(),
341"root `EvalCtxt` should not have any goals added to it"
342);
343if !search_graph.is_empty() {
::core::panicking::panic("assertion failed: search_graph.is_empty()")
};assert!(search_graph.is_empty());
344result345 }
346347/// Creates a nested evaluation context that shares the same search graph as the
348 /// one passed in. This is suitable for evaluation, granted that the search graph
349 /// has had the nested goal recorded on its stack. This method only be used by
350 /// `search_graph::Delegate::compute_goal`.
351 ///
352 /// This function takes care of setting up the inference context, setting the anchor,
353 /// and registering opaques from the canonicalized input.
354pub(super) fn enter_canonical<R>(
355 cx: I,
356 search_graph: &'a mut SearchGraph<D>,
357 canonical_input: CanonicalInput<I>,
358 proof_tree_builder: &mut inspect::ProofTreeBuilder<D>,
359 f: impl FnOnce(&mut EvalCtxt<'_, D>, Goal<I, I::Predicate>) -> R,
360 ) -> R {
361let (ref delegate, input, var_values) = D::build_with_canonical(cx, &canonical_input);
362for (key, ty) in input.predefined_opaques_in_body.iter() {
363let prev = delegate.register_hidden_type_in_storage(key, ty, I::Span::dummy());
364// It may be possible that two entries in the opaque type storage end up
365 // with the same key after resolving contained inference variables.
366 //
367 // We could put them in the duplicate list but don't have to. The opaques we
368 // encounter here are already tracked in the caller, so there's no need to
369 // also store them here. We'd take them out when computing the query response
370 // and then discard them, as they're already present in the input.
371 //
372 // Ideally we'd drop duplicate opaque type definitions when computing
373 // the canonical input. This is more annoying to implement and may cause a
374 // perf regression, so we do it inside of the query for now.
375if let Some(prev) = prev {
376{
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:376",
"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(376u32),
::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`");
377 }
378 }
379380let initial_opaque_types_storage_num_entries = delegate.opaque_types_storage_num_entries();
381let mut ecx = EvalCtxt {
382delegate,
383 var_kinds: canonical_input.canonical.var_kinds,
384var_values,
385 current_goal_kind: CurrentGoalKind::from_query_input(cx, input),
386 max_input_universe: canonical_input.canonical.max_universe,
387initial_opaque_types_storage_num_entries,
388search_graph,
389 nested_goals: Default::default(),
390 origin_span: I::Span::dummy(),
391 tainted: Ok(()),
392 inspect: proof_tree_builder.new_evaluation_step(var_values),
393 };
394395let result = f(&mut ecx, input.goal);
396ecx.inspect.probe_final_state(ecx.delegate, ecx.max_input_universe);
397proof_tree_builder.finish_evaluation_step(ecx.inspect);
398399// When creating a query response we clone the opaque type constraints
400 // instead of taking them. This would cause an ICE here, since we have
401 // assertions against dropping an `InferCtxt` without taking opaques.
402 // FIXME: Once we remove support for the old impl we can remove this.
403 // FIXME: Could we make `build_with_canonical` into `enter_with_canonical` and call this at the end?
404delegate.reset_opaque_types();
405406result407 }
408409pub(super) fn ignore_candidate_head_usages(&mut self, usages: CandidateHeadUsages) {
410self.search_graph.ignore_candidate_head_usages(usages);
411 }
412413/// Recursively evaluates `goal`, returning whether any inference vars have
414 /// been constrained and the certainty of the result.
415fn evaluate_goal(
416&mut self,
417 source: GoalSource,
418 goal: Goal<I, I::Predicate>,
419 stalled_on: Option<GoalStalledOn<I>>,
420 ) -> Result<GoalEvaluation<I>, NoSolution> {
421let (normalization_nested_goals, goal_evaluation) =
422self.evaluate_goal_raw(source, goal, stalled_on)?;
423if !normalization_nested_goals.is_empty() {
::core::panicking::panic("assertion failed: normalization_nested_goals.is_empty()")
};assert!(normalization_nested_goals.is_empty());
424Ok(goal_evaluation)
425 }
426427/// Recursively evaluates `goal`, returning the nested goals in case
428 /// the nested goal is a `NormalizesTo` goal.
429 ///
430 /// As all other goal kinds do not return any nested goals and
431 /// `NormalizesTo` is only used by `AliasRelate`, all other callsites
432 /// should use [`EvalCtxt::evaluate_goal`] which discards that empty
433 /// storage.
434pub(super) fn evaluate_goal_raw(
435&mut self,
436 source: GoalSource,
437 goal: Goal<I, I::Predicate>,
438 stalled_on: Option<GoalStalledOn<I>>,
439 ) -> Result<(NestedNormalizationGoals<I>, GoalEvaluation<I>), NoSolution> {
440// If we have run this goal before, and it was stalled, check that any of the goal's
441 // args have changed. Otherwise, we don't need to re-run the goal because it'll remain
442 // stalled, since it'll canonicalize the same way and evaluation is pure.
443if let Some(GoalStalledOn {
444 num_opaques,
445ref stalled_vars,
446ref sub_roots,
447 stalled_certainty,
448 }) = stalled_on449 && !stalled_vars.iter().any(|value| self.delegate.is_changed_arg(*value))
450 && !sub_roots451 .iter()
452 .any(|&vid| self.delegate.sub_unification_table_root_var(vid) != vid)
453 && !self.delegate.opaque_types_storage_num_entries().needs_reevaluation(num_opaques)
454 {
455return Ok((
456NestedNormalizationGoals::empty(),
457GoalEvaluation {
458goal,
459 certainty: stalled_certainty,
460 has_changed: HasChanged::No,
461stalled_on,
462 },
463 ));
464 }
465466// We only care about one entry per `OpaqueTypeKey` here,
467 // so we only canonicalize the lookup table and ignore
468 // duplicate entries.
469let opaque_types = self.delegate.clone_opaque_types_lookup_table();
470let (goal, opaque_types) = eager_resolve_vars(self.delegate, (goal, opaque_types));
471472let (orig_values, canonical_goal) = canonicalize_goal(self.delegate, goal, &opaque_types);
473let canonical_result = self.search_graph.evaluate_goal(
474self.cx(),
475canonical_goal,
476self.step_kind_for_source(source),
477&mut inspect::ProofTreeBuilder::new_noop(),
478 );
479let response = match canonical_result {
480Err(e) => return Err(e),
481Ok(response) => response,
482 };
483484let has_changed =
485if !has_only_region_constraints(response) { HasChanged::Yes } else { HasChanged::No };
486487let (normalization_nested_goals, certainty) = instantiate_and_apply_query_response(
488self.delegate,
489goal.param_env,
490&orig_values,
491response,
492self.origin_span,
493 );
494495// FIXME: We previously had an assert here that checked that recomputing
496 // a goal after applying its constraints did not change its response.
497 //
498 // This assert was removed as it did not hold for goals constraining
499 // an inference variable to a recursive alias, e.g. in
500 // tests/ui/traits/next-solver/overflow/recursive-self-normalization.rs.
501 //
502 // Once we have decided on how to handle trait-system-refactor-initiative#75,
503 // we should re-add an assert here.
504505let stalled_on = match certainty {
506 Certainty::Yes => None,
507 Certainty::Maybe { .. } => match has_changed {
508// FIXME: We could recompute a *new* set of stalled variables by walking
509 // through the orig values, resolving, and computing the root vars of anything
510 // that is not resolved. Only when *these* have changed is it meaningful
511 // to recompute this goal.
512HasChanged::Yes => None,
513 HasChanged::No => {
514let mut stalled_vars = orig_values;
515516// Remove the unconstrained RHS arg, which is expected to have changed.
517if let Some(normalizes_to) = goal.predicate.as_normalizes_to() {
518let normalizes_to = normalizes_to.skip_binder();
519let rhs_arg: I::GenericArg = normalizes_to.term.into();
520let idx = stalled_vars521 .iter()
522 .rposition(|arg| *arg == rhs_arg)
523 .expect("expected unconstrained arg");
524stalled_vars.swap_remove(idx);
525 }
526527// Remove the canonicalized universal vars, since we only care about stalled existentials.
528let mut sub_roots = Vec::new();
529stalled_vars.retain(|arg| match arg.kind() {
530// Lifetimes can never stall goals.
531ty::GenericArgKind::Lifetime(_) => false,
532 ty::GenericArgKind::Type(ty) => match ty.kind() {
533 ty::Infer(ty::TyVar(vid)) => {
534sub_roots.push(self.delegate.sub_unification_table_root_var(vid));
535true
536}
537 ty::Infer(_) => true,
538 ty::Param(_) | ty::Placeholder(_) => false,
539_ => {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("unexpected orig_value: {0:?}", ty)));
}unreachable!("unexpected orig_value: {ty:?}"),
540 },
541 ty::GenericArgKind::Const(ct) => match ct.kind() {
542 ty::ConstKind::Infer(_) => true,
543 ty::ConstKind::Param(_) | ty::ConstKind::Placeholder(_) => false,
544_ => {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("unexpected orig_value: {0:?}", ct)));
}unreachable!("unexpected orig_value: {ct:?}"),
545 },
546 });
547548Some(GoalStalledOn {
549 num_opaques: canonical_goal550 .canonical
551 .value
552 .predefined_opaques_in_body
553 .len(),
554stalled_vars,
555sub_roots,
556 stalled_certainty: certainty,
557 })
558 }
559 },
560 };
561562Ok((
563normalization_nested_goals,
564GoalEvaluation { goal, certainty, has_changed, stalled_on },
565 ))
566 }
567568pub(super) fn compute_goal(&mut self, goal: Goal<I, I::Predicate>) -> QueryResult<I> {
569let Goal { param_env, predicate } = goal;
570let kind = predicate.kind();
571self.enter_forall(kind, |ecx, kind| match kind {
572 ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)) => {
573ecx.compute_trait_goal(Goal { param_env, predicate }).map(|(r, _via)| r)
574 }
575 ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(predicate)) => {
576ecx.compute_host_effect_goal(Goal { param_env, predicate })
577 }
578 ty::PredicateKind::Clause(ty::ClauseKind::Projection(predicate)) => {
579ecx.compute_projection_goal(Goal { param_env, predicate })
580 }
581 ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(predicate)) => {
582ecx.compute_type_outlives_goal(Goal { param_env, predicate })
583 }
584 ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(predicate)) => {
585ecx.compute_region_outlives_goal(Goal { param_env, predicate })
586 }
587 ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ty)) => {
588ecx.compute_const_arg_has_type_goal(Goal { param_env, predicate: (ct, ty) })
589 }
590 ty::PredicateKind::Clause(ty::ClauseKind::UnstableFeature(symbol)) => {
591ecx.compute_unstable_feature_goal(param_env, symbol)
592 }
593 ty::PredicateKind::Subtype(predicate) => {
594ecx.compute_subtype_goal(Goal { param_env, predicate })
595 }
596 ty::PredicateKind::Coerce(predicate) => {
597ecx.compute_coerce_goal(Goal { param_env, predicate })
598 }
599 ty::PredicateKind::DynCompatible(trait_def_id) => {
600ecx.compute_dyn_compatible_goal(trait_def_id)
601 }
602 ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(term)) => {
603ecx.compute_well_formed_goal(Goal { param_env, predicate: term })
604 }
605 ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(ct)) => {
606ecx.compute_const_evaluatable_goal(Goal { param_env, predicate: ct })
607 }
608 ty::PredicateKind::ConstEquate(_, _) => {
609{
::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")610 }
611 ty::PredicateKind::NormalizesTo(predicate) => {
612ecx.compute_normalizes_to_goal(Goal { param_env, predicate })
613 }
614 ty::PredicateKind::AliasRelate(lhs, rhs, direction) => {
615ecx.compute_alias_relate_goal(Goal { param_env, predicate: (lhs, rhs, direction) })
616 }
617 ty::PredicateKind::Ambiguous => {
618ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
619 }
620 })
621 }
622623// Recursively evaluates all the goals added to this `EvalCtxt` to completion, returning
624 // the certainty of all the goals.
625#[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(625u32),
::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:638",
"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(638u32),
::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))]626pub(super) fn try_evaluate_added_goals(&mut self) -> Result<Certainty, NoSolution> {
627for _ in 0..FIXPOINT_STEP_LIMIT {
628match self.evaluate_added_goals_step() {
629Ok(None) => {}
630Ok(Some(cert)) => return Ok(cert),
631Err(NoSolution) => {
632self.tainted = Err(NoSolution);
633return Err(NoSolution);
634 }
635 }
636 }
637638debug!("try_evaluate_added_goals: encountered overflow");
639Ok(Certainty::overflow(false))
640 }
641642/// Iterate over all added goals: returning `Ok(Some(_))` in case we can stop rerunning.
643 ///
644 /// Goals for the next step get directly added to the nested goals of the `EvalCtxt`.
645fn evaluate_added_goals_step(&mut self) -> Result<Option<Certainty>, NoSolution> {
646let cx = self.cx();
647// If this loop did not result in any progress, what's our final certainty.
648let mut unchanged_certainty = Some(Certainty::Yes);
649for (source, goal, stalled_on) in mem::take(&mut self.nested_goals) {
650if let Some(certainty) = self.delegate.compute_goal_fast_path(goal, self.origin_span) {
651match certainty {
652 Certainty::Yes => {}
653 Certainty::Maybe { .. } => {
654self.nested_goals.push((source, goal, None));
655 unchanged_certainty = unchanged_certainty.map(|c| c.and(certainty));
656 }
657 }
658continue;
659 }
660661// We treat normalizes-to goals specially here. In each iteration we take the
662 // RHS of the projection, replace it with a fresh inference variable, and only
663 // after evaluating that goal do we equate the fresh inference variable with the
664 // actual RHS of the predicate.
665 //
666 // This is both to improve caching, and to avoid using the RHS of the
667 // projection predicate to influence the normalizes-to candidate we select.
668 //
669 // Forgetting to replace the RHS with a fresh inference variable when we evaluate
670 // this goal results in an ICE.
671if let Some(pred) = goal.predicate.as_normalizes_to() {
672// We should never encounter higher-ranked normalizes-to goals.
673let pred = pred.no_bound_vars().unwrap();
674// Replace the goal with an unconstrained infer var, so the
675 // RHS does not affect projection candidate assembly.
676let unconstrained_rhs = self.next_term_infer_of_kind(pred.term);
677let unconstrained_goal =
678 goal.with(cx, ty::NormalizesTo { alias: pred.alias, term: unconstrained_rhs });
679680let (
681 NestedNormalizationGoals(nested_goals),
682 GoalEvaluation { goal, certainty, stalled_on, has_changed: _ },
683 ) = self.evaluate_goal_raw(source, unconstrained_goal, stalled_on)?;
684// Add the nested goals from normalization to our own nested goals.
685{
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:685",
"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(685u32),
::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);
686self.nested_goals.extend(nested_goals.into_iter().map(|(s, g)| (s, g, None)));
687688// Finally, equate the goal's RHS with the unconstrained var.
689 //
690 // SUBTLE:
691 // We structurally relate aliases here. This is necessary
692 // as we otherwise emit a nested `AliasRelate` goal in case the
693 // returned term is a rigid alias, resulting in overflow.
694 //
695 // It is correct as both `goal.predicate.term` and `unconstrained_rhs`
696 // start out as an unconstrained inference variable so any aliases get
697 // fully normalized when instantiating it.
698 //
699 // FIXME: Strictly speaking this may be incomplete if the normalized-to
700 // type contains an ambiguous alias referencing bound regions. We should
701 // consider changing this to only use "shallow structural equality".
702self.eq_structurally_relating_aliases(
703 goal.param_env,
704 pred.term,
705 unconstrained_rhs,
706 )?;
707708// We only look at the `projection_ty` part here rather than
709 // looking at the "has changed" return from evaluate_goal,
710 // because we expect the `unconstrained_rhs` part of the predicate
711 // to have changed -- that means we actually normalized successfully!
712 // FIXME: Do we need to eagerly resolve here? Or should we check
713 // if the cache key has any changed vars?
714let with_resolved_vars = self.resolve_vars_if_possible(goal);
715if pred.alias
716 != with_resolved_vars
717 .predicate
718 .as_normalizes_to()
719 .unwrap()
720 .no_bound_vars()
721 .unwrap()
722 .alias
723 {
724 unchanged_certainty = None;
725 }
726727match certainty {
728 Certainty::Yes => {}
729 Certainty::Maybe { .. } => {
730self.nested_goals.push((source, with_resolved_vars, stalled_on));
731 unchanged_certainty = unchanged_certainty.map(|c| c.and(certainty));
732 }
733 }
734 } else {
735let GoalEvaluation { goal, certainty, has_changed, stalled_on } =
736self.evaluate_goal(source, goal, stalled_on)?;
737if has_changed == HasChanged::Yes {
738 unchanged_certainty = None;
739 }
740741match certainty {
742 Certainty::Yes => {}
743 Certainty::Maybe { .. } => {
744self.nested_goals.push((source, goal, stalled_on));
745 unchanged_certainty = unchanged_certainty.map(|c| c.and(certainty));
746 }
747 }
748 }
749 }
750751Ok(unchanged_certainty)
752 }
753754/// Record impl args in the proof tree for later access by `InspectCandidate`.
755pub(crate) fn record_impl_args(&mut self, impl_args: I::GenericArgs) {
756self.inspect.record_impl_args(self.delegate, self.max_input_universe, impl_args)
757 }
758759pub(super) fn cx(&self) -> I {
760self.delegate.cx()
761 }
762763#[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(763u32),
::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))]764pub(super) fn add_goal(&mut self, source: GoalSource, mut goal: Goal<I, I::Predicate>) {
765 goal.predicate =
766 goal.predicate.fold_with(&mut ReplaceAliasWithInfer::new(self, source, goal.param_env));
767self.inspect.add_goal(self.delegate, self.max_input_universe, source, goal);
768self.nested_goals.push((source, goal, None));
769 }
770771#[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(771u32),
::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))]772pub(super) fn add_goals(
773&mut self,
774 source: GoalSource,
775 goals: impl IntoIterator<Item = Goal<I, I::Predicate>>,
776 ) {
777for goal in goals {
778self.add_goal(source, goal);
779 }
780 }
781782pub(super) fn next_region_var(&mut self) -> I::Region {
783let region = self.delegate.next_region_infer();
784self.inspect.add_var_value(region);
785region786 }
787788pub(super) fn next_ty_infer(&mut self) -> I::Ty {
789let ty = self.delegate.next_ty_infer();
790self.inspect.add_var_value(ty);
791ty792 }
793794pub(super) fn next_const_infer(&mut self) -> I::Const {
795let ct = self.delegate.next_const_infer();
796self.inspect.add_var_value(ct);
797ct798 }
799800/// Returns a ty infer or a const infer depending on whether `kind` is a `Ty` or `Const`.
801 /// If `kind` is an integer inference variable this will still return a ty infer var.
802pub(super) fn next_term_infer_of_kind(&mut self, term: I::Term) -> I::Term {
803match term.kind() {
804 ty::TermKind::Ty(_) => self.next_ty_infer().into(),
805 ty::TermKind::Const(_) => self.next_const_infer().into(),
806 }
807 }
808809/// Is the projection predicate is of the form `exists<T> <Ty as Trait>::Assoc = T`.
810 ///
811 /// This is the case if the `term` does not occur in any other part of the predicate
812 /// and is able to name all other placeholder and inference variables.
813x;#[instrument(level = "trace", skip(self), ret)]814pub(super) fn term_is_fully_unconstrained(&self, goal: Goal<I, ty::NormalizesTo<I>>) -> bool {
815let universe_of_term = match goal.predicate.term.kind() {
816 ty::TermKind::Ty(ty) => {
817if let ty::Infer(ty::TyVar(vid)) = ty.kind() {
818self.delegate.universe_of_ty(vid).unwrap()
819 } else {
820return false;
821 }
822 }
823 ty::TermKind::Const(ct) => {
824if let ty::ConstKind::Infer(ty::InferConst::Var(vid)) = ct.kind() {
825self.delegate.universe_of_ct(vid).unwrap()
826 } else {
827return false;
828 }
829 }
830 };
831832struct ContainsTermOrNotNameable<'a, D: SolverDelegate<Interner = I>, I: Interner> {
833 term: I::Term,
834 universe_of_term: ty::UniverseIndex,
835 delegate: &'a D,
836 cache: HashSet<I::Ty>,
837 }
838839impl<D: SolverDelegate<Interner = I>, I: Interner> ContainsTermOrNotNameable<'_, D, I> {
840fn check_nameable(&self, universe: ty::UniverseIndex) -> ControlFlow<()> {
841if self.universe_of_term.can_name(universe) {
842 ControlFlow::Continue(())
843 } else {
844 ControlFlow::Break(())
845 }
846 }
847 }
848849impl<D: SolverDelegate<Interner = I>, I: Interner> TypeVisitor<I>
850for ContainsTermOrNotNameable<'_, D, I>
851 {
852type Result = ControlFlow<()>;
853fn visit_ty(&mut self, t: I::Ty) -> Self::Result {
854if self.cache.contains(&t) {
855return ControlFlow::Continue(());
856 }
857858match t.kind() {
859 ty::Infer(ty::TyVar(vid)) => {
860if let ty::TermKind::Ty(term) = self.term.kind()
861 && let ty::Infer(ty::TyVar(term_vid)) = term.kind()
862 && self.delegate.root_ty_var(vid) == self.delegate.root_ty_var(term_vid)
863 {
864return ControlFlow::Break(());
865 }
866867self.check_nameable(self.delegate.universe_of_ty(vid).unwrap())?;
868 }
869 ty::Placeholder(p) => self.check_nameable(p.universe())?,
870_ => {
871if t.has_non_region_infer() || t.has_placeholders() {
872 t.super_visit_with(self)?
873}
874 }
875 }
876877assert!(self.cache.insert(t));
878 ControlFlow::Continue(())
879 }
880881fn visit_const(&mut self, c: I::Const) -> Self::Result {
882match c.kind() {
883 ty::ConstKind::Infer(ty::InferConst::Var(vid)) => {
884if let ty::TermKind::Const(term) = self.term.kind()
885 && let ty::ConstKind::Infer(ty::InferConst::Var(term_vid)) = term.kind()
886 && self.delegate.root_const_var(vid)
887 == self.delegate.root_const_var(term_vid)
888 {
889return ControlFlow::Break(());
890 }
891892self.check_nameable(self.delegate.universe_of_ct(vid).unwrap())
893 }
894 ty::ConstKind::Placeholder(p) => self.check_nameable(p.universe()),
895_ => {
896if c.has_non_region_infer() || c.has_placeholders() {
897 c.super_visit_with(self)
898 } else {
899 ControlFlow::Continue(())
900 }
901 }
902 }
903 }
904905fn visit_predicate(&mut self, p: I::Predicate) -> Self::Result {
906if p.has_non_region_infer() || p.has_placeholders() {
907 p.super_visit_with(self)
908 } else {
909 ControlFlow::Continue(())
910 }
911 }
912913fn visit_clauses(&mut self, c: I::Clauses) -> Self::Result {
914if c.has_non_region_infer() || c.has_placeholders() {
915 c.super_visit_with(self)
916 } else {
917 ControlFlow::Continue(())
918 }
919 }
920 }
921922let mut visitor = ContainsTermOrNotNameable {
923 delegate: self.delegate,
924 universe_of_term,
925 term: goal.predicate.term,
926 cache: Default::default(),
927 };
928 goal.predicate.alias.visit_with(&mut visitor).is_continue()
929 && goal.param_env.visit_with(&mut visitor).is_continue()
930 }
931932pub(super) fn sub_unify_ty_vids_raw(&self, a: ty::TyVid, b: ty::TyVid) {
933self.delegate.sub_unify_ty_vids_raw(a, b)
934 }
935936x;#[instrument(level = "trace", skip(self, param_env), ret)]937pub(super) fn eq<T: Relate<I>>(
938&mut self,
939 param_env: I::ParamEnv,
940 lhs: T,
941 rhs: T,
942 ) -> Result<(), NoSolution> {
943self.relate(param_env, lhs, ty::Variance::Invariant, rhs)
944 }
945946/// This should be used when relating a rigid alias with another type.
947 ///
948 /// Normally we emit a nested `AliasRelate` when equating an inference
949 /// variable and an alias. This causes us to instead constrain the inference
950 /// variable to the alias without emitting a nested alias relate goals.
951x;#[instrument(level = "trace", skip(self, param_env), ret)]952pub(super) fn relate_rigid_alias_non_alias(
953&mut self,
954 param_env: I::ParamEnv,
955 alias: ty::AliasTerm<I>,
956 variance: ty::Variance,
957 term: I::Term,
958 ) -> Result<(), NoSolution> {
959// NOTE: this check is purely an optimization, the structural eq would
960 // always fail if the term is not an inference variable.
961if term.is_infer() {
962let cx = self.cx();
963// We need to relate `alias` to `term` treating only the outermost
964 // constructor as rigid, relating any contained generic arguments as
965 // normal. We do this by first structurally equating the `term`
966 // with the alias constructor instantiated with unconstrained infer vars,
967 // and then relate this with the whole `alias`.
968 //
969 // Alternatively we could modify `Equate` for this case by adding another
970 // variant to `StructurallyRelateAliases`.
971let identity_args = self.fresh_args_for_item(alias.def_id());
972let rigid_ctor = alias.with_args(cx, identity_args);
973let ctor_term = rigid_ctor.to_term(cx);
974let obligations = self.delegate.eq_structurally_relating_aliases(
975 param_env,
976 term,
977 ctor_term,
978self.origin_span,
979 )?;
980debug_assert!(obligations.is_empty());
981self.relate(param_env, alias, variance, rigid_ctor)
982 } else {
983Err(NoSolution)
984 }
985 }
986987/// This should only be used when we're either instantiating a previously
988 /// unconstrained "return value" or when we're sure that all aliases in
989 /// the types are rigid.
990x;#[instrument(level = "trace", skip(self, param_env), ret)]991pub(super) fn eq_structurally_relating_aliases<T: Relate<I>>(
992&mut self,
993 param_env: I::ParamEnv,
994 lhs: T,
995 rhs: T,
996 ) -> Result<(), NoSolution> {
997let result = self.delegate.eq_structurally_relating_aliases(
998 param_env,
999 lhs,
1000 rhs,
1001self.origin_span,
1002 )?;
1003assert_eq!(result, vec![]);
1004Ok(())
1005 }
10061007x;#[instrument(level = "trace", skip(self, param_env), ret)]1008pub(super) fn sub<T: Relate<I>>(
1009&mut self,
1010 param_env: I::ParamEnv,
1011 sub: T,
1012 sup: T,
1013 ) -> Result<(), NoSolution> {
1014self.relate(param_env, sub, ty::Variance::Covariant, sup)
1015 }
10161017x;#[instrument(level = "trace", skip(self, param_env), ret)]1018pub(super) fn relate<T: Relate<I>>(
1019&mut self,
1020 param_env: I::ParamEnv,
1021 lhs: T,
1022 variance: ty::Variance,
1023 rhs: T,
1024 ) -> Result<(), NoSolution> {
1025let goals = self.delegate.relate(param_env, lhs, variance, rhs, self.origin_span)?;
1026for &goal in goals.iter() {
1027let source = match goal.predicate.kind().skip_binder() {
1028 ty::PredicateKind::Subtype { .. } | ty::PredicateKind::AliasRelate(..) => {
1029 GoalSource::TypeRelating
1030 }
1031// FIXME(-Znext-solver=coinductive): should these WF goals also be unproductive?
1032ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_)) => GoalSource::Misc,
1033 p => unreachable!("unexpected nested goal in `relate`: {p:?}"),
1034 };
1035self.add_goal(source, goal);
1036 }
1037Ok(())
1038 }
10391040/// Equates two values returning the nested goals without adding them
1041 /// to the nested goals of the `EvalCtxt`.
1042 ///
1043 /// If possible, try using `eq` instead which automatically handles nested
1044 /// goals correctly.
1045x;#[instrument(level = "trace", skip(self, param_env), ret)]1046pub(super) fn eq_and_get_goals<T: Relate<I>>(
1047&self,
1048 param_env: I::ParamEnv,
1049 lhs: T,
1050 rhs: T,
1051 ) -> Result<Vec<Goal<I, I::Predicate>>, NoSolution> {
1052Ok(self.delegate.relate(param_env, lhs, ty::Variance::Invariant, rhs, self.origin_span)?)
1053 }
10541055pub(super) fn instantiate_binder_with_infer<T: TypeFoldable<I> + Copy>(
1056&self,
1057 value: ty::Binder<I, T>,
1058 ) -> T {
1059self.delegate.instantiate_binder_with_infer(value)
1060 }
10611062/// `enter_forall`, but takes `&mut self` and passes it back through the
1063 /// callback since it can't be aliased during the call.
1064pub(super) fn enter_forall<T: TypeFoldable<I>, U>(
1065&mut self,
1066 value: ty::Binder<I, T>,
1067 f: impl FnOnce(&mut Self, T) -> U,
1068 ) -> U {
1069self.delegate.enter_forall(value, |value| f(self, value))
1070 }
10711072pub(super) fn resolve_vars_if_possible<T>(&self, value: T) -> T
1073where
1074T: TypeFoldable<I>,
1075 {
1076self.delegate.resolve_vars_if_possible(value)
1077 }
10781079pub(super) fn shallow_resolve(&self, ty: I::Ty) -> I::Ty {
1080self.delegate.shallow_resolve(ty)
1081 }
10821083pub(super) fn eager_resolve_region(&self, r: I::Region) -> I::Region {
1084if let ty::ReVar(vid) = r.kind() {
1085self.delegate.opportunistic_resolve_lt_var(vid)
1086 } else {
1087r1088 }
1089 }
10901091pub(super) fn fresh_args_for_item(&mut self, def_id: I::DefId) -> I::GenericArgs {
1092let args = self.delegate.fresh_args_for_item(def_id);
1093for arg in args.iter() {
1094self.inspect.add_var_value(arg);
1095 }
1096args1097 }
10981099pub(super) fn register_ty_outlives(&self, ty: I::Ty, lt: I::Region) {
1100self.delegate.register_ty_outlives(ty, lt, self.origin_span);
1101 }
11021103pub(super) fn register_region_outlives(&self, a: I::Region, b: I::Region) {
1104// `'a: 'b` ==> `'b <= 'a`
1105self.delegate.sub_regions(b, a, self.origin_span);
1106 }
11071108/// Computes the list of goals required for `arg` to be well-formed
1109pub(super) fn well_formed_goals(
1110&self,
1111 param_env: I::ParamEnv,
1112 term: I::Term,
1113 ) -> Option<Vec<Goal<I, I::Predicate>>> {
1114self.delegate.well_formed_goals(param_env, term)
1115 }
11161117pub(super) fn trait_ref_is_knowable(
1118&mut self,
1119 param_env: I::ParamEnv,
1120 trait_ref: ty::TraitRef<I>,
1121 ) -> Result<bool, NoSolution> {
1122let delegate = self.delegate;
1123let lazily_normalize_ty = |ty| self.structurally_normalize_ty(param_env, ty);
1124 coherence::trait_ref_is_knowable(&**delegate, trait_ref, lazily_normalize_ty)
1125 .map(|is_knowable| is_knowable.is_ok())
1126 }
11271128pub(super) fn fetch_eligible_assoc_item(
1129&self,
1130 goal_trait_ref: ty::TraitRef<I>,
1131 trait_assoc_def_id: I::DefId,
1132 impl_def_id: I::ImplId,
1133 ) -> Result<Option<I::DefId>, I::ErrorGuaranteed> {
1134self.delegate.fetch_eligible_assoc_item(goal_trait_ref, trait_assoc_def_id, impl_def_id)
1135 }
11361137x;#[instrument(level = "debug", skip(self), ret)]1138pub(super) fn register_hidden_type_in_storage(
1139&mut self,
1140 opaque_type_key: ty::OpaqueTypeKey<I>,
1141 hidden_ty: I::Ty,
1142 ) -> Option<I::Ty> {
1143self.delegate.register_hidden_type_in_storage(opaque_type_key, hidden_ty, self.origin_span)
1144 }
11451146pub(super) fn add_item_bounds_for_hidden_type(
1147&mut self,
1148 opaque_def_id: I::DefId,
1149 opaque_args: I::GenericArgs,
1150 param_env: I::ParamEnv,
1151 hidden_ty: I::Ty,
1152 ) {
1153let mut goals = Vec::new();
1154self.delegate.add_item_bounds_for_hidden_type(
1155opaque_def_id,
1156opaque_args,
1157param_env,
1158hidden_ty,
1159&mut goals,
1160 );
1161self.add_goals(GoalSource::AliasWellFormed, goals);
1162 }
11631164// Try to evaluate a const, or return `None` if the const is too generic.
1165 // This doesn't mean the const isn't evaluatable, though, and should be treated
1166 // as an ambiguity rather than no-solution.
1167pub(super) fn evaluate_const(
1168&self,
1169 param_env: I::ParamEnv,
1170 uv: ty::UnevaluatedConst<I>,
1171 ) -> Option<I::Const> {
1172self.delegate.evaluate_const(param_env, uv)
1173 }
11741175pub(super) fn is_transmutable(
1176&mut self,
1177 src: I::Ty,
1178 dst: I::Ty,
1179 assume: I::Const,
1180 ) -> Result<Certainty, NoSolution> {
1181self.delegate.is_transmutable(dst, src, assume)
1182 }
11831184pub(super) fn replace_bound_vars<T: TypeFoldable<I>>(
1185&self,
1186 t: T,
1187 universes: &mut Vec<Option<ty::UniverseIndex>>,
1188 ) -> T {
1189BoundVarReplacer::replace_bound_vars(&**self.delegate, universes, t).0
1190}
11911192pub(super) fn may_use_unstable_feature(
1193&self,
1194 param_env: I::ParamEnv,
1195 symbol: I::Symbol,
1196 ) -> bool {
1197may_use_unstable_feature(&**self.delegate, param_env, symbol)
1198 }
11991200pub(crate) fn opaques_with_sub_unified_hidden_type(
1201&self,
1202 self_ty: I::Ty,
1203 ) -> Vec<ty::AliasTy<I>> {
1204if let ty::Infer(ty::TyVar(vid)) = self_ty.kind() {
1205self.delegate.opaques_with_sub_unified_hidden_type(vid)
1206 } else {
1207::alloc::vec::Vec::new()vec![]1208 }
1209 }
12101211/// To return the constraints of a canonical query to the caller, we canonicalize:
1212 ///
1213 /// - `var_values`: a map from bound variables in the canonical goal to
1214 /// the values inferred while solving the instantiated goal.
1215 /// - `external_constraints`: additional constraints which aren't expressible
1216 /// using simple unification of inference variables.
1217 ///
1218 /// This takes the `shallow_certainty` which represents whether we're confident
1219 /// that the final result of the current goal only depends on the nested goals.
1220 ///
1221 /// In case this is `Certainty::Maybe`, there may still be additional nested goals
1222 /// or inference constraints required for this candidate to be hold. The candidate
1223 /// always requires all already added constraints and nested goals.
1224x;#[instrument(level = "trace", skip(self), ret)]1225pub(in crate::solve) fn evaluate_added_goals_and_make_canonical_response(
1226&mut self,
1227 shallow_certainty: Certainty,
1228 ) -> QueryResult<I> {
1229self.inspect.make_canonical_response(shallow_certainty);
12301231let goals_certainty = self.try_evaluate_added_goals()?;
1232assert_eq!(
1233self.tainted,
1234Ok(()),
1235"EvalCtxt is tainted -- nested goals may have been dropped in a \
1236 previous call to `try_evaluate_added_goals!`"
1237);
12381239// We only check for leaks from universes which were entered inside
1240 // of the query.
1241self.delegate.leak_check(self.max_input_universe).map_err(|NoSolution| {
1242trace!("failed the leak check");
1243 NoSolution
1244 })?;
12451246let (certainty, normalization_nested_goals) =
1247match (self.current_goal_kind, shallow_certainty) {
1248// When normalizing, we've replaced the expected term with an unconstrained
1249 // inference variable. This means that we dropped information which could
1250 // have been important. We handle this by instead returning the nested goals
1251 // to the caller, where they are then handled. We only do so if we do not
1252 // need to recompute the `NormalizesTo` goal afterwards to avoid repeatedly
1253 // uplifting its nested goals. This is the case if the `shallow_certainty` is
1254 // `Certainty::Yes`.
1255(CurrentGoalKind::NormalizesTo, Certainty::Yes) => {
1256let goals = std::mem::take(&mut self.nested_goals);
1257// As we return all ambiguous nested goals, we can ignore the certainty
1258 // returned by `self.try_evaluate_added_goals()`.
1259if goals.is_empty() {
1260assert!(matches!(goals_certainty, Certainty::Yes));
1261 }
1262 (
1263 Certainty::Yes,
1264 NestedNormalizationGoals(
1265 goals.into_iter().map(|(s, g, _)| (s, g)).collect(),
1266 ),
1267 )
1268 }
1269_ => {
1270let certainty = shallow_certainty.and(goals_certainty);
1271 (certainty, NestedNormalizationGoals::empty())
1272 }
1273 };
12741275if let Certainty::Maybe(
1276 maybe_info @ MaybeInfo {
1277 cause: MaybeCause::Overflow { keep_constraints: false, .. },
1278 opaque_types_jank: _,
1279 stalled_on_coroutines: _,
1280 },
1281 ) = certainty
1282 {
1283// If we have overflow, it's probable that we're substituting a type
1284 // into itself infinitely and any partial substitutions in the query
1285 // response are probably not useful anyways, so just return an empty
1286 // query response.
1287 //
1288 // This may prevent us from potentially useful inference, e.g.
1289 // 2 candidates, one ambiguous and one overflow, which both
1290 // have the same inference constraints.
1291 //
1292 // Changing this to retain some constraints in the future
1293 // won't be a breaking change, so this is good enough for now.
1294return Ok(self.make_ambiguous_response_no_constraints(maybe_info));
1295 }
12961297let external_constraints =
1298self.compute_external_query_constraints(certainty, normalization_nested_goals);
1299let (var_values, mut external_constraints) =
1300 eager_resolve_vars(self.delegate, (self.var_values, external_constraints));
13011302// Remove any trivial or duplicated region constraints once we've resolved regions
1303let mut unique = HashSet::default();
1304 external_constraints
1305 .region_constraints
1306 .retain(|outlives| !outlives.is_trivial() && unique.insert(*outlives));
13071308let canonical = canonicalize_response(
1309self.delegate,
1310self.max_input_universe,
1311 Response {
1312 var_values,
1313 certainty,
1314 external_constraints: self.cx().mk_external_constraints(external_constraints),
1315 },
1316 );
13171318Ok(canonical)
1319 }
13201321/// Constructs a totally unconstrained, ambiguous response to a goal.
1322 ///
1323 /// Take care when using this, since often it's useful to respond with
1324 /// ambiguity but return constrained variables to guide inference.
1325pub(in crate::solve) fn make_ambiguous_response_no_constraints(
1326&self,
1327 maybe: MaybeInfo,
1328 ) -> CanonicalResponse<I> {
1329response_no_constraints_raw(
1330self.cx(),
1331self.max_input_universe,
1332self.var_kinds,
1333 Certainty::Maybe(maybe),
1334 )
1335 }
13361337/// Computes the region constraints and *new* opaque types registered when
1338 /// proving a goal.
1339 ///
1340 /// If an opaque was already constrained before proving this goal, then the
1341 /// external constraints do not need to record that opaque, since if it is
1342 /// further constrained by inference, that will be passed back in the var
1343 /// values.
1344x;#[instrument(level = "trace", skip(self), ret)]1345fn compute_external_query_constraints(
1346&self,
1347 certainty: Certainty,
1348 normalization_nested_goals: NestedNormalizationGoals<I>,
1349 ) -> ExternalConstraintsData<I> {
1350// We only return region constraints once the certainty is `Yes`. This
1351 // is necessary as we may drop nested goals on ambiguity, which may result
1352 // in unconstrained inference variables in the region constraints. It also
1353 // prevents us from emitting duplicate region constraints, avoiding some
1354 // unnecessary work. This slightly weakens the leak check in case it uses
1355 // region constraints from an ambiguous nested goal. This is tested in both
1356 // `tests/ui/higher-ranked/leak-check/leak-check-in-selection-5-ambig.rs` and
1357 // `tests/ui/higher-ranked/leak-check/leak-check-in-selection-6-ambig-unify.rs`.
1358let region_constraints = if certainty == Certainty::Yes {
1359self.delegate.make_deduplicated_region_constraints()
1360 } else {
1361 Default::default()
1362 };
13631364// We only return *newly defined* opaque types from canonical queries.
1365 //
1366 // Constraints for any existing opaque types are already tracked by changes
1367 // to the `var_values`.
1368let opaque_types = self
1369.delegate
1370 .clone_opaque_types_added_since(self.initial_opaque_types_storage_num_entries);
13711372 ExternalConstraintsData { region_constraints, opaque_types, normalization_nested_goals }
1373 }
1374}
13751376/// Eagerly replace aliases with inference variables, emitting `AliasRelate`
1377/// goals, used when adding goals to the `EvalCtxt`. We compute the
1378/// `AliasRelate` goals before evaluating the actual goal to get all the
1379/// constraints we can.
1380///
1381/// This is a performance optimization to more eagerly detect cycles during trait
1382/// solving. See tests/ui/traits/next-solver/cycles/cycle-modulo-ambig-aliases.rs.
1383///
1384/// The emitted goals get evaluated in the context of the parent goal; by
1385/// replacing aliases in nested goals we essentially pull the normalization out of
1386/// the nested goal. We want to treat the goal as if the normalization still happens
1387/// inside of the nested goal by inheriting the `step_kind` of the nested goal and
1388/// storing it in the `GoalSource` of the emitted `AliasRelate` goals.
1389/// This is necessary for tests/ui/sized/coinductive-1.rs to compile.
1390struct ReplaceAliasWithInfer<'me, 'a, D, I>
1391where
1392D: SolverDelegate<Interner = I>,
1393 I: Interner,
1394{
1395 ecx: &'me mut EvalCtxt<'a, D>,
1396 param_env: I::ParamEnv,
1397 normalization_goal_source: GoalSource,
1398 cache: HashMap<I::Ty, I::Ty>,
1399}
14001401impl<'me, 'a, D, I> ReplaceAliasWithInfer<'me, 'a, D, I>
1402where
1403D: SolverDelegate<Interner = I>,
1404 I: Interner,
1405{
1406fn new(
1407 ecx: &'me mut EvalCtxt<'a, D>,
1408 for_goal_source: GoalSource,
1409 param_env: I::ParamEnv,
1410 ) -> Self {
1411let step_kind = ecx.step_kind_for_source(for_goal_source);
1412ReplaceAliasWithInfer {
1413ecx,
1414param_env,
1415 normalization_goal_source: GoalSource::NormalizeGoal(step_kind),
1416 cache: Default::default(),
1417 }
1418 }
1419}
14201421impl<D, I> TypeFolder<I> for ReplaceAliasWithInfer<'_, '_, D, I>
1422where
1423D: SolverDelegate<Interner = I>,
1424 I: Interner,
1425{
1426fn cx(&self) -> I {
1427self.ecx.cx()
1428 }
14291430fn fold_ty(&mut self, ty: I::Ty) -> I::Ty {
1431match ty.kind() {
1432 ty::Alias(..) if !ty.has_escaping_bound_vars() => {
1433let infer_ty = self.ecx.next_ty_infer();
1434let normalizes_to = ty::PredicateKind::AliasRelate(
1435ty.into(),
1436infer_ty.into(),
1437 ty::AliasRelationDirection::Equate,
1438 );
1439self.ecx.add_goal(
1440self.normalization_goal_source,
1441Goal::new(self.cx(), self.param_env, normalizes_to),
1442 );
1443infer_ty1444 }
1445_ => {
1446if !ty.has_aliases() {
1447ty1448 } else if let Some(&entry) = self.cache.get(&ty) {
1449return entry;
1450 } else {
1451let res = ty.super_fold_with(self);
1452if !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());
1453res1454 }
1455 }
1456 }
1457 }
14581459fn fold_const(&mut self, ct: I::Const) -> I::Const {
1460match ct.kind() {
1461 ty::ConstKind::Unevaluated(..) if !ct.has_escaping_bound_vars() => {
1462let infer_ct = self.ecx.next_const_infer();
1463let normalizes_to = ty::PredicateKind::AliasRelate(
1464ct.into(),
1465infer_ct.into(),
1466 ty::AliasRelationDirection::Equate,
1467 );
1468self.ecx.add_goal(
1469self.normalization_goal_source,
1470Goal::new(self.cx(), self.param_env, normalizes_to),
1471 );
1472infer_ct1473 }
1474_ => ct.super_fold_with(self),
1475 }
1476 }
14771478fn fold_predicate(&mut self, predicate: I::Predicate) -> I::Predicate {
1479if predicate.allow_normalization() { predicate.super_fold_with(self) } else { predicate }
1480 }
1481}
14821483/// Do not call this directly, use the `tcx` query instead.
1484pub fn evaluate_root_goal_for_proof_tree_raw_provider<
1485 D: SolverDelegate<Interner = I>,
1486 I: Interner,
1487>(
1488 cx: I,
1489 canonical_goal: CanonicalInput<I>,
1490) -> (QueryResult<I>, I::Probe) {
1491let mut inspect = inspect::ProofTreeBuilder::new();
1492let canonical_result = SearchGraph::<D>::evaluate_root_goal_for_proof_tree(
1493cx,
1494cx.recursion_limit(),
1495canonical_goal,
1496&mut inspect,
1497 );
1498let final_revision = inspect.unwrap();
1499 (canonical_result, cx.mk_probe(final_revision))
1500}
15011502/// Evaluate a goal to build a proof tree.
1503///
1504/// This is a copy of [EvalCtxt::evaluate_goal_raw] which avoids relying on the
1505/// [EvalCtxt] and uses a separate cache.
1506pub(super) fn evaluate_root_goal_for_proof_tree<D: SolverDelegate<Interner = I>, I: Interner>(
1507 delegate: &D,
1508 goal: Goal<I, I::Predicate>,
1509 origin_span: I::Span,
1510) -> (Result<NestedNormalizationGoals<I>, NoSolution>, inspect::GoalEvaluation<I>) {
1511let opaque_types = delegate.clone_opaque_types_lookup_table();
1512let (goal, opaque_types) = eager_resolve_vars(delegate, (goal, opaque_types));
15131514let (orig_values, canonical_goal) = canonicalize_goal(delegate, goal, &opaque_types);
15151516let (canonical_result, final_revision) =
1517delegate.cx().evaluate_root_goal_for_proof_tree_raw(canonical_goal);
15181519let proof_tree = inspect::GoalEvaluation {
1520 uncanonicalized_goal: goal,
1521orig_values,
1522final_revision,
1523 result: canonical_result,
1524 };
15251526let response = match canonical_result {
1527Err(e) => return (Err(e), proof_tree),
1528Ok(response) => response,
1529 };
15301531let (normalization_nested_goals, _certainty) = instantiate_and_apply_query_response(
1532delegate,
1533goal.param_env,
1534&proof_tree.orig_values,
1535response,
1536origin_span,
1537 );
15381539 (Ok(normalization_nested_goals), proof_tree)
1540}