rustc_hir_analysis/variance/
constraints.rs

1//! Constraint construction and representation
2//!
3//! The second pass over the HIR determines the set of constraints.
4//! We walk the set of items and, for each member, generate new constraints.
5
6use hir::def_id::{DefId, LocalDefId};
7use rustc_hir as hir;
8use rustc_hir::def::DefKind;
9use rustc_middle::ty::{self, GenericArgKind, GenericArgsRef, Ty, TyCtxt};
10use rustc_middle::{bug, span_bug};
11use tracing::{debug, instrument};
12
13use super::terms::VarianceTerm::*;
14use super::terms::*;
15
16pub(crate) struct ConstraintContext<'a, 'tcx> {
17    pub terms_cx: TermsContext<'a, 'tcx>,
18
19    // These are pointers to common `ConstantTerm` instances
20    covariant: VarianceTermPtr<'a>,
21    contravariant: VarianceTermPtr<'a>,
22    invariant: VarianceTermPtr<'a>,
23    bivariant: VarianceTermPtr<'a>,
24
25    pub constraints: Vec<Constraint<'a>>,
26}
27
28/// Declares that the variable `decl_id` appears in a location with
29/// variance `variance`.
30#[derive(Copy, Clone)]
31pub(crate) struct Constraint<'a> {
32    pub inferred: InferredIndex,
33    pub variance: &'a VarianceTerm<'a>,
34}
35
36/// To build constraints, we visit one item (type, trait) at a time
37/// and look at its contents. So e.g., if we have
38/// ```ignore (illustrative)
39/// struct Foo<T> {
40///     b: Bar<T>
41/// }
42/// ```
43/// then while we are visiting `Bar<T>`, the `CurrentItem` would have
44/// the `DefId` and the start of `Foo`'s inferreds.
45struct CurrentItem {
46    inferred_start: InferredIndex,
47}
48
49pub(crate) fn add_constraints_from_crate<'a, 'tcx>(
50    terms_cx: TermsContext<'a, 'tcx>,
51) -> ConstraintContext<'a, 'tcx> {
52    let tcx = terms_cx.tcx;
53    let covariant = terms_cx.arena.alloc(ConstantTerm(ty::Covariant));
54    let contravariant = terms_cx.arena.alloc(ConstantTerm(ty::Contravariant));
55    let invariant = terms_cx.arena.alloc(ConstantTerm(ty::Invariant));
56    let bivariant = terms_cx.arena.alloc(ConstantTerm(ty::Bivariant));
57    let mut constraint_cx = ConstraintContext {
58        terms_cx,
59        covariant,
60        contravariant,
61        invariant,
62        bivariant,
63        constraints: Vec::new(),
64    };
65
66    let crate_items = tcx.hir_crate_items(());
67
68    for def_id in crate_items.definitions() {
69        let def_kind = tcx.def_kind(def_id);
70        match def_kind {
71            DefKind::Struct | DefKind::Union | DefKind::Enum => {
72                constraint_cx.build_constraints_for_item(def_id);
73
74                let adt = tcx.adt_def(def_id);
75                for variant in adt.variants() {
76                    if let Some(ctor_def_id) = variant.ctor_def_id() {
77                        constraint_cx.build_constraints_for_item(ctor_def_id.expect_local());
78                    }
79                }
80            }
81            DefKind::Fn | DefKind::AssocFn => constraint_cx.build_constraints_for_item(def_id),
82            DefKind::TyAlias if tcx.type_alias_is_lazy(def_id) => {
83                constraint_cx.build_constraints_for_item(def_id)
84            }
85            _ => {}
86        }
87    }
88
89    constraint_cx
90}
91
92impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
93    fn tcx(&self) -> TyCtxt<'tcx> {
94        self.terms_cx.tcx
95    }
96
97    fn build_constraints_for_item(&mut self, def_id: LocalDefId) {
98        let tcx = self.tcx();
99        debug!("build_constraints_for_item({})", tcx.def_path_str(def_id));
100
101        // Skip items with no generics - there's nothing to infer in them.
102        if tcx.generics_of(def_id).is_empty() {
103            return;
104        }
105
106        let inferred_start = self.terms_cx.inferred_starts[&def_id];
107        let current_item = &CurrentItem { inferred_start };
108        let ty = tcx.type_of(def_id).instantiate_identity();
109
110        // The type as returned by `type_of` is the underlying type and generally not a weak projection.
111        // Therefore we need to check the `DefKind` first.
112        if let DefKind::TyAlias = tcx.def_kind(def_id)
113            && tcx.type_alias_is_lazy(def_id)
114        {
115            self.add_constraints_from_ty(current_item, ty, self.covariant);
116            return;
117        }
118
119        match ty.kind() {
120            ty::Adt(def, _) => {
121                // Not entirely obvious: constraints on structs/enums do not
122                // affect the variance of their type parameters. See discussion
123                // in comment at top of module.
124                //
125                // self.add_constraints_from_generics(generics);
126
127                for field in def.all_fields() {
128                    self.add_constraints_from_ty(
129                        current_item,
130                        tcx.type_of(field.did).instantiate_identity(),
131                        self.covariant,
132                    );
133                }
134            }
135
136            ty::FnDef(..) => {
137                self.add_constraints_from_sig(
138                    current_item,
139                    tcx.fn_sig(def_id).instantiate_identity(),
140                    self.covariant,
141                );
142            }
143
144            ty::Error(_) => {}
145
146            _ => {
147                span_bug!(
148                    tcx.def_span(def_id),
149                    "`build_constraints_for_item` unsupported for this item"
150                );
151            }
152        }
153    }
154
155    fn add_constraint(&mut self, current: &CurrentItem, index: u32, variance: VarianceTermPtr<'a>) {
156        debug!("add_constraint(index={}, variance={:?})", index, variance);
157        self.constraints.push(Constraint {
158            inferred: InferredIndex(current.inferred_start.0 + index as usize),
159            variance,
160        });
161    }
162
163    fn contravariant(&mut self, variance: VarianceTermPtr<'a>) -> VarianceTermPtr<'a> {
164        self.xform(variance, self.contravariant)
165    }
166
167    fn invariant(&mut self, variance: VarianceTermPtr<'a>) -> VarianceTermPtr<'a> {
168        self.xform(variance, self.invariant)
169    }
170
171    fn constant_term(&self, v: ty::Variance) -> VarianceTermPtr<'a> {
172        match v {
173            ty::Covariant => self.covariant,
174            ty::Invariant => self.invariant,
175            ty::Contravariant => self.contravariant,
176            ty::Bivariant => self.bivariant,
177        }
178    }
179
180    fn xform(&mut self, v1: VarianceTermPtr<'a>, v2: VarianceTermPtr<'a>) -> VarianceTermPtr<'a> {
181        match (*v1, *v2) {
182            (_, ConstantTerm(ty::Covariant)) => {
183                // Applying a "covariant" transform is always a no-op
184                v1
185            }
186
187            (ConstantTerm(c1), ConstantTerm(c2)) => self.constant_term(c1.xform(c2)),
188
189            _ => &*self.terms_cx.arena.alloc(TransformTerm(v1, v2)),
190        }
191    }
192
193    #[instrument(level = "debug", skip(self, current))]
194    fn add_constraints_from_invariant_args(
195        &mut self,
196        current: &CurrentItem,
197        args: GenericArgsRef<'tcx>,
198        variance: VarianceTermPtr<'a>,
199    ) {
200        // Trait are always invariant so we can take advantage of that.
201        let variance_i = self.invariant(variance);
202
203        for k in args {
204            match k.unpack() {
205                GenericArgKind::Lifetime(lt) => {
206                    self.add_constraints_from_region(current, lt, variance_i)
207                }
208                GenericArgKind::Type(ty) => self.add_constraints_from_ty(current, ty, variance_i),
209                GenericArgKind::Const(val) => {
210                    self.add_constraints_from_const(current, val, variance_i)
211                }
212            }
213        }
214    }
215
216    /// Adds constraints appropriate for an instance of `ty` appearing
217    /// in a context with the generics defined in `generics` and
218    /// ambient variance `variance`
219    fn add_constraints_from_ty(
220        &mut self,
221        current: &CurrentItem,
222        ty: Ty<'tcx>,
223        variance: VarianceTermPtr<'a>,
224    ) {
225        debug!("add_constraints_from_ty(ty={:?}, variance={:?})", ty, variance);
226
227        match *ty.kind() {
228            ty::Bool
229            | ty::Char
230            | ty::Int(_)
231            | ty::Uint(_)
232            | ty::Float(_)
233            | ty::Str
234            | ty::Never
235            | ty::Foreign(..) => {
236                // leaf type -- noop
237            }
238
239            ty::FnDef(..) | ty::Coroutine(..) | ty::Closure(..) | ty::CoroutineClosure(..) => {
240                bug!("Unexpected unnameable type in variance computation: {ty}");
241            }
242
243            ty::Ref(region, ty, mutbl) => {
244                self.add_constraints_from_region(current, region, variance);
245                self.add_constraints_from_mt(current, &ty::TypeAndMut { ty, mutbl }, variance);
246            }
247
248            ty::Array(typ, len) => {
249                self.add_constraints_from_const(current, len, variance);
250                self.add_constraints_from_ty(current, typ, variance);
251            }
252
253            ty::Pat(typ, pat) => {
254                match *pat {
255                    ty::PatternKind::Range { start, end, include_end: _ } => {
256                        if let Some(start) = start {
257                            self.add_constraints_from_const(current, start, variance);
258                        }
259                        if let Some(end) = end {
260                            self.add_constraints_from_const(current, end, variance);
261                        }
262                    }
263                }
264                self.add_constraints_from_ty(current, typ, variance);
265            }
266
267            ty::Slice(typ) => {
268                self.add_constraints_from_ty(current, typ, variance);
269            }
270
271            ty::RawPtr(ty, mutbl) => {
272                self.add_constraints_from_mt(current, &ty::TypeAndMut { ty, mutbl }, variance);
273            }
274
275            ty::Tuple(subtys) => {
276                for subty in subtys {
277                    self.add_constraints_from_ty(current, subty, variance);
278                }
279            }
280
281            ty::Adt(def, args) => {
282                self.add_constraints_from_args(current, def.did(), args, variance);
283            }
284
285            ty::Alias(ty::Projection | ty::Inherent | ty::Opaque, ref data) => {
286                self.add_constraints_from_invariant_args(current, data.args, variance);
287            }
288
289            ty::Alias(ty::Weak, ref data) => {
290                self.add_constraints_from_args(current, data.def_id, data.args, variance);
291            }
292
293            ty::Dynamic(data, r, _) => {
294                // The type `dyn Trait<T> +'a` is covariant w/r/t `'a`:
295                self.add_constraints_from_region(current, r, variance);
296
297                if let Some(poly_trait_ref) = data.principal() {
298                    self.add_constraints_from_invariant_args(
299                        current,
300                        poly_trait_ref.skip_binder().args,
301                        variance,
302                    );
303                }
304
305                for projection in data.projection_bounds() {
306                    match projection.skip_binder().term.unpack() {
307                        ty::TermKind::Ty(ty) => {
308                            self.add_constraints_from_ty(current, ty, self.invariant);
309                        }
310                        ty::TermKind::Const(c) => {
311                            self.add_constraints_from_const(current, c, self.invariant)
312                        }
313                    }
314                }
315            }
316
317            ty::Param(ref data) => {
318                self.add_constraint(current, data.index, variance);
319            }
320
321            ty::FnPtr(sig_tys, hdr) => {
322                self.add_constraints_from_sig(current, sig_tys.with(hdr), variance);
323            }
324
325            ty::UnsafeBinder(ty) => {
326                // FIXME(unsafe_binders): This is covariant, right?
327                self.add_constraints_from_ty(current, ty.skip_binder(), variance);
328            }
329
330            ty::Error(_) => {
331                // we encounter this when walking the trait references for object
332                // types, where we use Error as the Self type
333            }
334
335            ty::Placeholder(..) | ty::CoroutineWitness(..) | ty::Bound(..) | ty::Infer(..) => {
336                bug!("unexpected type encountered in variance inference: {}", ty);
337            }
338        }
339    }
340
341    /// Adds constraints appropriate for a nominal type (enum, struct,
342    /// object, etc) appearing in a context with ambient variance `variance`
343    fn add_constraints_from_args(
344        &mut self,
345        current: &CurrentItem,
346        def_id: DefId,
347        args: GenericArgsRef<'tcx>,
348        variance: VarianceTermPtr<'a>,
349    ) {
350        debug!(
351            "add_constraints_from_args(def_id={:?}, args={:?}, variance={:?})",
352            def_id, args, variance
353        );
354
355        // We don't record `inferred_starts` entries for empty generics.
356        if args.is_empty() {
357            return;
358        }
359
360        let (local, remote) = if let Some(def_id) = def_id.as_local() {
361            (Some(self.terms_cx.inferred_starts[&def_id]), None)
362        } else {
363            (None, Some(self.tcx().variances_of(def_id)))
364        };
365
366        for (i, k) in args.iter().enumerate() {
367            let variance_decl = if let Some(InferredIndex(start)) = local {
368                // Parameter on an item defined within current crate:
369                // variance not yet inferred, so return a symbolic
370                // variance.
371                self.terms_cx.inferred_terms[start + i]
372            } else {
373                // Parameter on an item defined within another crate:
374                // variance already inferred, just look it up.
375                self.constant_term(remote.as_ref().unwrap()[i])
376            };
377            let variance_i = self.xform(variance, variance_decl);
378            debug!(
379                "add_constraints_from_args: variance_decl={:?} variance_i={:?}",
380                variance_decl, variance_i
381            );
382            match k.unpack() {
383                GenericArgKind::Lifetime(lt) => {
384                    self.add_constraints_from_region(current, lt, variance_i)
385                }
386                GenericArgKind::Type(ty) => self.add_constraints_from_ty(current, ty, variance_i),
387                GenericArgKind::Const(val) => {
388                    self.add_constraints_from_const(current, val, variance)
389                }
390            }
391        }
392    }
393
394    /// Adds constraints appropriate for a const expression `val`
395    /// in a context with ambient variance `variance`
396    fn add_constraints_from_const(
397        &mut self,
398        current: &CurrentItem,
399        c: ty::Const<'tcx>,
400        variance: VarianceTermPtr<'a>,
401    ) {
402        debug!("add_constraints_from_const(c={:?}, variance={:?})", c, variance);
403
404        match &c.kind() {
405            ty::ConstKind::Unevaluated(uv) => {
406                self.add_constraints_from_invariant_args(current, uv.args, variance);
407            }
408            _ => {}
409        }
410    }
411
412    /// Adds constraints appropriate for a function with signature
413    /// `sig` appearing in a context with ambient variance `variance`
414    fn add_constraints_from_sig(
415        &mut self,
416        current: &CurrentItem,
417        sig: ty::PolyFnSig<'tcx>,
418        variance: VarianceTermPtr<'a>,
419    ) {
420        let contra = self.contravariant(variance);
421        for &input in sig.skip_binder().inputs() {
422            self.add_constraints_from_ty(current, input, contra);
423        }
424        self.add_constraints_from_ty(current, sig.skip_binder().output(), variance);
425    }
426
427    /// Adds constraints appropriate for a region appearing in a
428    /// context with ambient variance `variance`
429    fn add_constraints_from_region(
430        &mut self,
431        current: &CurrentItem,
432        region: ty::Region<'tcx>,
433        variance: VarianceTermPtr<'a>,
434    ) {
435        match *region {
436            ty::ReEarlyParam(ref data) => {
437                self.add_constraint(current, data.index, variance);
438            }
439
440            ty::ReStatic => {}
441
442            ty::ReBound(..) => {
443                // Either a higher-ranked region inside of a type or a
444                // late-bound function parameter.
445                //
446                // We do not compute constraints for either of these.
447            }
448
449            ty::ReError(_) => {}
450
451            ty::ReLateParam(..) | ty::ReVar(..) | ty::RePlaceholder(..) | ty::ReErased => {
452                // We don't expect to see anything but 'static or bound
453                // regions when visiting member types or method types.
454                bug!(
455                    "unexpected region encountered in variance \
456                      inference: {:?}",
457                    region
458                );
459            }
460        }
461    }
462
463    /// Adds constraints appropriate for a mutability-type pair
464    /// appearing in a context with ambient variance `variance`
465    fn add_constraints_from_mt(
466        &mut self,
467        current: &CurrentItem,
468        mt: &ty::TypeAndMut<'tcx>,
469        variance: VarianceTermPtr<'a>,
470    ) {
471        match mt.mutbl {
472            hir::Mutability::Mut => {
473                let invar = self.invariant(variance);
474                self.add_constraints_from_ty(current, mt.ty, invar);
475            }
476
477            hir::Mutability::Not => {
478                self.add_constraints_from_ty(current, mt.ty, variance);
479            }
480        }
481    }
482}