1 //! Name resolution for lifetimes: type declarations.
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;
11 /// The origin of a named lifetime definition.
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>`
18 // In-band declarations like `fn foo(x: &'a u8)`
20 // Some kind of erroneous origin
24 impl LifetimeDefOrigin {
25 pub fn from_param(param: &GenericParam<'_>) -> Self {
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,
33 _ => bug!("expected a lifetime param"),
38 #[derive(Clone, Copy, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Debug, HashStable)]
41 EarlyBound(/* index */ u32, /* lifetime decl */ DefId, LifetimeDefOrigin),
44 /* late-bound index */ u32,
45 /* lifetime decl */ DefId,
48 LateBoundAnon(ty::DebruijnIndex, /* late-bound index */ u32, /* anon index */ u32),
49 Free(DefId, /* lifetime decl */ DefId),
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
59 NonElided(Vec<String>),
61 // Information that allows us to suggest args of the form `<'_>` in case
62 // no generic arguments were provided for a path.
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)]
76 impl<T: PartialEq> Set1<T> {
77 pub fn insert(&mut self, value: T) {
79 Set1::Empty => Set1::One(value),
80 Set1::One(old) if *old == value => return,
86 pub type ObjectLifetimeDefault = Set1<Region>;
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>>,
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>>,
101 pub late_bound_vars: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Vec<ty::BoundVariableKind>>>,