resolve_lifetime.rs 2.6 KB
Newer Older
C
Camille GILLOT 已提交
1
//! Name resolution for lifetimes: type declarations.
2 3 4

use crate::ty;

5
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
6
use rustc_hir::def_id::{DefId, LocalDefId};
M
Michael Goulet 已提交
7
use rustc_hir::ItemLocalId;
8
use rustc_macros::HashStable;
9
use rustc_span::symbol::Symbol;
10

M
Matthew Jasper 已提交
11
#[derive(Clone, Copy, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Debug, HashStable)]
12 13
pub enum Region {
    Static,
M
Michael Goulet 已提交
14 15
    EarlyBound(/* index */ u32, /* lifetime decl */ DefId),
    LateBound(ty::DebruijnIndex, /* late-bound index */ u32, /* lifetime decl */ DefId),
16
    LateBoundAnon(ty::DebruijnIndex, /* late-bound index */ u32, /* anon index */ u32),
17 18 19
    Free(DefId, /* lifetime decl */ DefId),
}

B
b-naber 已提交
20 21 22 23 24 25 26
/// This is used in diagnostics to improve suggestions for missing generic arguments.
/// It gives information on the type of lifetimes that are in scope for a particular `PathSegment`,
/// so that we can e.g. suggest elided-lifetimes-in-paths of the form <'_, '_> e.g.
#[derive(Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Debug, HashStable)]
pub enum LifetimeScopeForPath {
    // Contains all lifetime names that are in scope and could possibly be used in generics
    // arguments of path.
27
    NonElided(Vec<Symbol>),
B
b-naber 已提交
28 29 30 31 32 33

    // Information that allows us to suggest args of the form `<'_>` in case
    // no generic arguments were provided for a path.
    Elided,
}

34 35 36
/// A set containing, at most, one known element.
/// If two distinct values are inserted into a set, then it
/// becomes `Many`, which can be used to detect ambiguities.
M
Matthew Jasper 已提交
37
#[derive(Copy, Clone, PartialEq, Eq, TyEncodable, TyDecodable, Debug, HashStable)]
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
pub enum Set1<T> {
    Empty,
    One(T),
    Many,
}

impl<T: PartialEq> Set1<T> {
    pub fn insert(&mut self, value: T) {
        *self = match self {
            Set1::Empty => Set1::One(value),
            Set1::One(old) if *old == value => return,
            _ => Set1::Many,
        };
    }
}

pub type ObjectLifetimeDefault = Set1<Region>;

/// Maps the id of each lifetime reference to the lifetime decl
/// that it corresponds to.
58
#[derive(Default, HashStable, Debug)]
59
pub struct ResolveLifetimes {
60 61 62
    /// Maps from every use of a named (not anonymous) lifetime to a
    /// `Region` describing how that region is bound
    pub defs: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Region>>,
63

64 65 66
    /// Set of lifetime def ids that are late-bound; a region can
    /// be late-bound if (a) it does NOT appear in a where-clause and
    /// (b) it DOES appear in the arguments.
67
    pub late_bound: FxHashMap<LocalDefId, FxHashSet<LocalDefId>>,
68 69

    pub late_bound_vars: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Vec<ty::BoundVariableKind>>>,
70
}