]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/middle/resolve_lifetime.rs
Rollup merge of #99711 - tmiasko:coverage, r=wesleywiser
[rust.git] / compiler / rustc_middle / src / middle / resolve_lifetime.rs
1 //! Name resolution for lifetimes: type declarations.
2
3 use crate::ty;
4
5 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
6 use rustc_hir::def_id::{DefId, LocalDefId};
7 use rustc_hir::ItemLocalId;
8 use rustc_macros::HashStable;
9
10 #[derive(Clone, Copy, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Debug, HashStable)]
11 pub enum Region {
12     Static,
13     EarlyBound(/* index */ u32, /* lifetime decl */ DefId),
14     LateBound(ty::DebruijnIndex, /* late-bound index */ u32, /* lifetime decl */ DefId),
15     LateBoundAnon(ty::DebruijnIndex, /* late-bound index */ u32, /* anon index */ u32),
16     Free(DefId, /* lifetime decl */ DefId),
17 }
18
19 /// A set containing, at most, one known element.
20 /// If two distinct values are inserted into a set, then it
21 /// becomes `Many`, which can be used to detect ambiguities.
22 #[derive(Copy, Clone, PartialEq, Eq, TyEncodable, TyDecodable, Debug, HashStable)]
23 pub enum Set1<T> {
24     Empty,
25     One(T),
26     Many,
27 }
28
29 impl<T: PartialEq> Set1<T> {
30     pub fn insert(&mut self, value: T) {
31         *self = match self {
32             Set1::Empty => Set1::One(value),
33             Set1::One(old) if *old == value => return,
34             _ => Set1::Many,
35         };
36     }
37 }
38
39 pub type ObjectLifetimeDefault = Set1<Region>;
40
41 /// Maps the id of each lifetime reference to the lifetime decl
42 /// that it corresponds to.
43 #[derive(Default, HashStable, Debug)]
44 pub struct ResolveLifetimes {
45     /// Maps from every use of a named (not anonymous) lifetime to a
46     /// `Region` describing how that region is bound
47     pub defs: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Region>>,
48
49     /// Set of lifetime def ids that are late-bound; a region can
50     /// be late-bound if (a) it does NOT appear in a where-clause and
51     /// (b) it DOES appear in the arguments.
52     pub late_bound: FxHashMap<LocalDefId, FxHashSet<LocalDefId>>,
53
54     pub late_bound_vars: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Vec<ty::BoundVariableKind>>>,
55 }