2022-03-18 22:04:01 +03:00
|
|
|
// ignore-tidy-filelength
|
2014-11-25 21:17:11 -05:00
|
|
|
//! Name resolution for lifetimes.
|
|
|
|
//!
|
2019-02-28 22:43:53 +00:00
|
|
|
//! Name resolution for lifetimes follows *much* simpler rules than the
|
2014-11-25 21:17:11 -05:00
|
|
|
//! full resolve. For example, lifetime names are never exported or
|
|
|
|
//! used between functions, and they operate in a purely top-down
|
2019-02-08 14:53:55 +01:00
|
|
|
//! way. Therefore, we break lifetime name resolution into a separate pass.
|
2014-11-06 00:05:53 -08:00
|
|
|
|
2020-02-24 21:44:55 +03:00
|
|
|
use crate::late::diagnostics::{ForLifetimeSpanType, MissingLifetimeSpot};
|
2020-02-29 20:37:32 +03:00
|
|
|
use rustc_ast::walk_list;
|
2022-04-27 22:15:58 +02:00
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
|
2022-05-10 21:15:30 +02:00
|
|
|
use rustc_errors::struct_span_err;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::def::{DefKind, Res};
|
2021-02-18 21:01:44 +01:00
|
|
|
use rustc_hir::def_id::{DefIdMap, LocalDefId};
|
2020-11-22 13:57:11 +01:00
|
|
|
use rustc_hir::hir_id::ItemLocalId;
|
2021-11-03 18:03:12 -05:00
|
|
|
use rustc_hir::intravisit::{self, Visitor};
|
2022-04-27 22:15:58 +02:00
|
|
|
use rustc_hir::{GenericArg, GenericParam, LifetimeName, Node};
|
2022-05-26 08:59:15 +02:00
|
|
|
use rustc_hir::{GenericParamKind, HirIdMap};
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::hir::map::Map;
|
2021-11-03 18:03:12 -05:00
|
|
|
use rustc_middle::hir::nested_filter;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::middle::resolve_lifetime::*;
|
2022-05-26 08:59:15 +02:00
|
|
|
use rustc_middle::ty::{self, GenericParamDefKind, TyCtxt};
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::{bug, span_bug};
|
2021-02-18 21:01:44 +01:00
|
|
|
use rustc_span::def_id::DefId;
|
2021-12-04 21:20:58 +01:00
|
|
|
use rustc_span::symbol::{kw, sym, Ident};
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::Span;
|
2018-10-02 18:16:01 +02:00
|
|
|
use std::borrow::Cow;
|
2017-01-13 15:09:56 +02:00
|
|
|
use std::cell::Cell;
|
2021-02-27 21:31:56 -05:00
|
|
|
use std::fmt;
|
2020-04-17 13:59:14 -07:00
|
|
|
use std::mem::take;
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2020-10-26 14:18:31 -04:00
|
|
|
use tracing::{debug, span, Level};
|
2019-12-29 20:45:48 +01:00
|
|
|
|
2019-12-29 10:48:52 +01:00
|
|
|
trait RegionExt {
|
2022-04-27 22:15:58 +02:00
|
|
|
fn early(hir_map: Map<'_>, index: &mut u32, param: &GenericParam<'_>) -> (LocalDefId, Region);
|
2019-12-29 10:48:52 +01:00
|
|
|
|
2022-04-27 22:15:58 +02:00
|
|
|
fn late(index: u32, hir_map: Map<'_>, param: &GenericParam<'_>) -> (LocalDefId, Region);
|
2019-12-29 10:48:52 +01:00
|
|
|
|
2020-10-26 14:18:31 -04:00
|
|
|
fn late_anon(named_late_bound_vars: u32, index: &Cell<u32>) -> Region;
|
2019-12-29 10:48:52 +01:00
|
|
|
|
|
|
|
fn id(&self) -> Option<DefId>;
|
|
|
|
|
|
|
|
fn shifted(self, amount: u32) -> Region;
|
|
|
|
|
|
|
|
fn shifted_out_to_binder(self, binder: ty::DebruijnIndex) -> Region;
|
|
|
|
|
|
|
|
fn subst<'a, L>(self, params: L, map: &NamedRegionMap) -> Option<Region>
|
|
|
|
where
|
|
|
|
L: Iterator<Item = &'a hir::Lifetime>;
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
2013-10-28 17:37:10 -04:00
|
|
|
|
2019-12-29 10:48:52 +01:00
|
|
|
impl RegionExt for Region {
|
2022-04-27 22:15:58 +02:00
|
|
|
fn early(hir_map: Map<'_>, index: &mut u32, param: &GenericParam<'_>) -> (LocalDefId, Region) {
|
2017-01-11 17:35:54 +02:00
|
|
|
let i = *index;
|
|
|
|
*index += 1;
|
2019-06-27 11:28:14 +02:00
|
|
|
let def_id = hir_map.local_def_id(param.hir_id);
|
2017-10-15 13:43:06 -07:00
|
|
|
debug!("Region::early: index={} def_id={:?}", i, def_id);
|
2022-04-27 22:15:58 +02:00
|
|
|
(def_id, Region::EarlyBound(i, def_id.to_def_id()))
|
2017-01-11 17:35:54 +02:00
|
|
|
}
|
|
|
|
|
2022-04-27 22:15:58 +02:00
|
|
|
fn late(idx: u32, hir_map: Map<'_>, param: &GenericParam<'_>) -> (LocalDefId, Region) {
|
2018-06-10 14:44:15 +02:00
|
|
|
let depth = ty::INNERMOST;
|
2019-06-27 11:28:14 +02:00
|
|
|
let def_id = hir_map.local_def_id(param.hir_id);
|
2018-05-30 10:27:53 -04:00
|
|
|
debug!(
|
2022-02-20 10:22:57 -08:00
|
|
|
"Region::late: idx={:?}, param={:?} depth={:?} def_id={:?}",
|
|
|
|
idx, param, depth, def_id,
|
2018-05-30 10:27:53 -04:00
|
|
|
);
|
2022-04-27 22:15:58 +02:00
|
|
|
(def_id, Region::LateBound(depth, idx, def_id.to_def_id()))
|
2017-01-11 17:35:54 +02:00
|
|
|
}
|
|
|
|
|
2020-11-15 17:06:58 -05:00
|
|
|
fn late_anon(named_late_bound_vars: u32, index: &Cell<u32>) -> Region {
|
2017-01-13 15:09:56 +02:00
|
|
|
let i = index.get();
|
|
|
|
index.set(i + 1);
|
2018-06-10 14:44:15 +02:00
|
|
|
let depth = ty::INNERMOST;
|
2020-11-15 17:06:58 -05:00
|
|
|
Region::LateBoundAnon(depth, named_late_bound_vars + i, i)
|
2017-01-13 15:09:56 +02:00
|
|
|
}
|
|
|
|
|
2017-08-15 17:05:25 +02:00
|
|
|
fn id(&self) -> Option<DefId> {
|
2017-01-11 17:35:54 +02:00
|
|
|
match *self {
|
2017-12-11 09:00:05 -05:00
|
|
|
Region::Static | Region::LateBoundAnon(..) => None,
|
2017-01-13 15:09:56 +02:00
|
|
|
|
2022-02-20 10:22:57 -08:00
|
|
|
Region::EarlyBound(_, id) | Region::LateBound(_, _, id) | Region::Free(_, id) => {
|
2017-12-11 09:00:05 -05:00
|
|
|
Some(id)
|
|
|
|
}
|
2017-01-11 17:35:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn shifted(self, amount: u32) -> Region {
|
|
|
|
match self {
|
2022-02-20 10:22:57 -08:00
|
|
|
Region::LateBound(debruijn, idx, id) => {
|
|
|
|
Region::LateBound(debruijn.shifted_in(amount), idx, id)
|
2017-01-11 17:35:54 +02:00
|
|
|
}
|
2020-11-15 17:06:58 -05:00
|
|
|
Region::LateBoundAnon(debruijn, index, anon_index) => {
|
|
|
|
Region::LateBoundAnon(debruijn.shifted_in(amount), index, anon_index)
|
2017-01-13 15:09:56 +02:00
|
|
|
}
|
2017-12-11 09:00:05 -05:00
|
|
|
_ => self,
|
2017-01-13 15:09:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-28 09:53:10 -04:00
|
|
|
fn shifted_out_to_binder(self, binder: ty::DebruijnIndex) -> Region {
|
2017-01-13 15:09:56 +02:00
|
|
|
match self {
|
2022-02-20 10:22:57 -08:00
|
|
|
Region::LateBound(debruijn, index, id) => {
|
|
|
|
Region::LateBound(debruijn.shifted_out_to_binder(binder), index, id)
|
2018-10-18 05:39:53 -04:00
|
|
|
}
|
2020-11-15 17:06:58 -05:00
|
|
|
Region::LateBoundAnon(debruijn, index, anon_index) => {
|
|
|
|
Region::LateBoundAnon(debruijn.shifted_out_to_binder(binder), index, anon_index)
|
2018-10-18 05:39:53 -04:00
|
|
|
}
|
2017-12-11 09:00:05 -05:00
|
|
|
_ => self,
|
2017-01-11 17:35:54 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-25 17:32:44 +02:00
|
|
|
|
2018-02-23 17:48:54 +00:00
|
|
|
fn subst<'a, L>(self, mut params: L, map: &NamedRegionMap) -> Option<Region>
|
2018-10-18 05:39:53 -04:00
|
|
|
where
|
|
|
|
L: Iterator<Item = &'a hir::Lifetime>,
|
|
|
|
{
|
2022-02-20 10:22:57 -08:00
|
|
|
if let Region::EarlyBound(index, _) = self {
|
2019-12-24 17:38:22 -05:00
|
|
|
params.nth(index as usize).and_then(|lifetime| map.defs.get(&lifetime.hir_id).cloned())
|
2017-01-25 17:32:44 +02:00
|
|
|
} else {
|
|
|
|
Some(self)
|
|
|
|
}
|
|
|
|
}
|
2017-01-11 17:35:54 +02:00
|
|
|
}
|
|
|
|
|
2017-11-23 08:05:58 -05:00
|
|
|
/// Maps the id of each lifetime reference to the lifetime decl
|
|
|
|
/// that it corresponds to.
|
|
|
|
///
|
|
|
|
/// FIXME. This struct gets converted to a `ResolveLifetimes` for
|
2020-03-20 13:23:24 +01:00
|
|
|
/// actual use. It has the same data, but indexed by `LocalDefId`. This
|
2017-11-23 08:05:58 -05:00
|
|
|
/// is silly.
|
2021-02-27 21:31:56 -05:00
|
|
|
#[derive(Debug, Default)]
|
2017-11-23 08:05:58 -05:00
|
|
|
struct NamedRegionMap {
|
2016-04-21 05:10:10 -04:00
|
|
|
// maps from every use of a named (not anonymous) lifetime to a
|
2017-01-08 22:40:04 +02:00
|
|
|
// `Region` describing how that region is bound
|
2019-12-29 10:48:52 +01:00
|
|
|
defs: HirIdMap<Region>,
|
2016-04-21 05:10:10 -04:00
|
|
|
|
2021-03-26 17:40:15 -04:00
|
|
|
// Maps relevant hir items to the bound vars on them. These include:
|
|
|
|
// - function defs
|
|
|
|
// - function pointers
|
|
|
|
// - closures
|
|
|
|
// - trait refs
|
|
|
|
// - bound types (like `T` in `for<'a> T<'a>: Foo`)
|
2020-10-26 14:18:31 -04:00
|
|
|
late_bound_vars: HirIdMap<Vec<ty::BoundVariableKind>>,
|
2021-02-18 21:01:44 +01:00
|
|
|
|
|
|
|
// maps `PathSegment` `HirId`s to lifetime scopes.
|
|
|
|
scope_for_path: Option<FxHashMap<LocalDefId, FxHashMap<ItemLocalId, LifetimeScopeForPath>>>,
|
2017-11-23 08:05:58 -05:00
|
|
|
}
|
|
|
|
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) struct LifetimeContext<'a, 'tcx> {
|
|
|
|
pub(crate) tcx: TyCtxt<'tcx>,
|
2016-04-21 05:10:10 -04:00
|
|
|
map: &'a mut NamedRegionMap,
|
2017-01-08 22:40:04 +02:00
|
|
|
scope: ScopeRef<'a>,
|
2018-05-03 14:16:43 -04:00
|
|
|
|
2021-03-12 12:38:42 -05:00
|
|
|
/// Indicates that we only care about the definition of a trait. This should
|
|
|
|
/// be false if the `Item` we are resolving lifetimes for is not a trait or
|
|
|
|
/// we eventually need lifetimes resolve for trait items.
|
|
|
|
trait_definition_only: bool,
|
2021-02-27 21:31:56 -05:00
|
|
|
|
2018-05-03 14:16:43 -04:00
|
|
|
/// Cache for cross-crate per-definition object lifetime defaults.
|
2017-01-25 17:32:44 +02:00
|
|
|
xcrate_object_lifetime_defaults: DefIdMap<Vec<ObjectLifetimeDefault>>,
|
2017-11-23 08:05:58 -05:00
|
|
|
|
2020-01-15 18:34:30 -08:00
|
|
|
/// When encountering an undefined named lifetime, we will suggest introducing it in these
|
|
|
|
/// places.
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) missing_named_lifetime_spots: Vec<MissingLifetimeSpot<'tcx>>,
|
2013-10-28 17:37:10 -04:00
|
|
|
}
|
|
|
|
|
2017-01-11 17:35:54 +02:00
|
|
|
#[derive(Debug)]
|
2017-01-08 22:40:04 +02:00
|
|
|
enum Scope<'a> {
|
2017-01-11 17:35:54 +02:00
|
|
|
/// Declares lifetimes, and each can be early-bound or late-bound.
|
|
|
|
/// The `DebruijnIndex` of late-bound lifetimes starts at `1` and
|
|
|
|
/// it should be shifted by the number of `Binder`s in between the
|
|
|
|
/// declaration `Binder` and the location it's referenced from.
|
|
|
|
Binder {
|
2021-02-18 21:01:44 +01:00
|
|
|
/// We use an IndexMap here because we want these lifetimes in order
|
|
|
|
/// for diagnostics.
|
2022-04-27 22:15:58 +02:00
|
|
|
lifetimes: FxIndexMap<LocalDefId, Region>,
|
2017-10-15 13:43:06 -07:00
|
|
|
|
|
|
|
/// if we extend this scope with another scope, what is the next index
|
|
|
|
/// we should use for an early-bound region?
|
|
|
|
next_early_index: u32,
|
|
|
|
|
2018-01-17 17:22:40 -08:00
|
|
|
/// Whether or not this binder would serve as the parent
|
2019-08-02 00:05:19 +01:00
|
|
|
/// binder for opaque types introduced within. For example:
|
2018-01-17 17:22:40 -08:00
|
|
|
///
|
2019-12-29 11:17:20 +01:00
|
|
|
/// ```text
|
2018-01-17 17:22:40 -08:00
|
|
|
/// fn foo<'a>() -> impl for<'b> Trait<Item = impl Trait2<'a>>
|
2019-12-29 11:17:20 +01:00
|
|
|
/// ```
|
2018-01-17 17:22:40 -08:00
|
|
|
///
|
2019-08-02 00:05:19 +01:00
|
|
|
/// Here, the opaque types we create for the `impl Trait`
|
2018-01-17 17:22:40 -08:00
|
|
|
/// and `impl Trait2` references will both have the `foo` item
|
|
|
|
/// as their parent. When we get to `impl Trait2`, we find
|
|
|
|
/// that it is nested within the `for<>` binder -- this flag
|
|
|
|
/// allows us to skip that when looking for the parent binder
|
2019-08-02 00:05:19 +01:00
|
|
|
/// of the resulting opaque type.
|
|
|
|
opaque_type_parent: bool,
|
2018-01-17 17:22:40 -08:00
|
|
|
|
2021-04-13 16:58:00 -04:00
|
|
|
scope_type: BinderScopeType,
|
2021-04-12 09:12:10 -04:00
|
|
|
|
2020-10-26 14:18:31 -04:00
|
|
|
/// The late bound vars for a given item are stored by `HirId` to be
|
|
|
|
/// queried later. However, if we enter an elision scope, we have to
|
|
|
|
/// later append the elided bound vars to the list and need to know what
|
|
|
|
/// to append to.
|
|
|
|
hir_id: hir::HirId,
|
|
|
|
|
2017-12-11 09:00:05 -05:00
|
|
|
s: ScopeRef<'a>,
|
2022-03-18 22:04:01 +03:00
|
|
|
|
|
|
|
/// In some cases not allowing late bounds allows us to avoid ICEs.
|
|
|
|
/// This is almost ways set to true.
|
|
|
|
allow_late_bound: bool,
|
2017-01-08 22:40:04 +02:00
|
|
|
},
|
|
|
|
|
2017-01-11 17:35:54 +02:00
|
|
|
/// Lifetimes introduced by a fn are scoped to the call-site for that fn,
|
|
|
|
/// if this is a fn body, otherwise the original definitions are used.
|
2017-01-13 15:09:56 +02:00
|
|
|
/// Unspecified lifetimes are inferred, unless an elision scope is nested,
|
2018-11-27 02:59:49 +00:00
|
|
|
/// e.g., `(&T, fn(&T) -> &T);` becomes `(&'_ T, for<'a> fn(&'a T) -> &'a T)`.
|
2017-01-11 17:35:54 +02:00
|
|
|
Body {
|
|
|
|
id: hir::BodyId,
|
2017-12-11 09:00:05 -05:00
|
|
|
s: ScopeRef<'a>,
|
2017-01-08 22:40:04 +02:00
|
|
|
},
|
|
|
|
|
2017-01-13 15:09:56 +02:00
|
|
|
/// A scope which either determines unspecified lifetimes or errors
|
2018-11-27 02:59:49 +00:00
|
|
|
/// on them (e.g., due to ambiguity). For more details, see `Elide`.
|
2017-01-13 15:09:56 +02:00
|
|
|
Elision {
|
|
|
|
elide: Elide,
|
2017-12-11 09:00:05 -05:00
|
|
|
s: ScopeRef<'a>,
|
2017-01-13 15:09:56 +02:00
|
|
|
},
|
|
|
|
|
2017-01-25 17:32:44 +02:00
|
|
|
/// Use a specific lifetime (if `Some`) or leave it unset (to be
|
|
|
|
/// inferred in a function body or potentially error outside one),
|
|
|
|
/// for the default choice of lifetime in a trait object type.
|
|
|
|
ObjectLifetimeDefault {
|
|
|
|
lifetime: Option<Region>,
|
2017-12-11 09:00:05 -05:00
|
|
|
s: ScopeRef<'a>,
|
2017-01-25 17:32:44 +02:00
|
|
|
},
|
|
|
|
|
2022-03-30 15:14:15 -04:00
|
|
|
/// When we have nested trait refs, we concatenate late bound vars for inner
|
2020-10-26 14:18:31 -04:00
|
|
|
/// trait refs from outer ones. But we also need to include any HRTB
|
|
|
|
/// lifetimes encountered when identifying the trait that an associated type
|
|
|
|
/// is declared on.
|
|
|
|
Supertrait {
|
|
|
|
lifetimes: Vec<ty::BoundVariableKind>,
|
|
|
|
s: ScopeRef<'a>,
|
|
|
|
},
|
|
|
|
|
2021-04-05 00:10:09 -04:00
|
|
|
TraitRefBoundary {
|
|
|
|
s: ScopeRef<'a>,
|
|
|
|
},
|
|
|
|
|
2017-12-11 09:00:05 -05:00
|
|
|
Root,
|
2013-10-28 17:37:10 -04:00
|
|
|
}
|
|
|
|
|
2021-04-13 16:58:00 -04:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
enum BinderScopeType {
|
2021-04-21 12:26:19 -04:00
|
|
|
/// Any non-concatenating binder scopes.
|
|
|
|
Normal,
|
2021-04-20 16:39:41 -04:00
|
|
|
/// Within a syntactic trait ref, there may be multiple poly trait refs that
|
2022-03-30 15:14:15 -04:00
|
|
|
/// are nested (under the `associated_type_bounds` feature). The binders of
|
|
|
|
/// the inner poly trait refs are extended from the outer poly trait refs
|
2021-04-20 16:39:41 -04:00
|
|
|
/// and don't increase the late bound depth. If you had
|
|
|
|
/// `T: for<'a> Foo<Bar: for<'b> Baz<'a, 'b>>`, then the `for<'b>` scope
|
|
|
|
/// would be `Concatenating`. This also used in trait refs in where clauses
|
|
|
|
/// where we have two binders `for<> T: for<> Foo` (I've intentionally left
|
|
|
|
/// out any lifetimes because they aren't needed to show the two scopes).
|
2021-04-21 03:12:04 -04:00
|
|
|
/// The inner `for<>` has a scope of `Concatenating`.
|
2021-04-13 16:58:00 -04:00
|
|
|
Concatenating,
|
|
|
|
}
|
|
|
|
|
2021-02-27 21:31:56 -05:00
|
|
|
// A helper struct for debugging scopes without printing parent scopes
|
|
|
|
struct TruncatedScopeDebug<'a>(&'a Scope<'a>);
|
|
|
|
|
|
|
|
impl<'a> fmt::Debug for TruncatedScopeDebug<'a> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self.0 {
|
|
|
|
Scope::Binder {
|
|
|
|
lifetimes,
|
|
|
|
next_early_index,
|
|
|
|
opaque_type_parent,
|
2021-04-13 16:58:00 -04:00
|
|
|
scope_type,
|
2020-10-26 14:18:31 -04:00
|
|
|
hir_id,
|
2021-02-27 21:31:56 -05:00
|
|
|
s: _,
|
2022-03-18 22:04:01 +03:00
|
|
|
allow_late_bound,
|
2021-02-27 21:31:56 -05:00
|
|
|
} => f
|
|
|
|
.debug_struct("Binder")
|
|
|
|
.field("lifetimes", lifetimes)
|
|
|
|
.field("next_early_index", next_early_index)
|
|
|
|
.field("opaque_type_parent", opaque_type_parent)
|
2021-04-13 16:58:00 -04:00
|
|
|
.field("scope_type", scope_type)
|
2020-10-26 14:18:31 -04:00
|
|
|
.field("hir_id", hir_id)
|
2021-02-27 21:31:56 -05:00
|
|
|
.field("s", &"..")
|
2022-03-18 22:04:01 +03:00
|
|
|
.field("allow_late_bound", allow_late_bound)
|
2021-02-27 21:31:56 -05:00
|
|
|
.finish(),
|
|
|
|
Scope::Body { id, s: _ } => {
|
|
|
|
f.debug_struct("Body").field("id", id).field("s", &"..").finish()
|
|
|
|
}
|
|
|
|
Scope::Elision { elide, s: _ } => {
|
|
|
|
f.debug_struct("Elision").field("elide", elide).field("s", &"..").finish()
|
|
|
|
}
|
|
|
|
Scope::ObjectLifetimeDefault { lifetime, s: _ } => f
|
|
|
|
.debug_struct("ObjectLifetimeDefault")
|
|
|
|
.field("lifetime", lifetime)
|
|
|
|
.field("s", &"..")
|
|
|
|
.finish(),
|
2020-10-26 14:18:31 -04:00
|
|
|
Scope::Supertrait { lifetimes, s: _ } => f
|
|
|
|
.debug_struct("Supertrait")
|
|
|
|
.field("lifetimes", lifetimes)
|
|
|
|
.field("s", &"..")
|
|
|
|
.finish(),
|
2021-04-05 00:10:09 -04:00
|
|
|
Scope::TraitRefBoundary { s: _ } => f.debug_struct("TraitRefBoundary").finish(),
|
2021-02-27 21:31:56 -05:00
|
|
|
Scope::Root => f.debug_struct("Root").finish(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 15:09:56 +02:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
enum Elide {
|
|
|
|
/// Use a fresh anonymous late-bound lifetime each time, by
|
2020-11-15 17:06:58 -05:00
|
|
|
/// incrementing the counter to generate sequential indices. All
|
|
|
|
/// anonymous lifetimes must start *after* named bound vars.
|
|
|
|
FreshLateAnon(u32, Cell<u32>),
|
2017-01-13 15:09:56 +02:00
|
|
|
/// Always use this one lifetime.
|
|
|
|
Exact(Region),
|
|
|
|
/// Less or more than one lifetime were found, error on unspecified.
|
2017-12-11 09:00:05 -05:00
|
|
|
Error(Vec<ElisionFailureInfo>),
|
2020-06-07 19:52:05 +01:00
|
|
|
/// Forbid lifetime elision inside of a larger scope where it would be
|
|
|
|
/// permitted. For example, in let position impl trait.
|
2020-05-10 12:18:55 +01:00
|
|
|
Forbid,
|
2017-01-13 15:09:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) struct ElisionFailureInfo {
|
2017-01-13 15:09:56 +02:00
|
|
|
/// Where we can find the argument pattern.
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) parent: Option<hir::BodyId>,
|
2017-01-13 15:09:56 +02:00
|
|
|
/// The index of the argument in the original definition.
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) index: usize,
|
|
|
|
pub(crate) lifetime_count: usize,
|
|
|
|
pub(crate) have_bound_regions: bool,
|
|
|
|
pub(crate) span: Span,
|
2017-01-13 15:09:56 +02:00
|
|
|
}
|
|
|
|
|
2017-01-08 22:40:04 +02:00
|
|
|
type ScopeRef<'a> = &'a Scope<'a>;
|
2014-02-26 14:59:49 +01:00
|
|
|
|
2017-01-08 22:40:04 +02:00
|
|
|
const ROOT_SCOPE: ScopeRef<'static> = &Scope::Root;
|
2014-09-12 13:10:30 +03:00
|
|
|
|
2020-07-05 23:00:14 +03:00
|
|
|
pub fn provide(providers: &mut ty::query::Providers) {
|
2018-06-13 16:44:43 +03:00
|
|
|
*providers = ty::query::Providers {
|
2021-03-12 12:38:42 -05:00
|
|
|
resolve_lifetimes_trait_definition,
|
2017-11-23 08:05:58 -05:00
|
|
|
resolve_lifetimes,
|
|
|
|
|
2021-02-27 21:31:56 -05:00
|
|
|
named_region_map: |tcx, id| resolve_lifetimes_for(tcx, id).defs.get(&id),
|
2020-11-22 13:57:11 +01:00
|
|
|
is_late_bound_map,
|
2022-02-01 18:28:24 +01:00
|
|
|
object_lifetime_defaults: |tcx, id| match tcx.hir().find_by_def_id(id) {
|
2021-10-20 20:59:15 +02:00
|
|
|
Some(Node::Item(item)) => compute_object_lifetime_defaults(tcx, item),
|
|
|
|
_ => None,
|
2017-11-23 08:05:58 -05:00
|
|
|
},
|
2020-10-26 14:18:31 -04:00
|
|
|
late_bound_vars_map: |tcx, id| resolve_lifetimes_for(tcx, id).late_bound_vars.get(&id),
|
2021-02-18 21:01:44 +01:00
|
|
|
lifetime_scope_map: |tcx, id| {
|
|
|
|
let item_id = item_for(tcx, id);
|
|
|
|
do_resolve(tcx, item_id, false, true).scope_for_path.unwrap().remove(&id)
|
|
|
|
},
|
2017-11-23 08:05:58 -05:00
|
|
|
|
|
|
|
..*providers
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-27 21:31:56 -05:00
|
|
|
/// Like `resolve_lifetimes`, but does not resolve lifetimes for trait items.
|
|
|
|
/// Also does not generate any diagnostics.
|
2021-03-25 09:22:15 -04:00
|
|
|
///
|
|
|
|
/// This is ultimately a subset of the `resolve_lifetimes` work. It effectively
|
|
|
|
/// resolves lifetimes only within the trait "header" -- that is, the trait
|
|
|
|
/// and supertrait list. In contrast, `resolve_lifetimes` resolves all the
|
|
|
|
/// lifetimes within the trait and its items. There is room to refactor this,
|
|
|
|
/// for example to resolve lifetimes for each trait item in separate queries,
|
|
|
|
/// but it's convenient to do the entire trait at once because the lifetimes
|
|
|
|
/// from the trait definition are in scope within the trait items as well.
|
|
|
|
///
|
|
|
|
/// The reason for this separate call is to resolve what would otherwise
|
|
|
|
/// be a cycle. Consider this example:
|
|
|
|
///
|
2022-04-15 15:04:34 -07:00
|
|
|
/// ```ignore UNSOLVED (maybe @jackh726 knows what lifetime parameter to give Sub)
|
2021-03-25 09:22:15 -04:00
|
|
|
/// trait Base<'a> {
|
|
|
|
/// type BaseItem;
|
|
|
|
/// }
|
|
|
|
/// trait Sub<'b>: for<'a> Base<'a> {
|
|
|
|
/// type SubItem: Sub<BaseItem = &'b u32>;
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// When we resolve `Sub` and all its items, we also have to resolve `Sub<BaseItem = &'b u32>`.
|
|
|
|
/// To figure out the index of `'b`, we have to know about the supertraits
|
|
|
|
/// of `Sub` so that we can determine that the `for<'a>` will be in scope.
|
|
|
|
/// (This is because we -- currently at least -- flatten all the late-bound
|
|
|
|
/// lifetimes into a single binder.) This requires us to resolve the
|
|
|
|
/// *trait definition* of `Sub`; basically just enough lifetime information
|
|
|
|
/// to look at the supertraits.
|
2021-02-27 21:31:56 -05:00
|
|
|
#[tracing::instrument(level = "debug", skip(tcx))]
|
2021-03-12 12:38:42 -05:00
|
|
|
fn resolve_lifetimes_trait_definition(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
local_def_id: LocalDefId,
|
|
|
|
) -> ResolveLifetimes {
|
2022-05-26 08:59:15 +02:00
|
|
|
convert_named_region_map(do_resolve(tcx, local_def_id, true, false))
|
2021-02-27 21:31:56 -05:00
|
|
|
}
|
|
|
|
|
2021-03-12 12:38:42 -05:00
|
|
|
/// Computes the `ResolveLifetimes` map that contains data for an entire `Item`.
|
|
|
|
/// You should not read the result of this query directly, but rather use
|
|
|
|
/// `named_region_map`, `is_late_bound_map`, etc.
|
2021-02-27 21:31:56 -05:00
|
|
|
#[tracing::instrument(level = "debug", skip(tcx))]
|
|
|
|
fn resolve_lifetimes(tcx: TyCtxt<'_>, local_def_id: LocalDefId) -> ResolveLifetimes {
|
2022-05-26 08:59:15 +02:00
|
|
|
convert_named_region_map(do_resolve(tcx, local_def_id, false, false))
|
2021-02-27 21:31:56 -05:00
|
|
|
}
|
2017-11-23 08:05:58 -05:00
|
|
|
|
2021-02-27 21:31:56 -05:00
|
|
|
fn do_resolve(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
local_def_id: LocalDefId,
|
2021-03-12 12:38:42 -05:00
|
|
|
trait_definition_only: bool,
|
2021-02-18 21:01:44 +01:00
|
|
|
with_scope_for_path: bool,
|
|
|
|
) -> NamedRegionMap {
|
2021-10-20 22:38:10 +02:00
|
|
|
let item = tcx.hir().expect_item(local_def_id);
|
2020-10-26 14:18:31 -04:00
|
|
|
let mut named_region_map = NamedRegionMap {
|
|
|
|
defs: Default::default(),
|
|
|
|
late_bound_vars: Default::default(),
|
2021-02-18 21:01:44 +01:00
|
|
|
scope_for_path: with_scope_for_path.then(|| Default::default()),
|
2020-10-26 14:18:31 -04:00
|
|
|
};
|
2021-02-27 21:31:56 -05:00
|
|
|
let mut visitor = LifetimeContext {
|
|
|
|
tcx,
|
|
|
|
map: &mut named_region_map,
|
|
|
|
scope: ROOT_SCOPE,
|
2021-03-12 12:38:42 -05:00
|
|
|
trait_definition_only,
|
2021-02-27 21:31:56 -05:00
|
|
|
xcrate_object_lifetime_defaults: Default::default(),
|
|
|
|
missing_named_lifetime_spots: vec![],
|
|
|
|
};
|
|
|
|
visitor.visit_item(item);
|
2017-11-23 08:05:58 -05:00
|
|
|
|
2021-02-18 21:01:44 +01:00
|
|
|
named_region_map
|
|
|
|
}
|
|
|
|
|
2022-05-26 08:59:15 +02:00
|
|
|
fn convert_named_region_map(named_region_map: NamedRegionMap) -> ResolveLifetimes {
|
2019-12-30 12:48:32 +01:00
|
|
|
let mut rl = ResolveLifetimes::default();
|
|
|
|
|
|
|
|
for (hir_id, v) in named_region_map.defs {
|
2020-03-18 20:27:59 +02:00
|
|
|
let map = rl.defs.entry(hir_id.owner).or_default();
|
2019-12-30 12:48:32 +01:00
|
|
|
map.insert(hir_id.local_id, v);
|
|
|
|
}
|
2020-10-26 14:18:31 -04:00
|
|
|
for (hir_id, v) in named_region_map.late_bound_vars {
|
|
|
|
let map = rl.late_bound_vars.entry(hir_id.owner).or_default();
|
|
|
|
map.insert(hir_id.local_id, v);
|
|
|
|
}
|
2019-12-30 12:48:32 +01:00
|
|
|
|
2021-02-27 21:31:56 -05:00
|
|
|
debug!(?rl.defs);
|
2020-03-27 20:26:20 +01:00
|
|
|
rl
|
2017-11-23 08:05:58 -05:00
|
|
|
}
|
|
|
|
|
2021-03-12 12:38:42 -05:00
|
|
|
/// Given `any` owner (structs, traits, trait methods, etc.), does lifetime resolution.
|
|
|
|
/// There are two important things this does.
|
|
|
|
/// First, we have to resolve lifetimes for
|
|
|
|
/// the entire *`Item`* that contains this owner, because that's the largest "scope"
|
|
|
|
/// where we can have relevant lifetimes.
|
|
|
|
/// Second, if we are asking for lifetimes in a trait *definition*, we use `resolve_lifetimes_trait_definition`
|
|
|
|
/// instead of `resolve_lifetimes`, which does not descend into the trait items and does not emit diagnostics.
|
|
|
|
/// This allows us to avoid cycles. Importantly, if we ask for lifetimes for lifetimes that have an owner
|
|
|
|
/// other than the trait itself (like the trait methods or associated types), then we just use the regular
|
|
|
|
/// `resolve_lifetimes`.
|
2021-02-27 21:31:56 -05:00
|
|
|
fn resolve_lifetimes_for<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx ResolveLifetimes {
|
|
|
|
let item_id = item_for(tcx, def_id);
|
|
|
|
if item_id == def_id {
|
|
|
|
let item = tcx.hir().item(hir::ItemId { def_id: item_id });
|
|
|
|
match item.kind {
|
2021-03-12 12:38:42 -05:00
|
|
|
hir::ItemKind::Trait(..) => tcx.resolve_lifetimes_trait_definition(item_id),
|
2021-02-27 21:31:56 -05:00
|
|
|
_ => tcx.resolve_lifetimes(item_id),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tcx.resolve_lifetimes(item_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:38:42 -05:00
|
|
|
/// Finds the `Item` that contains the given `LocalDefId`
|
2021-02-27 21:31:56 -05:00
|
|
|
fn item_for(tcx: TyCtxt<'_>, local_def_id: LocalDefId) -> LocalDefId {
|
2021-10-20 20:59:15 +02:00
|
|
|
match tcx.hir().find_by_def_id(local_def_id) {
|
2021-02-27 21:31:56 -05:00
|
|
|
Some(Node::Item(item)) => {
|
|
|
|
return item.def_id;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
let item = {
|
2021-10-20 20:59:15 +02:00
|
|
|
let hir_id = tcx.hir().local_def_id_to_hir_id(local_def_id);
|
2021-09-18 15:48:07 -05:00
|
|
|
let mut parent_iter = tcx.hir().parent_iter(hir_id);
|
2021-02-27 21:31:56 -05:00
|
|
|
loop {
|
|
|
|
let node = parent_iter.next().map(|n| n.1);
|
|
|
|
match node {
|
|
|
|
Some(hir::Node::Item(item)) => break item.def_id,
|
|
|
|
Some(hir::Node::Crate(_)) | None => bug!("Called `item_for` on an Item."),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
item
|
|
|
|
}
|
|
|
|
|
2018-11-01 21:52:56 +00:00
|
|
|
/// In traits, there is an implicit `Self` type parameter which comes before the generics.
|
|
|
|
/// We have to account for this when computing the index of the other generic parameters.
|
|
|
|
/// This function returns whether there is such an implicit parameter defined on the given item.
|
2019-11-28 19:28:50 +01:00
|
|
|
fn sub_items_have_self_param(node: &hir::ItemKind<'_>) -> bool {
|
2020-10-26 21:02:48 -04:00
|
|
|
matches!(*node, hir::ItemKind::Trait(..) | hir::ItemKind::TraitAlias(..))
|
2018-11-01 21:52:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 14:18:31 -04:00
|
|
|
fn late_region_as_bound_region<'tcx>(tcx: TyCtxt<'tcx>, region: &Region) -> ty::BoundVariableKind {
|
|
|
|
match region {
|
2022-02-20 10:22:57 -08:00
|
|
|
Region::LateBound(_, _, def_id) => {
|
2020-10-26 14:18:31 -04:00
|
|
|
let name = tcx.hir().name(tcx.hir().local_def_id_to_hir_id(def_id.expect_local()));
|
|
|
|
ty::BoundVariableKind::Region(ty::BrNamed(*def_id, name))
|
|
|
|
}
|
|
|
|
Region::LateBoundAnon(_, _, anon_idx) => {
|
|
|
|
ty::BoundVariableKind::Region(ty::BrAnon(*anon_idx))
|
|
|
|
}
|
|
|
|
_ => bug!("{:?} is not a late region", region),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 21:01:44 +01:00
|
|
|
#[tracing::instrument(level = "debug")]
|
|
|
|
fn get_lifetime_scopes_for_path(mut scope: &Scope<'_>) -> LifetimeScopeForPath {
|
|
|
|
let mut available_lifetimes = vec![];
|
|
|
|
loop {
|
|
|
|
match scope {
|
|
|
|
Scope::Binder { lifetimes, s, .. } => {
|
2022-04-27 22:15:58 +02:00
|
|
|
available_lifetimes.extend(lifetimes.keys());
|
2021-02-18 21:01:44 +01:00
|
|
|
scope = s;
|
|
|
|
}
|
|
|
|
Scope::Body { s, .. } => {
|
|
|
|
scope = s;
|
|
|
|
}
|
|
|
|
Scope::Elision { elide, s } => {
|
|
|
|
if let Elide::Exact(_) = elide {
|
|
|
|
return LifetimeScopeForPath::Elided;
|
|
|
|
} else {
|
|
|
|
scope = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Scope::ObjectLifetimeDefault { s, .. } => {
|
|
|
|
scope = s;
|
|
|
|
}
|
|
|
|
Scope::Root => {
|
|
|
|
return LifetimeScopeForPath::NonElided(available_lifetimes);
|
|
|
|
}
|
|
|
|
Scope::Supertrait { s, .. } | Scope::TraitRefBoundary { s, .. } => {
|
|
|
|
scope = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 12:26:19 -04:00
|
|
|
impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
|
|
|
|
/// Returns the binders in scope and the type of `Binder` that should be created for a poly trait ref.
|
|
|
|
fn poly_trait_ref_binder_info(&mut self) -> (Vec<ty::BoundVariableKind>, BinderScopeType) {
|
|
|
|
let mut scope = self.scope;
|
|
|
|
let mut supertrait_lifetimes = vec![];
|
|
|
|
loop {
|
|
|
|
match scope {
|
|
|
|
Scope::Body { .. } | Scope::Root => {
|
|
|
|
break (vec![], BinderScopeType::Normal);
|
|
|
|
}
|
|
|
|
|
|
|
|
Scope::Elision { s, .. } | Scope::ObjectLifetimeDefault { s, .. } => {
|
|
|
|
scope = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
Scope::Supertrait { s, lifetimes } => {
|
|
|
|
supertrait_lifetimes = lifetimes.clone();
|
|
|
|
scope = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
Scope::TraitRefBoundary { .. } => {
|
|
|
|
// We should only see super trait lifetimes if there is a `Binder` above
|
|
|
|
assert!(supertrait_lifetimes.is_empty());
|
|
|
|
break (vec![], BinderScopeType::Normal);
|
|
|
|
}
|
|
|
|
|
|
|
|
Scope::Binder { hir_id, .. } => {
|
|
|
|
// Nested poly trait refs have the binders concatenated
|
|
|
|
let mut full_binders =
|
|
|
|
self.map.late_bound_vars.entry(*hir_id).or_default().clone();
|
|
|
|
full_binders.extend(supertrait_lifetimes.into_iter());
|
|
|
|
break (full_binders, BinderScopeType::Concatenating);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-09 16:45:26 -05:00
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
|
2021-11-03 18:03:12 -05:00
|
|
|
type NestedFilter = nested_filter::All;
|
2020-01-07 17:25:33 +01:00
|
|
|
|
2021-11-03 18:03:12 -05:00
|
|
|
fn nested_visit_map(&mut self) -> Self::Map {
|
|
|
|
self.tcx.hir()
|
2017-01-11 17:35:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// We want to nest trait/impl items in their parent, but nothing else.
|
|
|
|
fn visit_nested_item(&mut self, _: hir::ItemId) {}
|
|
|
|
|
2021-02-27 21:31:56 -05:00
|
|
|
fn visit_trait_item_ref(&mut self, ii: &'tcx hir::TraitItemRef) {
|
2021-03-12 12:38:42 -05:00
|
|
|
if !self.trait_definition_only {
|
2021-02-27 21:31:56 -05:00
|
|
|
intravisit::walk_trait_item_ref(self, ii)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-11 17:35:54 +02:00
|
|
|
fn visit_nested_body(&mut self, body: hir::BodyId) {
|
2018-12-04 13:45:36 +01:00
|
|
|
let body = self.tcx.hir().body(body);
|
2021-12-04 21:20:58 +01:00
|
|
|
self.with(Scope::Body { id: body.id(), s: self.scope }, |this| {
|
2019-12-24 17:38:22 -05:00
|
|
|
this.visit_body(body);
|
|
|
|
});
|
2016-11-04 18:20:15 -04:00
|
|
|
}
|
|
|
|
|
2020-10-26 14:18:31 -04:00
|
|
|
fn visit_fn(
|
|
|
|
&mut self,
|
|
|
|
fk: intravisit::FnKind<'tcx>,
|
|
|
|
fd: &'tcx hir::FnDecl<'tcx>,
|
|
|
|
b: hir::BodyId,
|
|
|
|
s: rustc_span::Span,
|
|
|
|
hir_id: hir::HirId,
|
|
|
|
) {
|
|
|
|
let name = match fk {
|
2022-02-13 15:40:08 +01:00
|
|
|
intravisit::FnKind::ItemFn(id, _, _) => id.name,
|
|
|
|
intravisit::FnKind::Method(id, _) => id.name,
|
2021-12-15 08:32:21 +11:00
|
|
|
intravisit::FnKind::Closure => sym::closure,
|
2020-10-26 14:18:31 -04:00
|
|
|
};
|
2021-12-15 14:39:23 +11:00
|
|
|
let name = name.as_str();
|
2020-10-26 14:18:31 -04:00
|
|
|
let span = span!(Level::DEBUG, "visit_fn", name);
|
|
|
|
let _enter = span.enter();
|
|
|
|
match fk {
|
|
|
|
// Any `Binders` are handled elsewhere
|
|
|
|
intravisit::FnKind::ItemFn(..) | intravisit::FnKind::Method(..) => {
|
|
|
|
intravisit::walk_fn(self, fk, fd, b, s, hir_id)
|
|
|
|
}
|
|
|
|
intravisit::FnKind::Closure => {
|
|
|
|
self.map.late_bound_vars.insert(hir_id, vec![]);
|
|
|
|
let scope = Scope::Binder {
|
|
|
|
hir_id,
|
2021-02-18 21:01:44 +01:00
|
|
|
lifetimes: FxIndexMap::default(),
|
2020-10-26 14:18:31 -04:00
|
|
|
next_early_index: self.next_early_index(),
|
|
|
|
s: self.scope,
|
|
|
|
opaque_type_parent: false,
|
2021-04-21 12:26:19 -04:00
|
|
|
scope_type: BinderScopeType::Normal,
|
2022-03-18 22:04:01 +03:00
|
|
|
allow_late_bound: true,
|
2020-10-26 14:18:31 -04:00
|
|
|
};
|
2021-12-04 21:20:58 +01:00
|
|
|
self.with(scope, move |this| intravisit::walk_fn(this, fk, fd, b, s, hir_id));
|
2020-10-26 14:18:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 19:28:50 +01:00
|
|
|
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
|
2020-10-26 14:18:31 -04:00
|
|
|
match &item.kind {
|
|
|
|
hir::ItemKind::Impl(hir::Impl { of_trait, .. }) => {
|
|
|
|
if let Some(of_trait) = of_trait {
|
|
|
|
self.map.late_bound_vars.insert(of_trait.hir_ref_id, Vec::default());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2019-09-26 17:51:36 +01:00
|
|
|
match item.kind {
|
2022-05-26 08:59:15 +02:00
|
|
|
hir::ItemKind::Fn(_, ref generics, _) => {
|
2020-01-27 11:26:06 -08:00
|
|
|
self.missing_named_lifetime_spots.push(generics.into());
|
2022-05-26 08:59:15 +02:00
|
|
|
self.visit_early_late(None, item.hir_id(), generics, |this| {
|
2022-02-17 16:00:04 +00:00
|
|
|
intravisit::walk_item(this, item);
|
|
|
|
});
|
2020-01-15 18:34:30 -08:00
|
|
|
self.missing_named_lifetime_spots.pop();
|
2017-01-11 17:35:54 +02:00
|
|
|
}
|
2017-12-01 10:01:23 -02:00
|
|
|
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::ExternCrate(_)
|
|
|
|
| hir::ItemKind::Use(..)
|
2021-07-30 23:50:57 -07:00
|
|
|
| hir::ItemKind::Macro(..)
|
2018-07-11 23:36:06 +08:00
|
|
|
| hir::ItemKind::Mod(..)
|
2020-11-11 22:40:09 +01:00
|
|
|
| hir::ItemKind::ForeignMod { .. }
|
2018-07-11 23:36:06 +08:00
|
|
|
| hir::ItemKind::GlobalAsm(..) => {
|
2017-01-11 17:35:54 +02:00
|
|
|
// These sorts of items have no lifetime parameters at all.
|
|
|
|
intravisit::walk_item(self, item);
|
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Static(..) | hir::ItemKind::Const(..) => {
|
2017-01-13 15:09:56 +02:00
|
|
|
// No lifetime parameters, but implied 'static.
|
2019-12-24 17:38:22 -05:00
|
|
|
let scope = Scope::Elision { elide: Elide::Exact(Region::Static), s: ROOT_SCOPE };
|
2021-12-04 21:20:58 +01:00
|
|
|
self.with(scope, |this| intravisit::walk_item(this, item));
|
2017-01-13 15:09:56 +02:00
|
|
|
}
|
2020-05-10 11:57:58 +01:00
|
|
|
hir::ItemKind::OpaqueTy(hir::OpaqueTy { .. }) => {
|
2020-06-07 18:56:17 +01:00
|
|
|
// Opaque types are visited when we visit the
|
|
|
|
// `TyKind::OpaqueDef`, so that they have the lifetimes from
|
|
|
|
// their parent opaque_ty in scope.
|
2021-03-12 12:38:42 -05:00
|
|
|
//
|
|
|
|
// The core idea here is that since OpaqueTys are generated with the impl Trait as
|
|
|
|
// their owner, we can keep going until we find the Item that owns that. We then
|
|
|
|
// conservatively add all resolved lifetimes. Otherwise we run into problems in
|
|
|
|
// cases like `type Foo<'a> = impl Bar<As = impl Baz + 'a>`.
|
2021-02-27 21:31:56 -05:00
|
|
|
for (_hir_id, node) in
|
|
|
|
self.tcx.hir().parent_iter(self.tcx.hir().local_def_id_to_hir_id(item.def_id))
|
|
|
|
{
|
|
|
|
match node {
|
|
|
|
hir::Node::Item(parent_item) => {
|
|
|
|
let resolved_lifetimes: &ResolveLifetimes =
|
|
|
|
self.tcx.resolve_lifetimes(item_for(self.tcx, parent_item.def_id));
|
|
|
|
// We need to add *all* deps, since opaque tys may want them from *us*
|
|
|
|
for (&owner, defs) in resolved_lifetimes.defs.iter() {
|
|
|
|
defs.iter().for_each(|(&local_id, region)| {
|
2021-02-18 21:01:44 +01:00
|
|
|
self.map.defs.insert(hir::HirId { owner, local_id }, *region);
|
2021-02-27 21:31:56 -05:00
|
|
|
});
|
|
|
|
}
|
2020-10-26 14:18:31 -04:00
|
|
|
for (&owner, late_bound_vars) in
|
|
|
|
resolved_lifetimes.late_bound_vars.iter()
|
|
|
|
{
|
|
|
|
late_bound_vars.iter().for_each(|(&local_id, late_bound_vars)| {
|
|
|
|
self.map.late_bound_vars.insert(
|
|
|
|
hir::HirId { owner, local_id },
|
|
|
|
late_bound_vars.clone(),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2021-02-27 21:31:56 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
hir::Node::Crate(_) => bug!("No Item about an OpaqueTy"),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2018-05-22 14:31:56 +02:00
|
|
|
}
|
2019-08-02 11:02:08 +01:00
|
|
|
hir::ItemKind::TyAlias(_, ref generics)
|
2018-07-11 23:36:06 +08:00
|
|
|
| hir::ItemKind::Enum(_, ref generics)
|
|
|
|
| hir::ItemKind::Struct(_, ref generics)
|
|
|
|
| hir::ItemKind::Union(_, ref generics)
|
|
|
|
| hir::ItemKind::Trait(_, _, ref generics, ..)
|
|
|
|
| hir::ItemKind::TraitAlias(ref generics, ..)
|
2020-11-22 17:46:21 -05:00
|
|
|
| hir::ItemKind::Impl(hir::Impl { ref generics, .. }) => {
|
2020-01-27 11:26:06 -08:00
|
|
|
self.missing_named_lifetime_spots.push(generics.into());
|
2020-01-15 18:34:30 -08:00
|
|
|
|
2018-11-01 21:52:56 +00:00
|
|
|
// These kinds of items have only early-bound lifetime parameters.
|
2019-09-26 17:51:36 +01:00
|
|
|
let mut index = if sub_items_have_self_param(&item.kind) {
|
2017-01-11 17:35:54 +02:00
|
|
|
1 // Self comes before lifetimes
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
2019-02-15 22:25:42 +00:00
|
|
|
let mut non_lifetime_count = 0;
|
2019-12-24 17:38:22 -05:00
|
|
|
let lifetimes = generics
|
|
|
|
.params
|
|
|
|
.iter()
|
|
|
|
.filter_map(|param| match param.kind {
|
|
|
|
GenericParamKind::Lifetime { .. } => {
|
2022-02-10 13:04:59 +01:00
|
|
|
Some(Region::early(self.tcx.hir(), &mut index, param))
|
2019-12-24 17:38:22 -05:00
|
|
|
}
|
|
|
|
GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => {
|
|
|
|
non_lifetime_count += 1;
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2020-10-26 14:18:31 -04:00
|
|
|
self.map.late_bound_vars.insert(item.hir_id(), vec![]);
|
2017-01-11 17:35:54 +02:00
|
|
|
let scope = Scope::Binder {
|
2020-10-26 14:18:31 -04:00
|
|
|
hir_id: item.hir_id(),
|
2017-07-03 11:19:51 -07:00
|
|
|
lifetimes,
|
2019-02-15 22:25:42 +00:00
|
|
|
next_early_index: index + non_lifetime_count,
|
2019-08-02 00:05:19 +01:00
|
|
|
opaque_type_parent: true,
|
2021-04-21 12:26:19 -04:00
|
|
|
scope_type: BinderScopeType::Normal,
|
2017-12-11 09:00:05 -05:00
|
|
|
s: ROOT_SCOPE,
|
2022-03-18 22:04:01 +03:00
|
|
|
allow_late_bound: false,
|
2017-01-11 17:35:54 +02:00
|
|
|
};
|
2021-12-04 21:20:58 +01:00
|
|
|
self.with(scope, |this| {
|
2021-04-12 09:26:39 -04:00
|
|
|
let scope = Scope::TraitRefBoundary { s: this.scope };
|
2021-12-04 21:20:58 +01:00
|
|
|
this.with(scope, |this| {
|
2021-04-12 09:26:39 -04:00
|
|
|
intravisit::walk_item(this, item);
|
|
|
|
});
|
2017-01-11 17:35:54 +02:00
|
|
|
});
|
2020-01-15 18:34:30 -08:00
|
|
|
self.missing_named_lifetime_spots.pop();
|
2014-11-15 17:25:05 -05:00
|
|
|
}
|
2017-01-11 17:35:54 +02:00
|
|
|
}
|
2013-10-28 17:37:10 -04:00
|
|
|
}
|
|
|
|
|
2019-11-28 20:18:29 +01:00
|
|
|
fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem<'tcx>) {
|
2019-09-26 17:58:14 +01:00
|
|
|
match item.kind {
|
2022-05-26 08:59:15 +02:00
|
|
|
hir::ForeignItemKind::Fn(_, _, ref generics) => {
|
|
|
|
self.visit_early_late(None, item.hir_id(), generics, |this| {
|
2018-03-21 16:59:28 -04:00
|
|
|
intravisit::walk_foreign_item(this, item);
|
2022-02-17 16:00:04 +00:00
|
|
|
})
|
|
|
|
}
|
2018-07-11 22:56:44 +08:00
|
|
|
hir::ForeignItemKind::Static(..) => {
|
2017-01-11 17:35:54 +02:00
|
|
|
intravisit::walk_foreign_item(self, item);
|
2013-10-28 17:37:10 -04:00
|
|
|
}
|
2018-07-11 22:56:44 +08:00
|
|
|
hir::ForeignItemKind::Type => {
|
2017-09-03 19:53:58 +01:00
|
|
|
intravisit::walk_foreign_item(self, item);
|
|
|
|
}
|
2013-10-28 17:37:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-27 21:31:56 -05:00
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
2019-11-30 17:46:46 +01:00
|
|
|
fn visit_ty(&mut self, ty: &'tcx hir::Ty<'tcx>) {
|
2019-09-26 17:25:31 +01:00
|
|
|
match ty.kind {
|
2018-07-11 22:41:03 +08:00
|
|
|
hir::TyKind::BareFn(ref c) => {
|
2017-10-15 13:43:06 -07:00
|
|
|
let next_early_index = self.next_early_index();
|
2020-04-24 20:03:45 -07:00
|
|
|
let lifetime_span: Option<Span> =
|
|
|
|
c.generic_params.iter().rev().find_map(|param| match param.kind {
|
2020-01-27 16:13:45 -08:00
|
|
|
GenericParamKind::Lifetime { .. } => Some(param.span),
|
|
|
|
_ => None,
|
2020-04-24 20:03:45 -07:00
|
|
|
});
|
2020-01-27 16:13:45 -08:00
|
|
|
let (span, span_type) = if let Some(span) = lifetime_span {
|
|
|
|
(span.shrink_to_hi(), ForLifetimeSpanType::TypeTail)
|
|
|
|
} else {
|
|
|
|
(ty.span.shrink_to_lo(), ForLifetimeSpanType::TypeEmpty)
|
|
|
|
};
|
|
|
|
self.missing_named_lifetime_spots
|
2020-01-27 17:33:13 -08:00
|
|
|
.push(MissingLifetimeSpot::HigherRanked { span, span_type });
|
2022-04-27 22:15:58 +02:00
|
|
|
let (lifetimes, binders): (FxIndexMap<LocalDefId, Region>, Vec<_>) = c
|
2020-10-26 14:18:31 -04:00
|
|
|
.generic_params
|
|
|
|
.iter()
|
2021-11-06 16:36:23 +01:00
|
|
|
.filter(|param| matches!(param.kind, GenericParamKind::Lifetime { .. }))
|
2021-03-26 17:40:15 -04:00
|
|
|
.enumerate()
|
|
|
|
.map(|(late_bound_idx, param)| {
|
2022-02-10 13:04:59 +01:00
|
|
|
let pair = Region::late(late_bound_idx as u32, self.tcx.hir(), param);
|
2021-03-26 17:40:15 -04:00
|
|
|
let r = late_region_as_bound_region(self.tcx, &pair.1);
|
|
|
|
(pair, r)
|
|
|
|
})
|
2020-10-26 14:18:31 -04:00
|
|
|
.unzip();
|
|
|
|
self.map.late_bound_vars.insert(ty.hir_id, binders);
|
2017-01-11 17:35:54 +02:00
|
|
|
let scope = Scope::Binder {
|
2020-10-26 14:18:31 -04:00
|
|
|
hir_id: ty.hir_id,
|
|
|
|
lifetimes,
|
2017-12-11 09:00:05 -05:00
|
|
|
s: self.scope,
|
2017-11-23 08:05:58 -05:00
|
|
|
next_early_index,
|
2019-08-02 00:05:19 +01:00
|
|
|
opaque_type_parent: false,
|
2021-04-21 12:26:19 -04:00
|
|
|
scope_type: BinderScopeType::Normal,
|
2022-03-18 22:04:01 +03:00
|
|
|
allow_late_bound: true,
|
2017-01-08 22:40:04 +02:00
|
|
|
};
|
2021-12-04 21:20:58 +01:00
|
|
|
self.with(scope, |this| {
|
2014-11-15 16:47:59 -05:00
|
|
|
// a bare fn has no bounds, so everything
|
|
|
|
// contained within is scoped within its binder.
|
2015-11-17 17:51:44 -05:00
|
|
|
intravisit::walk_ty(this, ty);
|
2014-11-15 16:47:59 -05:00
|
|
|
});
|
2020-01-27 16:13:45 -08:00
|
|
|
self.missing_named_lifetime_spots.pop();
|
2014-11-15 16:47:59 -05:00
|
|
|
}
|
2021-03-13 15:44:29 +03:00
|
|
|
hir::TyKind::TraitObject(bounds, ref lifetime, _) => {
|
2021-02-27 21:31:56 -05:00
|
|
|
debug!(?bounds, ?lifetime, "TraitObject");
|
2021-04-05 00:10:09 -04:00
|
|
|
let scope = Scope::TraitRefBoundary { s: self.scope };
|
2021-12-04 21:20:58 +01:00
|
|
|
self.with(scope, |this| {
|
2021-04-05 00:10:09 -04:00
|
|
|
for bound in bounds {
|
|
|
|
this.visit_poly_trait_ref(bound, hir::TraitBoundModifier::None);
|
|
|
|
}
|
|
|
|
});
|
2018-03-12 16:31:34 -04:00
|
|
|
match lifetime.name {
|
2022-03-10 23:12:35 +01:00
|
|
|
LifetimeName::Implicit => {
|
2019-08-08 03:36:24 -04:00
|
|
|
// For types like `dyn Foo`, we should
|
|
|
|
// generate a special form of elided.
|
2020-03-06 12:13:55 +01:00
|
|
|
span_bug!(ty.span, "object-lifetime-default expected, not implicit",);
|
2019-08-08 03:36:24 -04:00
|
|
|
}
|
|
|
|
LifetimeName::ImplicitObjectLifetimeDefault => {
|
2018-03-12 16:31:34 -04:00
|
|
|
// If the user does not write *anything*, we
|
|
|
|
// use the object lifetime defaulting
|
2018-11-27 02:59:49 +00:00
|
|
|
// rules. So e.g., `Box<dyn Debug>` becomes
|
2018-03-12 16:31:34 -04:00
|
|
|
// `Box<dyn Debug + 'static>`.
|
|
|
|
self.resolve_object_lifetime_default(lifetime)
|
|
|
|
}
|
|
|
|
LifetimeName::Underscore => {
|
|
|
|
// If the user writes `'_`, we use the *ordinary* elision
|
2018-11-27 02:59:49 +00:00
|
|
|
// rules. So the `'_` in e.g., `Box<dyn Debug + '_>` will be
|
2018-03-12 16:31:34 -04:00
|
|
|
// resolved the same as the `'_` in `&'_ Foo`.
|
|
|
|
//
|
|
|
|
// cc #48468
|
2020-11-15 17:06:58 -05:00
|
|
|
self.resolve_elided_lifetimes(&[lifetime])
|
2018-03-12 16:31:34 -04:00
|
|
|
}
|
2022-04-27 22:15:58 +02:00
|
|
|
LifetimeName::Param(..) | LifetimeName::Static => {
|
2018-03-12 16:31:34 -04:00
|
|
|
// If the user wrote an explicit name, use that.
|
|
|
|
self.visit_lifetime(lifetime);
|
|
|
|
}
|
2018-10-18 05:39:53 -04:00
|
|
|
LifetimeName::Error => {}
|
2017-01-24 17:17:06 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-11 22:41:03 +08:00
|
|
|
hir::TyKind::Rptr(ref lifetime_ref, ref mt) => {
|
2017-01-25 17:32:44 +02:00
|
|
|
self.visit_lifetime(lifetime_ref);
|
|
|
|
let scope = Scope::ObjectLifetimeDefault {
|
2019-02-18 10:59:17 +01:00
|
|
|
lifetime: self.map.defs.get(&lifetime_ref.hir_id).cloned(),
|
2017-12-11 09:00:05 -05:00
|
|
|
s: self.scope,
|
2017-01-25 17:32:44 +02:00
|
|
|
};
|
2021-12-04 21:20:58 +01:00
|
|
|
self.with(scope, |this| this.visit_ty(&mt.ty));
|
2017-01-25 17:32:44 +02:00
|
|
|
}
|
2020-06-07 18:56:17 +01:00
|
|
|
hir::TyKind::OpaqueDef(item_id, lifetimes) => {
|
2018-10-02 10:54:34 +02:00
|
|
|
// Resolve the lifetimes in the bounds to the lifetime defs in the generics.
|
|
|
|
// `fn foo<'a>() -> impl MyTrait<'a> { ... }` desugars to
|
2019-08-02 00:05:19 +01:00
|
|
|
// `type MyAnonTy<'b> = impl MyTrait<'b>;`
|
|
|
|
// ^ ^ this gets resolved in the scope of
|
|
|
|
// the opaque_ty generics
|
2021-01-30 12:06:04 +01:00
|
|
|
let opaque_ty = self.tcx.hir().item(item_id);
|
2020-05-10 11:57:58 +01:00
|
|
|
let (generics, bounds) = match opaque_ty.kind {
|
2019-08-01 00:41:54 +01:00
|
|
|
// Named opaque `impl Trait` types are reached via `TyKind::Path`.
|
|
|
|
// This arm is for `impl Trait` in the types of statics, constants and locals.
|
2021-11-30 19:11:35 +01:00
|
|
|
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
|
|
|
|
origin: hir::OpaqueTyOrigin::TyAlias,
|
|
|
|
..
|
|
|
|
}) => {
|
2018-10-02 10:54:34 +02:00
|
|
|
intravisit::walk_ty(self, ty);
|
2020-05-10 12:18:55 +01:00
|
|
|
|
|
|
|
// Elided lifetimes are not allowed in non-return
|
|
|
|
// position impl Trait
|
2021-04-12 09:26:39 -04:00
|
|
|
let scope = Scope::TraitRefBoundary { s: self.scope };
|
2021-12-04 21:20:58 +01:00
|
|
|
self.with(scope, |this| {
|
2021-04-12 09:26:39 -04:00
|
|
|
let scope = Scope::Elision { elide: Elide::Forbid, s: this.scope };
|
2021-12-04 21:20:58 +01:00
|
|
|
this.with(scope, |this| {
|
2021-04-12 09:26:39 -04:00
|
|
|
intravisit::walk_item(this, opaque_ty);
|
|
|
|
})
|
2020-05-10 12:18:55 +01:00
|
|
|
});
|
|
|
|
|
2018-10-02 10:54:34 +02:00
|
|
|
return;
|
2018-10-18 05:39:53 -04:00
|
|
|
}
|
2018-10-02 10:54:34 +02:00
|
|
|
// RPIT (return position impl trait)
|
2020-05-10 11:57:58 +01:00
|
|
|
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
|
2021-11-30 19:11:35 +01:00
|
|
|
origin: hir::OpaqueTyOrigin::FnReturn(..) | hir::OpaqueTyOrigin::AsyncFn(..),
|
2020-05-10 11:57:58 +01:00
|
|
|
ref generics,
|
|
|
|
bounds,
|
|
|
|
..
|
|
|
|
}) => (generics, bounds),
|
2019-08-01 00:41:54 +01:00
|
|
|
ref i => bug!("`impl Trait` pointed to non-opaque type?? {:#?}", i),
|
2018-10-02 10:54:34 +02:00
|
|
|
};
|
2018-07-03 19:38:14 +02:00
|
|
|
|
2019-08-01 00:41:54 +01:00
|
|
|
// Resolve the lifetimes that are applied to the opaque type.
|
2018-10-02 10:54:34 +02:00
|
|
|
// These are resolved in the current scope.
|
|
|
|
// `fn foo<'a>() -> impl MyTrait<'a> { ... }` desugars to
|
|
|
|
// `fn foo<'a>() -> MyAnonTy<'a> { ... }`
|
|
|
|
// ^ ^this gets resolved in the current scope
|
|
|
|
for lifetime in lifetimes {
|
2022-01-18 20:26:13 +01:00
|
|
|
let hir::GenericArg::Lifetime(lifetime) = lifetime else {
|
|
|
|
continue
|
|
|
|
};
|
|
|
|
self.visit_lifetime(lifetime);
|
|
|
|
|
|
|
|
// Check for predicates like `impl for<'a> Trait<impl OtherTrait<'a>>`
|
|
|
|
// and ban them. Type variables instantiated inside binders aren't
|
|
|
|
// well-supported at the moment, so this doesn't work.
|
|
|
|
// In the future, this should be fixed and this error should be removed.
|
|
|
|
let def = self.map.defs.get(&lifetime.hir_id).cloned();
|
2022-02-20 10:22:57 -08:00
|
|
|
let Some(Region::LateBound(_, _, def_id)) = def else {
|
2022-01-18 20:26:13 +01:00
|
|
|
continue
|
|
|
|
};
|
|
|
|
let Some(def_id) = def_id.as_local() else {
|
|
|
|
continue
|
|
|
|
};
|
|
|
|
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
|
|
|
|
// Ensure that the parent of the def is an item, not HRTB
|
|
|
|
let parent_id = self.tcx.hir().get_parent_node(hir_id);
|
2021-09-22 19:28:20 +02:00
|
|
|
if !parent_id.is_owner() {
|
2022-01-18 20:26:13 +01:00
|
|
|
if !self.trait_definition_only {
|
|
|
|
struct_span_err!(
|
|
|
|
self.tcx.sess,
|
|
|
|
lifetime.span,
|
|
|
|
E0657,
|
|
|
|
"`impl Trait` can only capture lifetimes \
|
|
|
|
bound at the fn or impl level"
|
|
|
|
)
|
|
|
|
.emit();
|
2017-10-15 13:43:06 -07:00
|
|
|
}
|
2022-01-18 20:26:13 +01:00
|
|
|
self.uninsert_lifetime_on_error(lifetime, def.unwrap());
|
2017-10-15 13:43:06 -07:00
|
|
|
}
|
2022-05-14 11:25:53 +02:00
|
|
|
if let hir::Node::Item(hir::Item {
|
|
|
|
kind: hir::ItemKind::OpaqueTy { .. }, ..
|
|
|
|
}) = self.tcx.hir().get(parent_id)
|
|
|
|
{
|
|
|
|
if !self.trait_definition_only {
|
|
|
|
let mut err = self.tcx.sess.struct_span_err(
|
|
|
|
lifetime.span,
|
|
|
|
"higher kinded lifetime bounds on nested opaque types are not supported yet",
|
|
|
|
);
|
|
|
|
err.span_note(self.tcx.def_span(def_id), "lifetime declared here");
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
self.uninsert_lifetime_on_error(lifetime, def.unwrap());
|
|
|
|
}
|
2018-10-02 10:54:34 +02:00
|
|
|
}
|
2017-10-15 13:43:06 -07:00
|
|
|
|
2018-10-02 10:54:34 +02:00
|
|
|
// We want to start our early-bound indices at the end of the parent scope,
|
|
|
|
// not including any parent `impl Trait`s.
|
2019-08-02 00:05:19 +01:00
|
|
|
let mut index = self.next_early_index_for_opaque_type();
|
2021-02-27 21:31:56 -05:00
|
|
|
debug!(?index);
|
2018-10-02 10:54:34 +02:00
|
|
|
|
|
|
|
let mut elision = None;
|
2021-02-18 21:01:44 +01:00
|
|
|
let mut lifetimes = FxIndexMap::default();
|
2019-02-15 22:25:42 +00:00
|
|
|
let mut non_lifetime_count = 0;
|
2019-12-01 17:10:12 +01:00
|
|
|
for param in generics.params {
|
2018-10-02 10:54:34 +02:00
|
|
|
match param.kind {
|
|
|
|
GenericParamKind::Lifetime { .. } => {
|
2022-04-27 22:15:58 +02:00
|
|
|
let (def_id, reg) = Region::early(self.tcx.hir(), &mut index, ¶m);
|
2020-12-04 08:06:53 +03:00
|
|
|
if let hir::ParamName::Plain(Ident {
|
|
|
|
name: kw::UnderscoreLifetime,
|
|
|
|
..
|
2022-04-27 22:15:58 +02:00
|
|
|
}) = param.name
|
2020-12-04 08:06:53 +03:00
|
|
|
{
|
|
|
|
// Pick the elided lifetime "definition" if one exists
|
|
|
|
// and use it to make an elision scope.
|
|
|
|
elision = Some(reg);
|
2018-10-02 10:54:34 +02:00
|
|
|
} else {
|
2022-04-27 22:15:58 +02:00
|
|
|
lifetimes.insert(def_id, reg);
|
2018-06-29 10:58:17 +02:00
|
|
|
}
|
2018-05-26 13:11:39 +01:00
|
|
|
}
|
2019-12-24 17:38:22 -05:00
|
|
|
GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => {
|
2019-02-15 22:25:42 +00:00
|
|
|
non_lifetime_count += 1;
|
2018-10-02 10:54:34 +02:00
|
|
|
}
|
2017-10-15 13:43:06 -07:00
|
|
|
}
|
2018-10-02 10:54:34 +02:00
|
|
|
}
|
2019-02-15 22:25:42 +00:00
|
|
|
let next_early_index = index + non_lifetime_count;
|
2020-10-26 14:18:31 -04:00
|
|
|
self.map.late_bound_vars.insert(ty.hir_id, vec![]);
|
2017-12-09 21:54:58 -08:00
|
|
|
|
2018-10-02 10:54:34 +02:00
|
|
|
if let Some(elision_region) = elision {
|
2019-12-24 17:38:22 -05:00
|
|
|
let scope =
|
|
|
|
Scope::Elision { elide: Elide::Exact(elision_region), s: self.scope };
|
2021-12-04 21:20:58 +01:00
|
|
|
self.with(scope, |this| {
|
2018-01-17 17:22:40 -08:00
|
|
|
let scope = Scope::Binder {
|
2020-10-26 14:18:31 -04:00
|
|
|
hir_id: ty.hir_id,
|
2018-01-17 17:22:40 -08:00
|
|
|
lifetimes,
|
|
|
|
next_early_index,
|
2018-10-02 10:54:34 +02:00
|
|
|
s: this.scope,
|
2019-08-02 00:05:19 +01:00
|
|
|
opaque_type_parent: false,
|
2021-04-21 12:26:19 -04:00
|
|
|
scope_type: BinderScopeType::Normal,
|
2022-03-18 22:04:01 +03:00
|
|
|
allow_late_bound: false,
|
2018-01-17 17:22:40 -08:00
|
|
|
};
|
2021-12-04 21:20:58 +01:00
|
|
|
this.with(scope, |this| {
|
2017-12-09 21:54:58 -08:00
|
|
|
this.visit_generics(generics);
|
2021-04-05 00:10:09 -04:00
|
|
|
let scope = Scope::TraitRefBoundary { s: this.scope };
|
2021-12-04 21:20:58 +01:00
|
|
|
this.with(scope, |this| {
|
2021-04-05 00:10:09 -04:00
|
|
|
for bound in bounds {
|
|
|
|
this.visit_param_bound(bound);
|
|
|
|
}
|
|
|
|
})
|
2017-12-09 21:54:58 -08:00
|
|
|
});
|
2018-10-02 10:54:34 +02:00
|
|
|
});
|
2017-12-09 21:54:58 -08:00
|
|
|
} else {
|
2018-10-02 10:54:34 +02:00
|
|
|
let scope = Scope::Binder {
|
2020-10-26 14:18:31 -04:00
|
|
|
hir_id: ty.hir_id,
|
2018-10-02 10:54:34 +02:00
|
|
|
lifetimes,
|
|
|
|
next_early_index,
|
|
|
|
s: self.scope,
|
2019-08-02 00:05:19 +01:00
|
|
|
opaque_type_parent: false,
|
2021-04-21 12:26:19 -04:00
|
|
|
scope_type: BinderScopeType::Normal,
|
2022-03-18 22:04:01 +03:00
|
|
|
allow_late_bound: false,
|
2018-10-02 10:54:34 +02:00
|
|
|
};
|
2021-12-04 21:20:58 +01:00
|
|
|
self.with(scope, |this| {
|
2021-04-05 00:10:09 -04:00
|
|
|
let scope = Scope::TraitRefBoundary { s: this.scope };
|
2021-12-04 21:20:58 +01:00
|
|
|
this.with(scope, |this| {
|
2021-04-05 00:10:09 -04:00
|
|
|
this.visit_generics(generics);
|
|
|
|
for bound in bounds {
|
|
|
|
this.visit_param_bound(bound);
|
|
|
|
}
|
|
|
|
})
|
2018-10-02 10:54:34 +02:00
|
|
|
});
|
2017-12-09 21:54:58 -08:00
|
|
|
}
|
2017-10-15 13:43:06 -07:00
|
|
|
}
|
2017-12-11 09:00:05 -05:00
|
|
|
_ => intravisit::walk_ty(self, ty),
|
2014-11-15 16:47:59 -05:00
|
|
|
}
|
2013-10-28 17:37:10 -04:00
|
|
|
}
|
|
|
|
|
2019-11-28 21:47:10 +01:00
|
|
|
fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
|
2017-12-12 21:11:15 -05:00
|
|
|
use self::hir::TraitItemKind::*;
|
2019-09-26 17:07:54 +01:00
|
|
|
match trait_item.kind {
|
2022-05-26 08:59:15 +02:00
|
|
|
Fn(_, _) => {
|
2020-08-10 13:26:16 -07:00
|
|
|
self.missing_named_lifetime_spots.push((&trait_item.generics).into());
|
2017-12-12 21:11:15 -05:00
|
|
|
let tcx = self.tcx;
|
|
|
|
self.visit_early_late(
|
2021-10-21 19:41:47 +02:00
|
|
|
Some(tcx.hir().get_parent_item(trait_item.hir_id())),
|
2020-10-26 14:18:31 -04:00
|
|
|
trait_item.hir_id(),
|
2017-12-12 21:11:15 -05:00
|
|
|
&trait_item.generics,
|
|
|
|
|this| intravisit::walk_trait_item(this, trait_item),
|
|
|
|
);
|
2020-08-10 13:26:16 -07:00
|
|
|
self.missing_named_lifetime_spots.pop();
|
2018-03-21 16:59:28 -04:00
|
|
|
}
|
2019-12-01 00:17:43 +01:00
|
|
|
Type(bounds, ref ty) => {
|
2020-08-10 13:26:16 -07:00
|
|
|
self.missing_named_lifetime_spots.push((&trait_item.generics).into());
|
2017-12-12 21:11:15 -05:00
|
|
|
let generics = &trait_item.generics;
|
|
|
|
let mut index = self.next_early_index();
|
|
|
|
debug!("visit_ty: index = {}", index);
|
2019-02-15 22:25:42 +00:00
|
|
|
let mut non_lifetime_count = 0;
|
2019-12-24 17:38:22 -05:00
|
|
|
let lifetimes = generics
|
|
|
|
.params
|
|
|
|
.iter()
|
|
|
|
.filter_map(|param| match param.kind {
|
|
|
|
GenericParamKind::Lifetime { .. } => {
|
2022-02-10 13:04:59 +01:00
|
|
|
Some(Region::early(self.tcx.hir(), &mut index, param))
|
2019-12-24 17:38:22 -05:00
|
|
|
}
|
|
|
|
GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => {
|
|
|
|
non_lifetime_count += 1;
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2020-10-26 14:18:31 -04:00
|
|
|
self.map.late_bound_vars.insert(trait_item.hir_id(), vec![]);
|
2018-01-17 17:22:40 -08:00
|
|
|
let scope = Scope::Binder {
|
2020-10-26 14:18:31 -04:00
|
|
|
hir_id: trait_item.hir_id(),
|
2018-01-17 17:22:40 -08:00
|
|
|
lifetimes,
|
2019-02-15 22:25:42 +00:00
|
|
|
next_early_index: index + non_lifetime_count,
|
2018-01-17 17:22:40 -08:00
|
|
|
s: self.scope,
|
2019-08-02 00:05:19 +01:00
|
|
|
opaque_type_parent: true,
|
2021-04-21 12:26:19 -04:00
|
|
|
scope_type: BinderScopeType::Normal,
|
2022-03-18 22:04:01 +03:00
|
|
|
allow_late_bound: false,
|
2018-01-17 17:22:40 -08:00
|
|
|
};
|
2021-12-04 21:20:58 +01:00
|
|
|
self.with(scope, |this| {
|
2021-04-05 00:10:09 -04:00
|
|
|
let scope = Scope::TraitRefBoundary { s: this.scope };
|
2021-12-04 21:20:58 +01:00
|
|
|
this.with(scope, |this| {
|
2021-04-05 00:10:09 -04:00
|
|
|
this.visit_generics(generics);
|
|
|
|
for bound in bounds {
|
|
|
|
this.visit_param_bound(bound);
|
|
|
|
}
|
|
|
|
if let Some(ty) = ty {
|
|
|
|
this.visit_ty(ty);
|
|
|
|
}
|
|
|
|
})
|
2017-12-12 21:11:15 -05:00
|
|
|
});
|
2020-08-10 13:26:16 -07:00
|
|
|
self.missing_named_lifetime_spots.pop();
|
2018-03-21 16:59:28 -04:00
|
|
|
}
|
2017-12-12 21:11:15 -05:00
|
|
|
Const(_, _) => {
|
|
|
|
// Only methods and types support generics.
|
2017-10-16 21:07:26 +02:00
|
|
|
assert!(trait_item.generics.params.is_empty());
|
2020-08-11 13:02:14 -07:00
|
|
|
self.missing_named_lifetime_spots.push(MissingLifetimeSpot::Static);
|
2017-12-12 21:11:15 -05:00
|
|
|
intravisit::walk_trait_item(self, trait_item);
|
2020-08-11 13:02:14 -07:00
|
|
|
self.missing_named_lifetime_spots.pop();
|
2018-03-21 16:59:28 -04:00
|
|
|
}
|
2015-03-10 12:28:44 +02:00
|
|
|
}
|
2017-01-11 17:35:54 +02:00
|
|
|
}
|
2015-04-03 02:51:38 +02:00
|
|
|
|
2019-11-28 22:16:44 +01:00
|
|
|
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
|
2017-12-12 21:11:15 -05:00
|
|
|
use self::hir::ImplItemKind::*;
|
2019-09-26 16:38:13 +01:00
|
|
|
match impl_item.kind {
|
2022-05-26 08:59:15 +02:00
|
|
|
Fn(..) => {
|
2020-08-11 13:02:14 -07:00
|
|
|
self.missing_named_lifetime_spots.push((&impl_item.generics).into());
|
2017-12-12 21:11:15 -05:00
|
|
|
let tcx = self.tcx;
|
|
|
|
self.visit_early_late(
|
2021-10-21 19:41:47 +02:00
|
|
|
Some(tcx.hir().get_parent_item(impl_item.hir_id())),
|
2020-10-26 14:18:31 -04:00
|
|
|
impl_item.hir_id(),
|
2017-12-12 21:11:15 -05:00
|
|
|
&impl_item.generics,
|
|
|
|
|this| intravisit::walk_impl_item(this, impl_item),
|
2020-08-11 13:02:14 -07:00
|
|
|
);
|
|
|
|
self.missing_named_lifetime_spots.pop();
|
2018-03-21 16:59:28 -04:00
|
|
|
}
|
2019-08-02 20:59:07 +01:00
|
|
|
TyAlias(ref ty) => {
|
2017-12-12 21:11:15 -05:00
|
|
|
let generics = &impl_item.generics;
|
2020-08-11 13:02:14 -07:00
|
|
|
self.missing_named_lifetime_spots.push(generics.into());
|
2017-12-12 21:11:15 -05:00
|
|
|
let mut index = self.next_early_index();
|
2019-02-15 22:25:42 +00:00
|
|
|
let mut non_lifetime_count = 0;
|
2017-12-12 21:11:15 -05:00
|
|
|
debug!("visit_ty: index = {}", index);
|
2022-04-27 22:15:58 +02:00
|
|
|
let lifetimes: FxIndexMap<LocalDefId, Region> = generics
|
2019-12-24 17:38:22 -05:00
|
|
|
.params
|
|
|
|
.iter()
|
|
|
|
.filter_map(|param| match param.kind {
|
|
|
|
GenericParamKind::Lifetime { .. } => {
|
2022-02-10 13:04:59 +01:00
|
|
|
Some(Region::early(self.tcx.hir(), &mut index, param))
|
2019-12-24 17:38:22 -05:00
|
|
|
}
|
|
|
|
GenericParamKind::Const { .. } | GenericParamKind::Type { .. } => {
|
|
|
|
non_lifetime_count += 1;
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2020-10-26 14:18:31 -04:00
|
|
|
self.map.late_bound_vars.insert(ty.hir_id, vec![]);
|
2018-01-17 17:22:40 -08:00
|
|
|
let scope = Scope::Binder {
|
2020-10-26 14:18:31 -04:00
|
|
|
hir_id: ty.hir_id,
|
2018-01-17 17:22:40 -08:00
|
|
|
lifetimes,
|
2019-02-15 22:25:42 +00:00
|
|
|
next_early_index: index + non_lifetime_count,
|
2018-01-17 17:22:40 -08:00
|
|
|
s: self.scope,
|
2019-08-02 00:05:19 +01:00
|
|
|
opaque_type_parent: true,
|
2021-04-21 12:26:19 -04:00
|
|
|
scope_type: BinderScopeType::Normal,
|
2022-03-18 22:04:01 +03:00
|
|
|
allow_late_bound: true,
|
2018-01-17 17:22:40 -08:00
|
|
|
};
|
2021-12-04 21:20:58 +01:00
|
|
|
self.with(scope, |this| {
|
2021-04-05 00:10:09 -04:00
|
|
|
let scope = Scope::TraitRefBoundary { s: this.scope };
|
2021-12-04 21:20:58 +01:00
|
|
|
this.with(scope, |this| {
|
2021-04-05 00:10:09 -04:00
|
|
|
this.visit_generics(generics);
|
|
|
|
this.visit_ty(ty);
|
|
|
|
})
|
2017-12-12 21:11:15 -05:00
|
|
|
});
|
2020-08-11 13:02:14 -07:00
|
|
|
self.missing_named_lifetime_spots.pop();
|
2018-03-21 16:59:28 -04:00
|
|
|
}
|
2017-12-12 21:11:15 -05:00
|
|
|
Const(_, _) => {
|
|
|
|
// Only methods and types support generics.
|
2017-10-16 21:07:26 +02:00
|
|
|
assert!(impl_item.generics.params.is_empty());
|
2020-08-11 13:02:14 -07:00
|
|
|
self.missing_named_lifetime_spots.push(MissingLifetimeSpot::Static);
|
2017-12-12 21:11:15 -05:00
|
|
|
intravisit::walk_impl_item(self, impl_item);
|
2020-08-11 13:02:14 -07:00
|
|
|
self.missing_named_lifetime_spots.pop();
|
2018-03-21 16:59:28 -04:00
|
|
|
}
|
2017-01-11 17:35:54 +02:00
|
|
|
}
|
2013-10-28 17:37:10 -04:00
|
|
|
}
|
|
|
|
|
2021-02-27 21:31:56 -05:00
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
2016-11-09 16:45:26 -05:00
|
|
|
fn visit_lifetime(&mut self, lifetime_ref: &'tcx hir::Lifetime) {
|
2022-04-27 22:15:58 +02:00
|
|
|
match lifetime_ref.name {
|
|
|
|
hir::LifetimeName::ImplicitObjectLifetimeDefault
|
|
|
|
| hir::LifetimeName::Implicit
|
|
|
|
| hir::LifetimeName::Underscore => self.resolve_elided_lifetimes(&[lifetime_ref]),
|
|
|
|
hir::LifetimeName::Static => self.insert_lifetime(lifetime_ref, Region::Static),
|
|
|
|
hir::LifetimeName::Param(param_def_id, _) => {
|
|
|
|
self.resolve_lifetime_ref(param_def_id, lifetime_ref)
|
|
|
|
}
|
|
|
|
// If we've already reported an error, just ignore `lifetime_ref`.
|
|
|
|
hir::LifetimeName::Error => {}
|
2013-10-28 17:37:10 -04:00
|
|
|
}
|
|
|
|
}
|
2014-09-05 12:21:02 -07:00
|
|
|
|
2021-02-18 21:01:44 +01:00
|
|
|
fn visit_assoc_type_binding(&mut self, type_binding: &'tcx hir::TypeBinding<'_>) {
|
|
|
|
let scope = self.scope;
|
|
|
|
if let Some(scope_for_path) = self.map.scope_for_path.as_mut() {
|
|
|
|
// We add lifetime scope information for `Ident`s in associated type bindings and use
|
|
|
|
// the `HirId` of the type binding as the key in `LifetimeMap`
|
|
|
|
let lifetime_scope = get_lifetime_scopes_for_path(scope);
|
|
|
|
let map = scope_for_path.entry(type_binding.hir_id.owner).or_default();
|
|
|
|
map.insert(type_binding.hir_id.local_id, lifetime_scope);
|
|
|
|
}
|
|
|
|
hir::intravisit::walk_assoc_type_binding(self, type_binding);
|
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
fn visit_path(&mut self, path: &'tcx hir::Path<'tcx>, _: hir::HirId) {
|
2017-01-25 17:32:44 +02:00
|
|
|
for (i, segment) in path.segments.iter().enumerate() {
|
|
|
|
let depth = path.segments.len() - i - 1;
|
2018-02-23 17:48:54 +00:00
|
|
|
if let Some(ref args) = segment.args {
|
2019-04-20 19:36:05 +03:00
|
|
|
self.visit_segment_args(path.res, depth, args);
|
2017-09-21 23:24:26 +03:00
|
|
|
}
|
2021-02-18 21:01:44 +01:00
|
|
|
|
|
|
|
let scope = self.scope;
|
|
|
|
if let Some(scope_for_path) = self.map.scope_for_path.as_mut() {
|
|
|
|
// Add lifetime scope information to path segment. Note we cannot call `visit_path_segment`
|
|
|
|
// here because that call would yield to resolution problems due to `walk_path_segment`
|
|
|
|
// being called, which processes the path segments generic args, which we have already
|
|
|
|
// processed using `visit_segment_args`.
|
|
|
|
let lifetime_scope = get_lifetime_scopes_for_path(scope);
|
|
|
|
if let Some(hir_id) = segment.hir_id {
|
|
|
|
let map = scope_for_path.entry(hir_id.owner).or_default();
|
|
|
|
map.insert(hir_id.local_id, lifetime_scope);
|
|
|
|
}
|
|
|
|
}
|
2017-01-13 15:09:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 21:01:44 +01:00
|
|
|
fn visit_path_segment(&mut self, path_span: Span, path_segment: &'tcx hir::PathSegment<'tcx>) {
|
|
|
|
let scope = self.scope;
|
|
|
|
if let Some(scope_for_path) = self.map.scope_for_path.as_mut() {
|
|
|
|
let lifetime_scope = get_lifetime_scopes_for_path(scope);
|
|
|
|
if let Some(hir_id) = path_segment.hir_id {
|
|
|
|
let map = scope_for_path.entry(hir_id.owner).or_default();
|
|
|
|
map.insert(hir_id.local_id, lifetime_scope);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
intravisit::walk_path_segment(self, path_span, path_segment);
|
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
fn visit_fn_decl(&mut self, fd: &'tcx hir::FnDecl<'tcx>) {
|
2017-01-13 15:09:56 +02:00
|
|
|
let output = match fd.output {
|
2020-02-15 12:10:59 +09:00
|
|
|
hir::FnRetTy::DefaultReturn(_) => None,
|
|
|
|
hir::FnRetTy::Return(ref ty) => Some(&**ty),
|
2017-01-13 15:09:56 +02:00
|
|
|
};
|
|
|
|
self.visit_fn_like_elision(&fd.inputs, output);
|
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) {
|
2021-04-12 09:26:39 -04:00
|
|
|
let scope = Scope::TraitRefBoundary { s: self.scope };
|
2021-12-04 21:20:58 +01:00
|
|
|
self.with(scope, |this| {
|
2021-04-12 09:26:39 -04:00
|
|
|
for param in generics.params {
|
|
|
|
match param.kind {
|
|
|
|
GenericParamKind::Lifetime { .. } => {}
|
|
|
|
GenericParamKind::Type { ref default, .. } => {
|
|
|
|
if let Some(ref ty) = default {
|
|
|
|
this.visit_ty(&ty);
|
|
|
|
}
|
|
|
|
}
|
2022-02-04 20:56:32 -08:00
|
|
|
GenericParamKind::Const { ref ty, default } => {
|
2021-04-12 09:26:39 -04:00
|
|
|
this.visit_ty(&ty);
|
2022-02-04 20:56:32 -08:00
|
|
|
if let Some(default) = default {
|
|
|
|
this.visit_body(this.tcx.hir().body(default.body));
|
|
|
|
}
|
2018-05-26 00:27:54 +01:00
|
|
|
}
|
2019-02-15 22:25:42 +00:00
|
|
|
}
|
2014-09-05 12:21:02 -07:00
|
|
|
}
|
2022-02-05 15:48:02 +01:00
|
|
|
for predicate in generics.predicates {
|
2021-04-12 09:26:39 -04:00
|
|
|
match predicate {
|
|
|
|
&hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
|
|
|
|
ref bounded_ty,
|
|
|
|
bounds,
|
|
|
|
ref bound_generic_params,
|
|
|
|
..
|
|
|
|
}) => {
|
2022-04-27 22:15:58 +02:00
|
|
|
let (lifetimes, binders): (FxIndexMap<LocalDefId, Region>, Vec<_>) =
|
2021-04-12 09:26:39 -04:00
|
|
|
bound_generic_params
|
|
|
|
.iter()
|
2021-11-06 16:36:23 +01:00
|
|
|
.filter(|param| {
|
|
|
|
matches!(param.kind, GenericParamKind::Lifetime { .. })
|
2021-04-12 09:26:39 -04:00
|
|
|
})
|
|
|
|
.enumerate()
|
|
|
|
.map(|(late_bound_idx, param)| {
|
|
|
|
let pair =
|
2022-02-10 13:04:59 +01:00
|
|
|
Region::late(late_bound_idx as u32, this.tcx.hir(), param);
|
2021-04-12 09:26:39 -04:00
|
|
|
let r = late_region_as_bound_region(this.tcx, &pair.1);
|
|
|
|
(pair, r)
|
|
|
|
})
|
|
|
|
.unzip();
|
|
|
|
this.map.late_bound_vars.insert(bounded_ty.hir_id, binders.clone());
|
2021-04-20 16:39:41 -04:00
|
|
|
let next_early_index = this.next_early_index();
|
|
|
|
// Even if there are no lifetimes defined here, we still wrap it in a binder
|
|
|
|
// scope. If there happens to be a nested poly trait ref (an error), that
|
|
|
|
// will be `Concatenating` anyways, so we don't have to worry about the depth
|
|
|
|
// being wrong.
|
|
|
|
let scope = Scope::Binder {
|
|
|
|
hir_id: bounded_ty.hir_id,
|
|
|
|
lifetimes,
|
|
|
|
s: this.scope,
|
|
|
|
next_early_index,
|
|
|
|
opaque_type_parent: false,
|
2021-04-21 12:26:19 -04:00
|
|
|
scope_type: BinderScopeType::Normal,
|
2022-03-18 22:04:01 +03:00
|
|
|
allow_late_bound: true,
|
2021-04-20 16:39:41 -04:00
|
|
|
};
|
2021-12-04 21:20:58 +01:00
|
|
|
this.with(scope, |this| {
|
2021-04-13 16:58:00 -04:00
|
|
|
this.visit_ty(&bounded_ty);
|
|
|
|
walk_list!(this, visit_param_bound, bounds);
|
2021-04-20 16:39:41 -04:00
|
|
|
})
|
2021-04-12 09:26:39 -04:00
|
|
|
}
|
|
|
|
&hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
|
|
|
|
ref lifetime,
|
|
|
|
bounds,
|
|
|
|
..
|
|
|
|
}) => {
|
|
|
|
this.visit_lifetime(lifetime);
|
|
|
|
walk_list!(this, visit_param_bound, bounds);
|
2022-02-07 22:58:30 +01:00
|
|
|
|
|
|
|
if lifetime.name != hir::LifetimeName::Static {
|
|
|
|
for bound in bounds {
|
|
|
|
let hir::GenericBound::Outlives(ref lt) = bound else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
if lt.name != hir::LifetimeName::Static {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
this.insert_lifetime(lt, Region::Static);
|
|
|
|
this.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_warn(
|
|
|
|
lifetime.span,
|
|
|
|
&format!(
|
|
|
|
"unnecessary lifetime parameter `{}`",
|
|
|
|
lifetime.name.ident(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.help(&format!(
|
|
|
|
"you can use the `'static` lifetime directly, in place of `{}`",
|
|
|
|
lifetime.name.ident(),
|
|
|
|
))
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
2021-04-12 09:26:39 -04:00
|
|
|
}
|
|
|
|
&hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
|
|
|
|
ref lhs_ty,
|
|
|
|
ref rhs_ty,
|
|
|
|
..
|
|
|
|
}) => {
|
|
|
|
this.visit_ty(lhs_ty);
|
|
|
|
this.visit_ty(rhs_ty);
|
|
|
|
}
|
2014-11-29 17:08:30 +13:00
|
|
|
}
|
|
|
|
}
|
2021-04-12 09:26:39 -04:00
|
|
|
})
|
2014-09-05 12:21:02 -07:00
|
|
|
}
|
|
|
|
|
2020-08-04 14:19:28 +01:00
|
|
|
fn visit_param_bound(&mut self, bound: &'tcx hir::GenericBound<'tcx>) {
|
|
|
|
match bound {
|
2021-04-21 11:49:59 -04:00
|
|
|
hir::GenericBound::LangItemTrait(_, _, hir_id, _) => {
|
2021-04-21 12:26:19 -04:00
|
|
|
// FIXME(jackh726): This is pretty weird. `LangItemTrait` doesn't go
|
|
|
|
// through the regular poly trait ref code, so we don't get another
|
|
|
|
// chance to introduce a binder. For now, I'm keeping the existing logic
|
|
|
|
// of "if there isn't a Binder scope above us, add one", but I
|
|
|
|
// imagine there's a better way to go about this.
|
|
|
|
let (binders, scope_type) = self.poly_trait_ref_binder_info();
|
|
|
|
|
2021-04-21 11:49:59 -04:00
|
|
|
self.map.late_bound_vars.insert(*hir_id, binders);
|
2020-08-04 14:19:28 +01:00
|
|
|
let scope = Scope::Binder {
|
2020-10-26 14:18:31 -04:00
|
|
|
hir_id: *hir_id,
|
2021-02-18 21:01:44 +01:00
|
|
|
lifetimes: FxIndexMap::default(),
|
2020-08-04 14:19:28 +01:00
|
|
|
s: self.scope,
|
|
|
|
next_early_index: self.next_early_index(),
|
|
|
|
opaque_type_parent: false,
|
2021-04-21 11:49:59 -04:00
|
|
|
scope_type,
|
2022-03-18 22:04:01 +03:00
|
|
|
allow_late_bound: true,
|
2020-08-04 14:19:28 +01:00
|
|
|
};
|
2021-12-04 21:20:58 +01:00
|
|
|
self.with(scope, |this| {
|
2020-08-04 14:19:28 +01:00
|
|
|
intravisit::walk_param_bound(this, bound);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_ => intravisit::walk_param_bound(self, bound),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-11 09:00:05 -05:00
|
|
|
fn visit_poly_trait_ref(
|
|
|
|
&mut self,
|
2019-11-30 17:46:46 +01:00
|
|
|
trait_ref: &'tcx hir::PolyTraitRef<'tcx>,
|
2017-12-11 09:00:05 -05:00
|
|
|
_modifier: hir::TraitBoundModifier,
|
|
|
|
) {
|
2019-02-28 22:43:53 +00:00
|
|
|
debug!("visit_poly_trait_ref(trait_ref={:?})", trait_ref);
|
2014-11-15 17:09:51 -05:00
|
|
|
|
2020-01-27 11:26:06 -08:00
|
|
|
let should_pop_missing_lt = self.is_trait_ref_fn_scope(trait_ref);
|
2020-05-30 17:03:32 +01:00
|
|
|
|
2020-10-26 14:18:31 -04:00
|
|
|
let next_early_index = self.next_early_index();
|
2021-04-21 12:26:19 -04:00
|
|
|
let (mut binders, scope_type) = self.poly_trait_ref_binder_info();
|
2021-04-20 16:39:41 -04:00
|
|
|
|
|
|
|
let initial_bound_vars = binders.len() as u32;
|
2022-04-27 22:15:58 +02:00
|
|
|
let mut lifetimes: FxIndexMap<LocalDefId, Region> = FxIndexMap::default();
|
2021-04-20 16:39:41 -04:00
|
|
|
let binders_iter = trait_ref
|
|
|
|
.bound_generic_params
|
|
|
|
.iter()
|
2021-11-06 16:36:23 +01:00
|
|
|
.filter(|param| matches!(param.kind, GenericParamKind::Lifetime { .. }))
|
2021-04-20 16:39:41 -04:00
|
|
|
.enumerate()
|
|
|
|
.map(|(late_bound_idx, param)| {
|
2022-02-10 13:04:59 +01:00
|
|
|
let pair =
|
|
|
|
Region::late(initial_bound_vars + late_bound_idx as u32, self.tcx.hir(), param);
|
2021-04-20 16:39:41 -04:00
|
|
|
let r = late_region_as_bound_region(self.tcx, &pair.1);
|
|
|
|
lifetimes.insert(pair.0, pair.1);
|
|
|
|
r
|
|
|
|
});
|
|
|
|
binders.extend(binders_iter);
|
2020-10-26 14:18:31 -04:00
|
|
|
|
|
|
|
debug!(?binders);
|
|
|
|
self.map.late_bound_vars.insert(trait_ref.trait_ref.hir_ref_id, binders);
|
|
|
|
|
2021-04-20 16:39:41 -04:00
|
|
|
// Always introduce a scope here, even if this is in a where clause and
|
|
|
|
// we introduced the binders around the bounded Ty. In that case, we
|
|
|
|
// just reuse the concatenation functionality also present in nested trait
|
2021-04-21 03:12:04 -04:00
|
|
|
// refs.
|
2021-04-20 16:39:41 -04:00
|
|
|
let scope = Scope::Binder {
|
|
|
|
hir_id: trait_ref.trait_ref.hir_ref_id,
|
|
|
|
lifetimes,
|
|
|
|
s: self.scope,
|
|
|
|
next_early_index,
|
|
|
|
opaque_type_parent: false,
|
|
|
|
scope_type,
|
2022-03-18 22:04:01 +03:00
|
|
|
allow_late_bound: true,
|
2021-04-20 16:39:41 -04:00
|
|
|
};
|
2021-12-04 21:20:58 +01:00
|
|
|
self.with(scope, |this| {
|
2021-04-20 16:39:41 -04:00
|
|
|
walk_list!(this, visit_generic_param, trait_ref.bound_generic_params);
|
|
|
|
this.visit_trait_ref(&trait_ref.trait_ref);
|
|
|
|
});
|
2021-04-13 16:58:00 -04:00
|
|
|
|
2020-01-27 11:26:06 -08:00
|
|
|
if should_pop_missing_lt {
|
|
|
|
self.missing_named_lifetime_spots.pop();
|
2015-02-05 21:46:01 +13:00
|
|
|
}
|
2014-09-05 12:21:02 -07:00
|
|
|
}
|
2014-11-15 16:47:59 -05:00
|
|
|
}
|
|
|
|
|
2022-02-01 18:28:24 +01:00
|
|
|
fn compute_object_lifetime_defaults<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2021-02-27 21:31:56 -05:00
|
|
|
item: &hir::Item<'_>,
|
2022-02-01 18:28:24 +01:00
|
|
|
) -> Option<&'tcx [ObjectLifetimeDefault]> {
|
2021-02-27 21:31:56 -05:00
|
|
|
match item.kind {
|
|
|
|
hir::ItemKind::Struct(_, ref generics)
|
|
|
|
| hir::ItemKind::Union(_, ref generics)
|
|
|
|
| hir::ItemKind::Enum(_, ref generics)
|
2021-11-30 19:11:35 +01:00
|
|
|
| hir::ItemKind::OpaqueTy(hir::OpaqueTy {
|
|
|
|
ref generics,
|
|
|
|
origin: hir::OpaqueTyOrigin::TyAlias,
|
|
|
|
..
|
|
|
|
})
|
2021-02-27 21:31:56 -05:00
|
|
|
| hir::ItemKind::TyAlias(_, ref generics)
|
|
|
|
| hir::ItemKind::Trait(_, _, ref generics, ..) => {
|
|
|
|
let result = object_lifetime_defaults_for_item(tcx, generics);
|
|
|
|
|
|
|
|
// Debugging aid.
|
|
|
|
let attrs = tcx.hir().attrs(item.hir_id());
|
|
|
|
if tcx.sess.contains_name(attrs, sym::rustc_object_lifetime_default) {
|
|
|
|
let object_lifetime_default_reprs: String = result
|
|
|
|
.iter()
|
|
|
|
.map(|set| match *set {
|
|
|
|
Set1::Empty => "BaseDefault".into(),
|
|
|
|
Set1::One(Region::Static) => "'static".into(),
|
2022-02-20 10:22:57 -08:00
|
|
|
Set1::One(Region::EarlyBound(mut i, _)) => generics
|
2021-02-27 21:31:56 -05:00
|
|
|
.params
|
|
|
|
.iter()
|
|
|
|
.find_map(|param| match param.kind {
|
|
|
|
GenericParamKind::Lifetime { .. } => {
|
|
|
|
if i == 0 {
|
|
|
|
return Some(param.name.ident().to_string().into());
|
2018-10-02 18:29:48 +02:00
|
|
|
}
|
2021-02-27 21:31:56 -05:00
|
|
|
i -= 1;
|
|
|
|
None
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
Set1::One(_) => bug!(),
|
|
|
|
Set1::Many => "Ambiguous".into(),
|
|
|
|
})
|
|
|
|
.collect::<Vec<Cow<'static, str>>>()
|
|
|
|
.join(",");
|
|
|
|
tcx.sess.span_err(item.span, &object_lifetime_default_reprs);
|
2017-01-25 17:32:44 +02:00
|
|
|
}
|
2021-02-27 21:31:56 -05:00
|
|
|
|
|
|
|
Some(result)
|
2017-01-25 17:32:44 +02:00
|
|
|
}
|
2021-02-27 21:31:56 -05:00
|
|
|
_ => None,
|
2017-01-25 17:32:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Scan the bounds and where-clauses on parameters to extract bounds
|
|
|
|
/// of the form `T:'a` so as to determine the `ObjectLifetimeDefault`
|
|
|
|
/// for each type parameter.
|
2022-02-01 18:28:24 +01:00
|
|
|
fn object_lifetime_defaults_for_item<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-11-30 17:46:46 +01:00
|
|
|
generics: &hir::Generics<'_>,
|
2022-02-01 18:28:24 +01:00
|
|
|
) -> &'tcx [ObjectLifetimeDefault] {
|
2019-11-30 17:46:46 +01:00
|
|
|
fn add_bounds(set: &mut Set1<hir::LifetimeName>, bounds: &[hir::GenericBound<'_>]) {
|
2017-01-25 17:32:44 +02:00
|
|
|
for bound in bounds {
|
2018-06-14 12:08:58 +01:00
|
|
|
if let hir::GenericBound::Outlives(ref lifetime) = *bound {
|
2020-03-14 01:36:46 +03:00
|
|
|
set.insert(lifetime.name.normalize_to_macros_2_0());
|
2017-01-25 17:32:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-01 18:28:24 +01:00
|
|
|
let process_param = |param: &hir::GenericParam<'_>| match param.kind {
|
|
|
|
GenericParamKind::Lifetime { .. } => None,
|
|
|
|
GenericParamKind::Type { .. } => {
|
|
|
|
let mut set = Set1::Empty;
|
2017-01-25 17:32:44 +02:00
|
|
|
|
2022-02-01 18:28:24 +01:00
|
|
|
let param_def_id = tcx.hir().local_def_id(param.hir_id);
|
2022-02-05 15:48:02 +01:00
|
|
|
for predicate in generics.predicates {
|
2022-02-01 18:28:24 +01:00
|
|
|
// Look for `type: ...` where clauses.
|
2022-02-19 00:48:49 +01:00
|
|
|
let hir::WherePredicate::BoundPredicate(ref data) = *predicate else { continue };
|
2017-01-25 17:32:44 +02:00
|
|
|
|
2022-02-01 18:28:24 +01:00
|
|
|
// Ignore `for<'a> type: ...` as they can change what
|
|
|
|
// lifetimes mean (although we could "just" handle it).
|
|
|
|
if !data.bound_generic_params.is_empty() {
|
|
|
|
continue;
|
2017-12-11 09:00:05 -05:00
|
|
|
}
|
2017-01-25 17:32:44 +02:00
|
|
|
|
2022-02-01 18:28:24 +01:00
|
|
|
let res = match data.bounded_ty.kind {
|
|
|
|
hir::TyKind::Path(hir::QPath::Resolved(None, ref path)) => path.res,
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
if res == Res::Def(DefKind::TyParam, param_def_id.to_def_id()) {
|
|
|
|
add_bounds(&mut set, &data.bounds);
|
|
|
|
}
|
2019-02-15 22:25:42 +00:00
|
|
|
}
|
2022-02-01 18:28:24 +01:00
|
|
|
|
|
|
|
Some(match set {
|
|
|
|
Set1::Empty => Set1::Empty,
|
|
|
|
Set1::One(name) => {
|
|
|
|
if name == hir::LifetimeName::Static {
|
|
|
|
Set1::One(Region::Static)
|
|
|
|
} else {
|
|
|
|
generics
|
|
|
|
.params
|
|
|
|
.iter()
|
|
|
|
.filter_map(|param| match param.kind {
|
2022-02-20 10:22:57 -08:00
|
|
|
GenericParamKind::Lifetime { .. } => {
|
2022-04-27 22:15:58 +02:00
|
|
|
let param_def_id = tcx.hir().local_def_id(param.hir_id);
|
|
|
|
Some((
|
|
|
|
param_def_id,
|
|
|
|
hir::LifetimeName::Param(param_def_id, param.name),
|
|
|
|
))
|
2022-02-20 10:22:57 -08:00
|
|
|
}
|
2022-02-01 18:28:24 +01:00
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.enumerate()
|
2022-02-20 10:22:57 -08:00
|
|
|
.find(|&(_, (_, lt_name))| lt_name == name)
|
2022-04-27 22:15:58 +02:00
|
|
|
.map_or(Set1::Many, |(i, (def_id, _))| {
|
2022-02-20 10:22:57 -08:00
|
|
|
Set1::One(Region::EarlyBound(i as u32, def_id.to_def_id()))
|
2022-02-01 18:28:24 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Set1::Many => Set1::Many,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
GenericParamKind::Const { .. } => {
|
|
|
|
// Generic consts don't impose any constraints.
|
|
|
|
//
|
|
|
|
// We still store a dummy value here to allow generic parameters
|
|
|
|
// in an arbitrary order.
|
|
|
|
Some(Set1::Empty)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
tcx.arena.alloc_from_iter(generics.params.iter().filter_map(process_param))
|
2017-01-25 17:32:44 +02:00
|
|
|
}
|
|
|
|
|
2016-04-21 05:10:10 -04:00
|
|
|
impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
|
2018-08-29 22:02:42 -07:00
|
|
|
fn with<F>(&mut self, wrap_scope: Scope<'_>, f: F)
|
2017-12-11 09:00:05 -05:00
|
|
|
where
|
2021-12-04 21:20:58 +01:00
|
|
|
F: for<'b> FnOnce(&mut LifetimeContext<'b, 'tcx>),
|
2014-12-08 20:26:43 -05:00
|
|
|
{
|
2022-05-10 21:15:30 +02:00
|
|
|
let LifetimeContext { tcx, map, .. } = self;
|
2019-06-30 11:30:01 -07:00
|
|
|
let xcrate_object_lifetime_defaults = take(&mut self.xcrate_object_lifetime_defaults);
|
2020-01-19 17:48:47 -08:00
|
|
|
let missing_named_lifetime_spots = take(&mut self.missing_named_lifetime_spots);
|
2014-11-15 16:47:59 -05:00
|
|
|
let mut this = LifetimeContext {
|
2018-05-03 18:43:28 -04:00
|
|
|
tcx: *tcx,
|
2020-03-06 19:28:44 +01:00
|
|
|
map,
|
2014-11-15 16:47:59 -05:00
|
|
|
scope: &wrap_scope,
|
2021-03-12 12:38:42 -05:00
|
|
|
trait_definition_only: self.trait_definition_only,
|
2017-07-03 11:19:51 -07:00
|
|
|
xcrate_object_lifetime_defaults,
|
2020-01-19 17:48:47 -08:00
|
|
|
missing_named_lifetime_spots,
|
2014-11-15 16:47:59 -05:00
|
|
|
};
|
2021-02-27 21:31:56 -05:00
|
|
|
let span = tracing::debug_span!("scope", scope = ?TruncatedScopeDebug(&this.scope));
|
|
|
|
{
|
|
|
|
let _enter = span.enter();
|
2021-12-04 21:20:58 +01:00
|
|
|
f(&mut this);
|
2021-02-27 21:31:56 -05:00
|
|
|
}
|
2017-01-25 17:32:44 +02:00
|
|
|
self.xcrate_object_lifetime_defaults = this.xcrate_object_lifetime_defaults;
|
2020-01-19 17:48:47 -08:00
|
|
|
self.missing_named_lifetime_spots = this.missing_named_lifetime_spots;
|
2018-05-03 18:43:28 -04:00
|
|
|
}
|
|
|
|
|
2014-03-07 08:43:39 +01:00
|
|
|
/// Visits self by adding a scope and handling recursive walk over the contents with `walk`.
|
2014-11-25 21:17:11 -05:00
|
|
|
///
|
|
|
|
/// Handles visiting fns and methods. These are a bit complicated because we must distinguish
|
|
|
|
/// early- vs late-bound lifetime parameters. We do this by checking which lifetimes appear
|
|
|
|
/// within type bounds; those are early bound lifetimes, and the rest are late bound.
|
|
|
|
///
|
|
|
|
/// For example:
|
|
|
|
///
|
|
|
|
/// fn foo<'a,'b,'c,T:Trait<'b>>(...)
|
|
|
|
///
|
|
|
|
/// Here `'a` and `'c` are late bound but `'b` is early bound. Note that early- and late-bound
|
|
|
|
/// lifetimes may be interspersed together.
|
|
|
|
///
|
|
|
|
/// If early bound lifetimes are present, we separate them into their own list (and likewise
|
|
|
|
/// for late bound). They will be numbered sequentially, starting from the lowest index that is
|
|
|
|
/// already in scope (for a fn item, that will be 0, but for a method it might not be). Late
|
2019-02-08 14:53:55 +01:00
|
|
|
/// bound lifetimes are resolved by name and associated with a binder ID (`binder_id`), so the
|
2014-11-25 21:17:11 -05:00
|
|
|
/// ordering is not important there.
|
2017-12-11 09:00:05 -05:00
|
|
|
fn visit_early_late<F>(
|
|
|
|
&mut self,
|
2021-10-20 22:38:10 +02:00
|
|
|
parent_id: Option<LocalDefId>,
|
2020-10-26 14:18:31 -04:00
|
|
|
hir_id: hir::HirId,
|
2019-11-30 17:46:46 +01:00
|
|
|
generics: &'tcx hir::Generics<'tcx>,
|
2017-12-11 09:00:05 -05:00
|
|
|
walk: F,
|
|
|
|
) where
|
2016-11-09 16:45:26 -05:00
|
|
|
F: for<'b, 'c> FnOnce(&'b mut LifetimeContext<'c, 'tcx>),
|
2014-12-08 20:26:43 -05:00
|
|
|
{
|
2018-11-27 02:59:49 +00:00
|
|
|
// Find the start of nested early scopes, e.g., in methods.
|
2020-10-26 14:18:31 -04:00
|
|
|
let mut next_early_index = 0;
|
2017-01-11 17:35:54 +02:00
|
|
|
if let Some(parent_id) = parent_id {
|
2019-06-14 18:58:55 +02:00
|
|
|
let parent = self.tcx.hir().expect_item(parent_id);
|
2019-09-26 17:51:36 +01:00
|
|
|
if sub_items_have_self_param(&parent.kind) {
|
2020-10-26 14:18:31 -04:00
|
|
|
next_early_index += 1; // Self comes before lifetimes
|
2016-08-27 01:13:48 +03:00
|
|
|
}
|
2019-09-26 17:51:36 +01:00
|
|
|
match parent.kind {
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Trait(_, _, ref generics, ..)
|
2020-11-22 17:46:21 -05:00
|
|
|
| hir::ItemKind::Impl(hir::Impl { ref generics, .. }) => {
|
2020-10-26 14:18:31 -04:00
|
|
|
next_early_index += generics.params.len() as u32;
|
2016-08-27 01:13:48 +03:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:25:42 +00:00
|
|
|
let mut non_lifetime_count = 0;
|
2020-11-15 17:06:58 -05:00
|
|
|
let mut named_late_bound_vars = 0;
|
2022-04-27 22:15:58 +02:00
|
|
|
let lifetimes: FxIndexMap<LocalDefId, Region> = generics
|
2019-12-24 17:38:22 -05:00
|
|
|
.params
|
|
|
|
.iter()
|
2020-10-26 14:18:31 -04:00
|
|
|
.filter_map(|param| match param.kind {
|
2019-12-24 17:38:22 -05:00
|
|
|
GenericParamKind::Lifetime { .. } => {
|
2022-05-26 08:59:15 +02:00
|
|
|
if self.tcx.is_late_bound(param.hir_id) {
|
2020-10-26 14:18:31 -04:00
|
|
|
let late_bound_idx = named_late_bound_vars;
|
2020-11-15 17:06:58 -05:00
|
|
|
named_late_bound_vars += 1;
|
2022-02-10 13:04:59 +01:00
|
|
|
Some(Region::late(late_bound_idx, self.tcx.hir(), param))
|
2019-12-24 17:38:22 -05:00
|
|
|
} else {
|
2022-02-10 13:04:59 +01:00
|
|
|
Some(Region::early(self.tcx.hir(), &mut next_early_index, param))
|
2019-12-24 17:38:22 -05:00
|
|
|
}
|
2018-10-18 05:39:53 -04:00
|
|
|
}
|
2019-12-24 17:38:22 -05:00
|
|
|
GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => {
|
|
|
|
non_lifetime_count += 1;
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2020-10-26 14:18:31 -04:00
|
|
|
let next_early_index = next_early_index + non_lifetime_count;
|
2017-01-11 17:35:54 +02:00
|
|
|
|
2020-10-26 14:18:31 -04:00
|
|
|
let binders: Vec<_> = generics
|
|
|
|
.params
|
|
|
|
.iter()
|
2021-11-06 16:36:23 +01:00
|
|
|
.filter(|param| {
|
|
|
|
matches!(param.kind, GenericParamKind::Lifetime { .. })
|
2022-05-26 08:59:15 +02:00
|
|
|
&& self.tcx.is_late_bound(param.hir_id)
|
2021-03-26 17:40:15 -04:00
|
|
|
})
|
|
|
|
.enumerate()
|
|
|
|
.map(|(late_bound_idx, param)| {
|
2022-02-10 13:04:59 +01:00
|
|
|
let pair = Region::late(late_bound_idx as u32, self.tcx.hir(), param);
|
2021-11-06 16:36:23 +01:00
|
|
|
late_region_as_bound_region(self.tcx, &pair.1)
|
2020-10-26 14:18:31 -04:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
self.map.late_bound_vars.insert(hir_id, binders);
|
2017-01-11 17:35:54 +02:00
|
|
|
let scope = Scope::Binder {
|
2020-10-26 14:18:31 -04:00
|
|
|
hir_id,
|
2017-07-03 11:19:51 -07:00
|
|
|
lifetimes,
|
2017-10-15 13:43:06 -07:00
|
|
|
next_early_index,
|
2017-12-11 09:00:05 -05:00
|
|
|
s: self.scope,
|
2019-08-02 00:05:19 +01:00
|
|
|
opaque_type_parent: true,
|
2021-04-21 12:26:19 -04:00
|
|
|
scope_type: BinderScopeType::Normal,
|
2022-03-18 22:04:01 +03:00
|
|
|
allow_late_bound: true,
|
2017-01-08 22:40:04 +02:00
|
|
|
};
|
2021-12-04 21:20:58 +01:00
|
|
|
self.with(scope, walk);
|
2014-03-07 08:43:39 +01:00
|
|
|
}
|
|
|
|
|
2019-08-02 00:05:19 +01:00
|
|
|
fn next_early_index_helper(&self, only_opaque_type_parent: bool) -> u32 {
|
2017-10-15 13:43:06 -07:00
|
|
|
let mut scope = self.scope;
|
|
|
|
loop {
|
|
|
|
match *scope {
|
2017-12-11 09:00:05 -05:00
|
|
|
Scope::Root => return 0,
|
2017-10-15 13:43:06 -07:00
|
|
|
|
2019-12-24 17:38:22 -05:00
|
|
|
Scope::Binder { next_early_index, opaque_type_parent, .. }
|
|
|
|
if (!only_opaque_type_parent || opaque_type_parent) =>
|
2018-03-21 16:59:28 -04:00
|
|
|
{
|
2019-12-24 17:38:22 -05:00
|
|
|
return next_early_index;
|
2018-03-21 16:59:28 -04:00
|
|
|
}
|
2017-10-15 13:43:06 -07:00
|
|
|
|
2018-01-17 17:22:40 -08:00
|
|
|
Scope::Binder { s, .. }
|
|
|
|
| Scope::Body { s, .. }
|
2017-12-11 09:00:05 -05:00
|
|
|
| Scope::Elision { s, .. }
|
2020-10-26 14:18:31 -04:00
|
|
|
| Scope::ObjectLifetimeDefault { s, .. }
|
2021-04-05 00:10:09 -04:00
|
|
|
| Scope::Supertrait { s, .. }
|
|
|
|
| Scope::TraitRefBoundary { s, .. } => scope = s,
|
2017-10-15 13:43:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-17 17:22:40 -08:00
|
|
|
/// Returns the next index one would use for an early-bound-region
|
|
|
|
/// if extending the current scope.
|
|
|
|
fn next_early_index(&self) -> u32 {
|
|
|
|
self.next_early_index_helper(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the next index one would use for an `impl Trait` that
|
2019-08-02 00:05:19 +01:00
|
|
|
/// is being converted into an opaque type alias `impl Trait`. This will be the
|
2018-01-17 17:22:40 -08:00
|
|
|
/// next early index from the enclosing item, for the most
|
2019-08-02 00:05:19 +01:00
|
|
|
/// part. See the `opaque_type_parent` field for more info.
|
|
|
|
fn next_early_index_for_opaque_type(&self) -> u32 {
|
2018-01-17 17:22:40 -08:00
|
|
|
self.next_early_index_helper(false)
|
|
|
|
}
|
|
|
|
|
2022-04-27 22:15:58 +02:00
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
|
|
|
fn resolve_lifetime_ref(
|
|
|
|
&mut self,
|
|
|
|
region_def_id: LocalDefId,
|
|
|
|
lifetime_ref: &'tcx hir::Lifetime,
|
|
|
|
) {
|
2013-10-28 17:37:10 -04:00
|
|
|
// Walk up the scope chain, tracking the number of fn scopes
|
|
|
|
// that we pass through, until we find a lifetime with the
|
2017-01-11 17:35:54 +02:00
|
|
|
// given name or we run out of scopes.
|
2013-10-28 17:37:10 -04:00
|
|
|
// search.
|
2014-11-15 16:47:59 -05:00
|
|
|
let mut late_depth = 0;
|
2014-09-12 13:10:30 +03:00
|
|
|
let mut scope = self.scope;
|
2017-01-11 17:35:54 +02:00
|
|
|
let mut outermost_body = None;
|
|
|
|
let result = loop {
|
2013-10-28 17:37:10 -04:00
|
|
|
match *scope {
|
2017-01-11 17:35:54 +02:00
|
|
|
Scope::Body { id, s } => {
|
|
|
|
outermost_body = Some(id);
|
|
|
|
scope = s;
|
2013-10-28 17:37:10 -04:00
|
|
|
}
|
|
|
|
|
2017-01-08 22:40:04 +02:00
|
|
|
Scope::Root => {
|
2017-01-11 17:35:54 +02:00
|
|
|
break None;
|
2013-10-28 17:37:10 -04:00
|
|
|
}
|
|
|
|
|
2021-04-13 16:58:00 -04:00
|
|
|
Scope::Binder { ref lifetimes, scope_type, s, .. } => {
|
2022-04-27 22:15:58 +02:00
|
|
|
if let Some(&def) = lifetimes.get(®ion_def_id) {
|
|
|
|
break Some(def.shifted(late_depth));
|
2013-10-28 17:37:10 -04:00
|
|
|
}
|
2021-04-13 16:58:00 -04:00
|
|
|
match scope_type {
|
2021-04-21 12:26:19 -04:00
|
|
|
BinderScopeType::Normal => late_depth += 1,
|
2021-04-13 16:58:00 -04:00
|
|
|
BinderScopeType::Concatenating => {}
|
|
|
|
}
|
2018-10-11 15:51:44 -04:00
|
|
|
scope = s;
|
2013-10-28 17:37:10 -04:00
|
|
|
}
|
2017-01-13 15:09:56 +02:00
|
|
|
|
2020-10-26 14:18:31 -04:00
|
|
|
Scope::Elision { s, .. }
|
|
|
|
| Scope::ObjectLifetimeDefault { s, .. }
|
2021-04-12 09:12:10 -04:00
|
|
|
| Scope::Supertrait { s, .. }
|
|
|
|
| Scope::TraitRefBoundary { s, .. } => {
|
2017-01-13 15:09:56 +02:00
|
|
|
scope = s;
|
|
|
|
}
|
2013-10-28 17:37:10 -04:00
|
|
|
}
|
2017-01-11 17:35:54 +02:00
|
|
|
};
|
2013-10-28 17:37:10 -04:00
|
|
|
|
2017-01-11 17:35:54 +02:00
|
|
|
if let Some(mut def) = result {
|
2017-05-11 15:05:00 +03:00
|
|
|
if let Region::EarlyBound(..) = def {
|
|
|
|
// Do not free early-bound regions, only late-bound ones.
|
|
|
|
} else if let Some(body_id) = outermost_body {
|
2018-12-04 13:45:36 +01:00
|
|
|
let fn_id = self.tcx.hir().body_owner(body_id);
|
2019-06-20 10:39:19 +02:00
|
|
|
match self.tcx.hir().get(fn_id) {
|
2019-12-24 17:38:22 -05:00
|
|
|
Node::Item(&hir::Item { kind: hir::ItemKind::Fn(..), .. })
|
2018-08-25 15:56:16 +01:00
|
|
|
| Node::TraitItem(&hir::TraitItem {
|
2020-03-03 12:46:22 -06:00
|
|
|
kind: hir::TraitItemKind::Fn(..), ..
|
2017-12-11 09:00:05 -05:00
|
|
|
})
|
2020-03-05 09:57:34 -06:00
|
|
|
| Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }) => {
|
2019-06-27 11:28:14 +02:00
|
|
|
let scope = self.tcx.hir().local_def_id(fn_id);
|
2020-04-09 09:43:00 +01:00
|
|
|
def = Region::Free(scope.to_def_id(), def.id().unwrap());
|
2013-10-28 17:37:10 -04:00
|
|
|
}
|
2017-01-11 17:35:54 +02:00
|
|
|
_ => {}
|
2013-10-28 17:37:10 -04:00
|
|
|
}
|
|
|
|
}
|
2017-11-16 22:59:45 -08:00
|
|
|
|
2017-01-11 17:35:54 +02:00
|
|
|
self.insert_lifetime(lifetime_ref, def);
|
|
|
|
} else {
|
2022-03-06 12:02:13 +01:00
|
|
|
self.tcx.sess.delay_span_bug(
|
|
|
|
lifetime_ref.span,
|
|
|
|
&format!("Could not resolve {:?} in scope {:#?}", lifetime_ref, self.scope,),
|
|
|
|
);
|
2013-10-28 17:37:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
fn visit_segment_args(
|
|
|
|
&mut self,
|
|
|
|
res: Res,
|
|
|
|
depth: usize,
|
|
|
|
generic_args: &'tcx hir::GenericArgs<'tcx>,
|
|
|
|
) {
|
2019-08-12 10:41:05 -04:00
|
|
|
debug!(
|
|
|
|
"visit_segment_args(res={:?}, depth={:?}, generic_args={:?})",
|
2019-12-24 17:38:22 -05:00
|
|
|
res, depth, generic_args,
|
2019-08-12 10:41:05 -04:00
|
|
|
);
|
|
|
|
|
2018-05-16 12:57:45 +01:00
|
|
|
if generic_args.parenthesized {
|
2019-05-08 15:57:06 -04:00
|
|
|
self.visit_fn_like_elision(generic_args.inputs(), Some(generic_args.bindings[0].ty()));
|
2017-07-29 01:13:40 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-01-25 17:32:44 +02:00
|
|
|
|
2018-05-27 01:43:03 +01:00
|
|
|
let mut elide_lifetimes = true;
|
2020-11-15 17:06:58 -05:00
|
|
|
let lifetimes: Vec<_> = generic_args
|
2018-10-18 05:39:53 -04:00
|
|
|
.args
|
|
|
|
.iter()
|
|
|
|
.filter_map(|arg| match arg {
|
|
|
|
hir::GenericArg::Lifetime(lt) => {
|
|
|
|
if !lt.is_elided() {
|
|
|
|
elide_lifetimes = false;
|
|
|
|
}
|
|
|
|
Some(lt)
|
2018-05-27 01:43:03 +01:00
|
|
|
}
|
2018-10-18 05:39:53 -04:00
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect();
|
2020-10-26 14:18:31 -04:00
|
|
|
// We short-circuit here if all are elided in order to pluralize
|
|
|
|
// possible errors
|
2018-05-27 01:43:03 +01:00
|
|
|
if elide_lifetimes {
|
2020-11-15 17:06:58 -05:00
|
|
|
self.resolve_elided_lifetimes(&lifetimes);
|
2017-01-25 17:32:44 +02:00
|
|
|
} else {
|
2018-05-27 16:56:01 +01:00
|
|
|
lifetimes.iter().for_each(|lt| self.visit_lifetime(lt));
|
2017-01-25 17:32:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out if this is a type/trait segment,
|
|
|
|
// which requires object lifetime defaults.
|
|
|
|
let parent_def_id = |this: &mut Self, def_id: DefId| {
|
2017-11-23 08:05:58 -05:00
|
|
|
let def_key = this.tcx.def_key(def_id);
|
2019-12-24 17:38:22 -05:00
|
|
|
DefId { krate: def_id.krate, index: def_key.parent.expect("missing parent") }
|
2017-01-25 17:32:44 +02:00
|
|
|
};
|
2019-04-20 19:36:05 +03:00
|
|
|
let type_def_id = match res {
|
2019-12-24 17:38:22 -05:00
|
|
|
Res::Def(DefKind::AssocTy, def_id) if depth == 1 => Some(parent_def_id(self, def_id)),
|
|
|
|
Res::Def(DefKind::Variant, def_id) if depth == 0 => Some(parent_def_id(self, def_id)),
|
2020-04-16 17:38:52 -07:00
|
|
|
Res::Def(
|
|
|
|
DefKind::Struct
|
|
|
|
| DefKind::Union
|
|
|
|
| DefKind::Enum
|
|
|
|
| DefKind::TyAlias
|
|
|
|
| DefKind::Trait,
|
|
|
|
def_id,
|
|
|
|
) if depth == 0 => Some(def_id),
|
2017-12-11 09:00:05 -05:00
|
|
|
_ => None,
|
2017-01-25 17:32:44 +02:00
|
|
|
};
|
|
|
|
|
2019-08-12 10:41:05 -04:00
|
|
|
debug!("visit_segment_args: type_def_id={:?}", type_def_id);
|
|
|
|
|
|
|
|
// Compute a vector of defaults, one for each type parameter,
|
|
|
|
// per the rules given in RFCs 599 and 1156. Example:
|
|
|
|
//
|
|
|
|
// ```rust
|
|
|
|
// struct Foo<'a, T: 'a, U> { }
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// If you have `Foo<'x, dyn Bar, dyn Baz>`, we want to default
|
|
|
|
// `dyn Bar` to `dyn Bar + 'x` (because of the `T: 'a` bound)
|
|
|
|
// and `dyn Baz` to `dyn Baz + 'static` (because there is no
|
|
|
|
// such bound).
|
|
|
|
//
|
|
|
|
// Therefore, we would compute `object_lifetime_defaults` to a
|
|
|
|
// vector like `['x, 'static]`. Note that the vector only
|
|
|
|
// includes type parameters.
|
2021-02-25 04:13:42 +03:00
|
|
|
let object_lifetime_defaults = type_def_id.map_or_else(Vec::new, |def_id| {
|
|
|
|
let in_body = {
|
|
|
|
let mut scope = self.scope;
|
|
|
|
loop {
|
|
|
|
match *scope {
|
|
|
|
Scope::Root => break false,
|
|
|
|
|
|
|
|
Scope::Body { .. } => break true,
|
|
|
|
|
|
|
|
Scope::Binder { s, .. }
|
|
|
|
| Scope::Elision { s, .. }
|
2020-10-26 14:18:31 -04:00
|
|
|
| Scope::ObjectLifetimeDefault { s, .. }
|
2021-04-05 00:10:09 -04:00
|
|
|
| Scope::Supertrait { s, .. }
|
|
|
|
| Scope::TraitRefBoundary { s, .. } => {
|
2021-02-25 04:13:42 +03:00
|
|
|
scope = s;
|
2017-01-25 17:32:44 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-25 04:13:42 +03:00
|
|
|
}
|
|
|
|
};
|
2017-01-25 17:32:44 +02:00
|
|
|
|
2021-02-25 04:13:42 +03:00
|
|
|
let map = &self.map;
|
2021-02-27 21:31:56 -05:00
|
|
|
let set_to_region = |set: &ObjectLifetimeDefault| match *set {
|
|
|
|
Set1::Empty => {
|
|
|
|
if in_body {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(Region::Static)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Set1::One(r) => {
|
|
|
|
let lifetimes = generic_args.args.iter().filter_map(|arg| match arg {
|
|
|
|
GenericArg::Lifetime(lt) => Some(lt),
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
r.subst(lifetimes, map)
|
|
|
|
}
|
|
|
|
Set1::Many => None,
|
|
|
|
};
|
|
|
|
if let Some(def_id) = def_id.as_local() {
|
2021-02-25 04:13:42 +03:00
|
|
|
let id = self.tcx.hir().local_def_id_to_hir_id(def_id);
|
2022-01-31 19:55:34 +01:00
|
|
|
self.tcx
|
2022-02-01 18:28:24 +01:00
|
|
|
.object_lifetime_defaults(id.owner)
|
2022-01-31 19:55:34 +01:00
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.map(set_to_region)
|
|
|
|
.collect()
|
2021-02-25 04:13:42 +03:00
|
|
|
} else {
|
|
|
|
let tcx = self.tcx;
|
2021-02-27 21:31:56 -05:00
|
|
|
self.xcrate_object_lifetime_defaults
|
|
|
|
.entry(def_id)
|
|
|
|
.or_insert_with(|| {
|
|
|
|
tcx.generics_of(def_id)
|
|
|
|
.params
|
|
|
|
.iter()
|
|
|
|
.filter_map(|param| match param.kind {
|
|
|
|
GenericParamDefKind::Type { object_lifetime_default, .. } => {
|
|
|
|
Some(object_lifetime_default)
|
|
|
|
}
|
2021-12-13 19:14:35 +00:00
|
|
|
GenericParamDefKind::Const { .. } => Some(Set1::Empty),
|
|
|
|
GenericParamDefKind::Lifetime => None,
|
2021-02-27 21:31:56 -05:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.iter()
|
|
|
|
.map(set_to_region)
|
|
|
|
.collect()
|
|
|
|
}
|
2021-02-25 04:13:42 +03:00
|
|
|
});
|
2017-01-25 17:32:44 +02:00
|
|
|
|
2019-08-12 10:41:05 -04:00
|
|
|
debug!("visit_segment_args: object_lifetime_defaults={:?}", object_lifetime_defaults);
|
|
|
|
|
2018-05-27 01:43:03 +01:00
|
|
|
let mut i = 0;
|
2019-12-01 17:10:12 +01:00
|
|
|
for arg in generic_args.args {
|
2018-05-27 01:43:03 +01:00
|
|
|
match arg {
|
|
|
|
GenericArg::Lifetime(_) => {}
|
|
|
|
GenericArg::Type(ty) => {
|
|
|
|
if let Some(<) = object_lifetime_defaults.get(i) {
|
2019-12-24 17:38:22 -05:00
|
|
|
let scope = Scope::ObjectLifetimeDefault { lifetime: lt, s: self.scope };
|
2021-12-04 21:20:58 +01:00
|
|
|
self.with(scope, |this| this.visit_ty(ty));
|
2018-05-27 01:43:03 +01:00
|
|
|
} else {
|
|
|
|
self.visit_ty(ty);
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
}
|
2019-02-15 22:25:42 +00:00
|
|
|
GenericArg::Const(ct) => {
|
|
|
|
self.visit_anon_const(&ct.value);
|
2021-12-13 19:14:35 +00:00
|
|
|
i += 1;
|
2019-02-15 22:25:42 +00:00
|
|
|
}
|
2021-04-24 21:41:57 +00:00
|
|
|
GenericArg::Infer(inf) => {
|
|
|
|
self.visit_id(inf.hir_id);
|
2021-12-13 19:14:35 +00:00
|
|
|
i += 1;
|
2021-04-24 21:41:57 +00:00
|
|
|
}
|
2017-01-25 17:32:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 10:41:05 -04:00
|
|
|
// Hack: when resolving the type `XX` in binding like `dyn
|
|
|
|
// Foo<'b, Item = XX>`, the current object-lifetime default
|
|
|
|
// would be to examine the trait `Foo` to check whether it has
|
|
|
|
// a lifetime bound declared on `Item`. e.g., if `Foo` is
|
|
|
|
// declared like so, then the default object lifetime bound in
|
|
|
|
// `XX` should be `'b`:
|
|
|
|
//
|
|
|
|
// ```rust
|
|
|
|
// trait Foo<'a> {
|
|
|
|
// type Item: 'a;
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// but if we just have `type Item;`, then it would be
|
|
|
|
// `'static`. However, we don't get all of this logic correct.
|
|
|
|
//
|
|
|
|
// Instead, we do something hacky: if there are no lifetime parameters
|
|
|
|
// to the trait, then we simply use a default object lifetime
|
|
|
|
// bound of `'static`, because there is no other possibility. On the other hand,
|
|
|
|
// if there ARE lifetime parameters, then we require the user to give an
|
|
|
|
// explicit bound for now.
|
|
|
|
//
|
|
|
|
// This is intended to leave room for us to implement the
|
|
|
|
// correct behavior in the future.
|
2020-10-26 21:02:48 -04:00
|
|
|
let has_lifetime_parameter =
|
|
|
|
generic_args.args.iter().any(|arg| matches!(arg, GenericArg::Lifetime(_)));
|
2019-08-12 10:41:05 -04:00
|
|
|
|
2021-03-26 17:40:15 -04:00
|
|
|
// Resolve lifetimes found in the bindings, so either in the type `XX` in `Item = XX` or
|
|
|
|
// in the trait ref `YY<...>` in `Item: YY<...>`.
|
2020-10-26 14:18:31 -04:00
|
|
|
for binding in generic_args.bindings {
|
2019-08-12 10:41:05 -04:00
|
|
|
let scope = Scope::ObjectLifetimeDefault {
|
2019-12-24 17:38:22 -05:00
|
|
|
lifetime: if has_lifetime_parameter { None } else { Some(Region::Static) },
|
2019-08-12 10:41:05 -04:00
|
|
|
s: self.scope,
|
|
|
|
};
|
2020-10-26 14:18:31 -04:00
|
|
|
if let Some(type_def_id) = type_def_id {
|
2021-03-26 17:40:15 -04:00
|
|
|
let lifetimes = LifetimeContext::supertrait_hrtb_lifetimes(
|
|
|
|
self.tcx,
|
|
|
|
type_def_id,
|
|
|
|
binding.ident,
|
|
|
|
);
|
2021-12-04 21:20:58 +01:00
|
|
|
self.with(scope, |this| {
|
2021-08-31 00:54:48 +02:00
|
|
|
let scope = Scope::Supertrait {
|
|
|
|
lifetimes: lifetimes.unwrap_or_default(),
|
|
|
|
s: this.scope,
|
|
|
|
};
|
2021-12-04 21:20:58 +01:00
|
|
|
this.with(scope, |this| this.visit_assoc_type_binding(binding));
|
2021-03-26 17:40:15 -04:00
|
|
|
});
|
2020-10-26 14:18:31 -04:00
|
|
|
} else {
|
2021-12-04 21:20:58 +01:00
|
|
|
self.with(scope, |this| this.visit_assoc_type_binding(binding));
|
2020-10-26 14:18:31 -04:00
|
|
|
}
|
2017-12-11 09:00:05 -05:00
|
|
|
}
|
2017-01-25 17:32:44 +02:00
|
|
|
}
|
|
|
|
|
2021-03-26 17:40:15 -04:00
|
|
|
/// Returns all the late-bound vars that come into scope from supertrait HRTBs, based on the
|
|
|
|
/// associated type name and starting trait.
|
|
|
|
/// For example, imagine we have
|
2022-04-15 15:04:34 -07:00
|
|
|
/// ```ignore (illustrative)
|
2021-03-26 17:40:15 -04:00
|
|
|
/// trait Foo<'a, 'b> {
|
|
|
|
/// type As;
|
|
|
|
/// }
|
|
|
|
/// trait Bar<'b>: for<'a> Foo<'a, 'b> {}
|
|
|
|
/// trait Bar: for<'b> Bar<'b> {}
|
|
|
|
/// ```
|
|
|
|
/// In this case, if we wanted to the supertrait HRTB lifetimes for `As` on
|
|
|
|
/// the starting trait `Bar`, we would return `Some(['b, 'a])`.
|
|
|
|
fn supertrait_hrtb_lifetimes(
|
2020-10-26 14:18:31 -04:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
def_id: DefId,
|
|
|
|
assoc_name: Ident,
|
|
|
|
) -> Option<Vec<ty::BoundVariableKind>> {
|
2021-03-26 17:40:15 -04:00
|
|
|
let trait_defines_associated_type_named = |trait_def_id: DefId| {
|
|
|
|
tcx.associated_items(trait_def_id)
|
|
|
|
.find_by_name_and_kind(tcx, assoc_name, ty::AssocKind::Type, trait_def_id)
|
|
|
|
.is_some()
|
|
|
|
};
|
|
|
|
|
|
|
|
use smallvec::{smallvec, SmallVec};
|
|
|
|
let mut stack: SmallVec<[(DefId, SmallVec<[ty::BoundVariableKind; 8]>); 8]> =
|
|
|
|
smallvec![(def_id, smallvec![])];
|
|
|
|
let mut visited: FxHashSet<DefId> = FxHashSet::default();
|
|
|
|
loop {
|
2022-02-19 00:48:49 +01:00
|
|
|
let Some((def_id, bound_vars)) = stack.pop() else {
|
|
|
|
break None;
|
2021-03-26 17:40:15 -04:00
|
|
|
};
|
2021-04-06 15:34:29 -04:00
|
|
|
// See issue #83753. If someone writes an associated type on a non-trait, just treat it as
|
|
|
|
// there being no supertrait HRTBs.
|
|
|
|
match tcx.def_kind(def_id) {
|
|
|
|
DefKind::Trait | DefKind::TraitAlias | DefKind::Impl => {}
|
|
|
|
_ => break None,
|
|
|
|
}
|
|
|
|
|
2021-03-26 17:40:15 -04:00
|
|
|
if trait_defines_associated_type_named(def_id) {
|
|
|
|
break Some(bound_vars.into_iter().collect());
|
|
|
|
}
|
|
|
|
let predicates =
|
|
|
|
tcx.super_predicates_that_define_assoc_type((def_id, Some(assoc_name)));
|
|
|
|
let obligations = predicates.predicates.iter().filter_map(|&(pred, _)| {
|
|
|
|
let bound_predicate = pred.kind();
|
|
|
|
match bound_predicate.skip_binder() {
|
2021-07-22 21:56:07 +08:00
|
|
|
ty::PredicateKind::Trait(data) => {
|
2021-03-26 17:40:15 -04:00
|
|
|
// The order here needs to match what we would get from `subst_supertrait`
|
|
|
|
let pred_bound_vars = bound_predicate.bound_vars();
|
|
|
|
let mut all_bound_vars = bound_vars.clone();
|
|
|
|
all_bound_vars.extend(pred_bound_vars.iter());
|
|
|
|
let super_def_id = data.trait_ref.def_id;
|
|
|
|
Some((super_def_id, all_bound_vars))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
});
|
2020-10-26 14:18:31 -04:00
|
|
|
|
2021-03-26 17:40:15 -04:00
|
|
|
let obligations = obligations.filter(|o| visited.insert(o.0));
|
|
|
|
stack.extend(obligations);
|
|
|
|
}
|
2020-10-26 14:18:31 -04:00
|
|
|
}
|
|
|
|
|
2021-02-27 21:31:56 -05:00
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
2019-11-30 17:46:46 +01:00
|
|
|
fn visit_fn_like_elision(
|
|
|
|
&mut self,
|
|
|
|
inputs: &'tcx [hir::Ty<'tcx>],
|
|
|
|
output: Option<&'tcx hir::Ty<'tcx>>,
|
|
|
|
) {
|
2020-11-15 17:06:58 -05:00
|
|
|
debug!("visit_fn_like_elision: enter");
|
2020-10-26 14:18:31 -04:00
|
|
|
let mut scope = &*self.scope;
|
2021-03-26 17:40:15 -04:00
|
|
|
let hir_id = loop {
|
2020-10-26 14:18:31 -04:00
|
|
|
match scope {
|
2022-03-18 22:04:01 +03:00
|
|
|
Scope::Binder { hir_id, allow_late_bound: true, .. } => {
|
2021-03-26 17:40:15 -04:00
|
|
|
break *hir_id;
|
2020-10-26 14:18:31 -04:00
|
|
|
}
|
|
|
|
Scope::ObjectLifetimeDefault { ref s, .. }
|
|
|
|
| Scope::Elision { ref s, .. }
|
2021-04-05 00:10:09 -04:00
|
|
|
| Scope::Supertrait { ref s, .. }
|
|
|
|
| Scope::TraitRefBoundary { ref s, .. } => {
|
2020-10-26 14:18:31 -04:00
|
|
|
scope = *s;
|
|
|
|
}
|
2022-03-18 22:04:01 +03:00
|
|
|
Scope::Root
|
|
|
|
| Scope::Body { .. }
|
|
|
|
| Scope::Binder { allow_late_bound: false, .. } => {
|
2021-06-18 18:08:26 +02:00
|
|
|
// See issues #83907 and #83693. Just bail out from looking inside.
|
2022-03-18 22:04:01 +03:00
|
|
|
// See the issue #95023 for not allowing late bound
|
2021-04-06 15:34:29 -04:00
|
|
|
self.tcx.sess.delay_span_bug(
|
|
|
|
rustc_span::DUMMY_SP,
|
|
|
|
"In fn_like_elision without appropriate scope above",
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2020-10-26 14:18:31 -04:00
|
|
|
}
|
2020-11-15 17:06:58 -05:00
|
|
|
};
|
2021-03-26 17:40:15 -04:00
|
|
|
// While not strictly necessary, we gather anon lifetimes *before* actually
|
|
|
|
// visiting the argument types.
|
2020-10-26 14:18:31 -04:00
|
|
|
let mut gather = GatherAnonLifetimes { anon_count: 0 };
|
|
|
|
for input in inputs {
|
|
|
|
gather.visit_ty(input);
|
|
|
|
}
|
2021-08-20 13:36:04 +00:00
|
|
|
trace!(?gather.anon_count);
|
2021-03-26 17:40:15 -04:00
|
|
|
let late_bound_vars = self.map.late_bound_vars.entry(hir_id).or_default();
|
|
|
|
let named_late_bound_vars = late_bound_vars.len() as u32;
|
|
|
|
late_bound_vars.extend(
|
2020-10-26 14:18:31 -04:00
|
|
|
(0..gather.anon_count).map(|var| ty::BoundVariableKind::Region(ty::BrAnon(var))),
|
|
|
|
);
|
2020-11-15 17:06:58 -05:00
|
|
|
let arg_scope = Scope::Elision {
|
|
|
|
elide: Elide::FreshLateAnon(named_late_bound_vars, Cell::new(0)),
|
|
|
|
s: self.scope,
|
|
|
|
};
|
2021-12-04 21:20:58 +01:00
|
|
|
self.with(arg_scope, |this| {
|
2017-01-13 15:09:56 +02:00
|
|
|
for input in inputs {
|
|
|
|
this.visit_ty(input);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-02-19 00:48:49 +01:00
|
|
|
let Some(output) = output else { return };
|
2017-01-13 15:09:56 +02:00
|
|
|
|
2021-02-27 21:31:56 -05:00
|
|
|
debug!("determine output");
|
2018-05-03 18:43:28 -04:00
|
|
|
|
2017-01-13 15:09:56 +02:00
|
|
|
// Figure out if there's a body we can get argument names from,
|
|
|
|
// and whether there's a `self` argument (treated specially).
|
|
|
|
let mut assoc_item_kind = None;
|
|
|
|
let mut impl_self = None;
|
2019-06-24 09:46:38 +02:00
|
|
|
let parent = self.tcx.hir().get_parent_node(output.hir_id);
|
2019-06-20 10:39:19 +02:00
|
|
|
let body = match self.tcx.hir().get(parent) {
|
2017-01-13 15:09:56 +02:00
|
|
|
// `fn` definitions and methods.
|
2019-12-24 17:38:22 -05:00
|
|
|
Node::Item(&hir::Item { kind: hir::ItemKind::Fn(.., body), .. }) => Some(body),
|
2017-01-13 15:09:56 +02:00
|
|
|
|
2020-03-03 12:46:22 -06:00
|
|
|
Node::TraitItem(&hir::TraitItem { kind: hir::TraitItemKind::Fn(_, ref m), .. }) => {
|
2019-12-24 17:38:22 -05:00
|
|
|
if let hir::ItemKind::Trait(.., ref trait_items) =
|
2021-10-21 19:41:47 +02:00
|
|
|
self.tcx.hir().expect_item(self.tcx.hir().get_parent_item(parent)).kind
|
2017-12-11 09:00:05 -05:00
|
|
|
{
|
2019-12-24 17:38:22 -05:00
|
|
|
assoc_item_kind =
|
2021-01-30 20:46:50 +01:00
|
|
|
trait_items.iter().find(|ti| ti.id.hir_id() == parent).map(|ti| ti.kind);
|
2017-01-13 15:09:56 +02:00
|
|
|
}
|
|
|
|
match *m {
|
2020-03-05 09:57:34 -06:00
|
|
|
hir::TraitFn::Required(_) => None,
|
|
|
|
hir::TraitFn::Provided(body) => Some(body),
|
2017-01-13 15:09:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 09:57:34 -06:00
|
|
|
Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(_, body), .. }) => {
|
2020-11-22 17:46:21 -05:00
|
|
|
if let hir::ItemKind::Impl(hir::Impl { ref self_ty, ref items, .. }) =
|
2021-10-21 19:41:47 +02:00
|
|
|
self.tcx.hir().expect_item(self.tcx.hir().get_parent_item(parent)).kind
|
2017-12-11 09:00:05 -05:00
|
|
|
{
|
2018-10-02 18:05:06 +02:00
|
|
|
impl_self = Some(self_ty);
|
2019-12-24 17:38:22 -05:00
|
|
|
assoc_item_kind =
|
2021-01-30 23:25:03 +01:00
|
|
|
items.iter().find(|ii| ii.id.hir_id() == parent).map(|ii| ii.kind);
|
2017-01-13 15:09:56 +02:00
|
|
|
}
|
|
|
|
Some(body)
|
|
|
|
}
|
|
|
|
|
2017-08-05 03:13:38 +03:00
|
|
|
// Foreign functions, `fn(...) -> R` and `Trait(...) -> R` (both types and bounds).
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::ForeignItem(_) | Node::Ty(_) | Node::TraitRef(_) => None,
|
2017-01-13 15:09:56 +02:00
|
|
|
// Everything else (only closures?) doesn't
|
|
|
|
// actually enjoy elision in return types.
|
|
|
|
_ => {
|
|
|
|
self.visit_ty(output);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let has_self = match assoc_item_kind {
|
2020-04-01 10:09:50 +08:00
|
|
|
Some(hir::AssocItemKind::Fn { has_self }) => has_self,
|
2017-12-11 09:00:05 -05:00
|
|
|
_ => false,
|
2017-01-13 15:09:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// In accordance with the rules for lifetime elision, we can determine
|
|
|
|
// what region to use for elision in the output type in two ways.
|
|
|
|
// First (determined here), if `self` is by-reference, then the
|
|
|
|
// implied output region is the region of the self parameter.
|
|
|
|
if has_self {
|
2019-05-29 17:37:28 +09:00
|
|
|
struct SelfVisitor<'a> {
|
|
|
|
map: &'a NamedRegionMap,
|
2019-11-30 17:46:46 +01:00
|
|
|
impl_self: Option<&'a hir::TyKind<'a>>,
|
2019-05-29 18:50:24 +09:00
|
|
|
lifetime: Set1<Region>,
|
2019-05-29 17:37:28 +09:00
|
|
|
}
|
2017-01-13 15:09:56 +02:00
|
|
|
|
2019-05-29 17:37:28 +09:00
|
|
|
impl SelfVisitor<'_> {
|
|
|
|
// Look for `self: &'a Self` - also desugared from `&'a self`,
|
|
|
|
// and if that matches, use it for elision and return early.
|
|
|
|
fn is_self_ty(&self, res: Res) -> bool {
|
2022-02-09 11:03:27 +00:00
|
|
|
if let Res::SelfTy { .. } = res {
|
2019-05-29 17:37:28 +09:00
|
|
|
return true;
|
2017-01-13 15:09:56 +02:00
|
|
|
}
|
|
|
|
|
2019-05-29 17:37:28 +09:00
|
|
|
// Can't always rely on literal (or implied) `Self` due
|
|
|
|
// to the way elision rules were originally specified.
|
|
|
|
if let Some(&hir::TyKind::Path(hir::QPath::Resolved(None, ref path))) =
|
|
|
|
self.impl_self
|
|
|
|
{
|
|
|
|
match path.res {
|
2020-07-07 11:12:44 -04:00
|
|
|
// Permit the types that unambiguously always
|
2019-05-29 17:37:28 +09:00
|
|
|
// result in the same type constructor being used
|
|
|
|
// (it can't differ between `Self` and `self`).
|
2020-04-16 17:38:52 -07:00
|
|
|
Res::Def(DefKind::Struct | DefKind::Union | DefKind::Enum, _)
|
2019-12-24 17:38:22 -05:00
|
|
|
| Res::PrimTy(_) => return res == path.res,
|
2019-05-29 17:37:28 +09:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2017-01-13 15:09:56 +02:00
|
|
|
|
2019-05-29 17:37:28 +09:00
|
|
|
false
|
|
|
|
}
|
2019-05-27 22:06:08 +09:00
|
|
|
}
|
|
|
|
|
2019-05-29 17:37:28 +09:00
|
|
|
impl<'a> Visitor<'a> for SelfVisitor<'a> {
|
2019-11-30 17:46:46 +01:00
|
|
|
fn visit_ty(&mut self, ty: &'a hir::Ty<'a>) {
|
2019-09-26 17:25:31 +01:00
|
|
|
if let hir::TyKind::Rptr(lifetime_ref, ref mt) = ty.kind {
|
|
|
|
if let hir::TyKind::Path(hir::QPath::Resolved(None, ref path)) = mt.ty.kind
|
2019-05-27 22:06:08 +09:00
|
|
|
{
|
2019-05-29 17:37:28 +09:00
|
|
|
if self.is_self_ty(path.res) {
|
2019-05-29 18:50:24 +09:00
|
|
|
if let Some(lifetime) = self.map.defs.get(&lifetime_ref.hir_id) {
|
|
|
|
self.lifetime.insert(*lifetime);
|
|
|
|
}
|
2019-05-27 01:43:12 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-27 22:06:08 +09:00
|
|
|
intravisit::walk_ty(self, ty)
|
2019-05-27 01:43:12 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 22:06:08 +09:00
|
|
|
let mut visitor = SelfVisitor {
|
|
|
|
map: self.map,
|
2019-09-26 17:25:31 +01:00
|
|
|
impl_self: impl_self.map(|ty| &ty.kind),
|
2019-05-29 18:50:24 +09:00
|
|
|
lifetime: Set1::Empty,
|
2019-05-27 22:06:08 +09:00
|
|
|
};
|
|
|
|
visitor.visit_ty(&inputs[0]);
|
2019-05-29 18:50:24 +09:00
|
|
|
if let Set1::One(lifetime) = visitor.lifetime {
|
2019-12-24 17:38:22 -05:00
|
|
|
let scope = Scope::Elision { elide: Elide::Exact(lifetime), s: self.scope };
|
2021-12-04 21:20:58 +01:00
|
|
|
self.with(scope, |this| this.visit_ty(output));
|
2019-05-27 22:06:08 +09:00
|
|
|
return;
|
2017-01-13 15:09:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Second, if there was exactly one lifetime (either a substitution or a
|
|
|
|
// reference) in the arguments, then any anonymous regions in the output
|
|
|
|
// have that lifetime.
|
|
|
|
let mut possible_implied_output_region = None;
|
|
|
|
let mut lifetime_count = 0;
|
2017-12-11 09:00:05 -05:00
|
|
|
let arg_lifetimes = inputs
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.skip(has_self as usize)
|
|
|
|
.map(|(i, input)| {
|
|
|
|
let mut gather = GatherLifetimes {
|
|
|
|
map: self.map,
|
2018-06-10 14:44:15 +02:00
|
|
|
outer_index: ty::INNERMOST,
|
2017-12-11 09:00:05 -05:00
|
|
|
have_bound_regions: false,
|
2018-10-16 16:57:53 +02:00
|
|
|
lifetimes: Default::default(),
|
2017-12-11 09:00:05 -05:00
|
|
|
};
|
|
|
|
gather.visit_ty(input);
|
2017-01-13 15:09:56 +02:00
|
|
|
|
2017-12-11 09:00:05 -05:00
|
|
|
lifetime_count += gather.lifetimes.len();
|
2017-01-13 15:09:56 +02:00
|
|
|
|
2017-12-11 09:00:05 -05:00
|
|
|
if lifetime_count == 1 && gather.lifetimes.len() == 1 {
|
|
|
|
// there's a chance that the unique lifetime of this
|
|
|
|
// iteration will be the appropriate lifetime for output
|
|
|
|
// parameters, so lets store it.
|
|
|
|
possible_implied_output_region = gather.lifetimes.iter().cloned().next();
|
|
|
|
}
|
2017-01-13 15:09:56 +02:00
|
|
|
|
2017-12-11 09:00:05 -05:00
|
|
|
ElisionFailureInfo {
|
|
|
|
parent: body,
|
|
|
|
index: i,
|
|
|
|
lifetime_count: gather.lifetimes.len(),
|
|
|
|
have_bound_regions: gather.have_bound_regions,
|
2020-01-27 11:48:52 -08:00
|
|
|
span: input.span,
|
2017-12-11 09:00:05 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2017-01-13 15:09:56 +02:00
|
|
|
|
|
|
|
let elide = if lifetime_count == 1 {
|
|
|
|
Elide::Exact(possible_implied_output_region.unwrap())
|
|
|
|
} else {
|
|
|
|
Elide::Error(arg_lifetimes)
|
|
|
|
};
|
|
|
|
|
2021-02-27 21:31:56 -05:00
|
|
|
debug!(?elide);
|
2018-05-03 18:43:28 -04:00
|
|
|
|
2019-12-24 17:38:22 -05:00
|
|
|
let scope = Scope::Elision { elide, s: self.scope };
|
2021-12-04 21:20:58 +01:00
|
|
|
self.with(scope, |this| this.visit_ty(output));
|
2017-01-13 15:09:56 +02:00
|
|
|
|
|
|
|
struct GatherLifetimes<'a> {
|
|
|
|
map: &'a NamedRegionMap,
|
2018-05-28 09:53:10 -04:00
|
|
|
outer_index: ty::DebruijnIndex,
|
2017-01-13 15:09:56 +02:00
|
|
|
have_bound_regions: bool,
|
|
|
|
lifetimes: FxHashSet<Region>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'v, 'a> Visitor<'v> for GatherLifetimes<'a> {
|
2019-11-30 17:46:46 +01:00
|
|
|
fn visit_ty(&mut self, ty: &hir::Ty<'_>) {
|
2019-09-26 17:25:31 +01:00
|
|
|
if let hir::TyKind::BareFn(_) = ty.kind {
|
2018-05-28 09:53:10 -04:00
|
|
|
self.outer_index.shift_in(1);
|
2017-01-17 16:46:23 +02:00
|
|
|
}
|
2019-09-26 17:25:31 +01:00
|
|
|
match ty.kind {
|
2021-03-13 15:44:29 +03:00
|
|
|
hir::TyKind::TraitObject(bounds, ref lifetime, _) => {
|
2018-11-30 15:53:44 +00:00
|
|
|
for bound in bounds {
|
|
|
|
self.visit_poly_trait_ref(bound, hir::TraitBoundModifier::None);
|
|
|
|
}
|
2017-01-25 17:32:44 +02:00
|
|
|
|
2018-11-30 15:53:44 +00:00
|
|
|
// Stay on the safe side and don't include the object
|
|
|
|
// lifetime default (which may not end up being used).
|
|
|
|
if !lifetime.is_elided() {
|
|
|
|
self.visit_lifetime(lifetime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
intravisit::walk_ty(self, ty);
|
2017-01-25 17:32:44 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-26 17:25:31 +01:00
|
|
|
if let hir::TyKind::BareFn(_) = ty.kind {
|
2018-05-28 09:53:10 -04:00
|
|
|
self.outer_index.shift_out(1);
|
2017-01-17 16:46:23 +02:00
|
|
|
}
|
2017-01-13 15:09:56 +02:00
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
fn visit_generic_param(&mut self, param: &hir::GenericParam<'_>) {
|
2018-05-26 00:27:54 +01:00
|
|
|
if let hir::GenericParamKind::Lifetime { .. } = param.kind {
|
2018-05-30 14:36:53 +03:00
|
|
|
// FIXME(eddyb) Do we want this? It only makes a difference
|
|
|
|
// if this `for<'a>` lifetime parameter is never used.
|
|
|
|
self.have_bound_regions = true;
|
2017-10-16 21:07:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
intravisit::walk_generic_param(self, param);
|
|
|
|
}
|
|
|
|
|
2017-12-11 09:00:05 -05:00
|
|
|
fn visit_poly_trait_ref(
|
|
|
|
&mut self,
|
2019-11-30 17:46:46 +01:00
|
|
|
trait_ref: &hir::PolyTraitRef<'_>,
|
2017-12-11 09:00:05 -05:00
|
|
|
modifier: hir::TraitBoundModifier,
|
|
|
|
) {
|
2018-05-28 09:53:10 -04:00
|
|
|
self.outer_index.shift_in(1);
|
2017-01-13 15:09:56 +02:00
|
|
|
intravisit::walk_poly_trait_ref(self, trait_ref, modifier);
|
2018-05-28 09:53:10 -04:00
|
|
|
self.outer_index.shift_out(1);
|
2017-01-13 15:09:56 +02:00
|
|
|
}
|
|
|
|
|
2020-08-04 14:19:28 +01:00
|
|
|
fn visit_param_bound(&mut self, bound: &hir::GenericBound<'_>) {
|
|
|
|
if let hir::GenericBound::LangItemTrait { .. } = bound {
|
|
|
|
self.outer_index.shift_in(1);
|
|
|
|
intravisit::walk_param_bound(self, bound);
|
|
|
|
self.outer_index.shift_out(1);
|
|
|
|
} else {
|
|
|
|
intravisit::walk_param_bound(self, bound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 15:09:56 +02:00
|
|
|
fn visit_lifetime(&mut self, lifetime_ref: &hir::Lifetime) {
|
2019-02-18 10:59:17 +01:00
|
|
|
if let Some(&lifetime) = self.map.defs.get(&lifetime_ref.hir_id) {
|
2017-01-13 15:09:56 +02:00
|
|
|
match lifetime {
|
2022-02-20 10:22:57 -08:00
|
|
|
Region::LateBound(debruijn, _, _)
|
2020-11-15 17:06:58 -05:00
|
|
|
| Region::LateBoundAnon(debruijn, _, _)
|
2018-05-28 09:53:10 -04:00
|
|
|
if debruijn < self.outer_index =>
|
2017-12-11 09:00:05 -05:00
|
|
|
{
|
2017-01-13 15:09:56 +02:00
|
|
|
self.have_bound_regions = true;
|
|
|
|
}
|
|
|
|
_ => {
|
2021-04-05 00:10:09 -04:00
|
|
|
// FIXME(jackh726): nested trait refs?
|
2019-12-24 17:38:22 -05:00
|
|
|
self.lifetimes.insert(lifetime.shifted_out_to_binder(self.outer_index));
|
2017-01-13 15:09:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-26 14:18:31 -04:00
|
|
|
|
|
|
|
struct GatherAnonLifetimes {
|
|
|
|
anon_count: u32,
|
|
|
|
}
|
|
|
|
impl<'v> Visitor<'v> for GatherAnonLifetimes {
|
2021-08-20 13:36:04 +00:00
|
|
|
#[instrument(skip(self), level = "trace")]
|
2020-10-26 14:18:31 -04:00
|
|
|
fn visit_ty(&mut self, ty: &hir::Ty<'_>) {
|
2021-03-26 17:40:15 -04:00
|
|
|
// If we enter a `BareFn`, then we enter a *new* binding scope
|
2020-10-26 14:18:31 -04:00
|
|
|
if let hir::TyKind::BareFn(_) = ty.kind {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
intravisit::walk_ty(self, ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_generic_args(
|
|
|
|
&mut self,
|
|
|
|
path_span: Span,
|
|
|
|
generic_args: &'v hir::GenericArgs<'v>,
|
|
|
|
) {
|
2022-03-30 15:14:15 -04:00
|
|
|
// parenthesized args enter a new elision scope
|
2020-10-26 14:18:31 -04:00
|
|
|
if generic_args.parenthesized {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
intravisit::walk_generic_args(self, path_span, generic_args)
|
|
|
|
}
|
|
|
|
|
2021-08-20 13:36:04 +00:00
|
|
|
#[instrument(skip(self), level = "trace")]
|
2020-10-26 14:18:31 -04:00
|
|
|
fn visit_lifetime(&mut self, lifetime_ref: &hir::Lifetime) {
|
|
|
|
if lifetime_ref.is_elided() {
|
|
|
|
self.anon_count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-13 15:09:56 +02:00
|
|
|
}
|
|
|
|
|
2020-11-15 17:06:58 -05:00
|
|
|
fn resolve_elided_lifetimes(&mut self, lifetime_refs: &[&'tcx hir::Lifetime]) {
|
2019-08-08 03:49:45 -04:00
|
|
|
debug!("resolve_elided_lifetimes(lifetime_refs={:?})", lifetime_refs);
|
|
|
|
|
2017-01-13 15:09:56 +02:00
|
|
|
if lifetime_refs.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut late_depth = 0;
|
|
|
|
let mut scope = self.scope;
|
2022-04-27 22:15:58 +02:00
|
|
|
let mut in_scope_lifetimes = FxIndexSet::default();
|
2017-01-13 15:09:56 +02:00
|
|
|
let error = loop {
|
|
|
|
match *scope {
|
|
|
|
// Do not assign any resolution, it will be inferred.
|
2022-03-10 23:12:35 +01:00
|
|
|
Scope::Body { .. } => return,
|
2017-01-13 15:09:56 +02:00
|
|
|
|
2022-03-10 23:12:35 +01:00
|
|
|
Scope::Root => break None,
|
2017-01-13 15:09:56 +02:00
|
|
|
|
2021-04-13 16:58:00 -04:00
|
|
|
Scope::Binder { s, ref lifetimes, scope_type, .. } => {
|
2019-03-12 18:18:24 -07:00
|
|
|
// collect named lifetimes for suggestions
|
2022-04-27 22:15:58 +02:00
|
|
|
in_scope_lifetimes.extend(lifetimes.keys().copied());
|
2021-04-13 16:58:00 -04:00
|
|
|
match scope_type {
|
2021-04-21 12:26:19 -04:00
|
|
|
BinderScopeType::Normal => late_depth += 1,
|
2021-04-13 16:58:00 -04:00
|
|
|
BinderScopeType::Concatenating => {}
|
|
|
|
}
|
2017-01-13 15:09:56 +02:00
|
|
|
scope = s;
|
|
|
|
}
|
|
|
|
|
2021-10-31 17:21:32 +01:00
|
|
|
Scope::Elision {
|
|
|
|
elide: Elide::FreshLateAnon(named_late_bound_vars, ref counter),
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
for lifetime_ref in lifetime_refs {
|
|
|
|
let lifetime =
|
|
|
|
Region::late_anon(named_late_bound_vars, counter).shifted(late_depth);
|
|
|
|
|
|
|
|
self.insert_lifetime(lifetime_ref, lifetime);
|
|
|
|
}
|
2022-03-10 23:12:35 +01:00
|
|
|
return;
|
2021-10-31 17:21:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Scope::Elision { elide: Elide::Exact(l), .. } => {
|
|
|
|
let lifetime = l.shifted(late_depth);
|
|
|
|
for lifetime_ref in lifetime_refs {
|
|
|
|
self.insert_lifetime(lifetime_ref, lifetime);
|
|
|
|
}
|
2022-03-10 23:12:35 +01:00
|
|
|
return;
|
2021-10-31 17:21:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Scope::Elision { elide: Elide::Error(ref e), ref s, .. } => {
|
|
|
|
let mut scope = s;
|
|
|
|
loop {
|
|
|
|
match scope {
|
|
|
|
Scope::Binder { ref lifetimes, s, .. } => {
|
|
|
|
// Collect named lifetimes for suggestions.
|
2022-04-27 22:15:58 +02:00
|
|
|
in_scope_lifetimes.extend(lifetimes.keys().copied());
|
2021-10-31 17:21:32 +01:00
|
|
|
scope = s;
|
|
|
|
}
|
|
|
|
Scope::ObjectLifetimeDefault { ref s, .. }
|
|
|
|
| Scope::Elision { ref s, .. }
|
|
|
|
| Scope::TraitRefBoundary { ref s, .. } => {
|
|
|
|
scope = s;
|
2019-03-12 14:57:13 -07:00
|
|
|
}
|
2021-10-31 17:21:32 +01:00
|
|
|
_ => break,
|
2019-03-12 14:57:13 -07:00
|
|
|
}
|
2017-01-13 15:09:56 +02:00
|
|
|
}
|
2022-03-10 23:12:35 +01:00
|
|
|
break Some(&e[..]);
|
2017-01-13 15:09:56 +02:00
|
|
|
}
|
2017-01-25 17:32:44 +02:00
|
|
|
|
2022-03-10 23:12:35 +01:00
|
|
|
Scope::Elision { elide: Elide::Forbid, .. } => break None,
|
2021-10-31 17:21:32 +01:00
|
|
|
|
2020-10-26 14:18:31 -04:00
|
|
|
Scope::ObjectLifetimeDefault { s, .. }
|
2021-04-12 09:12:10 -04:00
|
|
|
| Scope::Supertrait { s, .. }
|
|
|
|
| Scope::TraitRefBoundary { s, .. } => {
|
2017-01-25 17:32:44 +02:00
|
|
|
scope = s;
|
|
|
|
}
|
2017-01-13 15:09:56 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-18 21:01:44 +01:00
|
|
|
// If we specifically need the `scope_for_path` map, then we're in the
|
|
|
|
// diagnostic pass and we don't want to emit more errors.
|
|
|
|
if self.map.scope_for_path.is_some() {
|
|
|
|
self.tcx.sess.delay_span_bug(
|
|
|
|
rustc_span::DUMMY_SP,
|
|
|
|
"Encountered unexpected errors during diagnostics related part",
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-07 19:44:32 +02:00
|
|
|
let mut spans: Vec<_> = lifetime_refs.iter().map(|lt| lt.span).collect();
|
|
|
|
spans.sort();
|
|
|
|
let mut spans_dedup = spans.clone();
|
|
|
|
spans_dedup.dedup();
|
2021-05-09 22:31:49 +02:00
|
|
|
let spans_with_counts: Vec<_> = spans_dedup
|
|
|
|
.into_iter()
|
|
|
|
.map(|sp| (sp, spans.iter().filter(|nsp| *nsp == &sp).count()))
|
|
|
|
.collect();
|
2021-05-07 19:44:32 +02:00
|
|
|
|
|
|
|
let mut err = self.report_missing_lifetime_specifiers(spans.clone(), lifetime_refs.len());
|
2017-01-13 15:09:56 +02:00
|
|
|
|
2020-04-16 21:33:42 -07:00
|
|
|
self.add_missing_lifetime_specifiers_label(
|
|
|
|
&mut err,
|
2021-05-09 22:31:49 +02:00
|
|
|
spans_with_counts,
|
2022-04-27 22:15:58 +02:00
|
|
|
in_scope_lifetimes,
|
|
|
|
error,
|
2020-04-16 21:33:42 -07:00
|
|
|
);
|
2017-01-13 15:09:56 +02:00
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
|
2017-11-23 08:05:58 -05:00
|
|
|
fn resolve_object_lifetime_default(&mut self, lifetime_ref: &'tcx hir::Lifetime) {
|
2019-08-08 03:49:45 -04:00
|
|
|
debug!("resolve_object_lifetime_default(lifetime_ref={:?})", lifetime_ref);
|
2017-01-25 17:32:44 +02:00
|
|
|
let mut late_depth = 0;
|
|
|
|
let mut scope = self.scope;
|
|
|
|
let lifetime = loop {
|
|
|
|
match *scope {
|
2021-04-13 16:58:00 -04:00
|
|
|
Scope::Binder { s, scope_type, .. } => {
|
|
|
|
match scope_type {
|
2021-04-21 12:26:19 -04:00
|
|
|
BinderScopeType::Normal => late_depth += 1,
|
2021-04-13 16:58:00 -04:00
|
|
|
BinderScopeType::Concatenating => {}
|
|
|
|
}
|
2017-01-25 17:32:44 +02:00
|
|
|
scope = s;
|
|
|
|
}
|
|
|
|
|
2017-12-11 09:00:05 -05:00
|
|
|
Scope::Root | Scope::Elision { .. } => break Region::Static,
|
2017-01-25 17:32:44 +02:00
|
|
|
|
2017-12-11 09:00:05 -05:00
|
|
|
Scope::Body { .. } | Scope::ObjectLifetimeDefault { lifetime: None, .. } => return,
|
2017-01-25 17:32:44 +02:00
|
|
|
|
2019-12-24 17:38:22 -05:00
|
|
|
Scope::ObjectLifetimeDefault { lifetime: Some(l), .. } => break l,
|
2020-10-26 14:18:31 -04:00
|
|
|
|
2021-04-20 16:39:41 -04:00
|
|
|
Scope::Supertrait { s, .. } | Scope::TraitRefBoundary { s, .. } => {
|
2020-10-26 14:18:31 -04:00
|
|
|
scope = s;
|
|
|
|
}
|
2017-01-25 17:32:44 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
self.insert_lifetime(lifetime_ref, lifetime.shifted(late_depth));
|
|
|
|
}
|
|
|
|
|
2021-02-27 21:31:56 -05:00
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
2017-11-23 08:05:58 -05:00
|
|
|
fn insert_lifetime(&mut self, lifetime_ref: &'tcx hir::Lifetime, def: Region) {
|
2017-12-11 09:00:05 -05:00
|
|
|
debug!(
|
2021-02-27 21:31:56 -05:00
|
|
|
node = ?self.tcx.hir().node_to_string(lifetime_ref.hir_id),
|
2021-05-03 01:14:25 +01:00
|
|
|
span = ?self.tcx.sess.source_map().span_to_diagnostic_string(lifetime_ref.span)
|
2017-12-11 09:00:05 -05:00
|
|
|
);
|
2019-02-18 10:59:17 +01:00
|
|
|
self.map.defs.insert(lifetime_ref.hir_id, def);
|
2013-10-28 17:37:10 -04:00
|
|
|
}
|
2018-06-21 15:26:44 -04:00
|
|
|
|
|
|
|
/// Sometimes we resolve a lifetime, but later find that it is an
|
|
|
|
/// error (esp. around impl trait). In that case, we remove the
|
|
|
|
/// entry into `map.defs` so as not to confuse later code.
|
|
|
|
fn uninsert_lifetime_on_error(&mut self, lifetime_ref: &'tcx hir::Lifetime, bad_def: Region) {
|
2019-02-18 10:59:17 +01:00
|
|
|
let old_value = self.map.defs.remove(&lifetime_ref.hir_id);
|
2018-06-21 15:26:44 -04:00
|
|
|
assert_eq!(old_value, Some(bad_def));
|
|
|
|
}
|
2013-10-28 17:37:10 -04:00
|
|
|
}
|
|
|
|
|
2016-04-21 05:10:10 -04:00
|
|
|
/// Detects late-bound lifetimes and inserts them into
|
2022-05-26 08:59:15 +02:00
|
|
|
/// `late_bound`.
|
2016-04-21 05:10:10 -04:00
|
|
|
///
|
|
|
|
/// A region declared on a fn is **late-bound** if:
|
|
|
|
/// - it is constrained by an argument type;
|
|
|
|
/// - it does not appear in a where-clause.
|
|
|
|
///
|
|
|
|
/// "Constrained" basically means that it appears in any type but
|
2019-02-08 14:53:55 +01:00
|
|
|
/// not amongst the inputs to a projection. In other words, `<&'a
|
2016-04-21 05:10:10 -04:00
|
|
|
/// T as Trait<''b>>::Foo` does not constrain `'a` or `'b`.
|
2022-05-26 08:59:15 +02:00
|
|
|
fn is_late_bound_map(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<&FxHashSet<LocalDefId>> {
|
|
|
|
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
|
|
|
|
let decl = tcx.hir().fn_decl_by_hir_id(hir_id)?;
|
|
|
|
let generics = tcx.hir().get_generics(def_id)?;
|
|
|
|
|
|
|
|
let mut late_bound = FxHashSet::default();
|
|
|
|
|
2018-10-16 16:57:53 +02:00
|
|
|
let mut constrained_by_input = ConstrainedCollector::default();
|
2019-12-01 00:17:43 +01:00
|
|
|
for arg_ty in decl.inputs {
|
2016-12-20 22:46:11 +02:00
|
|
|
constrained_by_input.visit_ty(arg_ty);
|
2014-03-07 08:43:39 +01:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:44:06 +03:00
|
|
|
let mut appears_in_output = AllCollector::default();
|
2016-04-21 05:10:10 -04:00
|
|
|
intravisit::walk_fn_ret_ty(&mut appears_in_output, &decl.output);
|
|
|
|
|
2021-04-05 00:10:09 -04:00
|
|
|
debug!(?constrained_by_input.regions);
|
2016-04-21 05:10:10 -04:00
|
|
|
|
|
|
|
// Walk the lifetimes that appear in where clauses.
|
|
|
|
//
|
|
|
|
// Subtle point: because we disallow nested bindings, we can just
|
|
|
|
// ignore binders here and scrape up all names we see.
|
2018-07-25 15:44:06 +03:00
|
|
|
let mut appears_in_where_clause = AllCollector::default();
|
2018-05-30 14:36:53 +03:00
|
|
|
appears_in_where_clause.visit_generics(generics);
|
2021-04-05 00:10:09 -04:00
|
|
|
debug!(?appears_in_where_clause.regions);
|
2016-04-21 05:10:10 -04:00
|
|
|
|
|
|
|
// Late bound regions are those that:
|
|
|
|
// - appear in the inputs
|
|
|
|
// - do not appear in the where-clauses
|
2016-08-06 03:11:17 +03:00
|
|
|
// - are not implicitly captured by `impl Trait`
|
2019-12-01 17:10:12 +01:00
|
|
|
for param in generics.params {
|
2018-09-06 12:39:48 -04:00
|
|
|
match param.kind {
|
|
|
|
hir::GenericParamKind::Lifetime { .. } => { /* fall through */ }
|
|
|
|
|
2019-02-15 22:25:42 +00:00
|
|
|
// Neither types nor consts are late-bound.
|
2019-12-24 17:38:22 -05:00
|
|
|
hir::GenericParamKind::Type { .. } | hir::GenericParamKind::Const { .. } => continue,
|
2018-09-06 12:39:48 -04:00
|
|
|
}
|
|
|
|
|
2022-04-27 22:15:58 +02:00
|
|
|
let param_def_id = tcx.hir().local_def_id(param.hir_id);
|
|
|
|
|
2016-04-21 05:10:10 -04:00
|
|
|
// appears in the where clauses? early-bound.
|
2022-04-27 22:15:58 +02:00
|
|
|
if appears_in_where_clause.regions.contains(¶m_def_id) {
|
2017-12-11 09:00:05 -05:00
|
|
|
continue;
|
|
|
|
}
|
2016-04-21 05:10:10 -04:00
|
|
|
|
2017-07-29 17:19:57 +03:00
|
|
|
// does not appear in the inputs, but appears in the return type? early-bound.
|
2022-04-27 22:15:58 +02:00
|
|
|
if !constrained_by_input.regions.contains(¶m_def_id)
|
|
|
|
&& appears_in_output.regions.contains(¶m_def_id)
|
2017-12-11 09:00:05 -05:00
|
|
|
{
|
2017-01-06 14:35:23 -05:00
|
|
|
continue;
|
|
|
|
}
|
2016-04-21 05:10:10 -04:00
|
|
|
|
2021-04-05 00:10:09 -04:00
|
|
|
debug!("lifetime {:?} with id {:?} is late-bound", param.name.ident(), param.hir_id);
|
2016-04-21 05:10:10 -04:00
|
|
|
|
2022-05-26 08:59:15 +02:00
|
|
|
let inserted = late_bound.insert(param_def_id);
|
2019-02-18 14:53:25 +01:00
|
|
|
assert!(inserted, "visited lifetime {:?} twice", param.hir_id);
|
2016-04-21 05:10:10 -04:00
|
|
|
}
|
|
|
|
|
2022-05-26 08:59:15 +02:00
|
|
|
debug!(?late_bound);
|
|
|
|
return Some(tcx.arena.alloc(late_bound));
|
2016-04-21 05:10:10 -04:00
|
|
|
|
2018-10-16 16:57:53 +02:00
|
|
|
#[derive(Default)]
|
2016-04-21 05:10:10 -04:00
|
|
|
struct ConstrainedCollector {
|
2022-04-27 22:15:58 +02:00
|
|
|
regions: FxHashSet<LocalDefId>,
|
2016-04-21 05:10:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'v> Visitor<'v> for ConstrainedCollector {
|
2019-11-30 17:46:46 +01:00
|
|
|
fn visit_ty(&mut self, ty: &'v hir::Ty<'v>) {
|
2019-09-26 17:25:31 +01:00
|
|
|
match ty.kind {
|
2020-04-16 17:38:52 -07:00
|
|
|
hir::TyKind::Path(
|
|
|
|
hir::QPath::Resolved(Some(_), _) | hir::QPath::TypeRelative(..),
|
|
|
|
) => {
|
2016-04-21 05:10:10 -04:00
|
|
|
// ignore lifetimes appearing in associated type
|
|
|
|
// projections, as they are not *constrained*
|
|
|
|
// (defined above)
|
2014-11-29 17:08:30 +13:00
|
|
|
}
|
2014-12-20 02:48:43 -08:00
|
|
|
|
2018-07-11 22:41:03 +08:00
|
|
|
hir::TyKind::Path(hir::QPath::Resolved(None, ref path)) => {
|
2016-04-21 05:10:10 -04:00
|
|
|
// consider only the lifetimes on the final
|
|
|
|
// segment; I am not sure it's even currently
|
|
|
|
// valid to have them elsewhere, but even if it
|
|
|
|
// is, those would be potentially inputs to
|
|
|
|
// projections
|
|
|
|
if let Some(last_segment) = path.segments.last() {
|
|
|
|
self.visit_path_segment(path.span, last_segment);
|
2014-12-20 02:48:43 -08:00
|
|
|
}
|
2014-12-20 02:29:19 -08:00
|
|
|
}
|
2014-08-27 21:46:52 -04:00
|
|
|
|
2016-04-21 05:10:10 -04:00
|
|
|
_ => {
|
|
|
|
intravisit::walk_ty(self, ty);
|
|
|
|
}
|
2014-08-27 21:46:52 -04:00
|
|
|
}
|
|
|
|
}
|
2014-03-07 08:43:39 +01:00
|
|
|
|
2016-04-21 05:10:10 -04:00
|
|
|
fn visit_lifetime(&mut self, lifetime_ref: &'v hir::Lifetime) {
|
2022-04-27 22:15:58 +02:00
|
|
|
if let hir::LifetimeName::Param(def_id, _) = lifetime_ref.name {
|
|
|
|
self.regions.insert(def_id);
|
|
|
|
}
|
2016-04-21 05:10:10 -04:00
|
|
|
}
|
2014-03-07 08:43:39 +01:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:44:06 +03:00
|
|
|
#[derive(Default)]
|
2016-04-21 05:10:10 -04:00
|
|
|
struct AllCollector {
|
2022-04-27 22:15:58 +02:00
|
|
|
regions: FxHashSet<LocalDefId>,
|
2014-08-27 21:46:52 -04:00
|
|
|
}
|
|
|
|
|
2016-04-21 05:10:10 -04:00
|
|
|
impl<'v> Visitor<'v> for AllCollector {
|
|
|
|
fn visit_lifetime(&mut self, lifetime_ref: &'v hir::Lifetime) {
|
2022-04-27 22:15:58 +02:00
|
|
|
if let hir::LifetimeName::Param(def_id, _) = lifetime_ref.name {
|
|
|
|
self.regions.insert(def_id);
|
|
|
|
}
|
2014-03-07 08:43:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|