]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/middle/resolve_lifetime.rs
Rollup merge of #85174 - GuillaumeGomez:doc-code-block-border-radius, r=jsha
[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::{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, TyEncodable, TyDecodable, 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, TyEncodable, TyDecodable, Debug, HashStable)]
39 pub enum Region {
40     Static,
41     EarlyBound(/* index */ u32, /* lifetime decl */ DefId, LifetimeDefOrigin),
42     LateBound(
43         ty::DebruijnIndex,
44         /* late-bound index */ u32,
45         /* lifetime decl */ DefId,
46         LifetimeDefOrigin,
47     ),
48     LateBoundAnon(ty::DebruijnIndex, /* late-bound index */ u32, /* anon index */ u32),
49     Free(DefId, /* lifetime decl */ DefId),
50 }
51
52 /// This is used in diagnostics to improve suggestions for missing generic arguments.
53 /// It gives information on the type of lifetimes that are in scope for a particular `PathSegment`,
54 /// so that we can e.g. suggest elided-lifetimes-in-paths of the form <'_, '_> e.g.
55 #[derive(Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Debug, HashStable)]
56 pub enum LifetimeScopeForPath {
57     // Contains all lifetime names that are in scope and could possibly be used in generics
58     // arguments of path.
59     NonElided(Vec<String>),
60
61     // Information that allows us to suggest args of the form `<'_>` in case
62     // no generic arguments were provided for a path.
63     Elided,
64 }
65
66 /// A set containing, at most, one known element.
67 /// If two distinct values are inserted into a set, then it
68 /// becomes `Many`, which can be used to detect ambiguities.
69 #[derive(Copy, Clone, PartialEq, Eq, TyEncodable, TyDecodable, Debug, HashStable)]
70 pub enum Set1<T> {
71     Empty,
72     One(T),
73     Many,
74 }
75
76 impl<T: PartialEq> Set1<T> {
77     pub fn insert(&mut self, value: T) {
78         *self = match self {
79             Set1::Empty => Set1::One(value),
80             Set1::One(old) if *old == value => return,
81             _ => Set1::Many,
82         };
83     }
84 }
85
86 pub type ObjectLifetimeDefault = Set1<Region>;
87
88 /// Maps the id of each lifetime reference to the lifetime decl
89 /// that it corresponds to.
90 #[derive(Default, HashStable, Debug)]
91 pub struct ResolveLifetimes {
92     /// Maps from every use of a named (not anonymous) lifetime to a
93     /// `Region` describing how that region is bound
94     pub defs: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Region>>,
95
96     /// Set of lifetime def ids that are late-bound; a region can
97     /// be late-bound if (a) it does NOT appear in a where-clause and
98     /// (b) it DOES appear in the arguments.
99     pub late_bound: FxHashMap<LocalDefId, FxHashSet<ItemLocalId>>,
100
101     pub late_bound_vars: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Vec<ty::BoundVariableKind>>>,
102 }