2022-05-10 14:15:30 -05:00
|
|
|
// ignore-tidy-filelength
|
2019-08-12 18:46:42 -05:00
|
|
|
//! "Late resolution" is the pass that resolves most of names in a crate beside imports and macros.
|
|
|
|
//! It runs when the crate is fully expanded and its module structure is fully built.
|
|
|
|
//! So it just walks through the crate and resolves all the expressions, types, etc.
|
|
|
|
//!
|
|
|
|
//! If you wonder why there's no `early.rs`, that's because it's split into three files -
|
2019-12-29 10:42:23 -06:00
|
|
|
//! `build_reduced_graph.rs`, `macros.rs` and `imports.rs`.
|
2019-08-12 18:46:42 -05:00
|
|
|
|
2019-08-08 15:57:35 -05:00
|
|
|
use RibKind::*;
|
2019-08-07 18:39:02 -05:00
|
|
|
|
2022-03-23 18:55:22 -05:00
|
|
|
use crate::{path_names_to_string, BindingError, Finalize, LexicalScopeBinding};
|
2022-04-08 15:50:56 -05:00
|
|
|
use crate::{Module, ModuleOrUniformRoot, NameBinding, ParentScope, PathResult};
|
2019-08-08 15:57:35 -05:00
|
|
|
use crate::{ResolutionError, Resolver, Segment, UseError};
|
2019-08-07 18:39:02 -05:00
|
|
|
|
2020-02-29 11:37:32 -06:00
|
|
|
use rustc_ast::ptr::P;
|
2022-04-20 21:09:32 -05:00
|
|
|
use rustc_ast::visit::{self, AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor};
|
2020-04-27 12:56:11 -05:00
|
|
|
use rustc_ast::*;
|
2022-04-24 08:49:00 -05:00
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
|
2020-01-09 04:18:47 -06:00
|
|
|
use rustc_errors::DiagnosticId;
|
2020-01-04 19:37:57 -06:00
|
|
|
use rustc_hir::def::Namespace::{self, *};
|
2021-07-05 15:26:23 -05:00
|
|
|
use rustc_hir::def::{self, CtorKind, DefKind, LifetimeRes, PartialRes, PerNS};
|
2022-05-10 14:15:30 -05:00
|
|
|
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
|
2021-02-02 12:16:35 -06:00
|
|
|
use rustc_hir::{PrimTy, TraitCandidate};
|
2022-06-05 11:33:09 -05:00
|
|
|
use rustc_middle::middle::resolve_lifetime::Set1;
|
2022-03-10 16:12:35 -06:00
|
|
|
use rustc_middle::ty::DefIdTree;
|
2021-10-21 08:09:24 -05:00
|
|
|
use rustc_middle::{bug, span_bug};
|
2020-03-11 06:49:08 -05:00
|
|
|
use rustc_session::lint;
|
2020-04-19 06:00:18 -05:00
|
|
|
use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
2022-03-10 16:12:35 -06:00
|
|
|
use rustc_span::{BytePos, Span};
|
2019-08-07 18:39:02 -05:00
|
|
|
use smallvec::{smallvec, SmallVec};
|
|
|
|
|
2021-10-21 08:09:24 -05:00
|
|
|
use rustc_span::source_map::{respan, Spanned};
|
2020-12-20 14:08:55 -06:00
|
|
|
use std::collections::{hash_map::Entry, BTreeSet};
|
2020-06-02 13:16:23 -05:00
|
|
|
use std::mem::{replace, take};
|
2021-10-21 08:09:24 -05:00
|
|
|
use tracing::debug;
|
2019-08-07 18:39:02 -05:00
|
|
|
|
|
|
|
mod diagnostics;
|
2022-05-20 18:51:09 -05:00
|
|
|
pub(crate) mod lifetimes;
|
2019-08-07 18:39:02 -05:00
|
|
|
|
|
|
|
type Res = def::Res<NodeId>;
|
|
|
|
|
2021-10-21 08:09:24 -05:00
|
|
|
type IdentMap<T> = FxHashMap<Ident, T>;
|
|
|
|
|
|
|
|
/// Map from the name in a pattern to its binding mode.
|
|
|
|
type BindingMap = IdentMap<BindingInfo>;
|
|
|
|
|
2022-06-05 11:33:09 -05:00
|
|
|
use diagnostics::{
|
|
|
|
ElisionFnParameter, LifetimeElisionCandidate, MissingLifetime, MissingLifetimeKind,
|
|
|
|
};
|
|
|
|
|
2019-08-07 18:39:02 -05:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
struct BindingInfo {
|
|
|
|
span: Span,
|
|
|
|
binding_mode: BindingMode,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
2022-07-08 06:01:30 -05:00
|
|
|
pub enum PatternSource {
|
2019-08-07 18:39:02 -05:00
|
|
|
Match,
|
|
|
|
Let,
|
|
|
|
For,
|
|
|
|
FnParam,
|
|
|
|
}
|
|
|
|
|
2020-10-22 03:32:41 -05:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
enum IsRepeatExpr {
|
|
|
|
No,
|
|
|
|
Yes,
|
|
|
|
}
|
|
|
|
|
2019-08-07 18:39:02 -05:00
|
|
|
impl PatternSource {
|
2022-07-08 06:01:30 -05:00
|
|
|
pub fn descr(self) -> &'static str {
|
2019-08-07 18:39:02 -05:00
|
|
|
match self {
|
|
|
|
PatternSource::Match => "match binding",
|
|
|
|
PatternSource::Let => "let binding",
|
|
|
|
PatternSource::For => "for binding",
|
|
|
|
PatternSource::FnParam => "function parameter",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-04 18:43:53 -05:00
|
|
|
/// Denotes whether the context for the set of already bound bindings is a `Product`
|
|
|
|
/// or `Or` context. This is used in e.g., `fresh_binding` and `resolve_pattern_inner`.
|
|
|
|
/// See those functions for more information.
|
2019-11-22 12:13:51 -06:00
|
|
|
#[derive(PartialEq)]
|
2019-09-04 18:43:53 -05:00
|
|
|
enum PatBoundCtx {
|
|
|
|
/// A product pattern context, e.g., `Variant(a, b)`.
|
|
|
|
Product,
|
|
|
|
/// An or-pattern context, e.g., `p_0 | ... | p_n`.
|
|
|
|
Or,
|
|
|
|
}
|
|
|
|
|
2019-10-05 10:55:58 -05:00
|
|
|
/// Does this the item (from the item rib scope) allow generic parameters?
|
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
2022-05-20 18:51:09 -05:00
|
|
|
pub(crate) enum HasGenericParams {
|
2019-10-05 10:55:58 -05:00
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
}
|
|
|
|
|
2022-05-02 10:36:48 -05:00
|
|
|
impl HasGenericParams {
|
|
|
|
fn force_yes_if(self, b: bool) -> Self {
|
|
|
|
if b { Self::Yes } else { self }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-13 09:32:59 -06:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
2022-05-20 18:51:09 -05:00
|
|
|
pub(crate) enum ConstantItemKind {
|
2020-12-13 09:32:59 -06:00
|
|
|
Const,
|
|
|
|
Static,
|
|
|
|
}
|
|
|
|
|
2019-08-08 15:57:35 -05:00
|
|
|
/// The rib kind restricts certain accesses,
|
|
|
|
/// e.g. to a `Res::Local` of an outer item.
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
2022-05-20 18:51:09 -05:00
|
|
|
pub(crate) enum RibKind<'a> {
|
2019-08-08 15:57:35 -05:00
|
|
|
/// No restriction needs to be applied.
|
|
|
|
NormalRibKind,
|
|
|
|
|
|
|
|
/// We passed through an impl or trait and are now in one of its
|
|
|
|
/// methods or associated types. Allow references to ty params that impl or trait
|
|
|
|
/// binds. Disallow any other upvars (including other ty params that are
|
|
|
|
/// upvars).
|
|
|
|
AssocItemRibKind,
|
|
|
|
|
2020-06-25 09:16:38 -05:00
|
|
|
/// We passed through a closure. Disallow labels.
|
|
|
|
ClosureOrAsyncRibKind,
|
|
|
|
|
2019-08-08 15:57:35 -05:00
|
|
|
/// We passed through a function definition. Disallow upvars.
|
|
|
|
/// Permit only those const parameters that are specified in the function's generics.
|
|
|
|
FnItemRibKind,
|
|
|
|
|
|
|
|
/// We passed through an item scope. Disallow upvars.
|
2019-10-05 10:55:58 -05:00
|
|
|
ItemRibKind(HasGenericParams),
|
2019-08-08 15:57:35 -05:00
|
|
|
|
|
|
|
/// We're in a constant item. Can't refer to dynamic stuff.
|
2020-09-01 07:30:16 -05:00
|
|
|
///
|
2022-05-02 10:36:48 -05:00
|
|
|
/// The item may reference generic parameters in trivial constant expressions.
|
|
|
|
/// All other constants aren't allowed to use generic params at all.
|
|
|
|
ConstantItemRibKind(HasGenericParams, Option<(Ident, ConstantItemKind)>),
|
2019-08-08 15:57:35 -05:00
|
|
|
|
|
|
|
/// We passed through a module.
|
|
|
|
ModuleRibKind(Module<'a>),
|
|
|
|
|
|
|
|
/// We passed through a `macro_rules!` statement
|
|
|
|
MacroDefinition(DefId),
|
|
|
|
|
2020-12-30 09:34:53 -06:00
|
|
|
/// All bindings in this rib are generic parameters that can't be used
|
|
|
|
/// from the default of a generic parameter because they're not declared
|
|
|
|
/// before said generic parameter. Also see the `visit_generics` override.
|
|
|
|
ForwardGenericParamBanRibKind,
|
2020-07-08 15:16:18 -05:00
|
|
|
|
|
|
|
/// We are inside of the type of a const parameter. Can't refer to any
|
|
|
|
/// parameters.
|
|
|
|
ConstParamTyRibKind,
|
2022-02-28 18:50:56 -06:00
|
|
|
|
|
|
|
/// We are inside a `sym` inline assembly operand. Can only refer to
|
|
|
|
/// globals.
|
|
|
|
InlineAsmSymRibKind,
|
2019-08-08 15:57:35 -05:00
|
|
|
}
|
|
|
|
|
2019-08-11 12:09:50 -05:00
|
|
|
impl RibKind<'_> {
|
2020-06-25 09:16:38 -05:00
|
|
|
/// Whether this rib kind contains generic parameters, as opposed to local
|
|
|
|
/// variables.
|
2022-05-20 18:51:09 -05:00
|
|
|
pub(crate) fn contains_params(&self) -> bool {
|
2019-08-11 12:09:50 -05:00
|
|
|
match self {
|
2020-06-25 09:16:38 -05:00
|
|
|
NormalRibKind
|
|
|
|
| ClosureOrAsyncRibKind
|
|
|
|
| FnItemRibKind
|
2020-12-13 09:32:59 -06:00
|
|
|
| ConstantItemRibKind(..)
|
2020-06-25 09:16:38 -05:00
|
|
|
| ModuleRibKind(_)
|
2020-07-08 15:16:18 -05:00
|
|
|
| MacroDefinition(_)
|
2022-02-28 18:50:56 -06:00
|
|
|
| ConstParamTyRibKind
|
|
|
|
| InlineAsmSymRibKind => false,
|
2020-12-30 09:34:53 -06:00
|
|
|
AssocItemRibKind | ItemRibKind(_) | ForwardGenericParamBanRibKind => true,
|
2019-08-11 12:09:50 -05:00
|
|
|
}
|
|
|
|
}
|
2021-12-04 14:20:58 -06:00
|
|
|
|
|
|
|
/// This rib forbids referring to labels defined in upwards ribs.
|
|
|
|
fn is_label_barrier(self) -> bool {
|
|
|
|
match self {
|
|
|
|
NormalRibKind | MacroDefinition(..) => false,
|
|
|
|
|
|
|
|
AssocItemRibKind
|
|
|
|
| ClosureOrAsyncRibKind
|
|
|
|
| FnItemRibKind
|
|
|
|
| ItemRibKind(..)
|
|
|
|
| ConstantItemRibKind(..)
|
|
|
|
| ModuleRibKind(..)
|
|
|
|
| ForwardGenericParamBanRibKind
|
|
|
|
| ConstParamTyRibKind
|
|
|
|
| InlineAsmSymRibKind => true,
|
|
|
|
}
|
|
|
|
}
|
2019-08-11 12:09:50 -05:00
|
|
|
}
|
|
|
|
|
2019-08-08 15:57:35 -05:00
|
|
|
/// A single local scope.
|
|
|
|
///
|
|
|
|
/// A rib represents a scope names can live in. Note that these appear in many places, not just
|
|
|
|
/// around braces. At any place where the list of accessible names (of the given namespace)
|
|
|
|
/// changes or a new restrictions on the name accessibility are introduced, a new rib is put onto a
|
|
|
|
/// stack. This may be, for example, a `let` statement (because it introduces variables), a macro,
|
|
|
|
/// etc.
|
|
|
|
///
|
2022-05-19 11:09:08 -05:00
|
|
|
/// Different [rib kinds](enum@RibKind) are transparent for different names.
|
2019-08-08 15:57:35 -05:00
|
|
|
///
|
|
|
|
/// The resolution keeps a separate stack of ribs as it traverses the AST for each namespace. When
|
|
|
|
/// resolving, the name is looked up from inside out.
|
|
|
|
#[derive(Debug)]
|
2022-05-20 18:51:09 -05:00
|
|
|
pub(crate) struct Rib<'a, R = Res> {
|
2021-10-21 08:09:24 -05:00
|
|
|
pub bindings: IdentMap<R>,
|
2019-08-08 15:57:35 -05:00
|
|
|
pub kind: RibKind<'a>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, R> Rib<'a, R> {
|
|
|
|
fn new(kind: RibKind<'a>) -> Rib<'a, R> {
|
|
|
|
Rib { bindings: Default::default(), kind }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-10 14:15:30 -05:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
enum LifetimeUseSet {
|
|
|
|
One { use_span: Span, use_ctxt: visit::LifetimeCtxt },
|
|
|
|
Many,
|
|
|
|
}
|
|
|
|
|
2022-03-06 05:02:13 -06:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
enum LifetimeRibKind {
|
|
|
|
/// This rib acts as a barrier to forbid reference to lifetimes of a parent item.
|
|
|
|
Item,
|
|
|
|
|
|
|
|
/// This rib declares generic parameters.
|
2022-05-10 14:15:30 -05:00
|
|
|
Generics { binder: NodeId, span: Span, kind: LifetimeBinderKind },
|
2022-03-06 05:02:13 -06:00
|
|
|
|
2021-12-04 17:07:21 -06:00
|
|
|
/// FIXME(const_generics): This patches over an ICE caused by non-'static lifetimes in const
|
|
|
|
/// generics. We are disallowing this until we can decide on how we want to handle non-'static
|
|
|
|
/// lifetimes in const generics. See issue #74052 for discussion.
|
|
|
|
ConstGeneric,
|
|
|
|
|
2022-03-31 13:10:37 -05:00
|
|
|
/// Non-static lifetimes are prohibited in anonymous constants under `min_const_generics`.
|
|
|
|
/// This function will emit an error if `generic_const_exprs` is not enabled, the body identified by
|
|
|
|
/// `body_id` is an anonymous constant and `lifetime_ref` is non-static.
|
|
|
|
AnonConst,
|
|
|
|
|
2022-06-05 14:43:02 -05:00
|
|
|
/// Create a new anonymous lifetime parameter and reference it.
|
2022-03-06 05:02:13 -06:00
|
|
|
///
|
2022-05-21 07:06:26 -05:00
|
|
|
/// If `report_in_path`, report an error when encountering lifetime elision in a path:
|
2022-05-11 15:49:39 -05:00
|
|
|
/// ```compile_fail
|
|
|
|
/// struct Foo<'a> { x: &'a () }
|
|
|
|
/// async fn foo(x: Foo) {}
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Note: the error should not trigger when the elided lifetime is in a pattern or
|
|
|
|
/// expression-position path:
|
|
|
|
/// ```
|
|
|
|
/// struct Foo<'a> { x: &'a () }
|
|
|
|
/// async fn foo(Foo { x: _ }: Foo<'_>) {}
|
2022-05-21 07:06:26 -05:00
|
|
|
/// ```
|
|
|
|
AnonymousCreateParameter { binder: NodeId, report_in_path: bool },
|
2022-03-06 05:02:13 -06:00
|
|
|
|
|
|
|
/// Give a hard error when either `&` or `'_` is written. Used to
|
|
|
|
/// rule out things like `where T: Foo<'_>`. Does not imply an
|
|
|
|
/// error on default object bounds (e.g., `Box<dyn Foo>`).
|
|
|
|
AnonymousReportError,
|
|
|
|
|
2022-04-18 10:51:39 -05:00
|
|
|
/// Replace all anonymous lifetimes by provided lifetime.
|
|
|
|
Elided(LifetimeRes),
|
2022-06-05 11:33:09 -05:00
|
|
|
|
|
|
|
/// Signal we cannot find which should be the anonymous lifetime.
|
|
|
|
ElisionFailure,
|
2022-03-06 05:02:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
enum LifetimeBinderKind {
|
|
|
|
BareFnType,
|
|
|
|
PolyTrait,
|
|
|
|
WhereBound,
|
|
|
|
Item,
|
|
|
|
Function,
|
2022-07-03 10:15:39 -05:00
|
|
|
Closure,
|
2022-03-06 05:02:13 -06:00
|
|
|
ImplBlock,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LifetimeBinderKind {
|
|
|
|
fn descr(self) -> &'static str {
|
|
|
|
use LifetimeBinderKind::*;
|
|
|
|
match self {
|
|
|
|
BareFnType => "type",
|
|
|
|
PolyTrait => "bound",
|
|
|
|
WhereBound => "bound",
|
|
|
|
Item => "item",
|
|
|
|
ImplBlock => "impl block",
|
|
|
|
Function => "function",
|
2022-07-03 10:15:39 -05:00
|
|
|
Closure => "closure",
|
2022-03-06 05:02:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct LifetimeRib {
|
|
|
|
kind: LifetimeRibKind,
|
2022-04-24 08:49:00 -05:00
|
|
|
// We need to preserve insertion order for async fns.
|
|
|
|
bindings: FxIndexMap<Ident, (NodeId, LifetimeRes)>,
|
2022-03-06 05:02:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LifetimeRib {
|
|
|
|
fn new(kind: LifetimeRibKind) -> LifetimeRib {
|
|
|
|
LifetimeRib { bindings: Default::default(), kind }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-08 15:57:35 -05:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
2022-05-20 18:51:09 -05:00
|
|
|
pub(crate) enum AliasPossibility {
|
2019-08-08 15:57:35 -05:00
|
|
|
No,
|
|
|
|
Maybe,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
2022-05-20 18:51:09 -05:00
|
|
|
pub(crate) enum PathSource<'a> {
|
2019-08-08 15:57:35 -05:00
|
|
|
// Type paths `Path`.
|
|
|
|
Type,
|
|
|
|
// Trait paths in bounds or impls.
|
|
|
|
Trait(AliasPossibility),
|
|
|
|
// Expression paths `path`, with optional parent context.
|
|
|
|
Expr(Option<&'a Expr>),
|
|
|
|
// Paths in path patterns `Path`.
|
|
|
|
Pat,
|
|
|
|
// Paths in struct expressions and patterns `Path { .. }`.
|
|
|
|
Struct,
|
|
|
|
// Paths in tuple struct patterns `Path(..)`.
|
2020-09-08 17:14:09 -05:00
|
|
|
TupleStruct(Span, &'a [Span]),
|
2019-08-08 15:57:35 -05:00
|
|
|
// `m::A::B` in `<T as m::A>::B::C`.
|
|
|
|
TraitItem(Namespace),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> PathSource<'a> {
|
|
|
|
fn namespace(self) -> Namespace {
|
|
|
|
match self {
|
|
|
|
PathSource::Type | PathSource::Trait(_) | PathSource::Struct => TypeNS,
|
2020-09-08 17:14:09 -05:00
|
|
|
PathSource::Expr(..) | PathSource::Pat | PathSource::TupleStruct(..) => ValueNS,
|
2019-08-08 15:57:35 -05:00
|
|
|
PathSource::TraitItem(ns) => ns,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn defer_to_typeck(self) -> bool {
|
|
|
|
match self {
|
|
|
|
PathSource::Type
|
|
|
|
| PathSource::Expr(..)
|
|
|
|
| PathSource::Pat
|
|
|
|
| PathSource::Struct
|
2020-09-08 17:14:09 -05:00
|
|
|
| PathSource::TupleStruct(..) => true,
|
2019-08-08 15:57:35 -05:00
|
|
|
PathSource::Trait(_) | PathSource::TraitItem(..) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn descr_expected(self) -> &'static str {
|
2019-10-14 19:20:50 -05:00
|
|
|
match &self {
|
2019-08-08 15:57:35 -05:00
|
|
|
PathSource::Type => "type",
|
|
|
|
PathSource::Trait(_) => "trait",
|
2019-10-14 19:20:50 -05:00
|
|
|
PathSource::Pat => "unit struct, unit variant or constant",
|
2019-08-08 15:57:35 -05:00
|
|
|
PathSource::Struct => "struct, variant or union type",
|
2020-09-08 17:14:09 -05:00
|
|
|
PathSource::TupleStruct(..) => "tuple struct or tuple variant",
|
2019-08-08 15:57:35 -05:00
|
|
|
PathSource::TraitItem(ns) => match ns {
|
|
|
|
TypeNS => "associated type",
|
|
|
|
ValueNS => "method or associated constant",
|
|
|
|
MacroNS => bug!("associated macro"),
|
|
|
|
},
|
2020-08-07 13:52:02 -05:00
|
|
|
PathSource::Expr(parent) => match parent.as_ref().map(|p| &p.kind) {
|
2019-08-08 15:57:35 -05:00
|
|
|
// "function" here means "anything callable" rather than `DefKind::Fn`,
|
|
|
|
// this is not precise but usually more helpful than just "value".
|
2019-10-14 19:20:50 -05:00
|
|
|
Some(ExprKind::Call(call_expr, _)) => match &call_expr.kind {
|
2021-01-15 11:05:11 -06:00
|
|
|
// the case of `::some_crate()`
|
|
|
|
ExprKind::Path(_, path)
|
|
|
|
if path.segments.len() == 2
|
|
|
|
&& path.segments[0].ident.name == kw::PathRoot =>
|
|
|
|
{
|
|
|
|
"external crate"
|
|
|
|
}
|
2019-10-14 19:20:50 -05:00
|
|
|
ExprKind::Path(_, path) => {
|
|
|
|
let mut msg = "function";
|
|
|
|
if let Some(segment) = path.segments.iter().last() {
|
|
|
|
if let Some(c) = segment.ident.to_string().chars().next() {
|
|
|
|
if c.is_uppercase() {
|
|
|
|
msg = "function, tuple struct or tuple variant";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-22 16:42:04 -06:00
|
|
|
msg
|
2019-10-14 19:20:50 -05:00
|
|
|
}
|
|
|
|
_ => "function",
|
|
|
|
},
|
2019-08-08 15:57:35 -05:00
|
|
|
_ => "value",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 13:16:23 -05:00
|
|
|
fn is_call(self) -> bool {
|
2020-10-26 20:02:48 -05:00
|
|
|
matches!(self, PathSource::Expr(Some(&Expr { kind: ExprKind::Call(..), .. })))
|
2020-06-02 13:16:23 -05:00
|
|
|
}
|
|
|
|
|
2022-05-20 18:51:09 -05:00
|
|
|
pub(crate) fn is_expected(self, res: Res) -> bool {
|
2019-08-08 15:57:35 -05:00
|
|
|
match self {
|
2021-01-07 06:23:25 -06:00
|
|
|
PathSource::Type => matches!(
|
|
|
|
res,
|
|
|
|
Res::Def(
|
2020-04-16 19:38:52 -05:00
|
|
|
DefKind::Struct
|
2021-01-07 06:23:25 -06:00
|
|
|
| DefKind::Union
|
|
|
|
| DefKind::Enum
|
|
|
|
| DefKind::Trait
|
|
|
|
| DefKind::TraitAlias
|
|
|
|
| DefKind::TyAlias
|
|
|
|
| DefKind::AssocTy
|
|
|
|
| DefKind::TyParam
|
|
|
|
| DefKind::OpaqueTy
|
|
|
|
| DefKind::ForeignTy,
|
2020-04-16 19:38:52 -05:00
|
|
|
_,
|
2021-01-07 06:23:25 -06:00
|
|
|
) | Res::PrimTy(..)
|
2022-02-09 05:03:27 -06:00
|
|
|
| Res::SelfTy { .. }
|
2021-01-07 06:23:25 -06:00
|
|
|
),
|
2020-10-26 20:02:48 -05:00
|
|
|
PathSource::Trait(AliasPossibility::No) => matches!(res, Res::Def(DefKind::Trait, _)),
|
|
|
|
PathSource::Trait(AliasPossibility::Maybe) => {
|
|
|
|
matches!(res, Res::Def(DefKind::Trait | DefKind::TraitAlias, _))
|
|
|
|
}
|
2021-01-07 06:23:25 -06:00
|
|
|
PathSource::Expr(..) => matches!(
|
|
|
|
res,
|
|
|
|
Res::Def(
|
2020-04-16 19:38:52 -05:00
|
|
|
DefKind::Ctor(_, CtorKind::Const | CtorKind::Fn)
|
2021-01-07 06:23:25 -06:00
|
|
|
| DefKind::Const
|
2022-03-29 10:11:12 -05:00
|
|
|
| DefKind::Static(_)
|
2021-01-07 06:23:25 -06:00
|
|
|
| DefKind::Fn
|
|
|
|
| DefKind::AssocFn
|
|
|
|
| DefKind::AssocConst
|
|
|
|
| DefKind::ConstParam,
|
2020-04-16 19:38:52 -05:00
|
|
|
_,
|
2021-01-07 06:23:25 -06:00
|
|
|
) | Res::Local(..)
|
|
|
|
| Res::SelfCtor(..)
|
|
|
|
),
|
2022-04-01 22:24:32 -05:00
|
|
|
PathSource::Pat => {
|
|
|
|
res.expected_in_unit_struct_pat()
|
|
|
|
|| matches!(res, Res::Def(DefKind::Const | DefKind::AssocConst, _))
|
|
|
|
}
|
2020-11-07 08:28:55 -06:00
|
|
|
PathSource::TupleStruct(..) => res.expected_in_tuple_struct_pat(),
|
2021-01-07 06:23:25 -06:00
|
|
|
PathSource::Struct => matches!(
|
|
|
|
res,
|
|
|
|
Res::Def(
|
2020-04-16 19:38:52 -05:00
|
|
|
DefKind::Struct
|
2021-01-07 06:23:25 -06:00
|
|
|
| DefKind::Union
|
|
|
|
| DefKind::Variant
|
|
|
|
| DefKind::TyAlias
|
|
|
|
| DefKind::AssocTy,
|
2020-04-16 19:38:52 -05:00
|
|
|
_,
|
2022-02-09 05:03:27 -06:00
|
|
|
) | Res::SelfTy { .. }
|
2021-01-07 06:23:25 -06:00
|
|
|
),
|
2019-08-08 15:57:35 -05:00
|
|
|
PathSource::TraitItem(ns) => match res {
|
2020-04-16 19:38:52 -05:00
|
|
|
Res::Def(DefKind::AssocConst | DefKind::AssocFn, _) if ns == ValueNS => true,
|
2019-08-08 15:57:35 -05:00
|
|
|
Res::Def(DefKind::AssocTy, _) if ns == TypeNS => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 14:25:16 -06:00
|
|
|
fn error_code(self, has_unexpected_resolution: bool) -> DiagnosticId {
|
2020-01-09 04:18:47 -06:00
|
|
|
use rustc_errors::error_code;
|
2019-08-08 15:57:35 -05:00
|
|
|
match (self, has_unexpected_resolution) {
|
2019-12-31 14:25:16 -06:00
|
|
|
(PathSource::Trait(_), true) => error_code!(E0404),
|
|
|
|
(PathSource::Trait(_), false) => error_code!(E0405),
|
|
|
|
(PathSource::Type, true) => error_code!(E0573),
|
|
|
|
(PathSource::Type, false) => error_code!(E0412),
|
|
|
|
(PathSource::Struct, true) => error_code!(E0574),
|
|
|
|
(PathSource::Struct, false) => error_code!(E0422),
|
|
|
|
(PathSource::Expr(..), true) => error_code!(E0423),
|
|
|
|
(PathSource::Expr(..), false) => error_code!(E0425),
|
2020-09-08 17:14:09 -05:00
|
|
|
(PathSource::Pat | PathSource::TupleStruct(..), true) => error_code!(E0532),
|
|
|
|
(PathSource::Pat | PathSource::TupleStruct(..), false) => error_code!(E0531),
|
2019-12-31 14:25:16 -06:00
|
|
|
(PathSource::TraitItem(..), true) => error_code!(E0575),
|
|
|
|
(PathSource::TraitItem(..), false) => error_code!(E0576),
|
2019-08-08 15:57:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-19 15:27:54 -05:00
|
|
|
#[derive(Default)]
|
2020-01-22 01:01:21 -06:00
|
|
|
struct DiagnosticMetadata<'ast> {
|
2020-10-26 18:28:56 -05:00
|
|
|
/// The current trait's associated items' ident, used for diagnostic suggestions.
|
|
|
|
current_trait_assoc_items: Option<&'ast [P<AssocItem>]>,
|
2019-08-07 18:39:02 -05:00
|
|
|
|
|
|
|
/// The current self type if inside an impl (used for better errors).
|
|
|
|
current_self_type: Option<Ty>,
|
|
|
|
|
|
|
|
/// The current self item if inside an ADT (used for better errors).
|
|
|
|
current_self_item: Option<NodeId>,
|
|
|
|
|
2020-01-22 01:01:21 -06:00
|
|
|
/// The current trait (used to suggest).
|
|
|
|
current_item: Option<&'ast Item>,
|
|
|
|
|
|
|
|
/// When processing generics and encountering a type not found, suggest introducing a type
|
|
|
|
/// param.
|
|
|
|
currently_processing_generics: bool,
|
|
|
|
|
2020-10-17 06:36:59 -05:00
|
|
|
/// The current enclosing (non-closure) function (used for better errors).
|
2020-05-17 17:28:12 -05:00
|
|
|
current_function: Option<(FnKind<'ast>, Span)>,
|
2019-10-17 22:26:21 -05:00
|
|
|
|
2019-08-07 18:39:02 -05:00
|
|
|
/// A list of labels as of yet unused. Labels will be removed from this map when
|
|
|
|
/// they are used (in a `break` or `continue` statement)
|
|
|
|
unused_labels: FxHashMap<NodeId, Span>,
|
|
|
|
|
|
|
|
/// Only used for better errors on `fn(): fn()`.
|
|
|
|
current_type_ascription: Vec<Span>,
|
2019-10-18 12:33:25 -05:00
|
|
|
|
2021-09-02 13:34:03 -05:00
|
|
|
/// Only used for better errors on `let x = { foo: bar };`.
|
|
|
|
/// In the case of a parse error with `let x = { foo: bar, };`, this isn't needed, it's only
|
|
|
|
/// needed for cases where this parses as a correct type ascription.
|
|
|
|
current_block_could_be_bare_struct_literal: Option<Span>,
|
|
|
|
|
2019-10-18 12:33:25 -05:00
|
|
|
/// Only used for better errors on `let <pat>: <expr, not type>;`.
|
2019-10-19 12:13:56 -05:00
|
|
|
current_let_binding: Option<(Span, Option<Span>, Option<Span>)>,
|
2020-08-25 22:28:25 -05:00
|
|
|
|
|
|
|
/// Used to detect possible `if let` written without `let` and to provide structured suggestion.
|
|
|
|
in_if_condition: Option<&'ast Expr>,
|
2020-09-22 19:35:03 -05:00
|
|
|
|
2020-09-24 01:20:49 -05:00
|
|
|
/// If we are currently in a trait object definition. Used to point at the bounds when
|
|
|
|
/// encountering a struct or enum.
|
2020-09-22 19:35:03 -05:00
|
|
|
current_trait_object: Option<&'ast [ast::GenericBound]>,
|
2020-09-24 01:01:17 -05:00
|
|
|
|
|
|
|
/// Given `where <T as Bar>::Baz: String`, suggest `where T: Bar<Baz = String>`.
|
|
|
|
current_where_predicate: Option<&'ast WherePredicate>,
|
2022-01-11 15:56:10 -06:00
|
|
|
|
|
|
|
current_type_path: Option<&'ast Ty>,
|
2022-04-28 02:54:28 -05:00
|
|
|
|
|
|
|
/// The current impl items (used to suggest).
|
|
|
|
current_impl_items: Option<&'ast [P<AssocItem>]>,
|
2022-06-06 12:33:23 -05:00
|
|
|
|
|
|
|
/// When processing impl trait
|
|
|
|
currently_processing_impl_trait: Option<(TraitRef, Ty)>,
|
2022-06-05 11:33:09 -05:00
|
|
|
|
|
|
|
/// Accumulate the errors due to missed lifetime elision,
|
|
|
|
/// and report them all at once for each function.
|
|
|
|
current_elision_failures: Vec<MissingLifetime>,
|
2019-10-19 12:13:56 -05:00
|
|
|
}
|
|
|
|
|
2020-01-22 01:01:21 -06:00
|
|
|
struct LateResolutionVisitor<'a, 'b, 'ast> {
|
2019-10-19 12:13:56 -05:00
|
|
|
r: &'b mut Resolver<'a>,
|
|
|
|
|
|
|
|
/// The module that represents the current item scope.
|
|
|
|
parent_scope: ParentScope<'a>,
|
|
|
|
|
|
|
|
/// The current set of local scopes for types and values.
|
|
|
|
/// FIXME #4948: Reuse ribs to avoid allocation.
|
|
|
|
ribs: PerNS<Vec<Rib<'a>>>,
|
|
|
|
|
|
|
|
/// The current set of local scopes, for labels.
|
|
|
|
label_ribs: Vec<Rib<'a, NodeId>>,
|
|
|
|
|
2022-03-06 05:02:13 -06:00
|
|
|
/// The current set of local scopes for lifetimes.
|
|
|
|
lifetime_ribs: Vec<LifetimeRib>,
|
|
|
|
|
2022-06-05 11:33:09 -05:00
|
|
|
/// We are looking for lifetimes in an elision context.
|
|
|
|
/// The set contains all the resolutions that we encountered so far.
|
|
|
|
/// They will be used to determine the correct lifetime for the fn return type.
|
|
|
|
/// The `LifetimeElisionCandidate` is used for diagnostics, to suggest introducing named
|
|
|
|
/// lifetimes.
|
|
|
|
lifetime_elision_candidates: Option<FxIndexMap<LifetimeRes, LifetimeElisionCandidate>>,
|
|
|
|
|
2019-10-19 12:13:56 -05:00
|
|
|
/// The trait that the current context can refer to.
|
|
|
|
current_trait_ref: Option<(Module<'a>, TraitRef)>,
|
|
|
|
|
|
|
|
/// Fields used to add information to diagnostic errors.
|
2022-06-14 21:23:19 -05:00
|
|
|
diagnostic_metadata: Box<DiagnosticMetadata<'ast>>,
|
2020-06-20 14:57:23 -05:00
|
|
|
|
2020-07-10 15:55:06 -05:00
|
|
|
/// State used to know whether to ignore resolution errors for function bodies.
|
2020-06-20 14:57:23 -05:00
|
|
|
///
|
|
|
|
/// In particular, rustdoc uses this to avoid giving errors for `cfg()` items.
|
2020-06-20 16:01:03 -05:00
|
|
|
/// In most cases this will be `None`, in which case errors will always be reported.
|
2020-08-25 22:28:25 -05:00
|
|
|
/// If it is `true`, then it will be updated when entering a nested function or trait body.
|
2020-07-10 15:55:06 -05:00
|
|
|
in_func_body: bool,
|
2022-05-10 14:15:30 -05:00
|
|
|
|
|
|
|
/// Count the number of places a lifetime is used.
|
|
|
|
lifetime_uses: FxHashMap<LocalDefId, LifetimeUseSet>,
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Walks the whole crate in DFS order, visiting each item, resolving names as it goes.
|
2020-09-08 17:14:09 -05:00
|
|
|
impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
|
2021-11-24 16:07:13 -06:00
|
|
|
fn visit_attribute(&mut self, _: &'ast Attribute) {
|
|
|
|
// We do not want to resolve expressions that appear in attributes,
|
|
|
|
// as they do not correspond to actual code.
|
|
|
|
}
|
2020-01-22 01:01:21 -06:00
|
|
|
fn visit_item(&mut self, item: &'ast Item) {
|
|
|
|
let prev = replace(&mut self.diagnostic_metadata.current_item, Some(item));
|
2020-07-14 20:14:09 -05:00
|
|
|
// Always report errors in items we just entered.
|
|
|
|
let old_ignore = replace(&mut self.in_func_body, false);
|
2022-03-06 05:02:13 -06:00
|
|
|
self.with_lifetime_rib(LifetimeRibKind::Item, |this| this.resolve_item(item));
|
2020-07-14 20:14:09 -05:00
|
|
|
self.in_func_body = old_ignore;
|
2020-01-22 01:01:21 -06:00
|
|
|
self.diagnostic_metadata.current_item = prev;
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
2020-01-22 01:01:21 -06:00
|
|
|
fn visit_arm(&mut self, arm: &'ast Arm) {
|
2019-08-07 18:39:02 -05:00
|
|
|
self.resolve_arm(arm);
|
|
|
|
}
|
2020-01-22 01:01:21 -06:00
|
|
|
fn visit_block(&mut self, block: &'ast Block) {
|
2019-08-07 18:39:02 -05:00
|
|
|
self.resolve_block(block);
|
|
|
|
}
|
2020-01-22 01:01:21 -06:00
|
|
|
fn visit_anon_const(&mut self, constant: &'ast AnonConst) {
|
2020-10-22 03:32:41 -05:00
|
|
|
// We deal with repeat expressions explicitly in `resolve_expr`.
|
2022-03-31 13:10:37 -05:00
|
|
|
self.with_lifetime_rib(LifetimeRibKind::AnonConst, |this| {
|
2022-04-18 10:51:39 -05:00
|
|
|
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| {
|
|
|
|
this.resolve_anon_const(constant, IsRepeatExpr::No);
|
|
|
|
})
|
2022-03-31 13:10:37 -05:00
|
|
|
})
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
2020-01-22 01:01:21 -06:00
|
|
|
fn visit_expr(&mut self, expr: &'ast Expr) {
|
2019-08-07 18:39:02 -05:00
|
|
|
self.resolve_expr(expr, None);
|
|
|
|
}
|
2020-01-22 01:01:21 -06:00
|
|
|
fn visit_local(&mut self, local: &'ast Local) {
|
2019-10-19 12:13:56 -05:00
|
|
|
let local_spans = match local.pat.kind {
|
|
|
|
// We check for this to avoid tuple struct fields.
|
|
|
|
PatKind::Wild => None,
|
|
|
|
_ => Some((
|
|
|
|
local.pat.span,
|
|
|
|
local.ty.as_ref().map(|ty| ty.span),
|
2021-06-22 13:00:58 -05:00
|
|
|
local.kind.init().map(|init| init.span),
|
2019-10-19 12:13:56 -05:00
|
|
|
)),
|
2019-10-18 12:33:25 -05:00
|
|
|
};
|
2019-10-19 12:13:56 -05:00
|
|
|
let original = replace(&mut self.diagnostic_metadata.current_let_binding, local_spans);
|
2019-08-07 18:39:02 -05:00
|
|
|
self.resolve_local(local);
|
2019-10-19 12:13:56 -05:00
|
|
|
self.diagnostic_metadata.current_let_binding = original;
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
2020-01-22 01:01:21 -06:00
|
|
|
fn visit_ty(&mut self, ty: &'ast Ty) {
|
2020-09-22 19:35:03 -05:00
|
|
|
let prev = self.diagnostic_metadata.current_trait_object;
|
2022-01-11 15:56:10 -06:00
|
|
|
let prev_ty = self.diagnostic_metadata.current_type_path;
|
2019-09-26 11:25:31 -05:00
|
|
|
match ty.kind {
|
2022-03-06 05:02:13 -06:00
|
|
|
TyKind::Rptr(None, _) => {
|
|
|
|
// Elided lifetime in reference: we resolve as if there was some lifetime `'_` with
|
|
|
|
// NodeId `ty.id`.
|
2022-06-05 11:33:09 -05:00
|
|
|
// This span will be used in case of elision failure.
|
2022-03-06 05:02:13 -06:00
|
|
|
let span = self.r.session.source_map().next_point(ty.span.shrink_to_lo());
|
|
|
|
self.resolve_elided_lifetime(ty.id, span);
|
2022-06-05 11:33:09 -05:00
|
|
|
visit::walk_ty(self, ty);
|
2022-03-06 05:02:13 -06:00
|
|
|
}
|
2019-08-07 18:39:02 -05:00
|
|
|
TyKind::Path(ref qself, ref path) => {
|
2022-01-11 15:56:10 -06:00
|
|
|
self.diagnostic_metadata.current_type_path = Some(ty);
|
2019-08-07 18:39:02 -05:00
|
|
|
self.smart_resolve_path(ty.id, qself.as_ref(), path, PathSource::Type);
|
2022-06-25 12:16:56 -05:00
|
|
|
|
|
|
|
// Check whether we should interpret this as a bare trait object.
|
|
|
|
if qself.is_none()
|
|
|
|
&& let Some(partial_res) = self.r.partial_res_map.get(&ty.id)
|
|
|
|
&& partial_res.unresolved_segments() == 0
|
|
|
|
&& let Res::Def(DefKind::Trait | DefKind::TraitAlias, _) = partial_res.base_res()
|
|
|
|
{
|
|
|
|
// This path is actually a bare trait object. In case of a bare `Fn`-trait
|
|
|
|
// object with anonymous lifetimes, we need this rib to correctly place the
|
|
|
|
// synthetic lifetimes.
|
|
|
|
let span = ty.span.shrink_to_lo().to(path.span.shrink_to_lo());
|
|
|
|
self.with_generic_param_rib(
|
|
|
|
&[],
|
|
|
|
NormalRibKind,
|
|
|
|
LifetimeRibKind::Generics {
|
|
|
|
binder: ty.id,
|
|
|
|
kind: LifetimeBinderKind::PolyTrait,
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
|this| this.visit_path(&path, ty.id),
|
|
|
|
);
|
2022-06-05 11:33:09 -05:00
|
|
|
} else {
|
|
|
|
visit::walk_ty(self, ty)
|
2022-06-25 12:16:56 -05:00
|
|
|
}
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
TyKind::ImplicitSelf => {
|
2019-08-10 18:20:18 -05:00
|
|
|
let self_ty = Ident::with_dummy_span(kw::SelfUpper);
|
2019-08-07 18:39:02 -05:00
|
|
|
let res = self
|
2022-03-23 16:32:00 -05:00
|
|
|
.resolve_ident_in_lexical_scope(
|
|
|
|
self_ty,
|
|
|
|
TypeNS,
|
2022-04-30 08:26:36 -05:00
|
|
|
Some(Finalize::new(ty.id, ty.span)),
|
2022-04-08 15:50:56 -05:00
|
|
|
None,
|
2022-03-23 16:32:00 -05:00
|
|
|
)
|
2019-08-07 18:39:02 -05:00
|
|
|
.map_or(Res::Err, |d| d.res());
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.record_partial_res(ty.id, PartialRes::new(res));
|
2022-06-05 11:33:09 -05:00
|
|
|
visit::walk_ty(self, ty)
|
|
|
|
}
|
|
|
|
TyKind::ImplTrait(..) => {
|
|
|
|
let candidates = self.lifetime_elision_candidates.take();
|
|
|
|
visit::walk_ty(self, ty);
|
|
|
|
self.lifetime_elision_candidates = candidates;
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
2020-09-22 19:35:03 -05:00
|
|
|
TyKind::TraitObject(ref bounds, ..) => {
|
|
|
|
self.diagnostic_metadata.current_trait_object = Some(&bounds[..]);
|
2022-06-05 11:33:09 -05:00
|
|
|
visit::walk_ty(self, ty)
|
2020-09-22 19:35:03 -05:00
|
|
|
}
|
2022-03-06 05:02:13 -06:00
|
|
|
TyKind::BareFn(ref bare_fn) => {
|
2022-05-10 14:17:21 -05:00
|
|
|
let span = ty.span.shrink_to_lo().to(bare_fn.decl_span.shrink_to_lo());
|
2022-03-06 05:02:13 -06:00
|
|
|
self.with_generic_param_rib(
|
|
|
|
&bare_fn.generic_params,
|
|
|
|
NormalRibKind,
|
2022-04-07 13:54:13 -05:00
|
|
|
LifetimeRibKind::Generics {
|
2022-05-10 14:15:30 -05:00
|
|
|
binder: ty.id,
|
2022-04-07 13:54:13 -05:00
|
|
|
kind: LifetimeBinderKind::BareFnType,
|
|
|
|
span,
|
|
|
|
},
|
2022-03-06 05:02:13 -06:00
|
|
|
|this| {
|
2022-06-05 04:12:31 -05:00
|
|
|
this.visit_generic_params(&bare_fn.generic_params, false);
|
2022-05-22 16:31:39 -05:00
|
|
|
this.with_lifetime_rib(
|
|
|
|
LifetimeRibKind::AnonymousCreateParameter {
|
|
|
|
binder: ty.id,
|
|
|
|
report_in_path: false,
|
|
|
|
},
|
|
|
|
|this| {
|
|
|
|
this.resolve_fn_signature(
|
|
|
|
ty.id,
|
|
|
|
false,
|
|
|
|
// We don't need to deal with patterns in parameters, because
|
|
|
|
// they are not possible for foreign or bodiless functions.
|
|
|
|
bare_fn
|
|
|
|
.decl
|
|
|
|
.inputs
|
|
|
|
.iter()
|
|
|
|
.map(|Param { ty, .. }| (None, &**ty)),
|
|
|
|
&bare_fn.decl.output,
|
|
|
|
)
|
|
|
|
},
|
2022-04-07 13:54:13 -05:00
|
|
|
);
|
2022-03-06 05:02:13 -06:00
|
|
|
},
|
2022-06-05 11:33:09 -05:00
|
|
|
)
|
2022-03-06 05:02:13 -06:00
|
|
|
}
|
2022-06-05 11:33:09 -05:00
|
|
|
_ => visit::walk_ty(self, ty),
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
2020-09-22 19:35:03 -05:00
|
|
|
self.diagnostic_metadata.current_trait_object = prev;
|
2022-01-11 15:56:10 -06:00
|
|
|
self.diagnostic_metadata.current_type_path = prev_ty;
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
2022-03-06 05:02:13 -06:00
|
|
|
fn visit_poly_trait_ref(&mut self, tref: &'ast PolyTraitRef, _: &'ast TraitBoundModifier) {
|
2022-05-10 14:17:21 -05:00
|
|
|
let span = tref.span.shrink_to_lo().to(tref.trait_ref.path.span.shrink_to_lo());
|
2022-03-06 05:02:13 -06:00
|
|
|
self.with_generic_param_rib(
|
|
|
|
&tref.bound_generic_params,
|
|
|
|
NormalRibKind,
|
2022-04-07 13:54:13 -05:00
|
|
|
LifetimeRibKind::Generics {
|
2022-05-10 14:15:30 -05:00
|
|
|
binder: tref.trait_ref.ref_id,
|
2022-04-07 13:54:13 -05:00
|
|
|
kind: LifetimeBinderKind::PolyTrait,
|
|
|
|
span,
|
|
|
|
},
|
2022-03-06 05:02:13 -06:00
|
|
|
|this| {
|
2022-06-05 04:12:31 -05:00
|
|
|
this.visit_generic_params(&tref.bound_generic_params, false);
|
2022-03-06 05:02:13 -06:00
|
|
|
this.smart_resolve_path(
|
|
|
|
tref.trait_ref.ref_id,
|
|
|
|
None,
|
|
|
|
&tref.trait_ref.path,
|
|
|
|
PathSource::Trait(AliasPossibility::Maybe),
|
|
|
|
);
|
|
|
|
this.visit_trait_ref(&tref.trait_ref);
|
|
|
|
},
|
2019-08-07 18:39:02 -05:00
|
|
|
);
|
|
|
|
}
|
2020-01-22 01:01:21 -06:00
|
|
|
fn visit_foreign_item(&mut self, foreign_item: &'ast ForeignItem) {
|
2019-10-05 10:55:58 -05:00
|
|
|
match foreign_item.kind {
|
2022-03-06 05:02:13 -06:00
|
|
|
ForeignItemKind::TyAlias(box TyAlias { ref generics, .. }) => {
|
|
|
|
self.with_lifetime_rib(LifetimeRibKind::Item, |this| {
|
|
|
|
this.with_generic_param_rib(
|
|
|
|
&generics.params,
|
|
|
|
ItemRibKind(HasGenericParams::Yes),
|
|
|
|
LifetimeRibKind::Generics {
|
2022-05-10 14:15:30 -05:00
|
|
|
binder: foreign_item.id,
|
2022-03-06 05:02:13 -06:00
|
|
|
kind: LifetimeBinderKind::Item,
|
|
|
|
span: generics.span,
|
|
|
|
},
|
|
|
|
|this| visit::walk_foreign_item(this, foreign_item),
|
|
|
|
)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
ForeignItemKind::Fn(box Fn { ref generics, .. }) => {
|
|
|
|
self.with_lifetime_rib(LifetimeRibKind::Item, |this| {
|
|
|
|
this.with_generic_param_rib(
|
|
|
|
&generics.params,
|
|
|
|
ItemRibKind(HasGenericParams::Yes),
|
|
|
|
LifetimeRibKind::Generics {
|
2022-05-10 14:15:30 -05:00
|
|
|
binder: foreign_item.id,
|
2022-03-06 05:02:13 -06:00
|
|
|
kind: LifetimeBinderKind::Function,
|
|
|
|
span: generics.span,
|
|
|
|
},
|
|
|
|
|this| visit::walk_foreign_item(this, foreign_item),
|
|
|
|
)
|
2019-10-05 10:55:58 -05:00
|
|
|
});
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
2020-02-29 06:51:08 -06:00
|
|
|
ForeignItemKind::Static(..) => {
|
2022-03-06 05:02:13 -06:00
|
|
|
self.with_item_rib(|this| {
|
2019-10-05 10:55:58 -05:00
|
|
|
visit::walk_foreign_item(this, foreign_item);
|
|
|
|
});
|
|
|
|
}
|
2020-02-29 10:32:20 -06:00
|
|
|
ForeignItemKind::MacCall(..) => {
|
2022-03-06 05:02:13 -06:00
|
|
|
panic!("unexpanded macro in resolve!")
|
2019-10-05 10:55:58 -05:00
|
|
|
}
|
|
|
|
}
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
2022-04-07 13:54:13 -05:00
|
|
|
fn visit_fn(&mut self, fn_kind: FnKind<'ast>, sp: Span, fn_id: NodeId) {
|
2019-08-28 01:48:10 -05:00
|
|
|
let rib_kind = match fn_kind {
|
2022-01-02 23:11:16 -06:00
|
|
|
// Bail if the function is foreign, and thus cannot validly have
|
|
|
|
// a body, or if there's no body for some other reason.
|
2021-11-19 15:03:43 -06:00
|
|
|
FnKind::Fn(FnCtxt::Foreign, _, sig, _, generics, _)
|
|
|
|
| FnKind::Fn(_, _, sig, _, generics, None) => {
|
2022-05-11 15:49:39 -05:00
|
|
|
self.visit_fn_header(&sig.header);
|
|
|
|
self.visit_generics(generics);
|
2022-05-22 16:31:39 -05:00
|
|
|
self.with_lifetime_rib(
|
|
|
|
LifetimeRibKind::AnonymousCreateParameter {
|
|
|
|
binder: fn_id,
|
|
|
|
report_in_path: false,
|
|
|
|
},
|
|
|
|
|this| {
|
|
|
|
this.resolve_fn_signature(
|
|
|
|
fn_id,
|
|
|
|
sig.decl.has_self(),
|
|
|
|
sig.decl.inputs.iter().map(|Param { ty, .. }| (None, &**ty)),
|
|
|
|
&sig.decl.output,
|
|
|
|
)
|
|
|
|
},
|
2022-05-10 14:15:30 -05:00
|
|
|
);
|
2022-01-02 23:11:16 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
FnKind::Fn(FnCtxt::Free, ..) => FnItemRibKind,
|
2020-06-25 09:16:38 -05:00
|
|
|
FnKind::Fn(FnCtxt::Assoc(_), ..) => NormalRibKind,
|
|
|
|
FnKind::Closure(..) => ClosureOrAsyncRibKind,
|
2019-08-07 18:39:02 -05:00
|
|
|
};
|
2020-10-17 06:36:59 -05:00
|
|
|
let previous_value = self.diagnostic_metadata.current_function;
|
|
|
|
if matches!(fn_kind, FnKind::Fn(..)) {
|
|
|
|
self.diagnostic_metadata.current_function = Some((fn_kind, sp));
|
|
|
|
}
|
2020-01-29 17:18:54 -06:00
|
|
|
debug!("(resolving function) entering function");
|
2019-08-07 18:39:02 -05:00
|
|
|
|
|
|
|
// Create a value rib for the function.
|
2019-08-28 01:48:10 -05:00
|
|
|
self.with_rib(ValueNS, rib_kind, |this| {
|
|
|
|
// Create a label rib for the function.
|
2021-12-04 14:20:58 -06:00
|
|
|
this.with_label_rib(FnItemRibKind, |this| {
|
2022-06-05 11:24:22 -05:00
|
|
|
match fn_kind {
|
|
|
|
FnKind::Fn(_, _, sig, _, generics, body) => {
|
|
|
|
this.visit_generics(generics);
|
2022-03-06 05:02:13 -06:00
|
|
|
|
2022-06-05 11:24:22 -05:00
|
|
|
let declaration = &sig.decl;
|
|
|
|
let async_node_id = sig.header.asyncness.opt_return_id();
|
2021-11-19 15:03:43 -06:00
|
|
|
|
2022-05-22 16:31:39 -05:00
|
|
|
this.with_lifetime_rib(
|
|
|
|
LifetimeRibKind::AnonymousCreateParameter {
|
|
|
|
binder: fn_id,
|
|
|
|
report_in_path: async_node_id.is_some(),
|
|
|
|
},
|
|
|
|
|this| {
|
|
|
|
this.resolve_fn_signature(
|
|
|
|
fn_id,
|
|
|
|
declaration.has_self(),
|
|
|
|
declaration
|
|
|
|
.inputs
|
|
|
|
.iter()
|
|
|
|
.map(|Param { pat, ty, .. }| (Some(&**pat), &**ty)),
|
|
|
|
&declaration.output,
|
|
|
|
)
|
|
|
|
},
|
2022-04-24 08:49:00 -05:00
|
|
|
);
|
2022-06-05 11:24:22 -05:00
|
|
|
|
|
|
|
// Construct the list of in-scope lifetime parameters for async lowering.
|
|
|
|
// We include all lifetime parameters, either named or "Fresh".
|
|
|
|
// The order of those parameters does not matter, as long as it is
|
|
|
|
// deterministic.
|
|
|
|
if let Some(async_node_id) = async_node_id {
|
|
|
|
let mut extra_lifetime_params = this
|
|
|
|
.r
|
|
|
|
.extra_lifetime_params_map
|
|
|
|
.get(&fn_id)
|
|
|
|
.cloned()
|
|
|
|
.unwrap_or_default();
|
|
|
|
for rib in this.lifetime_ribs.iter().rev() {
|
|
|
|
extra_lifetime_params.extend(
|
|
|
|
rib.bindings
|
|
|
|
.iter()
|
|
|
|
.map(|(&ident, &(node_id, res))| (ident, node_id, res)),
|
|
|
|
);
|
|
|
|
match rib.kind {
|
|
|
|
LifetimeRibKind::Item => break,
|
2022-05-21 07:06:26 -05:00
|
|
|
LifetimeRibKind::AnonymousCreateParameter {
|
|
|
|
binder, ..
|
|
|
|
} => {
|
2022-06-05 11:24:22 -05:00
|
|
|
if let Some(earlier_fresh) =
|
|
|
|
this.r.extra_lifetime_params_map.get(&binder)
|
|
|
|
{
|
|
|
|
extra_lifetime_params.extend(earlier_fresh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
2022-04-24 08:49:00 -05:00
|
|
|
}
|
|
|
|
}
|
2022-06-05 11:24:22 -05:00
|
|
|
this.r
|
|
|
|
.extra_lifetime_params_map
|
|
|
|
.insert(async_node_id, extra_lifetime_params);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(body) = body {
|
|
|
|
// Ignore errors in function bodies if this is rustdoc
|
|
|
|
// Be sure not to set this until the function signature has been resolved.
|
|
|
|
let previous_state = replace(&mut this.in_func_body, true);
|
|
|
|
// Resolve the function body, potentially inside the body of an async closure
|
|
|
|
this.with_lifetime_rib(
|
2022-06-13 01:22:06 -05:00
|
|
|
LifetimeRibKind::Elided(LifetimeRes::Infer),
|
2022-06-05 11:24:22 -05:00
|
|
|
|this| this.visit_block(body),
|
|
|
|
);
|
|
|
|
|
|
|
|
debug!("(resolving function) leaving function");
|
|
|
|
this.in_func_body = previous_state;
|
2022-04-24 08:49:00 -05:00
|
|
|
}
|
|
|
|
}
|
2022-07-12 04:34:24 -05:00
|
|
|
FnKind::Closure(binder, declaration, body) => {
|
|
|
|
this.visit_closure_binder(binder);
|
|
|
|
|
2022-06-05 11:24:22 -05:00
|
|
|
this.with_lifetime_rib(
|
2022-07-12 04:34:24 -05:00
|
|
|
match binder {
|
|
|
|
// We do not have any explicit generic lifetime parameter.
|
|
|
|
ClosureBinder::NotPresent => {
|
|
|
|
LifetimeRibKind::AnonymousCreateParameter {
|
|
|
|
binder: fn_id,
|
|
|
|
report_in_path: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ClosureBinder::For { .. } => LifetimeRibKind::AnonymousReportError,
|
2022-05-23 08:50:02 -05:00
|
|
|
},
|
2022-06-05 11:24:22 -05:00
|
|
|
// Add each argument to the rib.
|
|
|
|
|this| this.resolve_params(&declaration.inputs),
|
|
|
|
);
|
|
|
|
this.with_lifetime_rib(
|
2022-07-12 04:34:24 -05:00
|
|
|
match binder {
|
|
|
|
ClosureBinder::NotPresent => {
|
2022-06-13 01:22:06 -05:00
|
|
|
LifetimeRibKind::Elided(LifetimeRes::Infer)
|
2022-07-12 04:34:24 -05:00
|
|
|
}
|
|
|
|
ClosureBinder::For { .. } => LifetimeRibKind::AnonymousReportError,
|
|
|
|
},
|
2022-06-05 11:24:22 -05:00
|
|
|
|this| visit::walk_fn_ret_ty(this, &declaration.output),
|
|
|
|
);
|
2019-08-28 01:48:10 -05:00
|
|
|
|
2022-06-05 11:24:22 -05:00
|
|
|
// Ignore errors in function bodies if this is rustdoc
|
|
|
|
// Be sure not to set this until the function signature has been resolved.
|
|
|
|
let previous_state = replace(&mut this.in_func_body, true);
|
|
|
|
// Resolve the function body, potentially inside the body of an async closure
|
|
|
|
this.with_lifetime_rib(
|
2022-06-13 01:22:06 -05:00
|
|
|
LifetimeRibKind::Elided(LifetimeRes::Infer),
|
2022-06-05 11:24:22 -05:00
|
|
|
|this| this.visit_expr(body),
|
|
|
|
);
|
|
|
|
|
|
|
|
debug!("(resolving function) leaving function");
|
|
|
|
this.in_func_body = previous_state;
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 01:48:10 -05:00
|
|
|
})
|
|
|
|
});
|
2019-10-19 12:13:56 -05:00
|
|
|
self.diagnostic_metadata.current_function = previous_value;
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
2022-05-10 14:15:30 -05:00
|
|
|
fn visit_lifetime(&mut self, lifetime: &'ast Lifetime, use_ctxt: visit::LifetimeCtxt) {
|
|
|
|
self.resolve_lifetime(lifetime, use_ctxt)
|
2022-03-06 05:02:13 -06:00
|
|
|
}
|
2019-08-07 18:39:02 -05:00
|
|
|
|
2020-01-22 01:01:21 -06:00
|
|
|
fn visit_generics(&mut self, generics: &'ast Generics) {
|
2022-06-05 04:12:31 -05:00
|
|
|
self.visit_generic_params(
|
2022-03-06 05:02:13 -06:00
|
|
|
&generics.params,
|
|
|
|
self.diagnostic_metadata.current_self_item.is_some(),
|
|
|
|
);
|
2019-08-07 18:39:02 -05:00
|
|
|
for p in &generics.where_clause.predicates {
|
|
|
|
self.visit_where_predicate(p);
|
|
|
|
}
|
|
|
|
}
|
2019-11-18 13:24:13 -06:00
|
|
|
|
2022-07-12 04:34:24 -05:00
|
|
|
fn visit_closure_binder(&mut self, b: &'ast ClosureBinder) {
|
|
|
|
match b {
|
|
|
|
ClosureBinder::NotPresent => {}
|
|
|
|
ClosureBinder::For { generic_params, .. } => {
|
|
|
|
self.visit_generic_params(
|
|
|
|
&generic_params,
|
|
|
|
self.diagnostic_metadata.current_self_item.is_some(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 01:01:21 -06:00
|
|
|
fn visit_generic_arg(&mut self, arg: &'ast GenericArg) {
|
2019-11-18 13:24:13 -06:00
|
|
|
debug!("visit_generic_arg({:?})", arg);
|
2020-01-22 01:01:21 -06:00
|
|
|
let prev = replace(&mut self.diagnostic_metadata.currently_processing_generics, true);
|
2019-11-18 13:24:13 -06:00
|
|
|
match arg {
|
|
|
|
GenericArg::Type(ref ty) => {
|
2020-03-06 05:13:55 -06:00
|
|
|
// We parse const arguments as path types as we cannot distinguish them during
|
2019-11-18 13:24:13 -06:00
|
|
|
// parsing. We try to resolve that ambiguity by attempting resolution the type
|
|
|
|
// namespace first, and if that fails we try again in the value namespace. If
|
|
|
|
// resolution in the value namespace succeeds, we have an generic const argument on
|
|
|
|
// our hands.
|
|
|
|
if let TyKind::Path(ref qself, ref path) = ty.kind {
|
|
|
|
// We cannot disambiguate multi-segment paths right now as that requires type
|
|
|
|
// checking.
|
|
|
|
if path.segments.len() == 1 && path.segments[0].args.is_none() {
|
|
|
|
let mut check_ns = |ns| {
|
2022-04-08 15:50:56 -05:00
|
|
|
self.maybe_resolve_ident_in_lexical_scope(path.segments[0].ident, ns)
|
|
|
|
.is_some()
|
2019-11-18 13:24:13 -06:00
|
|
|
};
|
|
|
|
if !check_ns(TypeNS) && check_ns(ValueNS) {
|
|
|
|
// This must be equivalent to `visit_anon_const`, but we cannot call it
|
|
|
|
// directly due to visitor lifetimes so we have to copy-paste some code.
|
2020-10-22 03:32:41 -05:00
|
|
|
//
|
|
|
|
// Note that we might not be inside of an repeat expression here,
|
|
|
|
// but considering that `IsRepeatExpr` is only relevant for
|
|
|
|
// non-trivial constants this is doesn't matter.
|
2022-05-02 10:36:48 -05:00
|
|
|
self.with_constant_rib(
|
|
|
|
IsRepeatExpr::No,
|
|
|
|
HasGenericParams::Yes,
|
|
|
|
None,
|
|
|
|
|this| {
|
|
|
|
this.smart_resolve_path(
|
|
|
|
ty.id,
|
|
|
|
qself.as_ref(),
|
|
|
|
path,
|
|
|
|
PathSource::Expr(None),
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Some(ref qself) = *qself {
|
|
|
|
this.visit_ty(&qself.ty);
|
|
|
|
}
|
|
|
|
this.visit_path(path, ty.id);
|
|
|
|
},
|
|
|
|
);
|
2019-11-18 13:24:13 -06:00
|
|
|
|
2020-01-22 01:01:21 -06:00
|
|
|
self.diagnostic_metadata.currently_processing_generics = prev;
|
2019-11-18 13:24:13 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.visit_ty(ty);
|
|
|
|
}
|
2022-05-10 12:56:46 -05:00
|
|
|
GenericArg::Lifetime(lt) => self.visit_lifetime(lt, visit::LifetimeCtxt::GenericArg),
|
2019-11-18 13:24:13 -06:00
|
|
|
GenericArg::Const(ct) => self.visit_anon_const(ct),
|
|
|
|
}
|
2020-01-22 01:01:21 -06:00
|
|
|
self.diagnostic_metadata.currently_processing_generics = prev;
|
2019-11-18 13:24:13 -06:00
|
|
|
}
|
2020-09-24 01:01:17 -05:00
|
|
|
|
2022-05-11 15:49:39 -05:00
|
|
|
fn visit_assoc_constraint(&mut self, constraint: &'ast AssocConstraint) {
|
|
|
|
self.visit_ident(constraint.ident);
|
|
|
|
if let Some(ref gen_args) = constraint.gen_args {
|
|
|
|
// Forbid anonymous lifetimes in GAT parameters until proper semantics are decided.
|
|
|
|
self.with_lifetime_rib(LifetimeRibKind::AnonymousReportError, |this| {
|
|
|
|
this.visit_generic_args(gen_args.span(), gen_args)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
match constraint.kind {
|
|
|
|
AssocConstraintKind::Equality { ref term } => match term {
|
|
|
|
Term::Ty(ty) => self.visit_ty(ty),
|
|
|
|
Term::Const(c) => self.visit_anon_const(c),
|
|
|
|
},
|
|
|
|
AssocConstraintKind::Bound { ref bounds } => {
|
|
|
|
walk_list!(self, visit_param_bound, bounds, BoundKind::Bound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-06 05:02:13 -06:00
|
|
|
fn visit_path_segment(&mut self, path_span: Span, path_segment: &'ast PathSegment) {
|
|
|
|
if let Some(ref args) = path_segment.args {
|
|
|
|
match &**args {
|
|
|
|
GenericArgs::AngleBracketed(..) => visit::walk_generic_args(self, path_span, args),
|
2022-05-11 15:49:39 -05:00
|
|
|
GenericArgs::Parenthesized(p_args) => {
|
|
|
|
// Probe the lifetime ribs to know how to behave.
|
|
|
|
for rib in self.lifetime_ribs.iter().rev() {
|
|
|
|
match rib.kind {
|
|
|
|
// We are inside a `PolyTraitRef`. The lifetimes are
|
|
|
|
// to be intoduced in that (maybe implicit) `for<>` binder.
|
|
|
|
LifetimeRibKind::Generics {
|
|
|
|
binder,
|
|
|
|
kind: LifetimeBinderKind::PolyTrait,
|
|
|
|
..
|
|
|
|
} => {
|
2022-05-22 16:31:39 -05:00
|
|
|
self.with_lifetime_rib(
|
|
|
|
LifetimeRibKind::AnonymousCreateParameter {
|
|
|
|
binder,
|
|
|
|
report_in_path: false,
|
|
|
|
},
|
|
|
|
|this| {
|
|
|
|
this.resolve_fn_signature(
|
|
|
|
binder,
|
|
|
|
false,
|
|
|
|
p_args.inputs.iter().map(|ty| (None, &**ty)),
|
|
|
|
&p_args.output,
|
|
|
|
)
|
|
|
|
},
|
2022-05-11 15:49:39 -05:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// We have nowhere to introduce generics. Code is malformed,
|
|
|
|
// so use regular lifetime resolution to avoid spurious errors.
|
|
|
|
LifetimeRibKind::Item | LifetimeRibKind::Generics { .. } => {
|
|
|
|
visit::walk_generic_args(self, path_span, args);
|
|
|
|
break;
|
|
|
|
}
|
2022-05-22 03:22:20 -05:00
|
|
|
LifetimeRibKind::AnonymousCreateParameter { .. }
|
2022-05-11 15:49:39 -05:00
|
|
|
| LifetimeRibKind::AnonymousReportError
|
2022-04-18 10:51:39 -05:00
|
|
|
| LifetimeRibKind::Elided(_)
|
2022-06-05 11:33:09 -05:00
|
|
|
| LifetimeRibKind::ElisionFailure
|
2022-05-11 15:49:39 -05:00
|
|
|
| LifetimeRibKind::AnonConst
|
|
|
|
| LifetimeRibKind::ConstGeneric => {}
|
|
|
|
}
|
|
|
|
}
|
2022-05-10 14:15:30 -05:00
|
|
|
}
|
2022-03-06 05:02:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 01:01:17 -05:00
|
|
|
fn visit_where_predicate(&mut self, p: &'ast WherePredicate) {
|
|
|
|
debug!("visit_where_predicate {:?}", p);
|
|
|
|
let previous_value =
|
|
|
|
replace(&mut self.diagnostic_metadata.current_where_predicate, Some(p));
|
2022-03-06 05:02:13 -06:00
|
|
|
self.with_lifetime_rib(LifetimeRibKind::AnonymousReportError, |this| {
|
|
|
|
if let WherePredicate::BoundPredicate(WhereBoundPredicate {
|
|
|
|
ref bounded_ty,
|
|
|
|
ref bounds,
|
|
|
|
ref bound_generic_params,
|
|
|
|
span: predicate_span,
|
|
|
|
..
|
|
|
|
}) = p
|
|
|
|
{
|
2022-05-10 14:17:21 -05:00
|
|
|
let span = predicate_span.shrink_to_lo().to(bounded_ty.span.shrink_to_lo());
|
2022-03-06 05:02:13 -06:00
|
|
|
this.with_generic_param_rib(
|
|
|
|
&bound_generic_params,
|
|
|
|
NormalRibKind,
|
2022-04-07 13:54:13 -05:00
|
|
|
LifetimeRibKind::Generics {
|
2022-05-10 14:15:30 -05:00
|
|
|
binder: bounded_ty.id,
|
2022-04-07 13:54:13 -05:00
|
|
|
kind: LifetimeBinderKind::WhereBound,
|
|
|
|
span,
|
|
|
|
},
|
2022-03-06 05:02:13 -06:00
|
|
|
|this| {
|
2022-06-05 04:12:31 -05:00
|
|
|
this.visit_generic_params(&bound_generic_params, false);
|
2022-03-06 05:02:13 -06:00
|
|
|
this.visit_ty(bounded_ty);
|
|
|
|
for bound in bounds {
|
2022-04-20 21:09:32 -05:00
|
|
|
this.visit_param_bound(bound, BoundKind::Bound)
|
2022-03-06 05:02:13 -06:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
visit::walk_where_predicate(this, p);
|
|
|
|
}
|
|
|
|
});
|
2020-09-24 01:01:17 -05:00
|
|
|
self.diagnostic_metadata.current_where_predicate = previous_value;
|
|
|
|
}
|
2022-02-28 18:50:56 -06:00
|
|
|
|
2022-05-06 20:57:42 -05:00
|
|
|
fn visit_inline_asm(&mut self, asm: &'ast InlineAsm) {
|
|
|
|
for (op, _) in &asm.operands {
|
|
|
|
match op {
|
|
|
|
InlineAsmOperand::In { expr, .. }
|
|
|
|
| InlineAsmOperand::Out { expr: Some(expr), .. }
|
|
|
|
| InlineAsmOperand::InOut { expr, .. } => self.visit_expr(expr),
|
|
|
|
InlineAsmOperand::Out { expr: None, .. } => {}
|
|
|
|
InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
|
|
|
|
self.visit_expr(in_expr);
|
|
|
|
if let Some(out_expr) = out_expr {
|
|
|
|
self.visit_expr(out_expr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
InlineAsmOperand::Const { anon_const, .. } => {
|
|
|
|
// Although this is `DefKind::AnonConst`, it is allowed to reference outer
|
|
|
|
// generic parameters like an inline const.
|
|
|
|
self.resolve_inline_const(anon_const);
|
|
|
|
}
|
|
|
|
InlineAsmOperand::Sym { sym } => self.visit_inline_asm_sym(sym),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 18:50:56 -06:00
|
|
|
fn visit_inline_asm_sym(&mut self, sym: &'ast InlineAsmSym) {
|
|
|
|
// This is similar to the code for AnonConst.
|
|
|
|
self.with_rib(ValueNS, InlineAsmSymRibKind, |this| {
|
|
|
|
this.with_rib(TypeNS, InlineAsmSymRibKind, |this| {
|
|
|
|
this.with_label_rib(InlineAsmSymRibKind, |this| {
|
|
|
|
this.smart_resolve_path(
|
|
|
|
sym.id,
|
|
|
|
sym.qself.as_ref(),
|
|
|
|
&sym.path,
|
|
|
|
PathSource::Expr(None),
|
|
|
|
);
|
|
|
|
visit::walk_inline_asm_sym(this, sym);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
2020-09-08 17:14:09 -05:00
|
|
|
impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
2020-07-10 15:55:06 -05:00
|
|
|
fn new(resolver: &'b mut Resolver<'a>) -> LateResolutionVisitor<'a, 'b, 'ast> {
|
2019-08-07 19:44:16 -05:00
|
|
|
// During late resolution we only track the module component of the parent scope,
|
|
|
|
// although it may be useful to track other components as well for diagnostics.
|
2019-08-07 18:39:02 -05:00
|
|
|
let graph_root = resolver.graph_root;
|
2020-11-06 07:11:21 -06:00
|
|
|
let parent_scope = ParentScope::module(graph_root, resolver);
|
2019-08-28 01:48:10 -05:00
|
|
|
let start_rib_kind = ModuleRibKind(graph_root);
|
2019-08-07 18:39:02 -05:00
|
|
|
LateResolutionVisitor {
|
2019-08-08 06:06:42 -05:00
|
|
|
r: resolver,
|
2019-08-07 19:44:16 -05:00
|
|
|
parent_scope,
|
2019-08-07 18:39:02 -05:00
|
|
|
ribs: PerNS {
|
2019-08-28 01:48:10 -05:00
|
|
|
value_ns: vec![Rib::new(start_rib_kind)],
|
|
|
|
type_ns: vec![Rib::new(start_rib_kind)],
|
|
|
|
macro_ns: vec![Rib::new(start_rib_kind)],
|
2019-08-07 18:39:02 -05:00
|
|
|
},
|
|
|
|
label_ribs: Vec::new(),
|
2022-03-06 05:02:13 -06:00
|
|
|
lifetime_ribs: Vec::new(),
|
2022-06-05 11:33:09 -05:00
|
|
|
lifetime_elision_candidates: None,
|
2019-08-07 18:39:02 -05:00
|
|
|
current_trait_ref: None,
|
2022-06-14 21:23:19 -05:00
|
|
|
diagnostic_metadata: Box::new(DiagnosticMetadata::default()),
|
2020-07-10 15:55:06 -05:00
|
|
|
// errors at module scope should always be reported
|
|
|
|
in_func_body: false,
|
2022-05-10 14:15:30 -05:00
|
|
|
lifetime_uses: Default::default(),
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-08 15:50:56 -05:00
|
|
|
fn maybe_resolve_ident_in_lexical_scope(
|
|
|
|
&mut self,
|
|
|
|
ident: Ident,
|
|
|
|
ns: Namespace,
|
|
|
|
) -> Option<LexicalScopeBinding<'a>> {
|
|
|
|
self.r.resolve_ident_in_lexical_scope(
|
|
|
|
ident,
|
|
|
|
ns,
|
|
|
|
&self.parent_scope,
|
2022-04-30 08:26:36 -05:00
|
|
|
None,
|
2022-04-08 15:50:56 -05:00
|
|
|
&self.ribs[ns],
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-08-07 18:39:02 -05:00
|
|
|
fn resolve_ident_in_lexical_scope(
|
|
|
|
&mut self,
|
|
|
|
ident: Ident,
|
|
|
|
ns: Namespace,
|
2022-04-30 08:26:36 -05:00
|
|
|
finalize: Option<Finalize>,
|
2022-04-30 10:01:01 -05:00
|
|
|
ignore_binding: Option<&'a NameBinding<'a>>,
|
2021-10-21 08:09:24 -05:00
|
|
|
) -> Option<LexicalScopeBinding<'a>> {
|
|
|
|
self.r.resolve_ident_in_lexical_scope(
|
|
|
|
ident,
|
|
|
|
ns,
|
|
|
|
&self.parent_scope,
|
2022-03-23 18:55:22 -05:00
|
|
|
finalize,
|
2021-10-21 08:09:24 -05:00
|
|
|
&self.ribs[ns],
|
2022-04-30 10:01:01 -05:00
|
|
|
ignore_binding,
|
2021-10-21 08:09:24 -05:00
|
|
|
)
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_path(
|
|
|
|
&mut self,
|
|
|
|
path: &[Segment],
|
|
|
|
opt_ns: Option<Namespace>, // `None` indicates a module path in import
|
2022-04-30 08:26:36 -05:00
|
|
|
finalize: Option<Finalize>,
|
2019-08-07 18:39:02 -05:00
|
|
|
) -> PathResult<'a> {
|
2022-04-08 15:50:56 -05:00
|
|
|
self.r.resolve_path_with_ribs(
|
|
|
|
path,
|
|
|
|
opt_ns,
|
|
|
|
&self.parent_scope,
|
|
|
|
finalize,
|
|
|
|
Some(&self.ribs),
|
|
|
|
None,
|
|
|
|
)
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// AST resolution
|
|
|
|
//
|
|
|
|
// We maintain a list of value ribs and type ribs.
|
|
|
|
//
|
|
|
|
// Simultaneously, we keep track of the current position in the module
|
2019-08-07 19:44:16 -05:00
|
|
|
// graph in the `parent_scope.module` pointer. When we go to resolve a name in
|
2019-08-07 18:39:02 -05:00
|
|
|
// the value or type namespaces, we first look through all the ribs and
|
|
|
|
// then query the module graph. When we resolve a name in the module
|
|
|
|
// namespace, we can skip all the ribs (since nested modules are not
|
|
|
|
// allowed within blocks in Rust) and jump straight to the current module
|
|
|
|
// graph node.
|
|
|
|
//
|
|
|
|
// Named implementations are handled separately. When we find a method
|
|
|
|
// call, we consult the module node to find all of the implementations in
|
|
|
|
// scope. This information is lazily cached in the module node. We then
|
|
|
|
// generate a fake "implementation scope" containing all the
|
|
|
|
// implementations thus found, for compatibility with old resolve pass.
|
|
|
|
|
2019-08-28 01:48:10 -05:00
|
|
|
/// Do some `work` within a new innermost rib of the given `kind` in the given namespace (`ns`).
|
|
|
|
fn with_rib<T>(
|
|
|
|
&mut self,
|
|
|
|
ns: Namespace,
|
|
|
|
kind: RibKind<'a>,
|
|
|
|
work: impl FnOnce(&mut Self) -> T,
|
|
|
|
) -> T {
|
|
|
|
self.ribs[ns].push(Rib::new(kind));
|
|
|
|
let ret = work(self);
|
|
|
|
self.ribs[ns].pop();
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_scope<T>(&mut self, id: NodeId, f: impl FnOnce(&mut Self) -> T) -> T {
|
2021-09-11 18:06:27 -05:00
|
|
|
if let Some(module) = self.r.get_module(self.r.local_def_id(id).to_def_id()) {
|
2019-08-07 18:39:02 -05:00
|
|
|
// Move down in the graph.
|
2019-08-07 19:44:16 -05:00
|
|
|
let orig_module = replace(&mut self.parent_scope.module, module);
|
2019-08-28 01:48:10 -05:00
|
|
|
self.with_rib(ValueNS, ModuleRibKind(module), |this| {
|
|
|
|
this.with_rib(TypeNS, ModuleRibKind(module), |this| {
|
|
|
|
let ret = f(this);
|
|
|
|
this.parent_scope.module = orig_module;
|
|
|
|
ret
|
|
|
|
})
|
|
|
|
})
|
2019-08-07 18:39:02 -05:00
|
|
|
} else {
|
|
|
|
f(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 04:12:31 -05:00
|
|
|
fn visit_generic_params(&mut self, params: &'ast [GenericParam], add_self_upper: bool) {
|
2022-03-06 05:02:13 -06:00
|
|
|
// For type parameter defaults, we have to ban access
|
|
|
|
// to following type parameters, as the InternalSubsts can only
|
|
|
|
// provide previous type parameters as they're built. We
|
|
|
|
// put all the parameters on the ban list and then remove
|
|
|
|
// them one by one as they are processed and become available.
|
|
|
|
let mut forward_ty_ban_rib = Rib::new(ForwardGenericParamBanRibKind);
|
|
|
|
let mut forward_const_ban_rib = Rib::new(ForwardGenericParamBanRibKind);
|
|
|
|
for param in params.iter() {
|
|
|
|
match param.kind {
|
|
|
|
GenericParamKind::Type { .. } => {
|
|
|
|
forward_ty_ban_rib
|
|
|
|
.bindings
|
|
|
|
.insert(Ident::with_dummy_span(param.ident.name), Res::Err);
|
|
|
|
}
|
|
|
|
GenericParamKind::Const { .. } => {
|
|
|
|
forward_const_ban_rib
|
|
|
|
.bindings
|
|
|
|
.insert(Ident::with_dummy_span(param.ident.name), Res::Err);
|
|
|
|
}
|
|
|
|
GenericParamKind::Lifetime => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// rust-lang/rust#61631: The type `Self` is essentially
|
|
|
|
// another type parameter. For ADTs, we consider it
|
|
|
|
// well-defined only after all of the ADT type parameters have
|
|
|
|
// been provided. Therefore, we do not allow use of `Self`
|
|
|
|
// anywhere in ADT type parameter defaults.
|
|
|
|
//
|
|
|
|
// (We however cannot ban `Self` for defaults on *all* generic
|
|
|
|
// lists; e.g. trait generics can usefully refer to `Self`,
|
|
|
|
// such as in the case of `trait Add<Rhs = Self>`.)
|
|
|
|
if add_self_upper {
|
|
|
|
// (`Some` if + only if we are in ADT's generics.)
|
|
|
|
forward_ty_ban_rib.bindings.insert(Ident::with_dummy_span(kw::SelfUpper), Res::Err);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.with_lifetime_rib(LifetimeRibKind::AnonymousReportError, |this| {
|
|
|
|
for param in params {
|
|
|
|
match param.kind {
|
|
|
|
GenericParamKind::Lifetime => {
|
|
|
|
for bound in ¶m.bounds {
|
2022-04-20 21:09:32 -05:00
|
|
|
this.visit_param_bound(bound, BoundKind::Bound);
|
2022-03-06 05:02:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
GenericParamKind::Type { ref default } => {
|
|
|
|
for bound in ¶m.bounds {
|
2022-04-20 21:09:32 -05:00
|
|
|
this.visit_param_bound(bound, BoundKind::Bound);
|
2022-03-06 05:02:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ref ty) = default {
|
|
|
|
this.ribs[TypeNS].push(forward_ty_ban_rib);
|
|
|
|
this.ribs[ValueNS].push(forward_const_ban_rib);
|
|
|
|
this.visit_ty(ty);
|
|
|
|
forward_const_ban_rib = this.ribs[ValueNS].pop().unwrap();
|
|
|
|
forward_ty_ban_rib = this.ribs[TypeNS].pop().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow all following defaults to refer to this type parameter.
|
|
|
|
forward_ty_ban_rib
|
|
|
|
.bindings
|
|
|
|
.remove(&Ident::with_dummy_span(param.ident.name));
|
|
|
|
}
|
|
|
|
GenericParamKind::Const { ref ty, kw_span: _, ref default } => {
|
|
|
|
// Const parameters can't have param bounds.
|
|
|
|
assert!(param.bounds.is_empty());
|
|
|
|
|
|
|
|
this.ribs[TypeNS].push(Rib::new(ConstParamTyRibKind));
|
|
|
|
this.ribs[ValueNS].push(Rib::new(ConstParamTyRibKind));
|
2021-12-04 17:07:21 -06:00
|
|
|
this.with_lifetime_rib(LifetimeRibKind::ConstGeneric, |this| {
|
|
|
|
this.visit_ty(ty)
|
|
|
|
});
|
2022-03-06 05:02:13 -06:00
|
|
|
this.ribs[TypeNS].pop().unwrap();
|
|
|
|
this.ribs[ValueNS].pop().unwrap();
|
|
|
|
|
|
|
|
if let Some(ref expr) = default {
|
|
|
|
this.ribs[TypeNS].push(forward_ty_ban_rib);
|
|
|
|
this.ribs[ValueNS].push(forward_const_ban_rib);
|
2021-12-04 17:07:21 -06:00
|
|
|
this.with_lifetime_rib(LifetimeRibKind::ConstGeneric, |this| {
|
2022-03-31 13:10:37 -05:00
|
|
|
this.resolve_anon_const(expr, IsRepeatExpr::No)
|
2021-12-04 17:07:21 -06:00
|
|
|
});
|
2022-03-06 05:02:13 -06:00
|
|
|
forward_const_ban_rib = this.ribs[ValueNS].pop().unwrap();
|
|
|
|
forward_ty_ban_rib = this.ribs[TypeNS].pop().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow all following defaults to refer to this const parameter.
|
|
|
|
forward_const_ban_rib
|
|
|
|
.bindings
|
|
|
|
.remove(&Ident::with_dummy_span(param.ident.name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip(self, work))]
|
|
|
|
fn with_lifetime_rib<T>(
|
|
|
|
&mut self,
|
|
|
|
kind: LifetimeRibKind,
|
|
|
|
work: impl FnOnce(&mut Self) -> T,
|
|
|
|
) -> T {
|
|
|
|
self.lifetime_ribs.push(LifetimeRib::new(kind));
|
2022-06-05 11:33:09 -05:00
|
|
|
let outer_elision_candidates = self.lifetime_elision_candidates.take();
|
2022-03-06 05:02:13 -06:00
|
|
|
let ret = work(self);
|
2022-06-05 11:33:09 -05:00
|
|
|
self.lifetime_elision_candidates = outer_elision_candidates;
|
2022-03-06 05:02:13 -06:00
|
|
|
self.lifetime_ribs.pop();
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
2022-05-10 14:15:30 -05:00
|
|
|
fn resolve_lifetime(&mut self, lifetime: &'ast Lifetime, use_ctxt: visit::LifetimeCtxt) {
|
2022-03-06 05:02:13 -06:00
|
|
|
let ident = lifetime.ident;
|
|
|
|
|
|
|
|
if ident.name == kw::StaticLifetime {
|
2022-06-05 11:33:09 -05:00
|
|
|
self.record_lifetime_res(
|
|
|
|
lifetime.id,
|
|
|
|
LifetimeRes::Static,
|
|
|
|
LifetimeElisionCandidate::Named,
|
|
|
|
);
|
2022-03-06 05:02:13 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ident.name == kw::UnderscoreLifetime {
|
|
|
|
return self.resolve_anonymous_lifetime(lifetime, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut indices = (0..self.lifetime_ribs.len()).rev();
|
|
|
|
for i in &mut indices {
|
|
|
|
let rib = &self.lifetime_ribs[i];
|
|
|
|
let normalized_ident = ident.normalize_to_macros_2_0();
|
2022-06-05 14:43:02 -05:00
|
|
|
if let Some(&(_, res)) = rib.bindings.get(&normalized_ident) {
|
2022-06-05 11:33:09 -05:00
|
|
|
self.record_lifetime_res(lifetime.id, res, LifetimeElisionCandidate::Named);
|
2022-05-10 14:15:30 -05:00
|
|
|
|
2022-06-05 14:43:02 -05:00
|
|
|
if let LifetimeRes::Param { param, .. } = res {
|
2022-05-10 14:15:30 -05:00
|
|
|
match self.lifetime_uses.entry(param) {
|
|
|
|
Entry::Vacant(v) => {
|
2022-06-05 14:43:02 -05:00
|
|
|
debug!("First use of {:?} at {:?}", res, ident.span);
|
2022-05-10 14:15:30 -05:00
|
|
|
let use_set = self
|
|
|
|
.lifetime_ribs
|
|
|
|
.iter()
|
|
|
|
.rev()
|
|
|
|
.find_map(|rib| match rib.kind {
|
|
|
|
// Do not suggest eliding a lifetime where an anonymous
|
|
|
|
// lifetime would be illegal.
|
|
|
|
LifetimeRibKind::Item
|
2022-06-05 11:33:09 -05:00
|
|
|
| LifetimeRibKind::AnonymousReportError
|
|
|
|
| LifetimeRibKind::ElisionFailure => Some(LifetimeUseSet::Many),
|
2022-05-10 14:15:30 -05:00
|
|
|
// An anonymous lifetime is legal here, go ahead.
|
2022-05-22 03:22:20 -05:00
|
|
|
LifetimeRibKind::AnonymousCreateParameter { .. } => {
|
2022-05-10 14:15:30 -05:00
|
|
|
Some(LifetimeUseSet::One { use_span: ident.span, use_ctxt })
|
|
|
|
}
|
2022-04-18 10:51:39 -05:00
|
|
|
// Only report if eliding the lifetime would have the same
|
|
|
|
// semantics.
|
|
|
|
LifetimeRibKind::Elided(r) => Some(if res == r {
|
|
|
|
LifetimeUseSet::One { use_span: ident.span, use_ctxt }
|
|
|
|
} else {
|
|
|
|
LifetimeUseSet::Many
|
|
|
|
}),
|
2022-06-05 14:55:30 -05:00
|
|
|
LifetimeRibKind::Generics { .. }
|
|
|
|
| LifetimeRibKind::ConstGeneric
|
|
|
|
| LifetimeRibKind::AnonConst => None,
|
2022-05-10 14:15:30 -05:00
|
|
|
})
|
|
|
|
.unwrap_or(LifetimeUseSet::Many);
|
|
|
|
debug!(?use_ctxt, ?use_set);
|
|
|
|
v.insert(use_set);
|
|
|
|
}
|
|
|
|
Entry::Occupied(mut o) => {
|
2022-06-05 14:43:02 -05:00
|
|
|
debug!("Many uses of {:?} at {:?}", res, ident.span);
|
2022-05-10 14:15:30 -05:00
|
|
|
*o.get_mut() = LifetimeUseSet::Many;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-06 05:02:13 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-04 17:07:21 -06:00
|
|
|
match rib.kind {
|
|
|
|
LifetimeRibKind::Item => break,
|
|
|
|
LifetimeRibKind::ConstGeneric => {
|
|
|
|
self.emit_non_static_lt_in_const_generic_error(lifetime);
|
2022-06-05 11:33:09 -05:00
|
|
|
self.record_lifetime_res(
|
|
|
|
lifetime.id,
|
|
|
|
LifetimeRes::Error,
|
|
|
|
LifetimeElisionCandidate::Ignore,
|
|
|
|
);
|
2021-12-04 17:07:21 -06:00
|
|
|
return;
|
|
|
|
}
|
2022-03-31 13:10:37 -05:00
|
|
|
LifetimeRibKind::AnonConst => {
|
|
|
|
self.maybe_emit_forbidden_non_static_lifetime_error(lifetime);
|
2022-06-05 11:33:09 -05:00
|
|
|
self.record_lifetime_res(
|
|
|
|
lifetime.id,
|
|
|
|
LifetimeRes::Error,
|
|
|
|
LifetimeElisionCandidate::Ignore,
|
|
|
|
);
|
2022-03-31 13:10:37 -05:00
|
|
|
return;
|
|
|
|
}
|
2021-12-04 17:07:21 -06:00
|
|
|
_ => {}
|
2022-03-06 05:02:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut outer_res = None;
|
|
|
|
for i in indices {
|
|
|
|
let rib = &self.lifetime_ribs[i];
|
|
|
|
let normalized_ident = ident.normalize_to_macros_2_0();
|
|
|
|
if let Some((&outer, _)) = rib.bindings.get_key_value(&normalized_ident) {
|
|
|
|
outer_res = Some(outer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emit_undeclared_lifetime_error(lifetime, outer_res);
|
2022-06-05 11:33:09 -05:00
|
|
|
self.record_lifetime_res(lifetime.id, LifetimeRes::Error, LifetimeElisionCandidate::Named);
|
2022-03-06 05:02:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
|
|
|
fn resolve_anonymous_lifetime(&mut self, lifetime: &Lifetime, elided: bool) {
|
|
|
|
debug_assert_eq!(lifetime.ident.name, kw::UnderscoreLifetime);
|
|
|
|
|
2022-06-05 11:33:09 -05:00
|
|
|
let missing_lifetime = MissingLifetime {
|
|
|
|
id: lifetime.id,
|
|
|
|
span: lifetime.ident.span,
|
|
|
|
kind: if elided {
|
|
|
|
MissingLifetimeKind::Ampersand
|
|
|
|
} else {
|
|
|
|
MissingLifetimeKind::Underscore
|
|
|
|
},
|
|
|
|
count: 1,
|
|
|
|
};
|
|
|
|
let elision_candidate = LifetimeElisionCandidate::Missing(missing_lifetime);
|
2022-03-06 05:02:13 -06:00
|
|
|
for i in (0..self.lifetime_ribs.len()).rev() {
|
|
|
|
let rib = &mut self.lifetime_ribs[i];
|
2022-06-05 11:33:09 -05:00
|
|
|
debug!(?rib.kind);
|
2022-03-06 05:02:13 -06:00
|
|
|
match rib.kind {
|
2022-05-21 07:06:26 -05:00
|
|
|
LifetimeRibKind::AnonymousCreateParameter { binder, .. } => {
|
|
|
|
let res = self.create_fresh_lifetime(lifetime.id, lifetime.ident, binder);
|
2022-06-05 11:33:09 -05:00
|
|
|
self.record_lifetime_res(lifetime.id, res, elision_candidate);
|
2022-04-07 13:54:13 -05:00
|
|
|
return;
|
|
|
|
}
|
2022-03-06 05:02:13 -06:00
|
|
|
LifetimeRibKind::AnonymousReportError => {
|
|
|
|
let (msg, note) = if elided {
|
|
|
|
(
|
|
|
|
"`&` without an explicit lifetime name cannot be used here",
|
|
|
|
"explicit lifetime name needed here",
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
("`'_` cannot be used here", "`'_` is a reserved lifetime name")
|
|
|
|
};
|
|
|
|
rustc_errors::struct_span_err!(
|
|
|
|
self.r.session,
|
|
|
|
lifetime.ident.span,
|
|
|
|
E0637,
|
|
|
|
"{}",
|
|
|
|
msg,
|
|
|
|
)
|
|
|
|
.span_label(lifetime.ident.span, note)
|
|
|
|
.emit();
|
|
|
|
|
2022-06-05 11:33:09 -05:00
|
|
|
self.record_lifetime_res(lifetime.id, LifetimeRes::Error, elision_candidate);
|
2022-04-07 13:54:13 -05:00
|
|
|
return;
|
|
|
|
}
|
2022-04-18 10:51:39 -05:00
|
|
|
LifetimeRibKind::Elided(res) => {
|
2022-06-05 11:33:09 -05:00
|
|
|
self.record_lifetime_res(lifetime.id, res, elision_candidate);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LifetimeRibKind::ElisionFailure => {
|
|
|
|
self.diagnostic_metadata.current_elision_failures.push(missing_lifetime);
|
|
|
|
self.record_lifetime_res(lifetime.id, LifetimeRes::Error, elision_candidate);
|
2022-04-18 10:51:39 -05:00
|
|
|
return;
|
|
|
|
}
|
2022-04-07 13:54:13 -05:00
|
|
|
LifetimeRibKind::Item => break,
|
2022-06-05 14:55:30 -05:00
|
|
|
LifetimeRibKind::Generics { .. }
|
|
|
|
| LifetimeRibKind::ConstGeneric
|
|
|
|
| LifetimeRibKind::AnonConst => {}
|
2022-03-06 05:02:13 -06:00
|
|
|
}
|
|
|
|
}
|
2022-06-05 11:33:09 -05:00
|
|
|
self.record_lifetime_res(lifetime.id, LifetimeRes::Error, elision_candidate);
|
|
|
|
self.report_missing_lifetime_specifiers(vec![missing_lifetime], None);
|
2022-03-06 05:02:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
|
|
|
fn resolve_elided_lifetime(&mut self, anchor_id: NodeId, span: Span) {
|
|
|
|
let id = self.r.next_node_id();
|
2022-06-05 11:33:09 -05:00
|
|
|
let lt = Lifetime { id, ident: Ident::new(kw::UnderscoreLifetime, span) };
|
|
|
|
|
2022-04-07 13:54:13 -05:00
|
|
|
self.record_lifetime_res(
|
|
|
|
anchor_id,
|
|
|
|
LifetimeRes::ElidedAnchor { start: id, end: NodeId::from_u32(id.as_u32() + 1) },
|
2022-06-05 11:33:09 -05:00
|
|
|
LifetimeElisionCandidate::Ignore,
|
2022-04-07 13:54:13 -05:00
|
|
|
);
|
2022-03-06 05:02:13 -06:00
|
|
|
self.resolve_anonymous_lifetime(<, true);
|
|
|
|
}
|
|
|
|
|
2022-04-07 13:54:13 -05:00
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
2022-06-06 02:02:24 -05:00
|
|
|
fn create_fresh_lifetime(&mut self, id: NodeId, ident: Ident, binder: NodeId) -> LifetimeRes {
|
2022-04-07 13:54:13 -05:00
|
|
|
debug_assert_eq!(ident.name, kw::UnderscoreLifetime);
|
|
|
|
debug!(?ident.span);
|
|
|
|
|
2022-06-06 02:02:24 -05:00
|
|
|
// Leave the responsibility to create the `LocalDefId` to lowering.
|
|
|
|
let param = self.r.next_node_id();
|
|
|
|
let res = LifetimeRes::Fresh { param, binder };
|
|
|
|
|
|
|
|
// Record the created lifetime parameter so lowering can pick it up and add it to HIR.
|
|
|
|
self.r
|
|
|
|
.extra_lifetime_params_map
|
|
|
|
.entry(binder)
|
|
|
|
.or_insert_with(Vec::new)
|
|
|
|
.push((ident, param, res));
|
2022-06-05 14:43:02 -05:00
|
|
|
res
|
2022-04-07 13:54:13 -05:00
|
|
|
}
|
|
|
|
|
2022-03-10 16:12:35 -06:00
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
|
|
|
fn resolve_elided_lifetimes_in_path(
|
|
|
|
&mut self,
|
|
|
|
path_id: NodeId,
|
|
|
|
partial_res: PartialRes,
|
|
|
|
path: &[Segment],
|
|
|
|
source: PathSource<'_>,
|
2022-04-30 08:26:36 -05:00
|
|
|
path_span: Span,
|
2022-03-10 16:12:35 -06:00
|
|
|
) {
|
|
|
|
let proj_start = path.len() - partial_res.unresolved_segments();
|
|
|
|
for (i, segment) in path.iter().enumerate() {
|
|
|
|
if segment.has_lifetime_args {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let Some(segment_id) = segment.id else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Figure out if this is a type/trait segment,
|
|
|
|
// which may need lifetime elision performed.
|
|
|
|
let type_def_id = match partial_res.base_res() {
|
2022-04-25 14:08:45 -05:00
|
|
|
Res::Def(DefKind::AssocTy, def_id) if i + 2 == proj_start => self.r.parent(def_id),
|
|
|
|
Res::Def(DefKind::Variant, def_id) if i + 1 == proj_start => self.r.parent(def_id),
|
2022-03-10 16:12:35 -06:00
|
|
|
Res::Def(DefKind::Struct, def_id)
|
|
|
|
| Res::Def(DefKind::Union, def_id)
|
|
|
|
| Res::Def(DefKind::Enum, def_id)
|
|
|
|
| Res::Def(DefKind::TyAlias, def_id)
|
|
|
|
| Res::Def(DefKind::Trait, def_id)
|
|
|
|
if i + 1 == proj_start =>
|
|
|
|
{
|
|
|
|
def_id
|
|
|
|
}
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
let expected_lifetimes = self.r.item_generics_num_lifetimes(type_def_id);
|
|
|
|
if expected_lifetimes == 0 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-05-11 15:49:39 -05:00
|
|
|
let missing = match source {
|
2022-03-10 16:12:35 -06:00
|
|
|
PathSource::Trait(..) | PathSource::TraitItem(..) | PathSource::Type => true,
|
|
|
|
PathSource::Expr(..)
|
|
|
|
| PathSource::Pat
|
|
|
|
| PathSource::Struct
|
|
|
|
| PathSource::TupleStruct(..) => false,
|
|
|
|
};
|
2022-06-13 00:36:45 -05:00
|
|
|
if !missing && !segment.has_generic_args {
|
|
|
|
continue;
|
|
|
|
}
|
2022-05-21 07:06:26 -05:00
|
|
|
|
|
|
|
let elided_lifetime_span = if segment.has_generic_args {
|
|
|
|
// If there are brackets, but not generic arguments, then use the opening bracket
|
|
|
|
segment.args_span.with_hi(segment.args_span.lo() + BytePos(1))
|
|
|
|
} else {
|
|
|
|
// If there are no brackets, use the identifier span.
|
|
|
|
// HACK: we use find_ancestor_inside to properly suggest elided spans in paths
|
|
|
|
// originating from macros, since the segment's span might be from a macro arg.
|
|
|
|
segment.ident.span.find_ancestor_inside(path_span).unwrap_or(path_span)
|
|
|
|
};
|
|
|
|
let ident = Ident::new(kw::UnderscoreLifetime, elided_lifetime_span);
|
|
|
|
|
|
|
|
let node_ids = self.r.next_node_ids(expected_lifetimes);
|
|
|
|
self.record_lifetime_res(
|
|
|
|
segment_id,
|
|
|
|
LifetimeRes::ElidedAnchor { start: node_ids.start, end: node_ids.end },
|
2022-06-05 11:33:09 -05:00
|
|
|
LifetimeElisionCandidate::Ignore,
|
2022-05-21 07:06:26 -05:00
|
|
|
);
|
|
|
|
|
2022-05-11 15:49:39 -05:00
|
|
|
if !missing {
|
|
|
|
// Do not create a parameter for patterns and expressions.
|
2022-06-13 01:22:06 -05:00
|
|
|
for id in node_ids {
|
|
|
|
self.record_lifetime_res(
|
|
|
|
id,
|
|
|
|
LifetimeRes::Infer,
|
|
|
|
LifetimeElisionCandidate::Named,
|
|
|
|
);
|
2022-05-11 15:49:39 -05:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-06-05 11:33:09 -05:00
|
|
|
let missing_lifetime = MissingLifetime {
|
|
|
|
id: node_ids.start,
|
|
|
|
span: elided_lifetime_span,
|
|
|
|
kind: if segment.has_generic_args {
|
|
|
|
MissingLifetimeKind::Comma
|
|
|
|
} else {
|
|
|
|
MissingLifetimeKind::Brackets
|
|
|
|
},
|
|
|
|
count: expected_lifetimes,
|
|
|
|
};
|
2022-05-11 15:49:39 -05:00
|
|
|
let mut should_lint = true;
|
2022-03-10 16:12:35 -06:00
|
|
|
for rib in self.lifetime_ribs.iter().rev() {
|
|
|
|
match rib.kind {
|
|
|
|
// In create-parameter mode we error here because we don't want to support
|
|
|
|
// deprecated impl elision in new features like impl elision and `async fn`,
|
|
|
|
// both of which work using the `CreateParameter` mode:
|
|
|
|
//
|
|
|
|
// impl Foo for std::cell::Ref<u32> // note lack of '_
|
|
|
|
// async fn foo(_: std::cell::Ref<u32>) { ... }
|
2022-05-21 07:06:26 -05:00
|
|
|
LifetimeRibKind::AnonymousCreateParameter { report_in_path: true, .. } => {
|
|
|
|
let sess = self.r.session;
|
|
|
|
let mut err = rustc_errors::struct_span_err!(
|
|
|
|
sess,
|
|
|
|
path_span,
|
|
|
|
E0726,
|
|
|
|
"implicit elided lifetime not allowed here"
|
|
|
|
);
|
|
|
|
rustc_errors::add_elided_lifetime_in_path_suggestion(
|
|
|
|
sess.source_map(),
|
|
|
|
&mut err,
|
|
|
|
expected_lifetimes,
|
|
|
|
path_span,
|
|
|
|
!segment.has_generic_args,
|
|
|
|
elided_lifetime_span,
|
|
|
|
);
|
|
|
|
err.note("assuming a `'static` lifetime...");
|
|
|
|
err.emit();
|
|
|
|
should_lint = false;
|
2022-05-11 15:49:39 -05:00
|
|
|
|
|
|
|
for id in node_ids {
|
2022-06-05 11:33:09 -05:00
|
|
|
self.record_lifetime_res(
|
|
|
|
id,
|
|
|
|
LifetimeRes::Error,
|
|
|
|
LifetimeElisionCandidate::Named,
|
|
|
|
);
|
2022-05-21 07:06:26 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2022-05-11 15:49:39 -05:00
|
|
|
// Do not create a parameter for patterns and expressions.
|
2022-05-21 07:06:26 -05:00
|
|
|
LifetimeRibKind::AnonymousCreateParameter { binder, .. } => {
|
2022-06-05 11:33:09 -05:00
|
|
|
// Group all suggestions into the first record.
|
|
|
|
let mut candidate = LifetimeElisionCandidate::Missing(missing_lifetime);
|
2022-05-11 15:49:39 -05:00
|
|
|
for id in node_ids {
|
2022-05-21 07:06:26 -05:00
|
|
|
let res = self.create_fresh_lifetime(id, ident, binder);
|
2022-06-05 11:33:09 -05:00
|
|
|
self.record_lifetime_res(
|
|
|
|
id,
|
|
|
|
res,
|
|
|
|
replace(&mut candidate, LifetimeElisionCandidate::Named),
|
|
|
|
);
|
2022-05-21 07:06:26 -05:00
|
|
|
}
|
2022-03-10 16:12:35 -06:00
|
|
|
break;
|
|
|
|
}
|
2022-04-18 10:51:39 -05:00
|
|
|
LifetimeRibKind::Elided(res) => {
|
2022-06-05 11:33:09 -05:00
|
|
|
let mut candidate = LifetimeElisionCandidate::Missing(missing_lifetime);
|
|
|
|
for id in node_ids {
|
|
|
|
self.record_lifetime_res(
|
|
|
|
id,
|
|
|
|
res,
|
|
|
|
replace(&mut candidate, LifetimeElisionCandidate::Ignore),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
LifetimeRibKind::ElisionFailure => {
|
|
|
|
self.diagnostic_metadata.current_elision_failures.push(missing_lifetime);
|
2022-04-18 10:51:39 -05:00
|
|
|
for id in node_ids {
|
2022-06-05 11:33:09 -05:00
|
|
|
self.record_lifetime_res(
|
|
|
|
id,
|
|
|
|
LifetimeRes::Error,
|
|
|
|
LifetimeElisionCandidate::Ignore,
|
|
|
|
);
|
2022-05-21 07:06:26 -05:00
|
|
|
}
|
2022-04-29 11:48:48 -05:00
|
|
|
break;
|
|
|
|
}
|
2022-05-11 15:49:39 -05:00
|
|
|
// `LifetimeRes::Error`, which would usually be used in the case of
|
|
|
|
// `ReportError`, is unsuitable here, as we don't emit an error yet. Instead,
|
|
|
|
// we simply resolve to an implicit lifetime, which will be checked later, at
|
|
|
|
// which point a suitable error will be emitted.
|
2022-04-29 11:48:48 -05:00
|
|
|
LifetimeRibKind::AnonymousReportError | LifetimeRibKind::Item => {
|
2022-05-11 15:49:39 -05:00
|
|
|
for id in node_ids {
|
2022-06-05 11:33:09 -05:00
|
|
|
self.record_lifetime_res(
|
|
|
|
id,
|
|
|
|
LifetimeRes::Error,
|
|
|
|
LifetimeElisionCandidate::Ignore,
|
|
|
|
);
|
2022-05-21 07:06:26 -05:00
|
|
|
}
|
2022-06-05 11:33:09 -05:00
|
|
|
self.report_missing_lifetime_specifiers(vec![missing_lifetime], None);
|
2022-04-29 11:48:48 -05:00
|
|
|
break;
|
|
|
|
}
|
2022-06-05 14:55:30 -05:00
|
|
|
LifetimeRibKind::Generics { .. }
|
|
|
|
| LifetimeRibKind::ConstGeneric
|
|
|
|
| LifetimeRibKind::AnonConst => {}
|
2022-03-10 16:12:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-21 07:06:26 -05:00
|
|
|
if should_lint {
|
2022-03-10 16:12:35 -06:00
|
|
|
self.r.lint_buffer.buffer_lint_with_diagnostic(
|
|
|
|
lint::builtin::ELIDED_LIFETIMES_IN_PATHS,
|
|
|
|
segment_id,
|
|
|
|
elided_lifetime_span,
|
|
|
|
"hidden lifetime parameters in types are deprecated",
|
|
|
|
lint::BuiltinLintDiagnostics::ElidedLifetimesInPaths(
|
|
|
|
expected_lifetimes,
|
|
|
|
path_span,
|
|
|
|
!segment.has_generic_args,
|
|
|
|
elided_lifetime_span,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 13:54:13 -05:00
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
2022-06-05 11:33:09 -05:00
|
|
|
fn record_lifetime_res(
|
|
|
|
&mut self,
|
|
|
|
id: NodeId,
|
|
|
|
res: LifetimeRes,
|
|
|
|
candidate: LifetimeElisionCandidate,
|
|
|
|
) {
|
2022-04-07 13:54:13 -05:00
|
|
|
if let Some(prev_res) = self.r.lifetimes_res_map.insert(id, res) {
|
|
|
|
panic!(
|
|
|
|
"lifetime {:?} resolved multiple times ({:?} before, {:?} now)",
|
|
|
|
id, prev_res, res
|
|
|
|
)
|
|
|
|
}
|
2022-06-05 11:33:09 -05:00
|
|
|
match res {
|
2022-06-13 01:22:06 -05:00
|
|
|
LifetimeRes::Param { .. } | LifetimeRes::Fresh { .. } | LifetimeRes::Static => {
|
2022-06-05 11:33:09 -05:00
|
|
|
if let Some(ref mut candidates) = self.lifetime_elision_candidates {
|
|
|
|
candidates.insert(res, candidate);
|
|
|
|
}
|
|
|
|
}
|
2022-06-13 01:22:06 -05:00
|
|
|
LifetimeRes::Infer | LifetimeRes::Error | LifetimeRes::ElidedAnchor { .. } => {}
|
2022-06-05 11:33:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
|
|
|
fn record_lifetime_param(&mut self, id: NodeId, res: LifetimeRes) {
|
|
|
|
if let Some(prev_res) = self.r.lifetimes_res_map.insert(id, res) {
|
|
|
|
panic!(
|
|
|
|
"lifetime parameter {:?} resolved multiple times ({:?} before, {:?} now)",
|
|
|
|
id, prev_res, res
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Perform resolution of a function signature, accounting for lifetime elision.
|
|
|
|
#[tracing::instrument(level = "debug", skip(self, inputs))]
|
|
|
|
fn resolve_fn_signature(
|
|
|
|
&mut self,
|
|
|
|
fn_id: NodeId,
|
|
|
|
has_self: bool,
|
|
|
|
inputs: impl Iterator<Item = (Option<&'ast Pat>, &'ast Ty)> + Clone,
|
|
|
|
output_ty: &'ast FnRetTy,
|
|
|
|
) {
|
|
|
|
// Add each argument to the rib.
|
2022-05-22 16:31:39 -05:00
|
|
|
let elision_lifetime = self.resolve_fn_params(has_self, inputs);
|
2022-06-05 11:33:09 -05:00
|
|
|
debug!(?elision_lifetime);
|
|
|
|
|
|
|
|
let outer_failures = take(&mut self.diagnostic_metadata.current_elision_failures);
|
|
|
|
let output_rib = if let Ok(res) = elision_lifetime.as_ref() {
|
|
|
|
LifetimeRibKind::Elided(*res)
|
|
|
|
} else {
|
|
|
|
LifetimeRibKind::ElisionFailure
|
|
|
|
};
|
|
|
|
self.with_lifetime_rib(output_rib, |this| visit::walk_fn_ret_ty(this, &output_ty));
|
|
|
|
let elision_failures =
|
|
|
|
replace(&mut self.diagnostic_metadata.current_elision_failures, outer_failures);
|
|
|
|
if !elision_failures.is_empty() {
|
|
|
|
let Err(failure_info) = elision_lifetime else { bug!() };
|
|
|
|
self.report_missing_lifetime_specifiers(elision_failures, Some(failure_info));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Resolve inside function parameters and parameter types.
|
|
|
|
/// Returns the lifetime for elision in fn return type,
|
|
|
|
/// or diagnostic information in case of elision failure.
|
|
|
|
fn resolve_fn_params(
|
|
|
|
&mut self,
|
|
|
|
has_self: bool,
|
|
|
|
inputs: impl Iterator<Item = (Option<&'ast Pat>, &'ast Ty)>,
|
|
|
|
) -> Result<LifetimeRes, (Vec<MissingLifetime>, Vec<ElisionFnParameter>)> {
|
|
|
|
let outer_candidates =
|
|
|
|
replace(&mut self.lifetime_elision_candidates, Some(Default::default()));
|
|
|
|
|
|
|
|
let mut elision_lifetime = None;
|
|
|
|
let mut lifetime_count = 0;
|
|
|
|
let mut parameter_info = Vec::new();
|
|
|
|
|
|
|
|
let mut bindings = smallvec![(PatBoundCtx::Product, Default::default())];
|
|
|
|
for (index, (pat, ty)) in inputs.enumerate() {
|
|
|
|
debug!(?pat, ?ty);
|
|
|
|
if let Some(pat) = pat {
|
|
|
|
self.resolve_pattern(pat, PatternSource::FnParam, &mut bindings);
|
|
|
|
}
|
|
|
|
self.visit_ty(ty);
|
|
|
|
|
|
|
|
if let Some(ref candidates) = self.lifetime_elision_candidates {
|
|
|
|
let new_count = candidates.len();
|
|
|
|
let local_count = new_count - lifetime_count;
|
|
|
|
if local_count != 0 {
|
|
|
|
parameter_info.push(ElisionFnParameter {
|
|
|
|
index,
|
|
|
|
ident: if let Some(pat) = pat && let PatKind::Ident(_, ident, _) = pat.kind {
|
|
|
|
Some(ident)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
lifetime_count: local_count,
|
|
|
|
span: ty.span,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
lifetime_count = new_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle `self` specially.
|
|
|
|
if index == 0 && has_self {
|
|
|
|
let self_lifetime = self.find_lifetime_for_self(ty);
|
|
|
|
if let Set1::One(lifetime) = self_lifetime {
|
|
|
|
elision_lifetime = Some(lifetime);
|
|
|
|
self.lifetime_elision_candidates = None;
|
|
|
|
} else {
|
|
|
|
self.lifetime_elision_candidates = Some(Default::default());
|
|
|
|
lifetime_count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug!("(resolving function / closure) recorded parameter");
|
|
|
|
}
|
|
|
|
|
|
|
|
let all_candidates = replace(&mut self.lifetime_elision_candidates, outer_candidates);
|
|
|
|
debug!(?all_candidates);
|
|
|
|
|
|
|
|
if let Some(res) = elision_lifetime {
|
|
|
|
return Ok(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We do not have a `self` candidate, look at the full list.
|
|
|
|
let all_candidates = all_candidates.unwrap();
|
|
|
|
if all_candidates.len() == 1 {
|
|
|
|
Ok(*all_candidates.first().unwrap().0)
|
|
|
|
} else {
|
|
|
|
let all_candidates = all_candidates
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|(_, candidate)| match candidate {
|
|
|
|
LifetimeElisionCandidate::Ignore | LifetimeElisionCandidate::Named => None,
|
|
|
|
LifetimeElisionCandidate::Missing(missing) => Some(missing),
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
Err((all_candidates, parameter_info))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// List all the lifetimes that appear in the provided type.
|
|
|
|
fn find_lifetime_for_self(&self, ty: &'ast Ty) -> Set1<LifetimeRes> {
|
|
|
|
struct SelfVisitor<'r, 'a> {
|
|
|
|
r: &'r Resolver<'a>,
|
|
|
|
impl_self: Option<Res>,
|
|
|
|
lifetime: Set1<LifetimeRes>,
|
|
|
|
}
|
|
|
|
|
|
|
|
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, ty: &Ty) -> bool {
|
|
|
|
match ty.kind {
|
|
|
|
TyKind::ImplicitSelf => true,
|
|
|
|
TyKind::Path(None, _) => {
|
|
|
|
let path_res = self.r.partial_res_map[&ty.id].base_res();
|
|
|
|
if let Res::SelfTy { .. } = path_res {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Some(path_res) == self.impl_self
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Visitor<'a> for SelfVisitor<'_, '_> {
|
|
|
|
fn visit_ty(&mut self, ty: &'a Ty) {
|
|
|
|
trace!("SelfVisitor considering ty={:?}", ty);
|
|
|
|
if let TyKind::Rptr(lt, ref mt) = ty.kind && self.is_self_ty(&mt.ty) {
|
|
|
|
let lt_id = if let Some(lt) = lt {
|
|
|
|
lt.id
|
|
|
|
} else {
|
|
|
|
let res = self.r.lifetimes_res_map[&ty.id];
|
|
|
|
let LifetimeRes::ElidedAnchor { start, .. } = res else { bug!() };
|
|
|
|
start
|
|
|
|
};
|
|
|
|
let lt_res = self.r.lifetimes_res_map[<_id];
|
|
|
|
trace!("SelfVisitor inserting res={:?}", lt_res);
|
|
|
|
self.lifetime.insert(lt_res);
|
|
|
|
}
|
|
|
|
visit::walk_ty(self, ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let impl_self = self
|
|
|
|
.diagnostic_metadata
|
|
|
|
.current_self_type
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|ty| {
|
|
|
|
if let TyKind::Path(None, _) = ty.kind {
|
|
|
|
self.r.partial_res_map.get(&ty.id)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map(|res| res.base_res())
|
|
|
|
.filter(|res| {
|
|
|
|
// Permit the types that unambiguously always
|
|
|
|
// result in the same type constructor being used
|
|
|
|
// (it can't differ between `Self` and `self`).
|
|
|
|
matches!(
|
|
|
|
res,
|
|
|
|
Res::Def(DefKind::Struct | DefKind::Union | DefKind::Enum, _,) | Res::PrimTy(_)
|
|
|
|
)
|
|
|
|
});
|
|
|
|
let mut visitor = SelfVisitor { r: self.r, impl_self, lifetime: Set1::Empty };
|
|
|
|
visitor.visit_ty(ty);
|
|
|
|
trace!("SelfVisitor found={:?}", visitor.lifetime);
|
|
|
|
visitor.lifetime
|
2022-04-07 13:54:13 -05:00
|
|
|
}
|
|
|
|
|
2020-06-25 09:16:38 -05:00
|
|
|
/// Searches the current set of local scopes for labels. Returns the `NodeId` of the resolved
|
|
|
|
/// label and reports an error if the label is not found or is unreachable.
|
2022-06-02 13:35:14 -05:00
|
|
|
fn resolve_label(&mut self, mut label: Ident) -> Result<(NodeId, Span), ResolutionError<'a>> {
|
2020-06-25 09:16:38 -05:00
|
|
|
let mut suggestion = None;
|
|
|
|
|
|
|
|
for i in (0..self.label_ribs.len()).rev() {
|
|
|
|
let rib = &self.label_ribs[i];
|
|
|
|
|
|
|
|
if let MacroDefinition(def) = rib.kind {
|
2019-08-07 18:39:02 -05:00
|
|
|
// If an invocation of this macro created `ident`, give up on `ident`
|
|
|
|
// and switch to `ident`'s source from the macro definition.
|
2020-06-25 09:16:38 -05:00
|
|
|
if def == self.r.macro_def(label.span.ctxt()) {
|
|
|
|
label.span.remove_mark();
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
}
|
2020-06-25 09:16:38 -05:00
|
|
|
|
|
|
|
let ident = label.normalize_to_macro_rules();
|
|
|
|
if let Some((ident, id)) = rib.bindings.get_key_value(&ident) {
|
2021-11-17 13:37:46 -06:00
|
|
|
let definition_span = ident.span;
|
2020-06-25 09:16:38 -05:00
|
|
|
return if self.is_label_valid_from_rib(i) {
|
2022-06-02 13:35:14 -05:00
|
|
|
Ok((*id, definition_span))
|
2020-06-25 09:16:38 -05:00
|
|
|
} else {
|
2022-06-02 13:35:14 -05:00
|
|
|
Err(ResolutionError::UnreachableLabel {
|
|
|
|
name: label.name,
|
|
|
|
definition_span,
|
|
|
|
suggestion,
|
|
|
|
})
|
2020-06-25 09:16:38 -05:00
|
|
|
};
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
2020-06-25 09:16:38 -05:00
|
|
|
|
|
|
|
// Diagnostics: Check if this rib contains a label with a similar name, keep track of
|
|
|
|
// the first such label that is encountered.
|
|
|
|
suggestion = suggestion.or_else(|| self.suggestion_for_label_in_rib(i, label));
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
2020-06-25 09:16:38 -05:00
|
|
|
|
2022-06-02 13:35:14 -05:00
|
|
|
Err(ResolutionError::UndeclaredLabel { name: label.name, suggestion })
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
2020-06-25 09:16:38 -05:00
|
|
|
/// Determine whether or not a label from the `rib_index`th label rib is reachable.
|
|
|
|
fn is_label_valid_from_rib(&self, rib_index: usize) -> bool {
|
|
|
|
let ribs = &self.label_ribs[rib_index + 1..];
|
|
|
|
|
|
|
|
for rib in ribs {
|
2021-12-04 14:20:58 -06:00
|
|
|
if rib.kind.is_label_barrier() {
|
|
|
|
return false;
|
2020-06-25 09:16:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2020-01-22 01:01:21 -06:00
|
|
|
fn resolve_adt(&mut self, item: &'ast Item, generics: &'ast Generics) {
|
2019-08-07 18:39:02 -05:00
|
|
|
debug!("resolve_adt");
|
|
|
|
self.with_current_self_item(item, |this| {
|
2022-03-06 05:02:13 -06:00
|
|
|
this.with_generic_param_rib(
|
|
|
|
&generics.params,
|
|
|
|
ItemRibKind(HasGenericParams::Yes),
|
2022-04-07 13:54:13 -05:00
|
|
|
LifetimeRibKind::Generics {
|
2022-05-10 14:15:30 -05:00
|
|
|
binder: item.id,
|
2022-04-07 13:54:13 -05:00
|
|
|
kind: LifetimeBinderKind::Item,
|
|
|
|
span: generics.span,
|
|
|
|
},
|
2022-03-06 05:02:13 -06:00
|
|
|
|this| {
|
|
|
|
let item_def_id = this.r.local_def_id(item.id).to_def_id();
|
|
|
|
this.with_self_rib(
|
|
|
|
Res::SelfTy { trait_: None, alias_to: Some((item_def_id, false)) },
|
|
|
|
|this| {
|
|
|
|
visit::walk_item(this, item);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2019-08-07 18:39:02 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn future_proof_import(&mut self, use_tree: &UseTree) {
|
|
|
|
let segments = &use_tree.prefix.segments;
|
|
|
|
if !segments.is_empty() {
|
|
|
|
let ident = segments[0].ident;
|
|
|
|
if ident.is_path_segment_keyword() || ident.span.rust_2015() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let nss = match use_tree.kind {
|
|
|
|
UseTreeKind::Simple(..) if segments.len() == 1 => &[TypeNS, ValueNS][..],
|
|
|
|
_ => &[TypeNS],
|
|
|
|
};
|
|
|
|
let report_error = |this: &Self, ns| {
|
|
|
|
let what = if ns == TypeNS { "type parameters" } else { "local variables" };
|
2021-10-21 08:09:24 -05:00
|
|
|
if this.should_report_errs() {
|
|
|
|
this.r
|
|
|
|
.session
|
|
|
|
.span_err(ident.span, &format!("imports cannot refer to {}", what));
|
|
|
|
}
|
2019-08-07 18:39:02 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
for &ns in nss {
|
2022-04-08 15:50:56 -05:00
|
|
|
match self.maybe_resolve_ident_in_lexical_scope(ident, ns) {
|
2021-10-21 08:09:24 -05:00
|
|
|
Some(LexicalScopeBinding::Res(..)) => {
|
2019-08-07 18:39:02 -05:00
|
|
|
report_error(self, ns);
|
2021-10-21 08:09:24 -05:00
|
|
|
}
|
|
|
|
Some(LexicalScopeBinding::Item(binding)) => {
|
2022-04-30 08:26:36 -05:00
|
|
|
if let Some(LexicalScopeBinding::Res(..)) =
|
|
|
|
self.resolve_ident_in_lexical_scope(ident, ns, None, Some(binding))
|
2021-10-21 08:09:24 -05:00
|
|
|
{
|
|
|
|
report_error(self, ns);
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
}
|
2021-10-21 08:09:24 -05:00
|
|
|
None => {}
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if let UseTreeKind::Nested(use_trees) = &use_tree.kind {
|
|
|
|
for (use_tree, _) in use_trees {
|
|
|
|
self.future_proof_import(use_tree);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 01:01:21 -06:00
|
|
|
fn resolve_item(&mut self, item: &'ast Item) {
|
2019-08-07 18:39:02 -05:00
|
|
|
let name = item.ident.name;
|
2019-09-26 11:51:36 -05:00
|
|
|
debug!("(resolving item) resolving {} ({:?})", name, item.kind);
|
2019-08-07 18:39:02 -05:00
|
|
|
|
2019-09-26 11:51:36 -05:00
|
|
|
match item.kind {
|
2022-03-06 05:02:13 -06:00
|
|
|
ItemKind::TyAlias(box TyAlias { ref generics, .. }) => {
|
|
|
|
self.with_generic_param_rib(
|
|
|
|
&generics.params,
|
|
|
|
ItemRibKind(HasGenericParams::Yes),
|
|
|
|
LifetimeRibKind::Generics {
|
2022-05-10 14:15:30 -05:00
|
|
|
binder: item.id,
|
2022-03-06 05:02:13 -06:00
|
|
|
kind: LifetimeBinderKind::Item,
|
|
|
|
span: generics.span,
|
|
|
|
},
|
|
|
|
|this| visit::walk_item(this, item),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemKind::Fn(box Fn { ref generics, .. }) => {
|
|
|
|
self.with_generic_param_rib(
|
|
|
|
&generics.params,
|
|
|
|
ItemRibKind(HasGenericParams::Yes),
|
|
|
|
LifetimeRibKind::Generics {
|
2022-05-10 14:15:30 -05:00
|
|
|
binder: item.id,
|
2022-03-06 05:02:13 -06:00
|
|
|
kind: LifetimeBinderKind::Function,
|
|
|
|
span: generics.span,
|
|
|
|
},
|
|
|
|
|this| visit::walk_item(this, item),
|
|
|
|
);
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ItemKind::Enum(_, ref generics)
|
|
|
|
| ItemKind::Struct(_, ref generics)
|
|
|
|
| ItemKind::Union(_, ref generics) => {
|
|
|
|
self.resolve_adt(item, generics);
|
|
|
|
}
|
|
|
|
|
2021-11-07 02:43:49 -06:00
|
|
|
ItemKind::Impl(box Impl {
|
2020-01-13 22:30:20 -06:00
|
|
|
ref generics,
|
|
|
|
ref of_trait,
|
|
|
|
ref self_ty,
|
|
|
|
items: ref impl_items,
|
|
|
|
..
|
2021-01-29 01:31:08 -06:00
|
|
|
}) => {
|
2022-04-28 02:54:28 -05:00
|
|
|
self.diagnostic_metadata.current_impl_items = Some(impl_items);
|
2020-01-13 22:30:20 -06:00
|
|
|
self.resolve_implementation(generics, of_trait, &self_ty, item.id, impl_items);
|
2022-04-28 02:54:28 -05:00
|
|
|
self.diagnostic_metadata.current_impl_items = None;
|
2019-12-22 16:42:04 -06:00
|
|
|
}
|
2019-08-07 18:39:02 -05:00
|
|
|
|
2021-11-07 02:43:49 -06:00
|
|
|
ItemKind::Trait(box Trait { ref generics, ref bounds, ref items, .. }) => {
|
2019-08-07 18:39:02 -05:00
|
|
|
// Create a new rib for the trait-wide type parameters.
|
2022-03-06 05:02:13 -06:00
|
|
|
self.with_generic_param_rib(
|
|
|
|
&generics.params,
|
|
|
|
ItemRibKind(HasGenericParams::Yes),
|
|
|
|
LifetimeRibKind::Generics {
|
2022-05-10 14:15:30 -05:00
|
|
|
binder: item.id,
|
2022-03-06 05:02:13 -06:00
|
|
|
kind: LifetimeBinderKind::Item,
|
|
|
|
span: generics.span,
|
|
|
|
},
|
|
|
|
|this| {
|
|
|
|
let local_def_id = this.r.local_def_id(item.id).to_def_id();
|
|
|
|
this.with_self_rib(
|
|
|
|
Res::SelfTy { trait_: Some(local_def_id), alias_to: None },
|
|
|
|
|this| {
|
|
|
|
this.visit_generics(generics);
|
2022-04-20 21:09:32 -05:00
|
|
|
walk_list!(this, visit_param_bound, bounds, BoundKind::SuperTraits);
|
2022-04-01 09:05:42 -05:00
|
|
|
this.resolve_trait_items(items);
|
2022-03-06 05:02:13 -06:00
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ItemKind::TraitAlias(ref generics, ref bounds) => {
|
|
|
|
// Create a new rib for the trait-wide type parameters.
|
2022-03-06 05:02:13 -06:00
|
|
|
self.with_generic_param_rib(
|
|
|
|
&generics.params,
|
|
|
|
ItemRibKind(HasGenericParams::Yes),
|
|
|
|
LifetimeRibKind::Generics {
|
2022-05-10 14:15:30 -05:00
|
|
|
binder: item.id,
|
2022-03-06 05:02:13 -06:00
|
|
|
kind: LifetimeBinderKind::Item,
|
|
|
|
span: generics.span,
|
|
|
|
},
|
|
|
|
|this| {
|
|
|
|
let local_def_id = this.r.local_def_id(item.id).to_def_id();
|
|
|
|
this.with_self_rib(
|
|
|
|
Res::SelfTy { trait_: Some(local_def_id), alias_to: None },
|
|
|
|
|this| {
|
|
|
|
this.visit_generics(generics);
|
2022-04-20 21:09:32 -05:00
|
|
|
walk_list!(this, visit_param_bound, bounds, BoundKind::Bound);
|
2022-03-06 05:02:13 -06:00
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
2021-02-16 15:56:07 -06:00
|
|
|
ItemKind::Mod(..) | ItemKind::ForeignMod(_) => {
|
2019-08-07 18:39:02 -05:00
|
|
|
self.with_scope(item.id, |this| {
|
|
|
|
visit::walk_item(this, item);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-23 03:24:30 -06:00
|
|
|
ItemKind::Static(ref ty, _, ref expr) | ItemKind::Const(_, ref ty, ref expr) => {
|
2022-03-06 05:02:13 -06:00
|
|
|
self.with_item_rib(|this| {
|
2022-06-05 11:33:09 -05:00
|
|
|
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| {
|
|
|
|
this.visit_ty(ty);
|
|
|
|
});
|
2022-06-13 01:22:06 -05:00
|
|
|
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Infer), |this| {
|
|
|
|
if let Some(expr) = expr {
|
|
|
|
let constant_item_kind = match item.kind {
|
|
|
|
ItemKind::Const(..) => ConstantItemKind::Const,
|
|
|
|
ItemKind::Static(..) => ConstantItemKind::Static,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
// We already forbid generic params because of the above item rib,
|
|
|
|
// so it doesn't matter whether this is a trivial constant.
|
|
|
|
this.with_constant_rib(
|
|
|
|
IsRepeatExpr::No,
|
|
|
|
HasGenericParams::Yes,
|
|
|
|
Some((item.ident, constant_item_kind)),
|
|
|
|
|this| this.visit_expr(expr),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2019-08-07 18:39:02 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemKind::Use(ref use_tree) => {
|
|
|
|
self.future_proof_import(use_tree);
|
|
|
|
}
|
|
|
|
|
2021-04-11 14:51:28 -05:00
|
|
|
ItemKind::ExternCrate(..) | ItemKind::MacroDef(..) => {
|
2019-08-07 18:39:02 -05:00
|
|
|
// do nothing, these are just around to be encoded
|
|
|
|
}
|
|
|
|
|
2021-04-11 14:51:28 -05:00
|
|
|
ItemKind::GlobalAsm(_) => {
|
|
|
|
visit::walk_item(self, item);
|
|
|
|
}
|
|
|
|
|
2020-02-29 10:32:20 -06:00
|
|
|
ItemKind::MacCall(_) => panic!("unexpanded macro in resolve!"),
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-06 05:02:13 -06:00
|
|
|
fn with_generic_param_rib<'c, F>(
|
|
|
|
&'c mut self,
|
2022-06-03 11:42:42 -05:00
|
|
|
params: &'c [GenericParam],
|
2022-03-06 05:02:13 -06:00
|
|
|
kind: RibKind<'a>,
|
|
|
|
lifetime_kind: LifetimeRibKind,
|
|
|
|
f: F,
|
|
|
|
) where
|
2019-08-28 01:48:10 -05:00
|
|
|
F: FnOnce(&mut Self),
|
2019-08-07 18:39:02 -05:00
|
|
|
{
|
|
|
|
debug!("with_generic_param_rib");
|
2022-05-10 14:15:30 -05:00
|
|
|
let LifetimeRibKind::Generics { binder, span: generics_span, kind: generics_kind, .. }
|
|
|
|
= lifetime_kind else { panic!() };
|
|
|
|
|
2019-10-05 10:55:58 -05:00
|
|
|
let mut function_type_rib = Rib::new(kind);
|
|
|
|
let mut function_value_rib = Rib::new(kind);
|
2022-03-06 05:02:13 -06:00
|
|
|
let mut function_lifetime_rib = LifetimeRib::new(lifetime_kind);
|
2019-10-05 10:55:58 -05:00
|
|
|
let mut seen_bindings = FxHashMap::default();
|
2022-06-02 13:46:40 -05:00
|
|
|
// Store all seen lifetimes names from outer scopes.
|
|
|
|
let mut seen_lifetimes = FxHashSet::default();
|
2019-10-05 10:55:58 -05:00
|
|
|
|
|
|
|
// We also can't shadow bindings from the parent item
|
|
|
|
if let AssocItemRibKind = kind {
|
|
|
|
let mut add_bindings_for_ns = |ns| {
|
|
|
|
let parent_rib = self.ribs[ns]
|
|
|
|
.iter()
|
2020-09-18 12:11:06 -05:00
|
|
|
.rfind(|r| matches!(r.kind, ItemRibKind(_)))
|
2019-10-05 10:55:58 -05:00
|
|
|
.expect("associated item outside of an item");
|
|
|
|
seen_bindings
|
|
|
|
.extend(parent_rib.bindings.iter().map(|(ident, _)| (*ident, ident.span)));
|
|
|
|
};
|
|
|
|
add_bindings_for_ns(ValueNS);
|
|
|
|
add_bindings_for_ns(TypeNS);
|
|
|
|
}
|
|
|
|
|
2021-12-04 14:20:58 -06:00
|
|
|
// Forbid shadowing lifetime bindings
|
|
|
|
for rib in self.lifetime_ribs.iter().rev() {
|
2022-06-02 13:46:40 -05:00
|
|
|
seen_lifetimes.extend(rib.bindings.iter().map(|(ident, _)| *ident));
|
2021-12-04 14:20:58 -06:00
|
|
|
if let LifetimeRibKind::Item = rib.kind {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-06 05:02:13 -06:00
|
|
|
for param in params {
|
2020-03-13 17:36:46 -05:00
|
|
|
let ident = param.ident.normalize_to_macros_2_0();
|
2019-10-05 10:55:58 -05:00
|
|
|
debug!("with_generic_param_rib: {}", param.id);
|
|
|
|
|
2022-06-02 13:46:40 -05:00
|
|
|
if let GenericParamKind::Lifetime = param.kind
|
|
|
|
&& let Some(&original) = seen_lifetimes.get(&ident)
|
|
|
|
{
|
|
|
|
diagnostics::signal_lifetime_shadowing(self.r.session, original, param.ident);
|
|
|
|
// Record lifetime res, so lowering knows there is something fishy.
|
2022-06-05 11:33:09 -05:00
|
|
|
self.record_lifetime_param(param.id, LifetimeRes::Error);
|
2022-06-02 13:46:40 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
match seen_bindings.entry(ident) {
|
|
|
|
Entry::Occupied(entry) => {
|
|
|
|
let span = *entry.get();
|
|
|
|
let err = ResolutionError::NameAlreadyUsedInParameterList(ident.name, span);
|
|
|
|
self.report_error(param.ident.span, err);
|
|
|
|
if let GenericParamKind::Lifetime = param.kind {
|
2022-05-27 04:29:18 -05:00
|
|
|
// Record lifetime res, so lowering knows there is something fishy.
|
2022-06-05 11:33:09 -05:00
|
|
|
self.record_lifetime_param(param.id, LifetimeRes::Error);
|
2022-05-27 04:29:18 -05:00
|
|
|
continue;
|
2021-12-04 14:20:58 -06:00
|
|
|
}
|
2020-12-20 14:08:55 -06:00
|
|
|
}
|
2022-06-02 13:46:40 -05:00
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
entry.insert(param.ident.span);
|
2020-12-20 14:08:55 -06:00
|
|
|
}
|
2019-10-05 10:55:58 -05:00
|
|
|
}
|
|
|
|
|
2022-03-06 05:02:13 -06:00
|
|
|
if param.ident.name == kw::UnderscoreLifetime {
|
|
|
|
rustc_errors::struct_span_err!(
|
|
|
|
self.r.session,
|
|
|
|
param.ident.span,
|
|
|
|
E0637,
|
|
|
|
"`'_` cannot be used here"
|
|
|
|
)
|
|
|
|
.span_label(param.ident.span, "`'_` is a reserved lifetime name")
|
|
|
|
.emit();
|
2022-05-27 04:29:18 -05:00
|
|
|
// Record lifetime res, so lowering knows there is something fishy.
|
2022-06-05 11:33:09 -05:00
|
|
|
self.record_lifetime_param(param.id, LifetimeRes::Error);
|
2022-03-06 05:02:13 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if param.ident.name == kw::StaticLifetime {
|
|
|
|
rustc_errors::struct_span_err!(
|
|
|
|
self.r.session,
|
|
|
|
param.ident.span,
|
|
|
|
E0262,
|
|
|
|
"invalid lifetime parameter name: `{}`",
|
|
|
|
param.ident,
|
|
|
|
)
|
2022-04-21 10:46:03 -05:00
|
|
|
.span_label(param.ident.span, "'static is a reserved lifetime name")
|
2022-03-06 05:02:13 -06:00
|
|
|
.emit();
|
2022-05-27 04:29:18 -05:00
|
|
|
// Record lifetime res, so lowering knows there is something fishy.
|
2022-06-05 11:33:09 -05:00
|
|
|
self.record_lifetime_param(param.id, LifetimeRes::Error);
|
2022-03-06 05:02:13 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let def_id = self.r.local_def_id(param.id);
|
|
|
|
|
2019-10-05 10:55:58 -05:00
|
|
|
// Plain insert (no renaming).
|
2020-12-20 14:08:55 -06:00
|
|
|
let (rib, def_kind) = match param.kind {
|
|
|
|
GenericParamKind::Type { .. } => (&mut function_type_rib, DefKind::TyParam),
|
|
|
|
GenericParamKind::Const { .. } => (&mut function_value_rib, DefKind::ConstParam),
|
2022-03-06 05:02:13 -06:00
|
|
|
GenericParamKind::Lifetime => {
|
2022-05-10 14:15:30 -05:00
|
|
|
let res = LifetimeRes::Param { param: def_id, binder };
|
2022-06-05 11:33:09 -05:00
|
|
|
self.record_lifetime_param(param.id, res);
|
2022-04-24 08:49:00 -05:00
|
|
|
function_lifetime_rib.bindings.insert(ident, (param.id, res));
|
2022-03-06 05:02:13 -06:00
|
|
|
continue;
|
|
|
|
}
|
2020-12-20 14:08:55 -06:00
|
|
|
};
|
2022-06-09 14:30:18 -05:00
|
|
|
|
|
|
|
let res = match kind {
|
|
|
|
ItemRibKind(..) | AssocItemRibKind => Res::Def(def_kind, def_id.to_def_id()),
|
|
|
|
NormalRibKind => Res::Err,
|
2022-07-22 23:26:45 -05:00
|
|
|
_ => span_bug!(param.ident.span, "Unexpected rib kind {:?}", kind),
|
2022-06-09 14:30:18 -05:00
|
|
|
};
|
2020-12-20 14:08:55 -06:00
|
|
|
self.r.record_partial_res(param.id, PartialRes::new(res));
|
2021-10-21 08:09:24 -05:00
|
|
|
rib.bindings.insert(ident, res);
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
2022-03-06 05:02:13 -06:00
|
|
|
self.lifetime_ribs.push(function_lifetime_rib);
|
2019-10-05 10:55:58 -05:00
|
|
|
self.ribs[ValueNS].push(function_value_rib);
|
|
|
|
self.ribs[TypeNS].push(function_type_rib);
|
|
|
|
|
2019-08-07 18:39:02 -05:00
|
|
|
f(self);
|
|
|
|
|
2019-10-05 10:55:58 -05:00
|
|
|
self.ribs[TypeNS].pop();
|
|
|
|
self.ribs[ValueNS].pop();
|
2022-06-05 11:33:09 -05:00
|
|
|
let function_lifetime_rib = self.lifetime_ribs.pop().unwrap();
|
|
|
|
|
|
|
|
// Do not account for the parameters we just bound for function lifetime elision.
|
|
|
|
if let Some(ref mut candidates) = self.lifetime_elision_candidates {
|
|
|
|
for (_, res) in function_lifetime_rib.bindings.values() {
|
|
|
|
candidates.remove(res);
|
|
|
|
}
|
|
|
|
}
|
2022-05-10 14:15:30 -05:00
|
|
|
|
|
|
|
if let LifetimeBinderKind::BareFnType
|
|
|
|
| LifetimeBinderKind::WhereBound
|
|
|
|
| LifetimeBinderKind::Function
|
|
|
|
| LifetimeBinderKind::ImplBlock = generics_kind
|
|
|
|
{
|
|
|
|
self.maybe_report_lifetime_uses(generics_span, params)
|
|
|
|
}
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
2019-08-28 01:48:10 -05:00
|
|
|
fn with_label_rib(&mut self, kind: RibKind<'a>, f: impl FnOnce(&mut Self)) {
|
|
|
|
self.label_ribs.push(Rib::new(kind));
|
2019-08-07 18:39:02 -05:00
|
|
|
f(self);
|
|
|
|
self.label_ribs.pop();
|
|
|
|
}
|
|
|
|
|
2022-03-06 05:02:13 -06:00
|
|
|
fn with_item_rib(&mut self, f: impl FnOnce(&mut Self)) {
|
|
|
|
let kind = ItemRibKind(HasGenericParams::No);
|
|
|
|
self.with_lifetime_rib(LifetimeRibKind::Item, |this| {
|
|
|
|
this.with_rib(ValueNS, kind, |this| this.with_rib(TypeNS, kind, f))
|
|
|
|
})
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
2020-10-22 03:32:41 -05:00
|
|
|
// HACK(min_const_generics,const_evaluatable_unchecked): We
|
|
|
|
// want to keep allowing `[0; std::mem::size_of::<*mut T>()]`
|
|
|
|
// with a future compat lint for now. We do this by adding an
|
|
|
|
// additional special case for repeat expressions.
|
|
|
|
//
|
|
|
|
// Note that we intentionally still forbid `[0; N + 1]` during
|
|
|
|
// name resolution so that we don't extend the future
|
|
|
|
// compat lint to new cases.
|
2022-05-02 10:36:48 -05:00
|
|
|
#[instrument(level = "debug", skip(self, f))]
|
2020-10-22 03:32:41 -05:00
|
|
|
fn with_constant_rib(
|
|
|
|
&mut self,
|
|
|
|
is_repeat: IsRepeatExpr,
|
2022-05-02 10:36:48 -05:00
|
|
|
may_use_generics: HasGenericParams,
|
2020-12-13 09:32:59 -06:00
|
|
|
item: Option<(Ident, ConstantItemKind)>,
|
2020-10-22 03:32:41 -05:00
|
|
|
f: impl FnOnce(&mut Self),
|
|
|
|
) {
|
2022-05-02 10:36:48 -05:00
|
|
|
self.with_rib(ValueNS, ConstantItemRibKind(may_use_generics, item), |this| {
|
2020-10-22 03:32:41 -05:00
|
|
|
this.with_rib(
|
|
|
|
TypeNS,
|
2022-05-02 10:36:48 -05:00
|
|
|
ConstantItemRibKind(
|
|
|
|
may_use_generics.force_yes_if(is_repeat == IsRepeatExpr::Yes),
|
|
|
|
item,
|
|
|
|
),
|
2020-10-22 03:32:41 -05:00
|
|
|
|this| {
|
2022-05-02 10:36:48 -05:00
|
|
|
this.with_label_rib(ConstantItemRibKind(may_use_generics, item), f);
|
2020-10-22 03:32:41 -05:00
|
|
|
},
|
|
|
|
)
|
2019-08-28 01:48:10 -05:00
|
|
|
});
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
2019-08-28 01:48:10 -05:00
|
|
|
fn with_current_self_type<T>(&mut self, self_type: &Ty, f: impl FnOnce(&mut Self) -> T) -> T {
|
2019-08-07 18:39:02 -05:00
|
|
|
// Handle nested impls (inside fn bodies)
|
2019-10-19 12:13:56 -05:00
|
|
|
let previous_value =
|
|
|
|
replace(&mut self.diagnostic_metadata.current_self_type, Some(self_type.clone()));
|
2019-08-07 18:39:02 -05:00
|
|
|
let result = f(self);
|
2019-10-19 12:13:56 -05:00
|
|
|
self.diagnostic_metadata.current_self_type = previous_value;
|
2019-08-07 18:39:02 -05:00
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2019-08-28 01:48:10 -05:00
|
|
|
fn with_current_self_item<T>(&mut self, self_item: &Item, f: impl FnOnce(&mut Self) -> T) -> T {
|
2019-10-19 12:13:56 -05:00
|
|
|
let previous_value =
|
|
|
|
replace(&mut self.diagnostic_metadata.current_self_item, Some(self_item.id));
|
2019-08-07 18:39:02 -05:00
|
|
|
let result = f(self);
|
2019-10-19 12:13:56 -05:00
|
|
|
self.diagnostic_metadata.current_self_item = previous_value;
|
2019-08-07 18:39:02 -05:00
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2020-10-26 18:28:56 -05:00
|
|
|
/// When evaluating a `trait` use its associated types' idents for suggestions in E0412.
|
2022-04-01 09:05:42 -05:00
|
|
|
fn resolve_trait_items(&mut self, trait_items: &'ast [P<AssocItem>]) {
|
2020-12-30 05:59:07 -06:00
|
|
|
let trait_assoc_items =
|
|
|
|
replace(&mut self.diagnostic_metadata.current_trait_assoc_items, Some(&trait_items));
|
2022-04-01 09:05:42 -05:00
|
|
|
|
|
|
|
let walk_assoc_item =
|
|
|
|
|this: &mut Self, generics: &Generics, kind, item: &'ast AssocItem| {
|
|
|
|
this.with_generic_param_rib(
|
|
|
|
&generics.params,
|
|
|
|
AssocItemRibKind,
|
|
|
|
LifetimeRibKind::Generics { binder: item.id, span: generics.span, kind },
|
|
|
|
|this| visit::walk_assoc_item(this, item, AssocCtxt::Trait),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
for item in trait_items {
|
|
|
|
match &item.kind {
|
|
|
|
AssocItemKind::Const(_, ty, default) => {
|
|
|
|
self.visit_ty(ty);
|
|
|
|
// Only impose the restrictions of `ConstRibKind` for an
|
|
|
|
// actual constant expression in a provided default.
|
|
|
|
if let Some(expr) = default {
|
|
|
|
// We allow arbitrary const expressions inside of associated consts,
|
|
|
|
// even if they are potentially not const evaluatable.
|
|
|
|
//
|
|
|
|
// Type parameters can already be used and as associated consts are
|
|
|
|
// not used as part of the type system, this is far less surprising.
|
2022-06-05 11:33:09 -05:00
|
|
|
self.with_lifetime_rib(
|
2022-06-13 01:22:06 -05:00
|
|
|
LifetimeRibKind::Elided(LifetimeRes::Infer),
|
2022-06-05 11:33:09 -05:00
|
|
|
|this| {
|
|
|
|
this.with_constant_rib(
|
|
|
|
IsRepeatExpr::No,
|
|
|
|
HasGenericParams::Yes,
|
|
|
|
None,
|
|
|
|
|this| this.visit_expr(expr),
|
|
|
|
)
|
|
|
|
},
|
2022-04-01 09:05:42 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AssocItemKind::Fn(box Fn { generics, .. }) => {
|
|
|
|
walk_assoc_item(self, generics, LifetimeBinderKind::Function, item);
|
|
|
|
}
|
2022-06-05 11:33:09 -05:00
|
|
|
AssocItemKind::TyAlias(box TyAlias { generics, .. }) => self
|
|
|
|
.with_lifetime_rib(LifetimeRibKind::AnonymousReportError, |this| {
|
|
|
|
walk_assoc_item(this, generics, LifetimeBinderKind::Item, item)
|
|
|
|
}),
|
2022-04-01 09:05:42 -05:00
|
|
|
AssocItemKind::MacCall(_) => {
|
|
|
|
panic!("unexpanded macro in resolve!")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-26 18:28:56 -05:00
|
|
|
self.diagnostic_metadata.current_trait_assoc_items = trait_assoc_items;
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This is called to resolve a trait reference from an `impl` (i.e., `impl Trait for Foo`).
|
2019-08-28 01:48:10 -05:00
|
|
|
fn with_optional_trait_ref<T>(
|
|
|
|
&mut self,
|
|
|
|
opt_trait_ref: Option<&TraitRef>,
|
2022-06-06 12:33:23 -05:00
|
|
|
self_type: &'ast Ty,
|
2019-08-28 01:48:10 -05:00
|
|
|
f: impl FnOnce(&mut Self, Option<DefId>) -> T,
|
|
|
|
) -> T {
|
2019-08-07 18:39:02 -05:00
|
|
|
let mut new_val = None;
|
|
|
|
let mut new_id = None;
|
|
|
|
if let Some(trait_ref) = opt_trait_ref {
|
|
|
|
let path: Vec<_> = Segment::from_path(&trait_ref.path);
|
2022-06-06 12:33:23 -05:00
|
|
|
self.diagnostic_metadata.currently_processing_impl_trait =
|
|
|
|
Some((trait_ref.clone(), self_type.clone()));
|
2020-01-22 01:01:21 -06:00
|
|
|
let res = self.smart_resolve_path_fragment(
|
|
|
|
None,
|
|
|
|
&path,
|
|
|
|
PathSource::Trait(AliasPossibility::No),
|
2022-04-30 08:26:36 -05:00
|
|
|
Finalize::new(trait_ref.ref_id, trait_ref.path.span),
|
2020-01-22 01:01:21 -06:00
|
|
|
);
|
2022-06-06 12:33:23 -05:00
|
|
|
self.diagnostic_metadata.currently_processing_impl_trait = None;
|
2022-03-23 14:47:19 -05:00
|
|
|
if let Some(def_id) = res.base_res().opt_def_id() {
|
|
|
|
new_id = Some(def_id);
|
|
|
|
new_val = Some((self.r.expect_module(def_id), trait_ref.clone()));
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let original_trait_ref = replace(&mut self.current_trait_ref, new_val);
|
|
|
|
let result = f(self, new_id);
|
|
|
|
self.current_trait_ref = original_trait_ref;
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2019-08-28 01:48:10 -05:00
|
|
|
fn with_self_rib_ns(&mut self, ns: Namespace, self_res: Res, f: impl FnOnce(&mut Self)) {
|
2019-08-07 18:39:02 -05:00
|
|
|
let mut self_type_rib = Rib::new(NormalRibKind);
|
|
|
|
|
|
|
|
// Plain insert (no renaming, since types are not currently hygienic)
|
2021-10-21 08:09:24 -05:00
|
|
|
self_type_rib.bindings.insert(Ident::with_dummy_span(kw::SelfUpper), self_res);
|
2019-08-28 01:48:10 -05:00
|
|
|
self.ribs[ns].push(self_type_rib);
|
2019-08-07 18:39:02 -05:00
|
|
|
f(self);
|
2019-08-28 01:48:10 -05:00
|
|
|
self.ribs[ns].pop();
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
2019-08-28 01:48:10 -05:00
|
|
|
fn with_self_rib(&mut self, self_res: Res, f: impl FnOnce(&mut Self)) {
|
|
|
|
self.with_self_rib_ns(TypeNS, self_res, f)
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_implementation(
|
|
|
|
&mut self,
|
2020-01-22 01:01:21 -06:00
|
|
|
generics: &'ast Generics,
|
|
|
|
opt_trait_reference: &'ast Option<TraitRef>,
|
|
|
|
self_type: &'ast Ty,
|
2019-08-07 18:39:02 -05:00
|
|
|
item_id: NodeId,
|
2019-12-11 23:41:18 -06:00
|
|
|
impl_items: &'ast [P<AssocItem>],
|
2019-12-07 17:08:09 -06:00
|
|
|
) {
|
2019-08-07 18:39:02 -05:00
|
|
|
debug!("resolve_implementation");
|
|
|
|
// If applicable, create a rib for the type parameters.
|
2022-06-05 10:31:49 -05:00
|
|
|
self.with_generic_param_rib(
|
|
|
|
&generics.params,
|
|
|
|
ItemRibKind(HasGenericParams::Yes),
|
|
|
|
LifetimeRibKind::Generics {
|
|
|
|
span: generics.span,
|
|
|
|
binder: item_id,
|
2022-06-05 11:33:09 -05:00
|
|
|
kind: LifetimeBinderKind::ImplBlock,
|
2022-06-05 10:31:49 -05:00
|
|
|
},
|
|
|
|
|this| {
|
|
|
|
// Dummy self type for better errors if `Self` is used in the trait path.
|
|
|
|
this.with_self_rib(Res::SelfTy { trait_: None, alias_to: None }, |this| {
|
|
|
|
this.with_lifetime_rib(
|
2022-05-21 07:06:26 -05:00
|
|
|
LifetimeRibKind::AnonymousCreateParameter {
|
|
|
|
binder: item_id,
|
|
|
|
report_in_path: true
|
|
|
|
},
|
2022-06-05 10:31:49 -05:00
|
|
|
|this| {
|
|
|
|
// Resolve the trait reference, if necessary.
|
|
|
|
this.with_optional_trait_ref(
|
|
|
|
opt_trait_reference.as_ref(),
|
|
|
|
self_type,
|
|
|
|
|this, trait_id| {
|
|
|
|
let item_def_id = this.r.local_def_id(item_id);
|
|
|
|
|
|
|
|
// Register the trait definitions from here.
|
|
|
|
if let Some(trait_id) = trait_id {
|
2022-06-05 11:33:09 -05:00
|
|
|
this.r
|
|
|
|
.trait_impls
|
|
|
|
.entry(trait_id)
|
|
|
|
.or_default()
|
|
|
|
.push(item_def_id);
|
2022-06-05 10:31:49 -05:00
|
|
|
}
|
2022-03-06 05:02:13 -06:00
|
|
|
|
2022-06-05 10:31:49 -05:00
|
|
|
let item_def_id = item_def_id.to_def_id();
|
|
|
|
let res = Res::SelfTy {
|
|
|
|
trait_: trait_id,
|
|
|
|
alias_to: Some((item_def_id, false)),
|
|
|
|
};
|
|
|
|
this.with_self_rib(res, |this| {
|
|
|
|
if let Some(trait_ref) = opt_trait_reference.as_ref() {
|
|
|
|
// Resolve type arguments in the trait path.
|
|
|
|
visit::walk_trait_ref(this, trait_ref);
|
|
|
|
}
|
|
|
|
// Resolve the self type.
|
|
|
|
this.visit_ty(self_type);
|
|
|
|
// Resolve the generic parameters.
|
|
|
|
this.visit_generics(generics);
|
|
|
|
|
|
|
|
// Resolve the items within the impl.
|
2022-06-05 11:33:09 -05:00
|
|
|
this.with_current_self_type(self_type, |this| {
|
|
|
|
this.with_self_rib_ns(ValueNS, Res::SelfCtor(item_def_id), |this| {
|
|
|
|
debug!("resolve_implementation with_self_rib_ns(ValueNS, ...)");
|
|
|
|
for item in impl_items {
|
|
|
|
this.resolve_impl_item(&**item);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2022-03-06 05:02:13 -06:00
|
|
|
});
|
|
|
|
},
|
2022-06-05 11:33:09 -05:00
|
|
|
)
|
2022-06-05 10:31:49 -05:00
|
|
|
},
|
|
|
|
);
|
2019-08-07 18:39:02 -05:00
|
|
|
});
|
2022-06-05 10:31:49 -05:00
|
|
|
},
|
|
|
|
);
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
2022-04-01 09:05:42 -05:00
|
|
|
fn resolve_impl_item(&mut self, item: &'ast AssocItem) {
|
|
|
|
use crate::ResolutionError::*;
|
|
|
|
match &item.kind {
|
2022-05-11 15:49:39 -05:00
|
|
|
AssocItemKind::Const(_, ty, default) => {
|
2022-04-01 09:05:42 -05:00
|
|
|
debug!("resolve_implementation AssocItemKind::Const");
|
|
|
|
// If this is a trait impl, ensure the const
|
|
|
|
// exists in trait
|
|
|
|
self.check_trait_item(
|
|
|
|
item.id,
|
|
|
|
item.ident,
|
|
|
|
&item.kind,
|
|
|
|
ValueNS,
|
|
|
|
item.span,
|
|
|
|
|i, s, c| ConstNotMemberOfTrait(i, s, c),
|
|
|
|
);
|
|
|
|
|
2022-05-11 15:49:39 -05:00
|
|
|
self.visit_ty(ty);
|
|
|
|
if let Some(expr) = default {
|
|
|
|
// We allow arbitrary const expressions inside of associated consts,
|
|
|
|
// even if they are potentially not const evaluatable.
|
|
|
|
//
|
|
|
|
// Type parameters can already be used and as associated consts are
|
|
|
|
// not used as part of the type system, this is far less surprising.
|
2022-06-13 01:22:06 -05:00
|
|
|
self.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Infer), |this| {
|
|
|
|
this.with_constant_rib(
|
|
|
|
IsRepeatExpr::No,
|
|
|
|
HasGenericParams::Yes,
|
|
|
|
None,
|
|
|
|
|this| this.visit_expr(expr),
|
|
|
|
)
|
|
|
|
});
|
2022-05-11 15:49:39 -05:00
|
|
|
}
|
2022-04-01 09:05:42 -05:00
|
|
|
}
|
|
|
|
AssocItemKind::Fn(box Fn { generics, .. }) => {
|
|
|
|
debug!("resolve_implementation AssocItemKind::Fn");
|
|
|
|
// We also need a new scope for the impl item type parameters.
|
|
|
|
self.with_generic_param_rib(
|
|
|
|
&generics.params,
|
|
|
|
AssocItemRibKind,
|
|
|
|
LifetimeRibKind::Generics {
|
|
|
|
binder: item.id,
|
|
|
|
span: generics.span,
|
|
|
|
kind: LifetimeBinderKind::Function,
|
|
|
|
},
|
|
|
|
|this| {
|
|
|
|
// If this is a trait impl, ensure the method
|
|
|
|
// exists in trait
|
|
|
|
this.check_trait_item(
|
|
|
|
item.id,
|
|
|
|
item.ident,
|
|
|
|
&item.kind,
|
|
|
|
ValueNS,
|
|
|
|
item.span,
|
|
|
|
|i, s, c| MethodNotMemberOfTrait(i, s, c),
|
|
|
|
);
|
|
|
|
|
|
|
|
visit::walk_assoc_item(this, item, AssocCtxt::Impl)
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
AssocItemKind::TyAlias(box TyAlias { generics, .. }) => {
|
|
|
|
debug!("resolve_implementation AssocItemKind::TyAlias");
|
|
|
|
// We also need a new scope for the impl item type parameters.
|
|
|
|
self.with_generic_param_rib(
|
|
|
|
&generics.params,
|
|
|
|
AssocItemRibKind,
|
|
|
|
LifetimeRibKind::Generics {
|
|
|
|
binder: item.id,
|
|
|
|
span: generics.span,
|
|
|
|
kind: LifetimeBinderKind::Item,
|
|
|
|
},
|
|
|
|
|this| {
|
2022-06-05 11:33:09 -05:00
|
|
|
this.with_lifetime_rib(LifetimeRibKind::AnonymousReportError, |this| {
|
|
|
|
// If this is a trait impl, ensure the type
|
|
|
|
// exists in trait
|
|
|
|
this.check_trait_item(
|
|
|
|
item.id,
|
|
|
|
item.ident,
|
|
|
|
&item.kind,
|
|
|
|
TypeNS,
|
|
|
|
item.span,
|
|
|
|
|i, s, c| TypeNotMemberOfTrait(i, s, c),
|
|
|
|
);
|
2022-04-01 09:05:42 -05:00
|
|
|
|
2022-06-05 11:33:09 -05:00
|
|
|
visit::walk_assoc_item(this, item, AssocCtxt::Impl)
|
|
|
|
});
|
2022-04-01 09:05:42 -05:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
AssocItemKind::MacCall(_) => {
|
|
|
|
panic!("unexpanded macro in resolve!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-25 10:53:37 -05:00
|
|
|
fn check_trait_item<F>(
|
|
|
|
&mut self,
|
2022-01-08 05:22:06 -06:00
|
|
|
id: NodeId,
|
2022-01-12 13:04:04 -06:00
|
|
|
mut ident: Ident,
|
2021-09-25 10:53:37 -05:00
|
|
|
kind: &AssocItemKind,
|
|
|
|
ns: Namespace,
|
|
|
|
span: Span,
|
|
|
|
err: F,
|
|
|
|
) where
|
2021-11-17 13:37:46 -06:00
|
|
|
F: FnOnce(Ident, String, Option<Symbol>) -> ResolutionError<'a>,
|
2019-08-07 18:39:02 -05:00
|
|
|
{
|
2022-01-08 05:22:06 -06:00
|
|
|
// If there is a TraitRef in scope for an impl, then the method must be in the trait.
|
|
|
|
let Some((module, _)) = &self.current_trait_ref else { return; };
|
2022-01-12 13:04:04 -06:00
|
|
|
ident.span.normalize_to_macros_2_0_and_adjust(module.expansion);
|
|
|
|
let key = self.r.new_key(ident, ns);
|
|
|
|
let mut binding = self.r.resolution(module, key).try_borrow().ok().and_then(|r| r.binding);
|
|
|
|
debug!(?binding);
|
|
|
|
if binding.is_none() {
|
2022-01-08 05:22:06 -06:00
|
|
|
// We could not find the trait item in the correct namespace.
|
|
|
|
// Check the other namespace to report an error.
|
|
|
|
let ns = match ns {
|
|
|
|
ValueNS => TypeNS,
|
|
|
|
TypeNS => ValueNS,
|
|
|
|
_ => ns,
|
|
|
|
};
|
2022-01-12 13:04:04 -06:00
|
|
|
let key = self.r.new_key(ident, ns);
|
|
|
|
binding = self.r.resolution(module, key).try_borrow().ok().and_then(|r| r.binding);
|
|
|
|
debug!(?binding);
|
2022-01-08 05:22:06 -06:00
|
|
|
}
|
2022-01-12 13:04:04 -06:00
|
|
|
let Some(binding) = binding else {
|
2022-01-08 05:22:06 -06:00
|
|
|
// We could not find the method: report an error.
|
|
|
|
let candidate = self.find_similarly_named_assoc_item(ident.name, kind);
|
|
|
|
let path = &self.current_trait_ref.as_ref().unwrap().1.path;
|
2021-11-17 13:37:46 -06:00
|
|
|
let path_names = path_names_to_string(path);
|
|
|
|
self.report_error(span, err(ident, path_names, candidate));
|
2022-01-08 05:22:06 -06:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = binding.res();
|
|
|
|
let Res::Def(def_kind, _) = res else { bug!() };
|
|
|
|
match (def_kind, kind) {
|
|
|
|
(DefKind::AssocTy, AssocItemKind::TyAlias(..))
|
|
|
|
| (DefKind::AssocFn, AssocItemKind::Fn(..))
|
|
|
|
| (DefKind::AssocConst, AssocItemKind::Const(..)) => {
|
|
|
|
self.r.record_partial_res(id, PartialRes::new(res));
|
|
|
|
return;
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
2022-01-08 05:22:06 -06:00
|
|
|
_ => {}
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
2022-01-08 05:22:06 -06:00
|
|
|
|
|
|
|
// The method kind does not correspond to what appeared in the trait, report.
|
|
|
|
let path = &self.current_trait_ref.as_ref().unwrap().1.path;
|
2022-01-08 07:49:38 -06:00
|
|
|
let (code, kind) = match kind {
|
|
|
|
AssocItemKind::Const(..) => (rustc_errors::error_code!(E0323), "const"),
|
|
|
|
AssocItemKind::Fn(..) => (rustc_errors::error_code!(E0324), "method"),
|
|
|
|
AssocItemKind::TyAlias(..) => (rustc_errors::error_code!(E0325), "type"),
|
|
|
|
AssocItemKind::MacCall(..) => span_bug!(span, "unexpanded macro"),
|
2022-01-08 05:22:06 -06:00
|
|
|
};
|
2021-11-17 13:37:46 -06:00
|
|
|
let trait_path = path_names_to_string(path);
|
2022-01-08 07:49:38 -06:00
|
|
|
self.report_error(
|
|
|
|
span,
|
|
|
|
ResolutionError::TraitImplMismatch {
|
|
|
|
name: ident.name,
|
|
|
|
kind,
|
|
|
|
code,
|
2021-11-17 13:37:46 -06:00
|
|
|
trait_path,
|
2022-01-08 07:49:38 -06:00
|
|
|
trait_item_span: binding.span,
|
|
|
|
},
|
|
|
|
);
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
2020-01-22 01:01:21 -06:00
|
|
|
fn resolve_params(&mut self, params: &'ast [Param]) {
|
2019-09-04 18:43:53 -05:00
|
|
|
let mut bindings = smallvec![(PatBoundCtx::Product, Default::default())];
|
2019-08-28 00:13:34 -05:00
|
|
|
for Param { pat, ty, .. } in params {
|
2019-09-01 10:35:45 -05:00
|
|
|
self.resolve_pattern(pat, PatternSource::FnParam, &mut bindings);
|
2019-08-28 00:13:34 -05:00
|
|
|
self.visit_ty(ty);
|
2019-08-28 00:20:54 -05:00
|
|
|
debug!("(resolving function / closure) recorded parameter");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 01:01:21 -06:00
|
|
|
fn resolve_local(&mut self, local: &'ast Local) {
|
2020-06-20 16:01:03 -05:00
|
|
|
debug!("resolving local ({:?})", local);
|
2019-08-07 18:39:02 -05:00
|
|
|
// Resolve the type.
|
|
|
|
walk_list!(self, visit_ty, &local.ty);
|
|
|
|
|
|
|
|
// Resolve the initializer.
|
2021-06-22 13:00:58 -05:00
|
|
|
if let Some((init, els)) = local.kind.init_else_opt() {
|
|
|
|
self.visit_expr(init);
|
|
|
|
|
|
|
|
// Resolve the `else` block
|
|
|
|
if let Some(els) = els {
|
|
|
|
self.visit_block(els);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 18:39:02 -05:00
|
|
|
|
|
|
|
// Resolve the pattern.
|
2019-08-28 00:13:34 -05:00
|
|
|
self.resolve_pattern_top(&local.pat, PatternSource::Let);
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
2019-09-01 15:52:02 -05:00
|
|
|
/// build a map from pattern identifiers to binding-info's.
|
|
|
|
/// this is done hygienically. This could arise for a macro
|
|
|
|
/// that expands into an or-pattern where one 'x' was from the
|
|
|
|
/// user and one 'x' came from the macro.
|
2021-10-21 08:09:24 -05:00
|
|
|
fn binding_mode_map(&mut self, pat: &Pat) -> BindingMap {
|
2019-08-07 18:39:02 -05:00
|
|
|
let mut binding_map = FxHashMap::default();
|
|
|
|
|
|
|
|
pat.walk(&mut |pat| {
|
2019-09-26 10:18:31 -05:00
|
|
|
match pat.kind {
|
2019-09-01 15:52:02 -05:00
|
|
|
PatKind::Ident(binding_mode, ident, ref sub_pat)
|
|
|
|
if sub_pat.is_some() || self.is_base_res_local(pat.id) =>
|
|
|
|
{
|
|
|
|
binding_map.insert(ident, BindingInfo { span: ident.span, binding_mode });
|
|
|
|
}
|
|
|
|
PatKind::Or(ref ps) => {
|
|
|
|
// Check the consistency of this or-pattern and
|
|
|
|
// then add all bindings to the larger map.
|
|
|
|
for bm in self.check_consistent_bindings(ps) {
|
|
|
|
binding_map.extend(bm);
|
|
|
|
}
|
|
|
|
return false;
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
2019-09-01 15:52:02 -05:00
|
|
|
_ => {}
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
2019-09-01 15:52:02 -05:00
|
|
|
|
2019-08-07 18:39:02 -05:00
|
|
|
true
|
|
|
|
});
|
|
|
|
|
|
|
|
binding_map
|
|
|
|
}
|
|
|
|
|
2019-09-01 15:52:02 -05:00
|
|
|
fn is_base_res_local(&self, nid: NodeId) -> bool {
|
2020-10-26 20:02:48 -05:00
|
|
|
matches!(self.r.partial_res_map.get(&nid).map(|res| res.base_res()), Some(Res::Local(..)))
|
2019-09-01 15:52:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks that all of the arms in an or-pattern have exactly the
|
|
|
|
/// same set of bindings, with the same binding modes for each.
|
2021-10-21 08:09:24 -05:00
|
|
|
fn check_consistent_bindings(&mut self, pats: &[P<Pat>]) -> Vec<BindingMap> {
|
2019-08-07 18:39:02 -05:00
|
|
|
let mut missing_vars = FxHashMap::default();
|
|
|
|
let mut inconsistent_vars = FxHashMap::default();
|
|
|
|
|
2019-09-01 15:52:02 -05:00
|
|
|
// 1) Compute the binding maps of all arms.
|
|
|
|
let maps = pats.iter().map(|pat| self.binding_mode_map(pat)).collect::<Vec<_>>();
|
|
|
|
|
|
|
|
// 2) Record any missing bindings or binding mode inconsistencies.
|
|
|
|
for (map_outer, pat_outer) in pats.iter().enumerate().map(|(idx, pat)| (&maps[idx], pat)) {
|
|
|
|
// Check against all arms except for the same pattern which is always self-consistent.
|
|
|
|
let inners = pats
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.filter(|(_, pat)| pat.id != pat_outer.id)
|
|
|
|
.flat_map(|(idx, _)| maps[idx].iter())
|
|
|
|
.map(|(key, binding)| (key.name, map_outer.get(&key), binding));
|
|
|
|
|
|
|
|
for (name, info, &binding_inner) in inners {
|
|
|
|
match info {
|
|
|
|
None => {
|
|
|
|
// The inner binding is missing in the outer.
|
|
|
|
let binding_error =
|
|
|
|
missing_vars.entry(name).or_insert_with(|| BindingError {
|
|
|
|
name,
|
|
|
|
origin: BTreeSet::new(),
|
|
|
|
target: BTreeSet::new(),
|
|
|
|
could_be_path: name.as_str().starts_with(char::is_uppercase),
|
|
|
|
});
|
|
|
|
binding_error.origin.insert(binding_inner.span);
|
|
|
|
binding_error.target.insert(pat_outer.span);
|
|
|
|
}
|
|
|
|
Some(binding_outer) => {
|
|
|
|
if binding_outer.binding_mode != binding_inner.binding_mode {
|
|
|
|
// The binding modes in the outer and inner bindings differ.
|
|
|
|
inconsistent_vars
|
|
|
|
.entry(name)
|
|
|
|
.or_insert((binding_inner.span, binding_outer.span));
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-09 04:43:26 -05:00
|
|
|
|
2019-09-01 15:52:02 -05:00
|
|
|
// 3) Report all missing variables we found.
|
2021-11-17 13:37:46 -06:00
|
|
|
let mut missing_vars = missing_vars.into_iter().collect::<Vec<_>>();
|
|
|
|
missing_vars.sort_by_key(|&(sym, ref _err)| sym);
|
2020-06-02 06:07:43 -05:00
|
|
|
|
2021-11-17 13:37:46 -06:00
|
|
|
for (name, mut v) in missing_vars.into_iter() {
|
|
|
|
if inconsistent_vars.contains_key(&name) {
|
2019-08-09 15:21:45 -05:00
|
|
|
v.could_be_path = false;
|
2019-08-09 04:43:26 -05:00
|
|
|
}
|
2020-06-20 14:57:23 -05:00
|
|
|
self.report_error(
|
2019-08-09 04:43:26 -05:00
|
|
|
*v.origin.iter().next().unwrap(),
|
2021-11-17 13:37:46 -06:00
|
|
|
ResolutionError::VariableNotBoundInPattern(v, self.parent_scope),
|
2019-08-09 04:43:26 -05:00
|
|
|
);
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
2019-08-09 15:21:45 -05:00
|
|
|
|
2019-09-01 15:52:02 -05:00
|
|
|
// 4) Report all inconsistencies in binding modes we found.
|
2019-08-07 18:39:02 -05:00
|
|
|
let mut inconsistent_vars = inconsistent_vars.iter().collect::<Vec<_>>();
|
|
|
|
inconsistent_vars.sort();
|
|
|
|
for (name, v) in inconsistent_vars {
|
2020-06-20 14:57:23 -05:00
|
|
|
self.report_error(v.0, ResolutionError::VariableBoundWithDifferentMode(*name, v.1));
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
2019-09-01 15:52:02 -05:00
|
|
|
|
|
|
|
// 5) Finally bubble up all the binding maps.
|
|
|
|
maps
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check the consistency of the outermost or-patterns.
|
2020-01-22 01:01:21 -06:00
|
|
|
fn check_consistent_bindings_top(&mut self, pat: &'ast Pat) {
|
2019-09-26 10:18:31 -05:00
|
|
|
pat.walk(&mut |pat| match pat.kind {
|
2019-09-01 15:52:02 -05:00
|
|
|
PatKind::Or(ref ps) => {
|
|
|
|
self.check_consistent_bindings(ps);
|
|
|
|
false
|
|
|
|
}
|
|
|
|
_ => true,
|
|
|
|
})
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
2020-01-22 01:01:21 -06:00
|
|
|
fn resolve_arm(&mut self, arm: &'ast Arm) {
|
2019-08-28 01:48:10 -05:00
|
|
|
self.with_rib(ValueNS, NormalRibKind, |this| {
|
2019-09-01 16:52:32 -05:00
|
|
|
this.resolve_pattern_top(&arm.pat, PatternSource::Match);
|
2019-08-28 01:51:41 -05:00
|
|
|
walk_list!(this, visit_expr, &arm.guard);
|
2019-08-28 01:48:10 -05:00
|
|
|
this.visit_expr(&arm.body);
|
|
|
|
});
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
2019-09-01 16:52:32 -05:00
|
|
|
/// Arising from `source`, resolve a top level pattern.
|
2020-01-22 01:01:21 -06:00
|
|
|
fn resolve_pattern_top(&mut self, pat: &'ast Pat, pat_src: PatternSource) {
|
2019-09-04 18:43:53 -05:00
|
|
|
let mut bindings = smallvec![(PatBoundCtx::Product, Default::default())];
|
|
|
|
self.resolve_pattern(pat, pat_src, &mut bindings);
|
2019-08-28 00:13:34 -05:00
|
|
|
}
|
|
|
|
|
2019-08-28 03:27:40 -05:00
|
|
|
fn resolve_pattern(
|
|
|
|
&mut self,
|
2020-01-22 01:01:21 -06:00
|
|
|
pat: &'ast Pat,
|
2019-08-28 03:27:40 -05:00
|
|
|
pat_src: PatternSource,
|
2019-09-04 18:43:53 -05:00
|
|
|
bindings: &mut SmallVec<[(PatBoundCtx, FxHashSet<Ident>); 1]>,
|
2019-08-28 00:13:34 -05:00
|
|
|
) {
|
2021-12-23 18:03:30 -06:00
|
|
|
// We walk the pattern before declaring the pattern's inner bindings,
|
|
|
|
// so that we avoid resolving a literal expression to a binding defined
|
|
|
|
// by the pattern.
|
|
|
|
visit::walk_pat(self, pat);
|
2019-09-01 10:35:45 -05:00
|
|
|
self.resolve_pattern_inner(pat, pat_src, bindings);
|
2019-09-01 16:52:32 -05:00
|
|
|
// This has to happen *after* we determine which pat_idents are variants:
|
|
|
|
self.check_consistent_bindings_top(pat);
|
2019-08-28 00:13:34 -05:00
|
|
|
}
|
|
|
|
|
2019-09-03 19:43:49 -05:00
|
|
|
/// Resolve bindings in a pattern. This is a helper to `resolve_pattern`.
|
|
|
|
///
|
|
|
|
/// ### `bindings`
|
|
|
|
///
|
|
|
|
/// A stack of sets of bindings accumulated.
|
|
|
|
///
|
2019-09-04 18:43:53 -05:00
|
|
|
/// In each set, `PatBoundCtx::Product` denotes that a found binding in it should
|
|
|
|
/// be interpreted as re-binding an already bound binding. This results in an error.
|
|
|
|
/// Meanwhile, `PatBound::Or` denotes that a found binding in the set should result
|
|
|
|
/// in reusing this binding rather than creating a fresh one.
|
2019-09-03 19:43:49 -05:00
|
|
|
///
|
2019-09-04 18:43:53 -05:00
|
|
|
/// When called at the top level, the stack must have a single element
|
|
|
|
/// with `PatBound::Product`. Otherwise, pushing to the stack happens as
|
|
|
|
/// or-patterns (`p_0 | ... | p_n`) are encountered and the context needs
|
|
|
|
/// to be switched to `PatBoundCtx::Or` and then `PatBoundCtx::Product` for each `p_i`.
|
2019-09-03 19:43:49 -05:00
|
|
|
/// When each `p_i` has been dealt with, the top set is merged with its parent.
|
|
|
|
/// When a whole or-pattern has been dealt with, the thing happens.
|
|
|
|
///
|
|
|
|
/// See the implementation and `fresh_binding` for more details.
|
2019-08-28 00:13:34 -05:00
|
|
|
fn resolve_pattern_inner(
|
|
|
|
&mut self,
|
|
|
|
pat: &Pat,
|
|
|
|
pat_src: PatternSource,
|
2019-09-04 18:43:53 -05:00
|
|
|
bindings: &mut SmallVec<[(PatBoundCtx, FxHashSet<Ident>); 1]>,
|
2019-08-28 03:27:40 -05:00
|
|
|
) {
|
|
|
|
// Visit all direct subpatterns of this pattern.
|
|
|
|
pat.walk(&mut |pat| {
|
2019-09-26 10:18:31 -05:00
|
|
|
debug!("resolve_pattern pat={:?} node={:?}", pat, pat.kind);
|
|
|
|
match pat.kind {
|
2020-07-23 10:02:44 -05:00
|
|
|
PatKind::Ident(bmode, ident, ref sub) => {
|
2020-07-30 16:52:00 -05:00
|
|
|
// First try to resolve the identifier as some existing entity,
|
|
|
|
// then fall back to a fresh binding.
|
|
|
|
let has_sub = sub.is_some();
|
|
|
|
let res = self
|
2022-03-23 16:32:00 -05:00
|
|
|
.try_resolve_as_non_binding(pat_src, bmode, ident, has_sub)
|
2020-07-30 17:19:49 -05:00
|
|
|
.unwrap_or_else(|| self.fresh_binding(ident, pat.id, pat_src, bindings));
|
2020-07-30 16:52:00 -05:00
|
|
|
self.r.record_partial_res(pat.id, PartialRes::new(res));
|
2021-10-21 08:09:24 -05:00
|
|
|
self.r.record_pat_span(pat.id, pat.span);
|
2019-08-28 03:27:40 -05:00
|
|
|
}
|
2020-12-10 06:20:07 -06:00
|
|
|
PatKind::TupleStruct(ref qself, ref path, ref sub_patterns) => {
|
2020-09-08 17:14:09 -05:00
|
|
|
self.smart_resolve_path(
|
|
|
|
pat.id,
|
2020-12-10 06:20:07 -06:00
|
|
|
qself.as_ref(),
|
2020-09-08 17:14:09 -05:00
|
|
|
path,
|
|
|
|
PathSource::TupleStruct(
|
|
|
|
pat.span,
|
|
|
|
self.r.arenas.alloc_pattern_spans(sub_patterns.iter().map(|p| p.span)),
|
|
|
|
),
|
|
|
|
);
|
2019-08-28 03:27:40 -05:00
|
|
|
}
|
|
|
|
PatKind::Path(ref qself, ref path) => {
|
|
|
|
self.smart_resolve_path(pat.id, qself.as_ref(), path, PathSource::Pat);
|
|
|
|
}
|
2020-12-10 06:20:07 -06:00
|
|
|
PatKind::Struct(ref qself, ref path, ..) => {
|
|
|
|
self.smart_resolve_path(pat.id, qself.as_ref(), path, PathSource::Struct);
|
2019-08-28 03:27:40 -05:00
|
|
|
}
|
2019-08-28 00:13:34 -05:00
|
|
|
PatKind::Or(ref ps) => {
|
2019-09-04 18:43:53 -05:00
|
|
|
// Add a new set of bindings to the stack. `Or` here records that when a
|
2019-09-01 10:35:45 -05:00
|
|
|
// binding already exists in this set, it should not result in an error because
|
|
|
|
// `V1(a) | V2(a)` must be allowed and are checked for consistency later.
|
2019-09-04 18:43:53 -05:00
|
|
|
bindings.push((PatBoundCtx::Or, Default::default()));
|
2019-08-28 00:13:34 -05:00
|
|
|
for p in ps {
|
2019-09-01 10:35:45 -05:00
|
|
|
// Now we need to switch back to a product context so that each
|
|
|
|
// part of the or-pattern internally rejects already bound names.
|
|
|
|
// For example, `V1(a) | V2(a, a)` and `V1(a, a) | V2(a)` are bad.
|
2019-09-04 18:43:53 -05:00
|
|
|
bindings.push((PatBoundCtx::Product, Default::default()));
|
2019-09-01 10:35:45 -05:00
|
|
|
self.resolve_pattern_inner(p, pat_src, bindings);
|
|
|
|
// Move up the non-overlapping bindings to the or-pattern.
|
|
|
|
// Existing bindings just get "merged".
|
|
|
|
let collected = bindings.pop().unwrap().1;
|
|
|
|
bindings.last_mut().unwrap().1.extend(collected);
|
2019-08-28 00:13:34 -05:00
|
|
|
}
|
2019-09-01 10:35:45 -05:00
|
|
|
// This or-pattern itself can itself be part of a product,
|
|
|
|
// e.g. `(V1(a) | V2(a), a)` or `(a, V1(a) | V2(a))`.
|
|
|
|
// Both cases bind `a` again in a product pattern and must be rejected.
|
|
|
|
let collected = bindings.pop().unwrap().1;
|
|
|
|
bindings.last_mut().unwrap().1.extend(collected);
|
2019-08-28 00:13:34 -05:00
|
|
|
|
|
|
|
// Prevent visiting `ps` as we've already done so above.
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-28 03:27:40 -05:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fresh_binding(
|
|
|
|
&mut self,
|
|
|
|
ident: Ident,
|
|
|
|
pat_id: NodeId,
|
|
|
|
pat_src: PatternSource,
|
2019-09-04 18:43:53 -05:00
|
|
|
bindings: &mut SmallVec<[(PatBoundCtx, FxHashSet<Ident>); 1]>,
|
2019-08-28 03:27:40 -05:00
|
|
|
) -> Res {
|
|
|
|
// Add the binding to the local ribs, if it doesn't already exist in the bindings map.
|
|
|
|
// (We must not add it if it's in the bindings map because that breaks the assumptions
|
|
|
|
// later passes make about or-patterns.)
|
2020-03-13 17:36:46 -05:00
|
|
|
let ident = ident.normalize_to_macro_rules();
|
2019-09-01 10:35:45 -05:00
|
|
|
|
2019-11-22 12:13:51 -06:00
|
|
|
let mut bound_iter = bindings.iter().filter(|(_, set)| set.contains(&ident));
|
|
|
|
// Already bound in a product pattern? e.g. `(a, a)` which is not allowed.
|
|
|
|
let already_bound_and = bound_iter.clone().any(|(ctx, _)| *ctx == PatBoundCtx::Product);
|
|
|
|
// Already bound in an or-pattern? e.g. `V1(a) | V2(a)`.
|
|
|
|
// This is *required* for consistency which is checked later.
|
|
|
|
let already_bound_or = bound_iter.any(|(ctx, _)| *ctx == PatBoundCtx::Or);
|
2019-09-01 10:35:45 -05:00
|
|
|
|
|
|
|
if already_bound_and {
|
|
|
|
// Overlap in a product pattern somewhere; report an error.
|
|
|
|
use ResolutionError::*;
|
|
|
|
let error = match pat_src {
|
|
|
|
// `fn f(a: u8, a: u8)`:
|
|
|
|
PatternSource::FnParam => IdentifierBoundMoreThanOnceInParameterList,
|
|
|
|
// `Variant(a, a)`:
|
|
|
|
_ => IdentifierBoundMoreThanOnceInSamePattern,
|
|
|
|
};
|
2020-06-20 14:57:23 -05:00
|
|
|
self.report_error(ident.span, error(ident.name));
|
2019-09-01 10:35:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Record as bound if it's valid:
|
2020-12-29 19:28:08 -06:00
|
|
|
let ident_valid = ident.name != kw::Empty;
|
2019-09-01 10:35:45 -05:00
|
|
|
if ident_valid {
|
|
|
|
bindings.last_mut().unwrap().1.insert(ident);
|
|
|
|
}
|
|
|
|
|
|
|
|
if already_bound_or {
|
|
|
|
// `Variant1(a) | Variant2(a)`, ok
|
|
|
|
// Reuse definition from the first `a`.
|
2021-10-21 08:09:24 -05:00
|
|
|
self.innermost_rib_bindings(ValueNS)[&ident]
|
2019-09-01 10:35:45 -05:00
|
|
|
} else {
|
|
|
|
let res = Res::Local(pat_id);
|
|
|
|
if ident_valid {
|
|
|
|
// A completely fresh binding add to the set if it's valid.
|
2021-10-21 08:09:24 -05:00
|
|
|
self.innermost_rib_bindings(ValueNS).insert(ident, res);
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
2019-09-01 10:35:45 -05:00
|
|
|
res
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 08:09:24 -05:00
|
|
|
fn innermost_rib_bindings(&mut self, ns: Namespace) -> &mut IdentMap<Res> {
|
2019-08-28 01:48:10 -05:00
|
|
|
&mut self.ribs[ns].last_mut().unwrap().bindings
|
|
|
|
}
|
|
|
|
|
2019-08-28 03:10:10 -05:00
|
|
|
fn try_resolve_as_non_binding(
|
|
|
|
&mut self,
|
|
|
|
pat_src: PatternSource,
|
|
|
|
bm: BindingMode,
|
|
|
|
ident: Ident,
|
|
|
|
has_sub: bool,
|
|
|
|
) -> Option<Res> {
|
2020-05-03 09:35:14 -05:00
|
|
|
// An immutable (no `mut`) by-value (no `ref`) binding pattern without
|
|
|
|
// a sub pattern (no `@ $pat`) is syntactically ambiguous as it could
|
|
|
|
// also be interpreted as a path to e.g. a constant, variant, etc.
|
|
|
|
let is_syntactic_ambiguity = !has_sub && bm == BindingMode::ByValue(Mutability::Not);
|
|
|
|
|
2022-04-08 15:50:56 -05:00
|
|
|
let ls_binding = self.maybe_resolve_ident_in_lexical_scope(ident, ValueNS)?;
|
2021-10-21 08:09:24 -05:00
|
|
|
let (res, binding) = match ls_binding {
|
|
|
|
LexicalScopeBinding::Item(binding)
|
|
|
|
if is_syntactic_ambiguity && binding.is_ambiguity() =>
|
|
|
|
{
|
|
|
|
// For ambiguous bindings we don't know all their definitions and cannot check
|
|
|
|
// whether they can be shadowed by fresh bindings or not, so force an error.
|
|
|
|
// issues/33118#issuecomment-233962221 (see below) still applies here,
|
|
|
|
// but we have to ignore it for backward compatibility.
|
|
|
|
self.r.record_use(ident, binding, false);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
LexicalScopeBinding::Item(binding) => (binding.res(), Some(binding)),
|
|
|
|
LexicalScopeBinding::Res(res) => (res, None),
|
|
|
|
};
|
2019-08-28 03:10:10 -05:00
|
|
|
|
|
|
|
match res {
|
2020-03-30 01:40:23 -05:00
|
|
|
Res::SelfCtor(_) // See #70549.
|
|
|
|
| Res::Def(
|
|
|
|
DefKind::Ctor(_, CtorKind::Const) | DefKind::Const | DefKind::ConstParam,
|
|
|
|
_,
|
|
|
|
) if is_syntactic_ambiguity => {
|
2019-08-28 03:10:10 -05:00
|
|
|
// Disambiguate in favor of a unit struct/variant or constant pattern.
|
2021-10-21 08:09:24 -05:00
|
|
|
if let Some(binding) = binding {
|
|
|
|
self.r.record_use(ident, binding, false);
|
|
|
|
}
|
2019-08-28 03:10:10 -05:00
|
|
|
Some(res)
|
|
|
|
}
|
2022-03-29 10:11:12 -05:00
|
|
|
Res::Def(DefKind::Ctor(..) | DefKind::Const | DefKind::Static(_), _) => {
|
2019-08-28 03:10:10 -05:00
|
|
|
// This is unambiguously a fresh binding, either syntactically
|
|
|
|
// (e.g., `IDENT @ PAT` or `ref IDENT`) or because `IDENT` resolves
|
|
|
|
// to something unusable as a pattern (e.g., constructor function),
|
|
|
|
// but we still conservatively report an error, see
|
|
|
|
// issues/33118#issuecomment-233962221 for one reason why.
|
2021-10-21 08:09:24 -05:00
|
|
|
let binding = binding.expect("no binding for a ctor or static");
|
2020-06-20 14:57:23 -05:00
|
|
|
self.report_error(
|
2019-08-28 03:10:10 -05:00
|
|
|
ident.span,
|
2021-05-19 11:51:42 -05:00
|
|
|
ResolutionError::BindingShadowsSomethingUnacceptable {
|
2022-07-08 06:01:30 -05:00
|
|
|
shadowing_binding: pat_src,
|
2021-05-19 11:51:42 -05:00
|
|
|
name: ident.name,
|
|
|
|
participle: if binding.is_import() { "imported" } else { "defined" },
|
|
|
|
article: binding.res().article(),
|
2022-07-08 05:20:05 -05:00
|
|
|
shadowed_binding: binding.res(),
|
2021-05-19 11:51:42 -05:00
|
|
|
shadowed_binding_span: binding.span,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
Res::Def(DefKind::ConstParam, def_id) => {
|
|
|
|
// Same as for DefKind::Const above, but here, `binding` is `None`, so we
|
|
|
|
// have to construct the error differently
|
|
|
|
self.report_error(
|
|
|
|
ident.span,
|
|
|
|
ResolutionError::BindingShadowsSomethingUnacceptable {
|
2022-07-08 06:01:30 -05:00
|
|
|
shadowing_binding: pat_src,
|
2021-05-19 11:51:42 -05:00
|
|
|
name: ident.name,
|
|
|
|
participle: "defined",
|
|
|
|
article: res.article(),
|
2022-07-08 05:20:05 -05:00
|
|
|
shadowed_binding: res,
|
2021-05-19 11:51:42 -05:00
|
|
|
shadowed_binding_span: self.r.opt_span(def_id).expect("const parameter defined outside of local crate"),
|
|
|
|
}
|
2019-08-28 03:10:10 -05:00
|
|
|
);
|
|
|
|
None
|
|
|
|
}
|
2020-03-14 11:44:11 -05:00
|
|
|
Res::Def(DefKind::Fn, _) | Res::Local(..) | Res::Err => {
|
2019-08-28 03:10:10 -05:00
|
|
|
// These entities are explicitly allowed to be shadowed by fresh bindings.
|
|
|
|
None
|
|
|
|
}
|
2022-04-10 22:55:10 -05:00
|
|
|
Res::SelfCtor(_) => {
|
|
|
|
// We resolve `Self` in pattern position as an ident sometimes during recovery,
|
|
|
|
// so delay a bug instead of ICEing.
|
|
|
|
self.r.session.delay_span_bug(
|
|
|
|
ident.span,
|
|
|
|
"unexpected `SelfCtor` in pattern, expected identifier"
|
|
|
|
);
|
|
|
|
None
|
|
|
|
}
|
2020-03-14 11:44:11 -05:00
|
|
|
_ => span_bug!(
|
|
|
|
ident.span,
|
|
|
|
"unexpected resolution for an identifier in pattern: {:?}",
|
2020-03-30 01:40:23 -05:00
|
|
|
res,
|
2020-03-14 11:44:11 -05:00
|
|
|
),
|
2019-08-28 03:10:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 18:39:02 -05:00
|
|
|
// High-level and context dependent path resolution routine.
|
|
|
|
// Resolves the path and records the resolution into definition map.
|
|
|
|
// If resolution fails tries several techniques to find likely
|
|
|
|
// resolution candidates, suggest imports or other help, and report
|
|
|
|
// errors in user friendly way.
|
|
|
|
fn smart_resolve_path(
|
|
|
|
&mut self,
|
|
|
|
id: NodeId,
|
|
|
|
qself: Option<&QSelf>,
|
|
|
|
path: &Path,
|
2020-01-22 01:01:21 -06:00
|
|
|
source: PathSource<'ast>,
|
2019-08-07 18:39:02 -05:00
|
|
|
) {
|
|
|
|
self.smart_resolve_path_fragment(
|
|
|
|
qself,
|
|
|
|
&Segment::from_path(path),
|
|
|
|
source,
|
2022-04-30 08:26:36 -05:00
|
|
|
Finalize::new(id, path.span),
|
2019-08-07 18:39:02 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn smart_resolve_path_fragment(
|
|
|
|
&mut self,
|
|
|
|
qself: Option<&QSelf>,
|
|
|
|
path: &[Segment],
|
2020-01-22 01:01:21 -06:00
|
|
|
source: PathSource<'ast>,
|
2022-03-23 18:55:22 -05:00
|
|
|
finalize: Finalize,
|
2019-08-07 18:39:02 -05:00
|
|
|
) -> PartialRes {
|
2020-08-14 01:05:01 -05:00
|
|
|
tracing::debug!(
|
2022-03-23 18:55:22 -05:00
|
|
|
"smart_resolve_path_fragment(qself={:?}, path={:?}, finalize={:?})",
|
2020-08-14 01:05:01 -05:00
|
|
|
qself,
|
2022-03-23 16:32:00 -05:00
|
|
|
path,
|
2022-03-23 18:55:22 -05:00
|
|
|
finalize,
|
2020-08-14 01:05:01 -05:00
|
|
|
);
|
2019-08-07 18:39:02 -05:00
|
|
|
let ns = source.namespace();
|
|
|
|
|
2022-04-30 08:26:36 -05:00
|
|
|
let Finalize { node_id, path_span, .. } = finalize;
|
2019-08-07 18:39:02 -05:00
|
|
|
let report_errors = |this: &mut Self, res: Option<Res>| {
|
2020-06-20 16:01:03 -05:00
|
|
|
if this.should_report_errs() {
|
2022-03-23 16:32:00 -05:00
|
|
|
let (err, candidates) =
|
|
|
|
this.smart_resolve_report_errors(path, path_span, source, res);
|
2020-06-20 16:01:03 -05:00
|
|
|
|
2021-09-13 16:13:14 -05:00
|
|
|
let def_id = this.parent_scope.module.nearest_parent_mod();
|
2020-06-20 16:01:03 -05:00
|
|
|
let instead = res.is_some();
|
|
|
|
let suggestion =
|
|
|
|
if res.is_none() { this.report_missing_type_error(path) } else { None };
|
|
|
|
|
|
|
|
this.r.use_injections.push(UseError {
|
|
|
|
err,
|
|
|
|
candidates,
|
|
|
|
def_id,
|
|
|
|
instead,
|
|
|
|
suggestion,
|
2022-04-23 18:41:36 -05:00
|
|
|
path: path.into(),
|
2020-06-20 16:01:03 -05:00
|
|
|
});
|
|
|
|
}
|
2020-06-02 13:16:23 -05:00
|
|
|
|
2019-08-07 18:39:02 -05:00
|
|
|
PartialRes::new(Res::Err)
|
|
|
|
};
|
|
|
|
|
2020-06-02 13:16:23 -05:00
|
|
|
// For paths originating from calls (like in `HashMap::new()`), tries
|
|
|
|
// to enrich the plain `failed to resolve: ...` message with hints
|
|
|
|
// about possible missing imports.
|
|
|
|
//
|
|
|
|
// Similar thing, for types, happens in `report_errors` above.
|
|
|
|
let report_errors_for_call = |this: &mut Self, parent_err: Spanned<ResolutionError<'a>>| {
|
|
|
|
if !source.is_call() {
|
|
|
|
return Some(parent_err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before we start looking for candidates, we have to get our hands
|
|
|
|
// on the type user is trying to perform invocation on; basically:
|
2021-02-18 08:53:09 -06:00
|
|
|
// we're transforming `HashMap::new` into just `HashMap`.
|
|
|
|
let path = match path.split_last() {
|
|
|
|
Some((_, path)) if !path.is_empty() => path,
|
|
|
|
_ => return Some(parent_err),
|
2020-06-02 13:16:23 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let (mut err, candidates) =
|
2022-03-23 16:32:00 -05:00
|
|
|
this.smart_resolve_report_errors(path, path_span, PathSource::Type, None);
|
2020-06-02 13:16:23 -05:00
|
|
|
|
|
|
|
if candidates.is_empty() {
|
|
|
|
err.cancel();
|
|
|
|
return Some(parent_err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// There are two different error messages user might receive at
|
|
|
|
// this point:
|
|
|
|
// - E0412 cannot find type `{}` in this scope
|
|
|
|
// - E0433 failed to resolve: use of undeclared type or module `{}`
|
|
|
|
//
|
|
|
|
// The first one is emitted for paths in type-position, and the
|
|
|
|
// latter one - for paths in expression-position.
|
|
|
|
//
|
|
|
|
// Thus (since we're in expression-position at this point), not to
|
2020-06-03 01:38:10 -05:00
|
|
|
// confuse the user, we want to keep the *message* from E0432 (so
|
2020-06-02 13:16:23 -05:00
|
|
|
// `parent_err`), but we want *hints* from E0412 (so `err`).
|
|
|
|
//
|
|
|
|
// And that's what happens below - we're just mixing both messages
|
|
|
|
// into a single one.
|
|
|
|
let mut parent_err = this.r.into_struct_error(parent_err.span, parent_err.node);
|
|
|
|
|
|
|
|
err.message = take(&mut parent_err.message);
|
|
|
|
err.code = take(&mut parent_err.code);
|
|
|
|
err.children = take(&mut parent_err.children);
|
|
|
|
|
2022-01-25 21:39:14 -06:00
|
|
|
parent_err.cancel();
|
2020-06-02 13:16:23 -05:00
|
|
|
|
2021-09-13 16:13:14 -05:00
|
|
|
let def_id = this.parent_scope.module.nearest_parent_mod();
|
2020-06-02 13:16:23 -05:00
|
|
|
|
2020-06-20 16:01:03 -05:00
|
|
|
if this.should_report_errs() {
|
|
|
|
this.r.use_injections.push(UseError {
|
|
|
|
err,
|
|
|
|
candidates,
|
|
|
|
def_id,
|
|
|
|
instead: false,
|
|
|
|
suggestion: None,
|
2022-04-23 18:41:36 -05:00
|
|
|
path: path.into(),
|
2020-06-20 16:01:03 -05:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
err.cancel();
|
|
|
|
}
|
2020-06-02 13:16:23 -05:00
|
|
|
|
|
|
|
// We don't return `Some(parent_err)` here, because the error will
|
|
|
|
// be already printed as part of the `use` injections
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2019-08-07 18:39:02 -05:00
|
|
|
let partial_res = match self.resolve_qpath_anywhere(
|
|
|
|
qself,
|
|
|
|
path,
|
|
|
|
ns,
|
2022-03-23 16:32:00 -05:00
|
|
|
path_span,
|
2019-08-07 18:39:02 -05:00
|
|
|
source.defer_to_typeck(),
|
2022-03-23 18:55:22 -05:00
|
|
|
finalize,
|
2019-08-07 18:39:02 -05:00
|
|
|
) {
|
2020-06-02 13:16:23 -05:00
|
|
|
Ok(Some(partial_res)) if partial_res.unresolved_segments() == 0 => {
|
2020-12-20 16:17:56 -06:00
|
|
|
if source.is_expected(partial_res.base_res()) || partial_res.base_res() == Res::Err
|
|
|
|
{
|
2019-08-07 18:39:02 -05:00
|
|
|
partial_res
|
|
|
|
} else {
|
2019-08-03 09:59:05 -05:00
|
|
|
report_errors(self, Some(partial_res.base_res()))
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
}
|
2020-06-02 13:16:23 -05:00
|
|
|
|
|
|
|
Ok(Some(partial_res)) if source.defer_to_typeck() => {
|
2019-08-07 18:39:02 -05:00
|
|
|
// Not fully resolved associated item `T::A::B` or `<T as Tr>::A::B`
|
|
|
|
// or `<T>::A::B`. If `B` should be resolved in value namespace then
|
|
|
|
// it needs to be added to the trait map.
|
|
|
|
if ns == ValueNS {
|
|
|
|
let item_name = path.last().unwrap().ident;
|
2021-01-06 14:50:02 -06:00
|
|
|
let traits = self.traits_in_scope(item_name, ns);
|
2022-04-30 08:26:36 -05:00
|
|
|
self.r.trait_map.insert(node_id, traits);
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
2021-02-02 12:16:35 -06:00
|
|
|
if PrimTy::from_name(path[0].ident.name).is_some() {
|
2020-12-20 16:55:03 -06:00
|
|
|
let mut std_path = Vec::with_capacity(1 + path.len());
|
|
|
|
|
|
|
|
std_path.push(Segment::from_ident(Ident::with_dummy_span(sym::std)));
|
|
|
|
std_path.extend(path);
|
2019-08-07 18:39:02 -05:00
|
|
|
if let PathResult::Module(_) | PathResult::NonModule(_) =
|
2022-04-30 08:26:36 -05:00
|
|
|
self.resolve_path(&std_path, Some(ns), None)
|
2019-08-07 18:39:02 -05:00
|
|
|
{
|
2020-06-02 13:16:23 -05:00
|
|
|
// Check if we wrote `str::from_utf8` instead of `std::str::from_utf8`
|
2019-08-07 18:39:02 -05:00
|
|
|
let item_span =
|
2022-03-23 16:32:00 -05:00
|
|
|
path.iter().last().map_or(path_span, |segment| segment.ident.span);
|
2020-06-02 13:16:23 -05:00
|
|
|
|
2022-03-23 16:32:00 -05:00
|
|
|
self.r.confused_type_with_std_module.insert(item_span, path_span);
|
|
|
|
self.r.confused_type_with_std_module.insert(path_span, path_span);
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
}
|
2020-06-02 13:16:23 -05:00
|
|
|
|
2019-08-07 18:39:02 -05:00
|
|
|
partial_res
|
|
|
|
}
|
2020-06-02 13:16:23 -05:00
|
|
|
|
|
|
|
Err(err) => {
|
|
|
|
if let Some(err) = report_errors_for_call(self, err) {
|
2020-06-20 14:57:23 -05:00
|
|
|
self.report_error(err.span, err.node);
|
2020-06-02 13:16:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
PartialRes::new(Res::Err)
|
|
|
|
}
|
|
|
|
|
2019-08-07 18:39:02 -05:00
|
|
|
_ => report_errors(self, None),
|
|
|
|
};
|
|
|
|
|
2021-01-11 15:22:04 -06:00
|
|
|
if !matches!(source, PathSource::TraitItem(..)) {
|
2019-08-07 18:39:02 -05:00
|
|
|
// Avoid recording definition of `A::B` in `<T as A>::B::C`.
|
2022-04-30 08:26:36 -05:00
|
|
|
self.r.record_partial_res(node_id, partial_res);
|
|
|
|
self.resolve_elided_lifetimes_in_path(node_id, partial_res, path, source, path_span);
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
2020-06-02 13:16:23 -05:00
|
|
|
|
2019-08-07 18:39:02 -05:00
|
|
|
partial_res
|
|
|
|
}
|
|
|
|
|
2022-03-23 16:32:00 -05:00
|
|
|
fn self_type_is_available(&mut self) -> bool {
|
2022-04-08 15:50:56 -05:00
|
|
|
let binding = self
|
|
|
|
.maybe_resolve_ident_in_lexical_scope(Ident::with_dummy_span(kw::SelfUpper), TypeNS);
|
2021-10-21 08:09:24 -05:00
|
|
|
if let Some(LexicalScopeBinding::Res(res)) = binding { res != Res::Err } else { false }
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
2022-03-23 16:32:00 -05:00
|
|
|
fn self_value_is_available(&mut self, self_span: Span) -> bool {
|
2019-08-07 18:39:02 -05:00
|
|
|
let ident = Ident::new(kw::SelfLower, self_span);
|
2022-04-08 15:50:56 -05:00
|
|
|
let binding = self.maybe_resolve_ident_in_lexical_scope(ident, ValueNS);
|
2021-10-21 08:09:24 -05:00
|
|
|
if let Some(LexicalScopeBinding::Res(res)) = binding { res != Res::Err } else { false }
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
2020-06-20 14:57:23 -05:00
|
|
|
/// A wrapper around [`Resolver::report_error`].
|
|
|
|
///
|
2020-07-11 07:52:36 -05:00
|
|
|
/// This doesn't emit errors for function bodies if this is rustdoc.
|
2021-11-17 13:37:46 -06:00
|
|
|
fn report_error(&mut self, span: Span, resolution_error: ResolutionError<'a>) {
|
2020-06-20 16:01:03 -05:00
|
|
|
if self.should_report_errs() {
|
2020-06-20 14:57:23 -05:00
|
|
|
self.r.report_error(span, resolution_error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-20 16:01:03 -05:00
|
|
|
#[inline]
|
2020-07-10 15:55:06 -05:00
|
|
|
/// If we're actually rustdoc then avoid giving a name resolution error for `cfg()` items.
|
2020-06-20 16:01:03 -05:00
|
|
|
fn should_report_errs(&self) -> bool {
|
2020-07-10 15:55:06 -05:00
|
|
|
!(self.r.session.opts.actually_rustdoc && self.in_func_body)
|
2020-06-20 16:01:03 -05:00
|
|
|
}
|
|
|
|
|
2019-08-07 18:39:02 -05:00
|
|
|
// Resolve in alternative namespaces if resolution in the primary namespace fails.
|
|
|
|
fn resolve_qpath_anywhere(
|
|
|
|
&mut self,
|
|
|
|
qself: Option<&QSelf>,
|
|
|
|
path: &[Segment],
|
|
|
|
primary_ns: Namespace,
|
|
|
|
span: Span,
|
|
|
|
defer_to_typeck: bool,
|
2022-03-23 18:55:22 -05:00
|
|
|
finalize: Finalize,
|
2020-06-02 13:16:23 -05:00
|
|
|
) -> Result<Option<PartialRes>, Spanned<ResolutionError<'a>>> {
|
2019-08-07 18:39:02 -05:00
|
|
|
let mut fin_res = None;
|
2020-06-02 13:16:23 -05:00
|
|
|
|
2020-12-20 14:41:15 -06:00
|
|
|
for (i, &ns) in [primary_ns, TypeNS, ValueNS].iter().enumerate() {
|
2019-08-07 18:39:02 -05:00
|
|
|
if i == 0 || ns != primary_ns {
|
2022-03-23 18:55:22 -05:00
|
|
|
match self.resolve_qpath(qself, path, ns, finalize)? {
|
2019-08-07 18:39:02 -05:00
|
|
|
Some(partial_res)
|
|
|
|
if partial_res.unresolved_segments() == 0 || defer_to_typeck =>
|
2019-12-22 16:42:04 -06:00
|
|
|
{
|
2020-06-02 13:16:23 -05:00
|
|
|
return Ok(Some(partial_res));
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
partial_res => {
|
|
|
|
if fin_res.is_none() {
|
2020-12-20 14:41:35 -06:00
|
|
|
fin_res = partial_res;
|
2019-12-22 16:42:04 -06:00
|
|
|
}
|
|
|
|
}
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(primary_ns != MacroNS);
|
2020-06-02 13:16:23 -05:00
|
|
|
|
2019-08-07 18:39:02 -05:00
|
|
|
if qself.is_none() {
|
|
|
|
let path_seg = |seg: &Segment| PathSegment::from_ident(seg.ident);
|
2020-08-21 17:51:23 -05:00
|
|
|
let path = Path { segments: path.iter().map(path_seg).collect(), span, tokens: None };
|
2019-08-08 06:06:42 -05:00
|
|
|
if let Ok((_, res)) =
|
2019-08-07 19:44:16 -05:00
|
|
|
self.r.resolve_macro_path(&path, None, &self.parent_scope, false, false)
|
|
|
|
{
|
2020-06-02 13:16:23 -05:00
|
|
|
return Ok(Some(PartialRes::new(res)));
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 13:16:23 -05:00
|
|
|
Ok(fin_res)
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Handles paths that may refer to associated items.
|
|
|
|
fn resolve_qpath(
|
|
|
|
&mut self,
|
|
|
|
qself: Option<&QSelf>,
|
|
|
|
path: &[Segment],
|
|
|
|
ns: Namespace,
|
2022-03-23 18:55:22 -05:00
|
|
|
finalize: Finalize,
|
2020-06-02 13:16:23 -05:00
|
|
|
) -> Result<Option<PartialRes>, Spanned<ResolutionError<'a>>> {
|
2019-08-07 18:39:02 -05:00
|
|
|
debug!(
|
2022-03-23 18:55:22 -05:00
|
|
|
"resolve_qpath(qself={:?}, path={:?}, ns={:?}, finalize={:?})",
|
|
|
|
qself, path, ns, finalize,
|
2019-08-07 18:39:02 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
if let Some(qself) = qself {
|
|
|
|
if qself.position == 0 {
|
|
|
|
// This is a case like `<T>::B`, where there is no
|
|
|
|
// trait to resolve. In that case, we leave the `B`
|
|
|
|
// segment to be resolved by type-check.
|
2020-06-02 13:16:23 -05:00
|
|
|
return Ok(Some(PartialRes::with_unresolved_segments(
|
2022-04-15 12:27:53 -05:00
|
|
|
Res::Def(DefKind::Mod, CRATE_DEF_ID.to_def_id()),
|
2019-08-07 18:39:02 -05:00
|
|
|
path.len(),
|
2020-06-02 13:16:23 -05:00
|
|
|
)));
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure `A::B` in `<T as A::B>::C` is a trait item.
|
|
|
|
//
|
|
|
|
// Currently, `path` names the full item (`A::B::C`, in
|
|
|
|
// our example). so we extract the prefix of that that is
|
|
|
|
// the trait (the slice upto and including
|
|
|
|
// `qself.position`). And then we recursively resolve that,
|
|
|
|
// but with `qself` set to `None`.
|
|
|
|
let ns = if qself.position + 1 == path.len() { ns } else { TypeNS };
|
|
|
|
let partial_res = self.smart_resolve_path_fragment(
|
|
|
|
None,
|
|
|
|
&path[..=qself.position],
|
|
|
|
PathSource::TraitItem(ns),
|
2022-04-30 08:26:36 -05:00
|
|
|
Finalize::with_root_span(finalize.node_id, finalize.path_span, qself.path_span),
|
2019-08-07 18:39:02 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// The remaining segments (the `C` in our example) will
|
|
|
|
// have to be resolved by type-check, since that requires doing
|
|
|
|
// trait resolution.
|
2020-06-02 13:16:23 -05:00
|
|
|
return Ok(Some(PartialRes::with_unresolved_segments(
|
2019-08-07 18:39:02 -05:00
|
|
|
partial_res.base_res(),
|
|
|
|
partial_res.unresolved_segments() + path.len() - qself.position - 1,
|
2020-06-02 13:16:23 -05:00
|
|
|
)));
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
2022-04-30 08:26:36 -05:00
|
|
|
let result = match self.resolve_path(&path, Some(ns), Some(finalize)) {
|
2019-08-07 18:39:02 -05:00
|
|
|
PathResult::NonModule(path_res) => path_res,
|
|
|
|
PathResult::Module(ModuleOrUniformRoot::Module(module)) if !module.is_normal() => {
|
|
|
|
PartialRes::new(module.res().unwrap())
|
|
|
|
}
|
|
|
|
// In `a(::assoc_item)*` `a` cannot be a module. If `a` does resolve to a module we
|
|
|
|
// don't report an error right away, but try to fallback to a primitive type.
|
|
|
|
// So, we are still able to successfully resolve something like
|
|
|
|
//
|
|
|
|
// use std::u8; // bring module u8 in scope
|
|
|
|
// fn f() -> u8 { // OK, resolves to primitive u8, not to std::u8
|
|
|
|
// u8::max_value() // OK, resolves to associated function <u8>::max_value,
|
|
|
|
// // not to non-existent std::u8::max_value
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Such behavior is required for backward compatibility.
|
|
|
|
// The same fallback is used when `a` resolves to nothing.
|
|
|
|
PathResult::Module(ModuleOrUniformRoot::Module(_)) | PathResult::Failed { .. }
|
|
|
|
if (ns == TypeNS || path.len() > 1)
|
2021-02-02 12:16:35 -06:00
|
|
|
&& PrimTy::from_name(path[0].ident.name).is_some() =>
|
2019-08-07 18:39:02 -05:00
|
|
|
{
|
2021-02-02 12:16:35 -06:00
|
|
|
let prim = PrimTy::from_name(path[0].ident.name).unwrap();
|
2019-08-07 18:39:02 -05:00
|
|
|
PartialRes::with_unresolved_segments(Res::PrimTy(prim), path.len() - 1)
|
|
|
|
}
|
|
|
|
PathResult::Module(ModuleOrUniformRoot::Module(module)) => {
|
|
|
|
PartialRes::new(module.res().unwrap())
|
2019-12-22 16:42:04 -06:00
|
|
|
}
|
2019-08-07 18:39:02 -05:00
|
|
|
PathResult::Failed { is_error_from_last_segment: false, span, label, suggestion } => {
|
2020-06-02 13:16:23 -05:00
|
|
|
return Err(respan(span, ResolutionError::FailedToResolve { label, suggestion }));
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
2020-06-02 13:16:23 -05:00
|
|
|
PathResult::Module(..) | PathResult::Failed { .. } => return Ok(None),
|
|
|
|
PathResult::Indeterminate => bug!("indeterminate path result in resolve_qpath"),
|
2019-08-07 18:39:02 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
if path.len() > 1
|
|
|
|
&& result.base_res() != Res::Err
|
|
|
|
&& path[0].ident.name != kw::PathRoot
|
|
|
|
&& path[0].ident.name != kw::DollarCrate
|
|
|
|
{
|
|
|
|
let unqualified_result = {
|
2022-04-30 08:26:36 -05:00
|
|
|
match self.resolve_path(&[*path.last().unwrap()], Some(ns), None) {
|
2019-08-07 18:39:02 -05:00
|
|
|
PathResult::NonModule(path_res) => path_res.base_res(),
|
|
|
|
PathResult::Module(ModuleOrUniformRoot::Module(module)) => {
|
|
|
|
module.res().unwrap()
|
2019-12-22 16:42:04 -06:00
|
|
|
}
|
2020-06-02 13:16:23 -05:00
|
|
|
_ => return Ok(Some(result)),
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
if result.base_res() == unqualified_result {
|
|
|
|
let lint = lint::builtin::UNUSED_QUALIFICATIONS;
|
2022-04-30 08:26:36 -05:00
|
|
|
self.r.lint_buffer.buffer_lint(
|
|
|
|
lint,
|
|
|
|
finalize.node_id,
|
|
|
|
finalize.path_span,
|
|
|
|
"unnecessary qualification",
|
|
|
|
)
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 13:16:23 -05:00
|
|
|
Ok(Some(result))
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
2019-08-28 01:48:10 -05:00
|
|
|
fn with_resolved_label(&mut self, label: Option<Label>, id: NodeId, f: impl FnOnce(&mut Self)) {
|
2019-08-07 18:39:02 -05:00
|
|
|
if let Some(label) = label {
|
2019-11-14 12:05:03 -06:00
|
|
|
if label.ident.as_str().as_bytes()[1] != b'_' {
|
|
|
|
self.diagnostic_metadata.unused_labels.insert(id, label.ident.span);
|
|
|
|
}
|
2021-12-04 14:20:58 -06:00
|
|
|
|
2022-06-02 13:35:14 -05:00
|
|
|
if let Ok((_, orig_span)) = self.resolve_label(label.ident) {
|
|
|
|
diagnostics::signal_label_shadowing(self.r.session, orig_span, label.ident)
|
2021-12-04 14:20:58 -06:00
|
|
|
}
|
|
|
|
|
2019-08-28 01:48:10 -05:00
|
|
|
self.with_label_rib(NormalRibKind, |this| {
|
2022-06-02 13:35:14 -05:00
|
|
|
let ident = label.ident.normalize_to_macro_rules();
|
2019-08-07 18:39:02 -05:00
|
|
|
this.label_ribs.last_mut().unwrap().bindings.insert(ident, id);
|
|
|
|
f(this);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
f(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 01:01:21 -06:00
|
|
|
fn resolve_labeled_block(&mut self, label: Option<Label>, id: NodeId, block: &'ast Block) {
|
2019-08-07 18:39:02 -05:00
|
|
|
self.with_resolved_label(label, id, |this| this.visit_block(block));
|
|
|
|
}
|
|
|
|
|
2020-01-22 01:01:21 -06:00
|
|
|
fn resolve_block(&mut self, block: &'ast Block) {
|
2019-08-28 03:59:15 -05:00
|
|
|
debug!("(resolving block) entering block");
|
|
|
|
// Move down in the graph, if there's an anonymous module rooted here.
|
|
|
|
let orig_module = self.parent_scope.module;
|
|
|
|
let anonymous_module = self.r.block_map.get(&block.id).cloned(); // clones a reference
|
|
|
|
|
|
|
|
let mut num_macro_definition_ribs = 0;
|
|
|
|
if let Some(anonymous_module) = anonymous_module {
|
|
|
|
debug!("(resolving block) found anonymous module, moving down");
|
|
|
|
self.ribs[ValueNS].push(Rib::new(ModuleRibKind(anonymous_module)));
|
|
|
|
self.ribs[TypeNS].push(Rib::new(ModuleRibKind(anonymous_module)));
|
|
|
|
self.parent_scope.module = anonymous_module;
|
|
|
|
} else {
|
|
|
|
self.ribs[ValueNS].push(Rib::new(NormalRibKind));
|
|
|
|
}
|
|
|
|
|
2021-09-02 13:34:03 -05:00
|
|
|
let prev = self.diagnostic_metadata.current_block_could_be_bare_struct_literal.take();
|
|
|
|
if let (true, [Stmt { kind: StmtKind::Expr(expr), .. }]) =
|
2022-04-03 22:49:02 -05:00
|
|
|
(block.could_be_bare_literal, &block.stmts[..])
|
|
|
|
&& let ExprKind::Type(..) = expr.kind
|
2021-09-02 13:34:03 -05:00
|
|
|
{
|
2022-04-03 21:10:40 -05:00
|
|
|
self.diagnostic_metadata.current_block_could_be_bare_struct_literal =
|
|
|
|
Some(block.span);
|
2021-09-02 13:34:03 -05:00
|
|
|
}
|
2019-08-28 03:59:15 -05:00
|
|
|
// Descend into the block.
|
|
|
|
for stmt in &block.stmts {
|
2022-04-03 22:49:02 -05:00
|
|
|
if let StmtKind::Item(ref item) = stmt.kind
|
|
|
|
&& let ItemKind::MacroDef(..) = item.kind {
|
2022-04-03 21:10:40 -05:00
|
|
|
num_macro_definition_ribs += 1;
|
|
|
|
let res = self.r.local_def_id(item.id).to_def_id();
|
|
|
|
self.ribs[ValueNS].push(Rib::new(MacroDefinition(res)));
|
|
|
|
self.label_ribs.push(Rib::new(MacroDefinition(res)));
|
2019-08-28 03:59:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
self.visit_stmt(stmt);
|
|
|
|
}
|
2021-09-02 13:34:03 -05:00
|
|
|
self.diagnostic_metadata.current_block_could_be_bare_struct_literal = prev;
|
2019-08-28 03:59:15 -05:00
|
|
|
|
|
|
|
// Move back up.
|
|
|
|
self.parent_scope.module = orig_module;
|
|
|
|
for _ in 0..num_macro_definition_ribs {
|
|
|
|
self.ribs[ValueNS].pop();
|
|
|
|
self.label_ribs.pop();
|
|
|
|
}
|
|
|
|
self.ribs[ValueNS].pop();
|
|
|
|
if anonymous_module.is_some() {
|
|
|
|
self.ribs[TypeNS].pop();
|
|
|
|
}
|
|
|
|
debug!("(resolving block) leaving block");
|
|
|
|
}
|
|
|
|
|
2020-10-22 03:32:41 -05:00
|
|
|
fn resolve_anon_const(&mut self, constant: &'ast AnonConst, is_repeat: IsRepeatExpr) {
|
|
|
|
debug!("resolve_anon_const {:?} is_repeat: {:?}", constant, is_repeat);
|
|
|
|
self.with_constant_rib(
|
|
|
|
is_repeat,
|
2022-05-02 10:36:48 -05:00
|
|
|
if constant.value.is_potential_trivial_const_param() {
|
|
|
|
HasGenericParams::Yes
|
|
|
|
} else {
|
|
|
|
HasGenericParams::No
|
|
|
|
},
|
2020-12-13 09:32:59 -06:00
|
|
|
None,
|
2022-03-31 13:10:37 -05:00
|
|
|
|this| visit::walk_anon_const(this, constant),
|
2020-10-22 03:32:41 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-29 09:27:59 -05:00
|
|
|
fn resolve_inline_const(&mut self, constant: &'ast AnonConst) {
|
|
|
|
debug!("resolve_anon_const {constant:?}");
|
|
|
|
self.with_constant_rib(IsRepeatExpr::No, HasGenericParams::Yes, None, |this| {
|
|
|
|
visit::walk_anon_const(this, constant);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-22 01:01:21 -06:00
|
|
|
fn resolve_expr(&mut self, expr: &'ast Expr, parent: Option<&'ast Expr>) {
|
2019-08-07 18:39:02 -05:00
|
|
|
// First, record candidate traits for this expression if it could
|
|
|
|
// result in the invocation of a method call.
|
|
|
|
|
|
|
|
self.record_candidate_traits_for_expr_if_necessary(expr);
|
|
|
|
|
|
|
|
// Next, resolve the node.
|
2019-09-26 08:39:48 -05:00
|
|
|
match expr.kind {
|
2019-08-07 18:39:02 -05:00
|
|
|
ExprKind::Path(ref qself, ref path) => {
|
|
|
|
self.smart_resolve_path(expr.id, qself.as_ref(), path, PathSource::Expr(parent));
|
|
|
|
visit::walk_expr(self, expr);
|
|
|
|
}
|
|
|
|
|
2021-03-15 19:15:53 -05:00
|
|
|
ExprKind::Struct(ref se) => {
|
2020-12-10 06:20:07 -06:00
|
|
|
self.smart_resolve_path(expr.id, se.qself.as_ref(), &se.path, PathSource::Struct);
|
2019-08-07 18:39:02 -05:00
|
|
|
visit::walk_expr(self, expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
ExprKind::Break(Some(label), _) | ExprKind::Continue(Some(label)) => {
|
2022-06-02 13:35:14 -05:00
|
|
|
match self.resolve_label(label.ident) {
|
|
|
|
Ok((node_id, _)) => {
|
|
|
|
// Since this res is a label, it is never read.
|
|
|
|
self.r.label_res_map.insert(expr.id, node_id);
|
|
|
|
self.diagnostic_metadata.unused_labels.remove(&node_id);
|
|
|
|
}
|
|
|
|
Err(error) => {
|
|
|
|
self.report_error(label.ident.span, error);
|
|
|
|
}
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// visit `break` argument if any
|
|
|
|
visit::walk_expr(self, expr);
|
|
|
|
}
|
|
|
|
|
2021-01-19 19:51:48 -06:00
|
|
|
ExprKind::Break(None, Some(ref e)) => {
|
|
|
|
// We use this instead of `visit::walk_expr` to keep the parent expr around for
|
2021-01-21 18:48:17 -06:00
|
|
|
// better diagnostics.
|
2021-01-19 19:51:48 -06:00
|
|
|
self.resolve_expr(e, Some(&expr));
|
|
|
|
}
|
|
|
|
|
2021-08-08 09:49:13 -05:00
|
|
|
ExprKind::Let(ref pat, ref scrutinee, _) => {
|
2019-08-07 18:39:02 -05:00
|
|
|
self.visit_expr(scrutinee);
|
2019-09-01 16:52:32 -05:00
|
|
|
self.resolve_pattern_top(pat, PatternSource::Let);
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ExprKind::If(ref cond, ref then, ref opt_else) => {
|
2019-08-28 01:48:10 -05:00
|
|
|
self.with_rib(ValueNS, NormalRibKind, |this| {
|
2020-08-25 22:28:25 -05:00
|
|
|
let old = this.diagnostic_metadata.in_if_condition.replace(cond);
|
2019-08-28 01:48:10 -05:00
|
|
|
this.visit_expr(cond);
|
2020-08-25 22:28:25 -05:00
|
|
|
this.diagnostic_metadata.in_if_condition = old;
|
2019-08-28 01:48:10 -05:00
|
|
|
this.visit_block(then);
|
|
|
|
});
|
2020-04-24 15:58:41 -05:00
|
|
|
if let Some(expr) = opt_else {
|
|
|
|
self.visit_expr(expr);
|
|
|
|
}
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ExprKind::Loop(ref block, label) => self.resolve_labeled_block(label, expr.id, &block),
|
|
|
|
|
2019-08-28 01:48:10 -05:00
|
|
|
ExprKind::While(ref cond, ref block, label) => {
|
2019-08-07 18:39:02 -05:00
|
|
|
self.with_resolved_label(label, expr.id, |this| {
|
2019-08-28 01:48:10 -05:00
|
|
|
this.with_rib(ValueNS, NormalRibKind, |this| {
|
2021-12-29 06:23:19 -06:00
|
|
|
let old = this.diagnostic_metadata.in_if_condition.replace(cond);
|
2019-08-28 01:48:10 -05:00
|
|
|
this.visit_expr(cond);
|
2021-12-29 06:23:19 -06:00
|
|
|
this.diagnostic_metadata.in_if_condition = old;
|
2019-08-28 01:48:10 -05:00
|
|
|
this.visit_block(block);
|
|
|
|
})
|
2019-08-07 18:39:02 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-28 01:48:10 -05:00
|
|
|
ExprKind::ForLoop(ref pat, ref iter_expr, ref block, label) => {
|
|
|
|
self.visit_expr(iter_expr);
|
|
|
|
self.with_rib(ValueNS, NormalRibKind, |this| {
|
2019-08-28 00:13:34 -05:00
|
|
|
this.resolve_pattern_top(pat, PatternSource::For);
|
2019-08-28 01:48:10 -05:00
|
|
|
this.resolve_labeled_block(label, expr.id, block);
|
|
|
|
});
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ExprKind::Block(ref block, label) => self.resolve_labeled_block(label, block.id, block),
|
|
|
|
|
|
|
|
// Equivalent to `visit::walk_expr` + passing some context to children.
|
|
|
|
ExprKind::Field(ref subexpression, _) => {
|
|
|
|
self.resolve_expr(subexpression, Some(expr));
|
|
|
|
}
|
2020-06-09 14:34:23 -05:00
|
|
|
ExprKind::MethodCall(ref segment, ref arguments, _) => {
|
2019-08-07 18:39:02 -05:00
|
|
|
let mut arguments = arguments.iter();
|
|
|
|
self.resolve_expr(arguments.next().unwrap(), Some(expr));
|
|
|
|
for argument in arguments {
|
|
|
|
self.resolve_expr(argument, None);
|
|
|
|
}
|
|
|
|
self.visit_path_segment(expr.span, segment);
|
|
|
|
}
|
|
|
|
|
|
|
|
ExprKind::Call(ref callee, ref arguments) => {
|
|
|
|
self.resolve_expr(callee, Some(expr));
|
2021-03-26 14:32:37 -05:00
|
|
|
let const_args = self.r.legacy_const_generic_args(callee).unwrap_or_default();
|
2021-02-24 00:46:30 -06:00
|
|
|
for (idx, argument) in arguments.iter().enumerate() {
|
2021-02-24 18:09:33 -06:00
|
|
|
// Constant arguments need to be treated as AnonConst since
|
|
|
|
// that is how they will be later lowered to HIR.
|
2021-02-24 00:46:30 -06:00
|
|
|
if const_args.contains(&idx) {
|
|
|
|
self.with_constant_rib(
|
|
|
|
IsRepeatExpr::No,
|
2022-05-02 10:36:48 -05:00
|
|
|
if argument.is_potential_trivial_const_param() {
|
|
|
|
HasGenericParams::Yes
|
|
|
|
} else {
|
|
|
|
HasGenericParams::No
|
|
|
|
},
|
2021-02-24 00:46:30 -06:00
|
|
|
None,
|
|
|
|
|this| {
|
|
|
|
this.resolve_expr(argument, None);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
self.resolve_expr(argument, None);
|
|
|
|
}
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
}
|
2020-07-09 15:49:55 -05:00
|
|
|
ExprKind::Type(ref type_expr, ref ty) => {
|
|
|
|
// `ParseSess::type_ascription_path_suggestions` keeps spans of colon tokens in
|
|
|
|
// type ascription. Here we are trying to retrieve the span of the colon token as
|
|
|
|
// well, but only if it's written without spaces `expr:Ty` and therefore confusable
|
|
|
|
// with `expr::Ty`, only in this case it will match the span from
|
|
|
|
// `type_ascription_path_suggestions`.
|
|
|
|
self.diagnostic_metadata
|
|
|
|
.current_type_ascription
|
|
|
|
.push(type_expr.span.between(ty.span));
|
2019-08-07 18:39:02 -05:00
|
|
|
visit::walk_expr(self, expr);
|
2019-10-19 12:13:56 -05:00
|
|
|
self.diagnostic_metadata.current_type_ascription.pop();
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
// `async |x| ...` gets desugared to `|x| future_from_generator(|| ...)`, so we need to
|
|
|
|
// resolve the arguments within the proper scopes so that usages of them inside the
|
|
|
|
// closure are detected as upvars rather than normal closure arg usages.
|
2022-06-02 11:15:05 -05:00
|
|
|
ExprKind::Closure(_, _, Async::Yes { .. }, _, ref fn_decl, ref body, _span) => {
|
2019-08-28 01:48:10 -05:00
|
|
|
self.with_rib(ValueNS, NormalRibKind, |this| {
|
2020-06-25 09:16:38 -05:00
|
|
|
this.with_label_rib(ClosureOrAsyncRibKind, |this| {
|
|
|
|
// Resolve arguments:
|
|
|
|
this.resolve_params(&fn_decl.inputs);
|
|
|
|
// No need to resolve return type --
|
|
|
|
// the outer closure return type is `FnRetTy::Default`.
|
2019-08-28 01:48:10 -05:00
|
|
|
|
2020-06-25 09:16:38 -05:00
|
|
|
// Now resolve the inner closure
|
|
|
|
{
|
|
|
|
// No need to resolve arguments: the inner closure has none.
|
|
|
|
// Resolve the return type:
|
|
|
|
visit::walk_fn_ret_ty(this, &fn_decl.output);
|
|
|
|
// Resolve the body
|
|
|
|
this.visit_expr(body);
|
|
|
|
}
|
|
|
|
})
|
2019-08-28 01:48:10 -05:00
|
|
|
});
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
2022-06-23 03:21:23 -05:00
|
|
|
// For closures, ClosureOrAsyncRibKind is added in visit_fn
|
2022-07-12 04:34:24 -05:00
|
|
|
ExprKind::Closure(ClosureBinder::For { ref generic_params, span }, ..) => {
|
|
|
|
self.with_generic_param_rib(
|
|
|
|
&generic_params,
|
|
|
|
NormalRibKind,
|
|
|
|
LifetimeRibKind::Generics {
|
|
|
|
binder: expr.id,
|
2022-07-03 10:15:39 -05:00
|
|
|
kind: LifetimeBinderKind::Closure,
|
2022-07-12 04:34:24 -05:00
|
|
|
span,
|
|
|
|
},
|
|
|
|
|this| visit::walk_expr(this, expr),
|
|
|
|
);
|
|
|
|
}
|
2022-06-23 03:21:23 -05:00
|
|
|
ExprKind::Closure(..) => visit::walk_expr(self, expr),
|
|
|
|
ExprKind::Async(..) => {
|
2020-06-25 09:16:38 -05:00
|
|
|
self.with_label_rib(ClosureOrAsyncRibKind, |this| visit::walk_expr(this, expr));
|
|
|
|
}
|
2020-10-22 03:32:41 -05:00
|
|
|
ExprKind::Repeat(ref elem, ref ct) => {
|
|
|
|
self.visit_expr(elem);
|
2022-03-31 13:10:37 -05:00
|
|
|
self.with_lifetime_rib(LifetimeRibKind::AnonConst, |this| {
|
2022-04-18 10:51:39 -05:00
|
|
|
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| {
|
|
|
|
this.resolve_anon_const(ct, IsRepeatExpr::Yes)
|
|
|
|
})
|
2022-03-31 13:10:37 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
ExprKind::ConstBlock(ref ct) => {
|
2022-04-29 09:27:59 -05:00
|
|
|
self.resolve_inline_const(ct);
|
2020-10-22 03:32:41 -05:00
|
|
|
}
|
2022-01-18 20:27:15 -06:00
|
|
|
ExprKind::Index(ref elem, ref idx) => {
|
|
|
|
self.resolve_expr(elem, Some(expr));
|
|
|
|
self.visit_expr(idx);
|
|
|
|
}
|
2019-08-07 18:39:02 -05:00
|
|
|
_ => {
|
|
|
|
visit::walk_expr(self, expr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 01:01:21 -06:00
|
|
|
fn record_candidate_traits_for_expr_if_necessary(&mut self, expr: &'ast Expr) {
|
2019-09-26 08:39:48 -05:00
|
|
|
match expr.kind {
|
2019-08-07 18:39:02 -05:00
|
|
|
ExprKind::Field(_, ident) => {
|
|
|
|
// FIXME(#6890): Even though you can't treat a method like a
|
|
|
|
// field, we need to add any trait methods we find that match
|
|
|
|
// the field name so that we can do some nice error reporting
|
|
|
|
// later on in typeck.
|
2021-01-06 14:50:02 -06:00
|
|
|
let traits = self.traits_in_scope(ident, ValueNS);
|
2021-07-16 07:42:26 -05:00
|
|
|
self.r.trait_map.insert(expr.id, traits);
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
ExprKind::MethodCall(ref segment, ..) => {
|
|
|
|
debug!("(recording candidate traits for expr) recording traits for {}", expr.id);
|
2021-01-06 14:50:02 -06:00
|
|
|
let traits = self.traits_in_scope(segment.ident, ValueNS);
|
2021-07-16 07:42:26 -05:00
|
|
|
self.r.trait_map.insert(expr.id, traits);
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Nothing to do.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-06 14:50:02 -06:00
|
|
|
fn traits_in_scope(&mut self, ident: Ident, ns: Namespace) -> Vec<TraitCandidate> {
|
|
|
|
self.r.traits_in_scope(
|
|
|
|
self.current_trait_ref.as_ref().map(|(module, _)| *module),
|
|
|
|
&self.parent_scope,
|
|
|
|
ident.span.ctxt(),
|
|
|
|
Some((ident.name, ns)),
|
|
|
|
)
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
2021-11-18 12:59:26 -06:00
|
|
|
}
|
2021-07-14 11:04:56 -05:00
|
|
|
|
2021-11-18 12:59:26 -06:00
|
|
|
struct LifetimeCountVisitor<'a, 'b> {
|
|
|
|
r: &'b mut Resolver<'a>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Walks the whole crate in DFS order, visiting each item, counting the declared number of
|
|
|
|
/// lifetime generic parameters.
|
|
|
|
impl<'ast> Visitor<'ast> for LifetimeCountVisitor<'_, '_> {
|
|
|
|
fn visit_item(&mut self, item: &'ast Item) {
|
|
|
|
match &item.kind {
|
|
|
|
ItemKind::TyAlias(box TyAlias { ref generics, .. })
|
|
|
|
| ItemKind::Fn(box Fn { ref generics, .. })
|
|
|
|
| ItemKind::Enum(_, ref generics)
|
|
|
|
| ItemKind::Struct(_, ref generics)
|
|
|
|
| ItemKind::Union(_, ref generics)
|
|
|
|
| ItemKind::Impl(box Impl { ref generics, .. })
|
|
|
|
| ItemKind::Trait(box Trait { ref generics, .. })
|
|
|
|
| ItemKind::TraitAlias(ref generics, _) => {
|
|
|
|
let def_id = self.r.local_def_id(item.id);
|
|
|
|
let count = generics
|
|
|
|
.params
|
|
|
|
.iter()
|
|
|
|
.filter(|param| matches!(param.kind, ast::GenericParamKind::Lifetime { .. }))
|
|
|
|
.count();
|
|
|
|
self.r.item_generics_num_lifetimes.insert(def_id, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemKind::Mod(..)
|
|
|
|
| ItemKind::ForeignMod(..)
|
|
|
|
| ItemKind::Static(..)
|
|
|
|
| ItemKind::Const(..)
|
|
|
|
| ItemKind::Use(..)
|
|
|
|
| ItemKind::ExternCrate(..)
|
|
|
|
| ItemKind::MacroDef(..)
|
|
|
|
| ItemKind::GlobalAsm(..)
|
|
|
|
| ItemKind::MacCall(..) => {}
|
|
|
|
}
|
|
|
|
visit::walk_item(self, item)
|
2021-07-14 11:04:56 -05:00
|
|
|
}
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Resolver<'a> {
|
2020-07-10 15:55:06 -05:00
|
|
|
pub(crate) fn late_resolve_crate(&mut self, krate: &Crate) {
|
2021-11-18 12:59:26 -06:00
|
|
|
visit::walk_crate(&mut LifetimeCountVisitor { r: self }, krate);
|
2020-07-10 15:55:06 -05:00
|
|
|
let mut late_resolution_visitor = LateResolutionVisitor::new(self);
|
2019-08-07 18:39:02 -05:00
|
|
|
visit::walk_crate(&mut late_resolution_visitor, krate);
|
2019-10-19 12:13:56 -05:00
|
|
|
for (id, span) in late_resolution_visitor.diagnostic_metadata.unused_labels.iter() {
|
2019-10-25 08:15:33 -05:00
|
|
|
self.lint_buffer.buffer_lint(lint::builtin::UNUSED_LABELS, *id, *span, "unused label");
|
2019-08-07 18:39:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|