-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Expand file tree
/
Copy pathsolve.rs
More file actions
278 lines (239 loc) · 10.2 KB
/
solve.rs
File metadata and controls
278 lines (239 loc) · 10.2 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//! Constraint solving
//!
//! The final phase iterates over the constraints, refining the variance
//! for each inferred until a fixed point is reached. This will be the
//! optimal solution to the constraints. The final variance for each
//! inferred is then written into the `variance_map` in the tcx.
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, DefIdMap};
use rustc_middle::ty::{self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor};
use tracing::debug;
use super::constraints::*;
use super::terms::VarianceTerm::*;
use super::terms::*;
fn glb(v1: ty::Variance, v2: ty::Variance) -> ty::Variance {
// Greatest lower bound of the variance lattice as defined in The Paper:
//
// *
// - +
// o
match (v1, v2) {
(ty::Invariant, _) | (_, ty::Invariant) => ty::Invariant,
(ty::Covariant, ty::Contravariant) => ty::Invariant,
(ty::Contravariant, ty::Covariant) => ty::Invariant,
(ty::Covariant, ty::Covariant) => ty::Covariant,
(ty::Contravariant, ty::Contravariant) => ty::Contravariant,
(x, ty::Bivariant) | (ty::Bivariant, x) => x,
}
}
struct SolveContext<'a, 'tcx> {
terms_cx: TermsContext<'a, 'tcx>,
constraints: Vec<Constraint<'a>>,
// Maps from an InferredIndex to the inferred value for that variable.
solutions: Vec<ty::Variance>,
}
/// Visitor to find "grounding" uses of type parameters which are any use that is not
/// solely through self-referential cycles.
///
/// For example, in `struct Thing<T>(*mut Thing<T>)`, the use of `T` is not grounding
/// because it only appears in a self-referential cycle. In contrast, in
/// `struct Thing<T>(Option<T>)`, the use of `T` is grounding because it appears in a
/// non-self-referential context (`Option<T>`).
struct GroundingUseVisitor {
item_def_id: DefId,
grounded_params: Vec<u32>,
in_self: bool,
in_alias: bool,
}
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for GroundingUseVisitor {
fn visit_ty(&mut self, ty: Ty<'tcx>) -> () {
match ty.kind() {
// Self-reference: visit args in a self-recursive context.
ty::Adt(def, _) if def.did() == self.item_def_id => {
let was_in_self = self.in_self;
self.in_self = true;
ty.super_visit_with(self);
self.in_self = was_in_self;
()
}
// Projections/aliases: treat parameter uses as grounding.
ty::Alias(..) => {
let was_in_alias = self.in_alias;
self.in_alias = true;
ty.super_visit_with(self);
self.in_alias = was_in_alias;
()
}
// Found a direct parameter use, record it
ty::Param(data) => {
if !self.in_self || self.in_alias {
self.grounded_params.push(data.index);
}
()
}
// Everything else: recurse normally via super_visit_with,
// which visits generic args of ADTs, elements of tuples, etc.
_ => ty.super_visit_with(self),
}
}
fn visit_region(&mut self, r: ty::Region<'tcx>) -> () {
if let ty::ReEarlyParam(ref data) = r.kind() {
if !self.in_self || self.in_alias {
self.grounded_params.push(data.index);
}
}
()
}
}
pub(crate) fn solve_constraints<'tcx>(
constraints_cx: ConstraintContext<'_, 'tcx>,
) -> ty::CrateVariancesMap<'tcx> {
let ConstraintContext { terms_cx, constraints, .. } = constraints_cx;
let mut solutions = vec![ty::Bivariant; terms_cx.inferred_terms.len()];
for (id, variances) in &terms_cx.lang_items {
let InferredIndex(start) = terms_cx.inferred_starts[id];
for (i, &variance) in variances.iter().enumerate() {
solutions[start + i] = variance;
}
}
let mut solutions_cx = SolveContext { terms_cx, constraints, solutions };
solutions_cx.solve();
solutions_cx.fix_purely_recursive_params();
let variances = solutions_cx.create_map();
ty::CrateVariancesMap { variances }
}
impl<'a, 'tcx> SolveContext<'a, 'tcx> {
fn solve(&mut self) {
// Propagate constraints until a fixed point is reached. Note
// that the maximum number of iterations is 2C where C is the
// number of constraints (each variable can change values at most
// twice). Since number of constraints is linear in size of the
// input, so is the inference process.
let mut changed = true;
while changed {
changed = false;
for constraint in &self.constraints {
let Constraint { inferred, variance: term } = *constraint;
let InferredIndex(inferred) = inferred;
let variance = self.evaluate(term);
let old_value = self.solutions[inferred];
let new_value = glb(variance, old_value);
if old_value != new_value {
debug!(
"updating inferred {} \
from {:?} to {:?} due to {:?}",
inferred, old_value, new_value, term
);
self.solutions[inferred] = new_value;
changed = true;
}
}
}
}
/// After the fixed-point solver, check for parameters whose non-bivariance
/// is solely due to self-referential cycles (e.g. `struct Thing<T>(*mut Thing<T>)`).
/// Those parameters have no "grounding" use and should be bivariant.
fn fix_purely_recursive_params(&mut self) {
let tcx = self.terms_cx.tcx;
// First pass: identify which solution slots need to be reset to Bivariant.
// We use a RefCell so the Fn closure required by `UnordItems::all` can
// accumulate results via interior mutability.
let to_reset: std::cell::RefCell<Vec<usize>> = std::cell::RefCell::new(Vec::new());
self.terms_cx.inferred_starts.items().all(|(&def_id, &InferredIndex(start))| {
// Skip lang items with hardcoded variance (e.g., PhantomData).
if self.terms_cx.lang_items.iter().any(|(id, _)| *id == def_id) {
return true;
}
// Only check ADTs (structs, enums, unions).
if !matches!(tcx.def_kind(def_id), DefKind::Struct | DefKind::Enum | DefKind::Union) {
return true;
}
let generics = tcx.generics_of(def_id);
let count = generics.count();
// Quick check: if all params are already bivariant, nothing to do.
if (0..count).all(|i| self.solutions[start + i] == ty::Bivariant) {
return true;
}
// Walk all fields to find grounding uses.
let mut visitor = GroundingUseVisitor {
item_def_id: def_id.to_def_id(),
grounded_params: Default::default(),
in_self: false,
in_alias: false,
};
let adt = tcx.adt_def(def_id);
for field in adt.all_fields() {
tcx.type_of(field.did).instantiate_identity().visit_with(&mut visitor);
}
// Collect solution indices with no grounding use.
for i in 0..count {
if !matches!(generics.param_at(i, tcx).kind, ty::GenericParamDefKind::Type { .. }) {
continue;
}
if self.solutions[start + i] != ty::Bivariant
&& !visitor.grounded_params.contains(&(i as u32))
{
debug!(
"fix_purely_recursive_params: param {} of {:?} has no grounding use \
(was {:?}), will reset to Bivariant",
i,
def_id,
self.solutions[start + i]
);
to_reset.borrow_mut().push(start + i);
}
}
true
});
// Second pass: apply the resets.
for idx in to_reset.into_inner() {
self.solutions[idx] = ty::Bivariant;
}
}
fn enforce_const_invariance(&self, generics: &ty::Generics, variances: &mut [ty::Variance]) {
let tcx = self.terms_cx.tcx;
// Make all const parameters invariant.
for param in generics.own_params.iter() {
if let ty::GenericParamDefKind::Const { .. } = param.kind {
variances[param.index as usize] = ty::Invariant;
}
}
// Make all the const parameters in the parent invariant (recursively).
if let Some(def_id) = generics.parent {
self.enforce_const_invariance(tcx.generics_of(def_id), variances);
}
}
fn create_map(&self) -> DefIdMap<&'tcx [ty::Variance]> {
let tcx = self.terms_cx.tcx;
let solutions = &self.solutions;
DefIdMap::from(self.terms_cx.inferred_starts.items().map(
|(&def_id, &InferredIndex(start))| {
let generics = tcx.generics_of(def_id);
let count = generics.count();
let variances = tcx.arena.alloc_slice(&solutions[start..(start + count)]);
// Const parameters are always invariant.
self.enforce_const_invariance(generics, variances);
// Functions are permitted to have unused generic parameters: make those invariant.
if let ty::FnDef(..) = tcx.type_of(def_id).instantiate_identity().kind() {
for variance in variances.iter_mut() {
if *variance == ty::Bivariant {
*variance = ty::Invariant;
}
}
}
(def_id.to_def_id(), &*variances)
},
))
}
fn evaluate(&self, term: VarianceTermPtr<'a>) -> ty::Variance {
match *term {
ConstantTerm(v) => v,
TransformTerm(t1, t2) => {
let v1 = self.evaluate(t1);
let v2 = self.evaluate(t2);
v1.xform(v2)
}
InferredTerm(InferredIndex(index)) => self.solutions[index],
}
}
}