rustc_middle/middle/
resolve_bound_vars.rs

1//! Name resolution for lifetimes and late-bound type and const variables: type declarations.
2
3use rustc_data_structures::sorted_map::SortedMap;
4use rustc_errors::ErrorGuaranteed;
5use rustc_hir::ItemLocalId;
6use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdMap};
7use rustc_macros::{Decodable, Encodable, HashStable, TyDecodable, TyEncodable};
8
9use crate::ty;
10
11#[derive(Clone, Copy, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Debug, HashStable)]
12pub enum ResolvedArg {
13    StaticLifetime,
14    EarlyBound(/* decl */ LocalDefId),
15    LateBound(ty::DebruijnIndex, /* late-bound index */ u32, /* decl */ LocalDefId),
16    Free(LocalDefId, /* lifetime decl */ LocalDefId),
17    Error(ErrorGuaranteed),
18}
19
20/// A set containing, at most, one known element.
21/// If two distinct values are inserted into a set, then it
22/// becomes `Many`, which can be used to detect ambiguities.
23#[derive(Copy, Clone, PartialEq, Eq, TyEncodable, TyDecodable, Debug, HashStable)]
24pub enum Set1<T> {
25    Empty,
26    One(T),
27    Many,
28}
29
30impl<T: PartialEq> Set1<T> {
31    pub fn insert(&mut self, value: T) {
32        *self = match self {
33            Set1::Empty => Set1::One(value),
34            Set1::One(old) if *old == value => return,
35            _ => Set1::Many,
36        };
37    }
38}
39
40#[derive(Copy, Clone, Debug, HashStable, Encodable, Decodable)]
41pub enum ObjectLifetimeDefault {
42    Empty,
43    Static,
44    Ambiguous,
45    Param(DefId),
46}
47
48/// Maps the id of each bound variable reference to the variable decl
49/// that it corresponds to.
50#[derive(Debug, Default, HashStable)]
51pub struct ResolveBoundVars {
52    // Maps from every use of a named (not anonymous) bound var to a
53    // `ResolvedArg` describing how that variable is bound.
54    pub defs: SortedMap<ItemLocalId, ResolvedArg>,
55
56    // Maps relevant hir items to the bound vars on them. These include:
57    // - function defs
58    // - function pointers
59    // - closures
60    // - trait refs
61    // - bound types (like `T` in `for<'a> T<'a>: Foo`)
62    pub late_bound_vars: SortedMap<ItemLocalId, Vec<ty::BoundVariableKind>>,
63
64    // List captured variables for each opaque type.
65    pub opaque_captured_lifetimes: LocalDefIdMap<Vec<(ResolvedArg, LocalDefId)>>,
66}