1pub mod inspect;
2
3use std::fmt;
4use std::hash::Hash;
5
6use derive_where::derive_where;
7#[cfg(feature = "nightly")]
8use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_NoContext};
9use rustc_type_ir_macros::{Lift_Generic, TypeFoldable_Generic, TypeVisitable_Generic};
10
11use crate::search_graph::PathKind;
12use crate::{self as ty, Canonical, CanonicalVarValues, Interner, Upcast};
13
14pub type CanonicalInput<I, T = <I as Interner>::Predicate> =
15 ty::CanonicalQueryInput<I, QueryInput<I, T>>;
16pub type CanonicalResponse<I> = Canonical<I, Response<I>>;
17pub type QueryResult<I> = Result<CanonicalResponse<I>, NoSolution>;
24
25#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
26#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
27pub struct NoSolution;
28
29#[derive_where(Clone; I: Interner, P: Clone)]
35#[derive_where(Copy; I: Interner, P: Copy)]
36#[derive_where(Hash; I: Interner, P: Hash)]
37#[derive_where(PartialEq; I: Interner, P: PartialEq)]
38#[derive_where(Eq; I: Interner, P: Eq)]
39#[derive_where(Debug; I: Interner, P: fmt::Debug)]
40#[derive(TypeVisitable_Generic, TypeFoldable_Generic, Lift_Generic)]
41#[cfg_attr(
42 feature = "nightly",
43 derive(Decodable_NoContext, Encodable_NoContext, HashStable_NoContext)
44)]
45pub struct Goal<I: Interner, P> {
46 pub param_env: I::ParamEnv,
47 pub predicate: P,
48}
49
50impl<I: Interner, P> Goal<I, P> {
51 pub fn new(cx: I, param_env: I::ParamEnv, predicate: impl Upcast<I, P>) -> Goal<I, P> {
52 Goal { param_env, predicate: predicate.upcast(cx) }
53 }
54
55 pub fn with<Q>(self, cx: I, predicate: impl Upcast<I, Q>) -> Goal<I, Q> {
57 Goal { param_env: self.param_env, predicate: predicate.upcast(cx) }
58 }
59}
60
61#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
70#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
71pub enum GoalSource {
72 Misc,
73 TypeRelating,
79 ImplWhereBound,
81 AliasBoundConstCondition,
83 InstantiateHigherRanked,
85 AliasWellFormed,
92 NormalizeGoal(PathKind),
98}
99
100#[derive_where(Clone; I: Interner, Goal<I, P>: Clone)]
101#[derive_where(Copy; I: Interner, Goal<I, P>: Copy)]
102#[derive_where(Hash; I: Interner, Goal<I, P>: Hash)]
103#[derive_where(PartialEq; I: Interner, Goal<I, P>: PartialEq)]
104#[derive_where(Eq; I: Interner, Goal<I, P>: Eq)]
105#[derive_where(Debug; I: Interner, Goal<I, P>: fmt::Debug)]
106#[derive(TypeVisitable_Generic, TypeFoldable_Generic)]
107#[cfg_attr(
108 feature = "nightly",
109 derive(Decodable_NoContext, Encodable_NoContext, HashStable_NoContext)
110)]
111pub struct QueryInput<I: Interner, P> {
112 pub goal: Goal<I, P>,
113 pub predefined_opaques_in_body: I::PredefinedOpaques,
114}
115
116#[derive_where(Clone, Hash, PartialEq, Eq, Debug, Default; I: Interner)]
118#[derive(TypeVisitable_Generic, TypeFoldable_Generic)]
119#[cfg_attr(
120 feature = "nightly",
121 derive(Decodable_NoContext, Encodable_NoContext, HashStable_NoContext)
122)]
123pub struct PredefinedOpaquesData<I: Interner> {
124 pub opaque_types: Vec<(ty::OpaqueTypeKey<I>, I::Ty)>,
125}
126
127#[derive_where(Clone, Copy, Hash, PartialEq, Eq, Debug; I: Interner)]
129pub enum CandidateSource<I: Interner> {
130 Impl(I::DefId),
142 BuiltinImpl(BuiltinImplSource),
150 ParamEnv(ParamEnvSource),
163 AliasBound,
184 CoherenceUnknowable,
189}
190
191#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
192pub enum ParamEnvSource {
193 NonGlobal,
195 Global,
197}
198
199#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
200#[cfg_attr(
201 feature = "nightly",
202 derive(HashStable_NoContext, Encodable_NoContext, Decodable_NoContext)
203)]
204pub enum BuiltinImplSource {
205 Trivial,
208 Misc,
211 Object(usize),
213 TraitUpcasting(usize),
217}
218
219#[derive_where(Clone, Copy, Hash, PartialEq, Eq, Debug; I: Interner)]
220#[derive(TypeVisitable_Generic, TypeFoldable_Generic)]
221#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
222pub struct Response<I: Interner> {
223 pub certainty: Certainty,
224 pub var_values: CanonicalVarValues<I>,
225 pub external_constraints: I::ExternalConstraints,
227}
228
229#[derive_where(Clone, Hash, PartialEq, Eq, Debug, Default; I: Interner)]
231#[derive(TypeVisitable_Generic, TypeFoldable_Generic)]
232#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
233pub struct ExternalConstraintsData<I: Interner> {
234 pub region_constraints: Vec<ty::OutlivesPredicate<I, I::GenericArg>>,
235 pub opaque_types: Vec<(ty::OpaqueTypeKey<I>, I::Ty)>,
236 pub normalization_nested_goals: NestedNormalizationGoals<I>,
237}
238
239#[derive_where(Clone, Hash, PartialEq, Eq, Debug, Default; I: Interner)]
240#[derive(TypeVisitable_Generic, TypeFoldable_Generic)]
241#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
242pub struct NestedNormalizationGoals<I: Interner>(pub Vec<(GoalSource, Goal<I, I::Predicate>)>);
243
244impl<I: Interner> NestedNormalizationGoals<I> {
245 pub fn empty() -> Self {
246 NestedNormalizationGoals(vec![])
247 }
248
249 pub fn is_empty(&self) -> bool {
250 self.0.is_empty()
251 }
252}
253
254#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
255#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
256pub enum Certainty {
257 Yes,
258 Maybe(MaybeCause),
259}
260
261impl Certainty {
262 pub const AMBIGUOUS: Certainty = Certainty::Maybe(MaybeCause::Ambiguity);
263
264 pub fn and(self, other: Certainty) -> Certainty {
277 match (self, other) {
278 (Certainty::Yes, Certainty::Yes) => Certainty::Yes,
279 (Certainty::Yes, Certainty::Maybe(_)) => other,
280 (Certainty::Maybe(_), Certainty::Yes) => self,
281 (Certainty::Maybe(a), Certainty::Maybe(b)) => Certainty::Maybe(a.and(b)),
282 }
283 }
284
285 pub const fn overflow(suggest_increasing_limit: bool) -> Certainty {
286 Certainty::Maybe(MaybeCause::Overflow { suggest_increasing_limit, keep_constraints: false })
287 }
288}
289
290#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
292#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
293pub enum MaybeCause {
294 Ambiguity,
298 Overflow { suggest_increasing_limit: bool, keep_constraints: bool },
300}
301
302impl MaybeCause {
303 fn and(self, other: MaybeCause) -> MaybeCause {
304 match (self, other) {
305 (MaybeCause::Ambiguity, MaybeCause::Ambiguity) => MaybeCause::Ambiguity,
306 (MaybeCause::Ambiguity, MaybeCause::Overflow { .. }) => other,
307 (MaybeCause::Overflow { .. }, MaybeCause::Ambiguity) => self,
308 (
309 MaybeCause::Overflow {
310 suggest_increasing_limit: limit_a,
311 keep_constraints: keep_a,
312 },
313 MaybeCause::Overflow {
314 suggest_increasing_limit: limit_b,
315 keep_constraints: keep_b,
316 },
317 ) => MaybeCause::Overflow {
318 suggest_increasing_limit: limit_a && limit_b,
319 keep_constraints: keep_a && keep_b,
320 },
321 }
322 }
323
324 pub fn or(self, other: MaybeCause) -> MaybeCause {
325 match (self, other) {
326 (MaybeCause::Ambiguity, MaybeCause::Ambiguity) => MaybeCause::Ambiguity,
327
328 (
330 MaybeCause::Ambiguity,
331 MaybeCause::Overflow { suggest_increasing_limit, keep_constraints: _ },
332 ) => MaybeCause::Overflow { suggest_increasing_limit, keep_constraints: true },
333 (
334 MaybeCause::Overflow { suggest_increasing_limit, keep_constraints: _ },
335 MaybeCause::Ambiguity,
336 ) => MaybeCause::Overflow { suggest_increasing_limit, keep_constraints: true },
337
338 (
339 MaybeCause::Overflow {
340 suggest_increasing_limit: limit_a,
341 keep_constraints: keep_a,
342 },
343 MaybeCause::Overflow {
344 suggest_increasing_limit: limit_b,
345 keep_constraints: keep_b,
346 },
347 ) => MaybeCause::Overflow {
348 suggest_increasing_limit: limit_a || limit_b,
349 keep_constraints: keep_a || keep_b,
350 },
351 }
352 }
353}
354
355#[derive(Debug)]
357pub enum AdtDestructorKind {
358 NotConst,
359 Const,
360}