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, RustcEncodable, RustcDecodable, 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, RustcEncodable, RustcDecodable, Debug, HashStable)]
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
),
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)]
57 impl<T
: PartialEq
> Set1
<T
> {
58 pub fn insert(&mut self, value
: T
) {
60 Set1
::Empty
=> Set1
::One(value
),
61 Set1
::One(old
) if *old
== value
=> return,
67 pub type ObjectLifetimeDefault
= Set1
<Region
>;
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
>>,
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
>>,
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
>>>,