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