Skip to main content

rustc_next_trait_solver/solve/
search_graph.rs

1use std::convert::Infallible;
2use std::marker::PhantomData;
3
4use rustc_type_ir::data_structures::ensure_sufficient_stack;
5use rustc_type_ir::search_graph::{self, PathKind};
6use rustc_type_ir::solve::{
7    AccessedOpaques, CanonicalInput, Certainty, NoSolution, NoSolutionOrRerunNonErased, QueryResult,
8};
9use rustc_type_ir::{Interner, MayBeErased, TypingMode};
10
11use crate::canonical::response_no_constraints_raw;
12use crate::delegate::SolverDelegate;
13use crate::solve::{
14    EvalCtxt, FIXPOINT_STEP_LIMIT, has_no_inference_or_external_constraints, inspect,
15};
16
17/// This type is never constructed. We only use it to implement `search_graph::Delegate`
18/// for all types which impl `SolverDelegate` and doing it directly fails in coherence.
19pub(super) struct SearchGraphDelegate<D: SolverDelegate> {
20    _marker: PhantomData<D>,
21}
22pub(super) type SearchGraph<D> = search_graph::SearchGraph<SearchGraphDelegate<D>>;
23impl<D, I> search_graph::Delegate for SearchGraphDelegate<D>
24where
25    D: SolverDelegate<Interner = I>,
26    I: Interner,
27{
28    type Cx = D::Interner;
29
30    const ENABLE_PROVISIONAL_CACHE: bool = true;
31    type ValidationScope = Infallible;
32    fn enter_validation_scope(
33        _cx: Self::Cx,
34        _input: CanonicalInput<I>,
35    ) -> Option<Self::ValidationScope> {
36        None
37    }
38
39    const FIXPOINT_STEP_LIMIT: usize = FIXPOINT_STEP_LIMIT;
40
41    type ProofTreeBuilder = inspect::ProofTreeBuilder<D>;
42    fn inspect_is_noop(inspect: &mut Self::ProofTreeBuilder) -> bool {
43        inspect.is_noop()
44    }
45
46    const DIVIDE_AVAILABLE_DEPTH_ON_OVERFLOW: usize = 4;
47
48    fn initial_provisional_result(
49        cx: I,
50        kind: PathKind,
51        input: CanonicalInput<I>,
52    ) -> (QueryResult<I>, AccessedOpaques<I>) {
53        match kind {
54            PathKind::Coinductive => response_no_constraints(cx, input, Certainty::Yes),
55            PathKind::Unknown | PathKind::ForcedAmbiguity => {
56                response_no_constraints(cx, input, Certainty::overflow(false))
57            }
58            // Even though we know these cycles to be unproductive, we still return
59            // overflow during coherence. This is both as we are not 100% confident in
60            // the implementation yet and any incorrect errors would be unsound there.
61            // The affected cases are also fairly artificial and not necessarily desirable
62            // so keeping this as ambiguity is fine for now.
63            //
64            // See `tests/ui/traits/next-solver/cycles/unproductive-in-coherence.rs` for an
65            // example where this would matter. We likely should change these cycles to `NoSolution`
66            // even in coherence once this is a bit more settled.
67            PathKind::Inductive => match input.typing_mode.0 {
68                TypingMode::Coherence => {
69                    response_no_constraints(cx, input, Certainty::overflow(false))
70                }
71                TypingMode::Typeck { .. }
72                | TypingMode::PostTypeckUntilBorrowck { .. }
73                | TypingMode::PostBorrowck { .. }
74                | TypingMode::PostAnalysis
75                | TypingMode::Codegen
76                | TypingMode::ErasedNotCoherence(MayBeErased) => {
77                    (Err(NoSolution), AccessedOpaques::default())
78                }
79            },
80        }
81    }
82
83    fn is_initial_provisional_result(
84        result: (QueryResult<I>, AccessedOpaques<I>),
85    ) -> Option<PathKind> {
86        match result.0 {
87            Ok(response) => {
88                if has_no_inference_or_external_constraints(response) {
89                    if response.value.certainty == Certainty::Yes {
90                        return Some(PathKind::Coinductive);
91                    } else if response.value.certainty == Certainty::overflow(false) {
92                        return Some(PathKind::Unknown);
93                    }
94                }
95
96                None
97            }
98            Err(NoSolution) => Some(PathKind::Inductive),
99        }
100    }
101
102    fn stack_overflow_result(
103        cx: I,
104        input: CanonicalInput<I>,
105    ) -> (QueryResult<I>, AccessedOpaques<I>) {
106        response_no_constraints(cx, input, Certainty::overflow(true))
107    }
108
109    fn fixpoint_overflow_result(
110        cx: I,
111        input: CanonicalInput<I>,
112    ) -> (QueryResult<I>, AccessedOpaques<I>) {
113        response_no_constraints(cx, input, Certainty::overflow(false))
114    }
115
116    fn is_ambiguous_result(result: (QueryResult<I>, AccessedOpaques<I>)) -> Option<Certainty> {
117        result.0.ok().and_then(|response| {
118            if has_no_inference_or_external_constraints(response)
119                && #[allow(non_exhaustive_omitted_patterns)] match response.value.certainty {
    Certainty::Maybe { .. } => true,
    _ => false,
}matches!(response.value.certainty, Certainty::Maybe { .. })
120            {
121                Some(response.value.certainty)
122            } else {
123                None
124            }
125        })
126    }
127
128    fn propagate_ambiguity(
129        cx: I,
130        for_input: CanonicalInput<I>,
131        certainty: Certainty,
132    ) -> (QueryResult<I>, AccessedOpaques<I>) {
133        response_no_constraints(cx, for_input, certainty)
134    }
135
136    fn compute_goal(
137        search_graph: &mut SearchGraph<D>,
138        cx: I,
139        input: CanonicalInput<I>,
140        inspect: &mut Self::ProofTreeBuilder,
141    ) -> (QueryResult<I>, AccessedOpaques<I>) {
142        ensure_sufficient_stack(|| {
143            EvalCtxt::enter_canonical(cx, search_graph, input, inspect, |ecx, goal| {
144                let result = ecx.compute_goal(goal);
145
146                // if we're in `RerunNonErased`, don't even bother with inspect,
147                // and immediately return
148                let result = match result {
149                    Ok(i) => Ok(i),
150                    Err(NoSolutionOrRerunNonErased::NoSolution(NoSolution)) => Err(NoSolution),
151                    Err(NoSolutionOrRerunNonErased::RerunNonErased(e)) => {
152                        return Err(e.into());
153                    }
154                };
155
156                ecx.inspect.query_result(result);
157                result.map_err(Into::into)
158            })
159        })
160    }
161}
162
163fn response_no_constraints<I: Interner>(
164    cx: I,
165    input: CanonicalInput<I>,
166    certainty: Certainty,
167) -> (QueryResult<I>, AccessedOpaques<I>) {
168    (
169        Ok(response_no_constraints_raw(
170            cx,
171            input.canonical.max_universe,
172            input.canonical.var_kinds,
173            certainty,
174        )),
175        AccessedOpaques::default(),
176    )
177}