]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/resolve_lifetime.rs
Rollup merge of #67501 - oli-obk:test-slice-patterns, r=RalfJung
[rust.git] / src / librustc / 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::{GenericParam, ItemLocalId};
8 use rustc_hir::{GenericParamKind, LifetimeParamKind};
9 use rustc_macros::HashStable;
10
11 /// The origin of a named lifetime definition.
12 ///
13 /// This is used to prevent the usage of in-band lifetimes in `Fn`/`fn` syntax.
14 #[derive(Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug, HashStable)]
15 pub enum LifetimeDefOrigin {
16     // Explicit binders like `fn foo<'a>(x: &'a u8)` or elided like `impl Foo<&u32>`
17     ExplicitOrElided,
18     // In-band declarations like `fn foo(x: &'a u8)`
19     InBand,
20     // Some kind of erroneous origin
21     Error,
22 }
23
24 impl LifetimeDefOrigin {
25     pub fn from_param(param: &GenericParam<'_>) -> Self {
26         match param.kind {
27             GenericParamKind::Lifetime { kind } => match kind {
28                 LifetimeParamKind::InBand => LifetimeDefOrigin::InBand,
29                 LifetimeParamKind::Explicit => LifetimeDefOrigin::ExplicitOrElided,
30                 LifetimeParamKind::Elided => LifetimeDefOrigin::ExplicitOrElided,
31                 LifetimeParamKind::Error => LifetimeDefOrigin::Error,
32             },
33             _ => bug!("expected a lifetime param"),
34         }
35     }
36 }
37
38 #[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug, HashStable)]
39 pub enum Region {
40     Static,
41     EarlyBound(/* index */ u32, /* lifetime decl */ DefId, LifetimeDefOrigin),
42     LateBound(ty::DebruijnIndex, /* lifetime decl */ DefId, LifetimeDefOrigin),
43     LateBoundAnon(ty::DebruijnIndex, /* anon index */ u32),
44     Free(DefId, /* lifetime decl */ DefId),
45 }
46
47 /// A set containing, at most, one known element.
48 /// If two distinct values are inserted into a set, then it
49 /// becomes `Many`, which can be used to detect ambiguities.
50 #[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Debug, HashStable)]
51 pub enum Set1<T> {
52     Empty,
53     One(T),
54     Many,
55 }
56
57 impl<T: PartialEq> Set1<T> {
58     pub fn insert(&mut self, value: T) {
59         *self = match self {
60             Set1::Empty => Set1::One(value),
61             Set1::One(old) if *old == value => return,
62             _ => Set1::Many,
63         };
64     }
65 }
66
67 pub type ObjectLifetimeDefault = Set1<Region>;
68
69 /// Maps the id of each lifetime reference to the lifetime decl
70 /// that it corresponds to.
71 #[derive(Default, HashStable)]
72 pub struct ResolveLifetimes {
73     /// Maps from every use of a named (not anonymous) lifetime to a
74     /// `Region` describing how that region is bound
75     pub defs: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Region>>,
76
77     /// Set of lifetime def ids that are late-bound; a region can
78     /// be late-bound if (a) it does NOT appear in a where-clause and
79     /// (b) it DOES appear in the arguments.
80     pub late_bound: FxHashMap<LocalDefId, FxHashSet<ItemLocalId>>,
81
82     /// For each type and trait definition, maps type parameters
83     /// to the trait object lifetime defaults computed from them.
84     pub object_lifetime_defaults:
85         FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Vec<ObjectLifetimeDefault>>>,
86 }