|
| 1 | +//! Name resolution for lifetimes. |
| 2 | +//! |
| 3 | +//! Name resolution for lifetimes follows *much* simpler rules than the |
| 4 | +//! full resolve. For example, lifetime names are never exported or |
| 5 | +//! used between functions, and they operate in a purely top-down |
| 6 | +//! way. Therefore, we break lifetime name resolution into a separate pass. |
| 7 | +
|
| 8 | +use crate::hir::def_id::{DefId, LocalDefId}; |
| 9 | +use crate::hir::{GenericParam, ItemLocalId}; |
| 10 | +use crate::hir::{GenericParamKind, LifetimeParamKind}; |
| 11 | +use crate::ty; |
| 12 | + |
| 13 | +use crate::util::nodemap::{FxHashMap, FxHashSet, HirIdMap, HirIdSet}; |
| 14 | +use rustc_macros::HashStable; |
| 15 | + |
| 16 | +/// The origin of a named lifetime definition. |
| 17 | +/// |
| 18 | +/// This is used to prevent the usage of in-band lifetimes in `Fn`/`fn` syntax. |
| 19 | +#[derive(Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug, HashStable)] |
| 20 | +pub enum LifetimeDefOrigin { |
| 21 | + // Explicit binders like `fn foo<'a>(x: &'a u8)` or elided like `impl Foo<&u32>` |
| 22 | + ExplicitOrElided, |
| 23 | + // In-band declarations like `fn foo(x: &'a u8)` |
| 24 | + InBand, |
| 25 | + // Some kind of erroneous origin |
| 26 | + Error, |
| 27 | +} |
| 28 | + |
| 29 | +impl LifetimeDefOrigin { |
| 30 | + pub fn from_param(param: &GenericParam<'_>) -> Self { |
| 31 | + match param.kind { |
| 32 | + GenericParamKind::Lifetime { kind } => match kind { |
| 33 | + LifetimeParamKind::InBand => LifetimeDefOrigin::InBand, |
| 34 | + LifetimeParamKind::Explicit => LifetimeDefOrigin::ExplicitOrElided, |
| 35 | + LifetimeParamKind::Elided => LifetimeDefOrigin::ExplicitOrElided, |
| 36 | + LifetimeParamKind::Error => LifetimeDefOrigin::Error, |
| 37 | + }, |
| 38 | + _ => bug!("expected a lifetime param"), |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +#[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug, HashStable)] |
| 44 | +pub enum Region { |
| 45 | + Static, |
| 46 | + EarlyBound(/* index */ u32, /* lifetime decl */ DefId, LifetimeDefOrigin), |
| 47 | + LateBound(ty::DebruijnIndex, /* lifetime decl */ DefId, LifetimeDefOrigin), |
| 48 | + LateBoundAnon(ty::DebruijnIndex, /* anon index */ u32), |
| 49 | + Free(DefId, /* lifetime decl */ DefId), |
| 50 | +} |
| 51 | + |
| 52 | +/// A set containing, at most, one known element. |
| 53 | +/// If two distinct values are inserted into a set, then it |
| 54 | +/// becomes `Many`, which can be used to detect ambiguities. |
| 55 | +#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Debug, HashStable)] |
| 56 | +pub enum Set1<T> { |
| 57 | + Empty, |
| 58 | + One(T), |
| 59 | + Many, |
| 60 | +} |
| 61 | + |
| 62 | +impl<T: PartialEq> Set1<T> { |
| 63 | + pub fn insert(&mut self, value: T) { |
| 64 | + *self = match self { |
| 65 | + Set1::Empty => Set1::One(value), |
| 66 | + Set1::One(old) if *old == value => return, |
| 67 | + _ => Set1::Many, |
| 68 | + }; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +pub type ObjectLifetimeDefault = Set1<Region>; |
| 73 | + |
| 74 | +/// Maps the id of each lifetime reference to the lifetime decl |
| 75 | +/// that it corresponds to. |
| 76 | +#[derive(HashStable)] |
| 77 | +pub struct ResolveLifetimes { |
| 78 | + defs: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Region>>, |
| 79 | + late_bound: FxHashMap<LocalDefId, FxHashSet<ItemLocalId>>, |
| 80 | + object_lifetime_defaults: |
| 81 | + FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Vec<ObjectLifetimeDefault>>>, |
| 82 | +} |
| 83 | + |
| 84 | +impl ResolveLifetimes { |
| 85 | + pub fn new( |
| 86 | + defs: HirIdMap<Region>, |
| 87 | + late_bound: HirIdSet, |
| 88 | + object_lifetime_defaults: HirIdMap<Vec<ObjectLifetimeDefault>>, |
| 89 | + ) -> Self { |
| 90 | + let defs = { |
| 91 | + let mut map = FxHashMap::<_, FxHashMap<_, _>>::default(); |
| 92 | + for (hir_id, v) in defs { |
| 93 | + let map = map.entry(hir_id.owner_local_def_id()).or_default(); |
| 94 | + map.insert(hir_id.local_id, v); |
| 95 | + } |
| 96 | + map |
| 97 | + }; |
| 98 | + let late_bound = { |
| 99 | + let mut map = FxHashMap::<_, FxHashSet<_>>::default(); |
| 100 | + for hir_id in late_bound { |
| 101 | + let map = map.entry(hir_id.owner_local_def_id()).or_default(); |
| 102 | + map.insert(hir_id.local_id); |
| 103 | + } |
| 104 | + map |
| 105 | + }; |
| 106 | + let object_lifetime_defaults = { |
| 107 | + let mut map = FxHashMap::<_, FxHashMap<_, _>>::default(); |
| 108 | + for (hir_id, v) in object_lifetime_defaults { |
| 109 | + let map = map.entry(hir_id.owner_local_def_id()).or_default(); |
| 110 | + map.insert(hir_id.local_id, v); |
| 111 | + } |
| 112 | + map |
| 113 | + }; |
| 114 | + |
| 115 | + Self { defs, late_bound, object_lifetime_defaults } |
| 116 | + } |
| 117 | + |
| 118 | + pub fn named_region_map(&self, id: &LocalDefId) -> Option<&FxHashMap<ItemLocalId, Region>> { |
| 119 | + self.defs.get(id) |
| 120 | + } |
| 121 | + |
| 122 | + pub fn is_late_bound_map(&self, id: &LocalDefId) -> Option<&FxHashSet<ItemLocalId>> { |
| 123 | + self.late_bound.get(id) |
| 124 | + } |
| 125 | + |
| 126 | + pub fn object_lifetime_defaults_map( |
| 127 | + &self, |
| 128 | + id: &LocalDefId, |
| 129 | + ) -> Option<&FxHashMap<ItemLocalId, Vec<ObjectLifetimeDefault>>> { |
| 130 | + self.object_lifetime_defaults.get(id) |
| 131 | + } |
| 132 | +} |
0 commit comments