2020-05-20 17:35:58 +00:00
|
|
|
|
// ignore-tidy-filelength
|
|
|
|
|
|
2019-08-12 23:46:42 +00:00
|
|
|
|
//! This crate is responsible for the part of name resolution that doesn't require type checker.
|
|
|
|
|
//!
|
|
|
|
|
//! Module structure of the crate is built here.
|
|
|
|
|
//! Paths in macros, imports, expressions, types, patterns are resolved here.
|
2020-01-12 10:29:00 +00:00
|
|
|
|
//! Label and lifetime names are resolved here as well.
|
2019-08-12 23:46:42 +00:00
|
|
|
|
//!
|
|
|
|
|
//! Type-relative name resolution (methods, fields, associated items) happens in `librustc_typeck`.
|
2014-12-18 22:46:26 +00:00
|
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
|
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
|
2019-10-08 00:14:42 +00:00
|
|
|
|
#![feature(bool_to_option)]
|
2018-05-22 15:10:17 +00:00
|
|
|
|
#![feature(crate_visibility_modifier)]
|
2019-02-10 07:13:30 +00:00
|
|
|
|
#![feature(nll)]
|
2020-03-30 06:40:23 +00:00
|
|
|
|
#![feature(or_patterns)]
|
2019-12-24 22:38:22 +00:00
|
|
|
|
#![recursion_limit = "256"]
|
2018-12-13 15:57:25 +00:00
|
|
|
|
|
2020-01-05 01:37:57 +00:00
|
|
|
|
pub use rustc_hir::def::{Namespace, PerNS};
|
2018-06-13 16:44:06 +00:00
|
|
|
|
|
2019-07-03 08:44:57 +00:00
|
|
|
|
use Determinacy::*;
|
2014-11-06 08:05:53 +00:00
|
|
|
|
|
2020-06-02 17:19:49 +00:00
|
|
|
|
use rustc_arena::TypedArena;
|
2020-05-24 22:39:39 +00:00
|
|
|
|
use rustc_ast::node_id::NodeMap;
|
2020-02-29 17:37:32 +00:00
|
|
|
|
use rustc_ast::unwrap_or;
|
|
|
|
|
use rustc_ast::visit::{self, Visitor};
|
2020-04-27 17:56:11 +00:00
|
|
|
|
use rustc_ast::{self as ast, FloatTy, IntTy, NodeId, UintTy};
|
|
|
|
|
use rustc_ast::{Crate, CRATE_NODE_ID};
|
|
|
|
|
use rustc_ast::{ItemKind, Path};
|
2020-06-27 20:51:28 +00:00
|
|
|
|
use rustc_ast_lowering::ResolverAstLowering;
|
2020-01-11 16:02:46 +00:00
|
|
|
|
use rustc_ast_pretty::pprust;
|
2019-12-24 04:02:53 +00:00
|
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
|
|
|
|
|
use rustc_data_structures::ptr_key::PtrKey;
|
|
|
|
|
use rustc_data_structures::sync::Lrc;
|
2020-01-09 10:18:47 +00:00
|
|
|
|
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
|
2019-12-29 14:23:55 +00:00
|
|
|
|
use rustc_expand::base::SyntaxExtension;
|
2020-01-05 01:37:57 +00:00
|
|
|
|
use rustc_hir::def::Namespace::*;
|
2020-01-12 10:29:00 +00:00
|
|
|
|
use rustc_hir::def::{self, CtorOf, DefKind, NonMacroAttrKind, PartialRes};
|
2020-04-07 23:29:50 +00:00
|
|
|
|
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX};
|
2020-06-20 18:59:29 +00:00
|
|
|
|
use rustc_hir::definitions::{DefKey, DefPathData, Definitions};
|
2020-01-05 01:37:57 +00:00
|
|
|
|
use rustc_hir::PrimTy::{self, Bool, Char, Float, Int, Str, Uint};
|
2020-06-14 21:35:29 +00:00
|
|
|
|
use rustc_hir::TraitCandidate;
|
2020-06-20 18:59:29 +00:00
|
|
|
|
use rustc_index::vec::IndexVec;
|
2019-12-24 04:02:53 +00:00
|
|
|
|
use rustc_metadata::creader::{CStore, CrateLoader};
|
2020-03-29 15:19:48 +00:00
|
|
|
|
use rustc_middle::hir::exports::ExportMap;
|
|
|
|
|
use rustc_middle::middle::cstore::{CrateStore, MetadataLoaderDyn};
|
|
|
|
|
use rustc_middle::ty::query::Providers;
|
|
|
|
|
use rustc_middle::ty::{self, DefIdTree, ResolverOutputs};
|
2020-08-08 17:06:45 +00:00
|
|
|
|
use rustc_middle::{bug, span_bug};
|
2020-03-11 11:49:08 +00:00
|
|
|
|
use rustc_session::lint;
|
2020-01-05 08:40:16 +00:00
|
|
|
|
use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer};
|
|
|
|
|
use rustc_session::Session;
|
2019-12-31 17:15:40 +00:00
|
|
|
|
use rustc_span::hygiene::{ExpnId, ExpnKind, MacroKind, SyntaxContext, Transparency};
|
2020-01-01 18:25:28 +00:00
|
|
|
|
use rustc_span::source_map::Spanned;
|
2020-04-19 11:00:18 +00:00
|
|
|
|
use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
2019-12-31 17:15:40 +00:00
|
|
|
|
use rustc_span::{Span, DUMMY_SP};
|
2016-06-21 22:08:13 +00:00
|
|
|
|
|
2020-08-08 17:06:45 +00:00
|
|
|
|
use smallvec::{smallvec, SmallVec};
|
2019-12-24 22:38:22 +00:00
|
|
|
|
use std::cell::{Cell, RefCell};
|
|
|
|
|
use std::collections::BTreeSet;
|
|
|
|
|
use std::{cmp, fmt, iter, ptr};
|
2020-08-14 06:05:01 +00:00
|
|
|
|
use tracing::debug;
|
2012-05-22 17:54:12 +00:00
|
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
|
use diagnostics::{extend_span_to_previous_binding, find_span_of_binding_until_next_binding};
|
2020-06-25 14:16:38 +00:00
|
|
|
|
use diagnostics::{ImportSuggestion, LabelSuggestion, Suggestion};
|
2020-03-07 15:49:13 +00:00
|
|
|
|
use imports::{Import, ImportKind, ImportResolver, NameResolution};
|
2019-10-05 15:55:58 +00:00
|
|
|
|
use late::{HasGenericParams, PathSource, Rib, RibKind::*};
|
2020-03-13 22:06:36 +00:00
|
|
|
|
use macros::{MacroRulesBinding, MacroRulesScope};
|
2015-03-15 21:44:19 +00:00
|
|
|
|
|
2019-04-20 16:36:05 +00:00
|
|
|
|
type Res = def::Res<NodeId>;
|
2019-04-03 07:07:45 +00:00
|
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
|
mod build_reduced_graph;
|
|
|
|
|
mod check_unused;
|
2019-11-23 15:19:57 +00:00
|
|
|
|
mod def_collector;
|
2016-03-17 01:05:29 +00:00
|
|
|
|
mod diagnostics;
|
2019-12-29 16:42:23 +00:00
|
|
|
|
mod imports;
|
2019-08-07 23:39:02 +00:00
|
|
|
|
mod late;
|
2016-09-07 23:21:59 +00:00
|
|
|
|
mod macros;
|
2014-12-19 07:13:54 +00:00
|
|
|
|
|
2018-11-17 17:13:25 +00:00
|
|
|
|
enum Weak {
|
|
|
|
|
Yes,
|
|
|
|
|
No,
|
2018-11-08 22:29:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 08:44:57 +00:00
|
|
|
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
|
|
|
|
pub enum Determinacy {
|
|
|
|
|
Determined,
|
|
|
|
|
Undetermined,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Determinacy {
|
|
|
|
|
fn determined(determined: bool) -> Determinacy {
|
|
|
|
|
if determined { Determinacy::Determined } else { Determinacy::Undetermined }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 18:45:43 +00:00
|
|
|
|
/// A specific scope in which a name can be looked up.
|
|
|
|
|
/// This enum is currently used only for early resolution (imports and macros),
|
|
|
|
|
/// but not for late resolution yet.
|
2019-07-11 20:05:35 +00:00
|
|
|
|
#[derive(Clone, Copy)]
|
2019-07-11 18:45:43 +00:00
|
|
|
|
enum Scope<'a> {
|
2019-10-03 22:53:20 +00:00
|
|
|
|
DeriveHelpers(ExpnId),
|
2019-10-03 22:44:57 +00:00
|
|
|
|
DeriveHelpersCompat,
|
2020-03-13 22:06:36 +00:00
|
|
|
|
MacroRules(MacroRulesScope<'a>),
|
2019-07-11 18:45:43 +00:00
|
|
|
|
CrateRoot,
|
|
|
|
|
Module(Module<'a>),
|
2019-11-03 17:28:20 +00:00
|
|
|
|
RegisteredAttrs,
|
2019-07-11 18:45:43 +00:00
|
|
|
|
MacroUsePrelude,
|
|
|
|
|
BuiltinAttrs,
|
|
|
|
|
ExternPrelude,
|
|
|
|
|
ToolPrelude,
|
|
|
|
|
StdLibPrelude,
|
|
|
|
|
BuiltinTypes,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Names from different contexts may want to visit different subsets of all specific scopes
|
|
|
|
|
/// with different restrictions when looking up the resolution.
|
|
|
|
|
/// This enum is currently used only for early resolution (imports and macros),
|
|
|
|
|
/// but not for late resolution yet.
|
2018-11-24 16:14:05 +00:00
|
|
|
|
enum ScopeSet {
|
2019-08-09 22:40:05 +00:00
|
|
|
|
/// All scopes with the given namespace.
|
|
|
|
|
All(Namespace, /*is_import*/ bool),
|
|
|
|
|
/// Crate root, then extern prelude (used for mixed 2015-2018 mode in macros).
|
2018-11-24 21:25:03 +00:00
|
|
|
|
AbsolutePath(Namespace),
|
2019-08-09 22:40:05 +00:00
|
|
|
|
/// All scopes with macro namespace and the given macro kind restriction.
|
2018-11-24 16:14:05 +00:00
|
|
|
|
Macro(MacroKind),
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 18:45:43 +00:00
|
|
|
|
/// Everything you need to know about a name's location to resolve it.
|
|
|
|
|
/// Serves as a starting point for the scope visitor.
|
|
|
|
|
/// This struct is currently used only for early resolution (imports and macros),
|
|
|
|
|
/// but not for late resolution yet.
|
2019-08-12 22:39:10 +00:00
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
2019-07-11 18:45:43 +00:00
|
|
|
|
pub struct ParentScope<'a> {
|
|
|
|
|
module: Module<'a>,
|
2019-07-15 22:04:05 +00:00
|
|
|
|
expansion: ExpnId,
|
2020-03-13 22:06:36 +00:00
|
|
|
|
macro_rules: MacroRulesScope<'a>,
|
2019-08-12 22:39:10 +00:00
|
|
|
|
derives: &'a [ast::Path],
|
2019-07-11 18:45:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 20:19:36 +00:00
|
|
|
|
impl<'a> ParentScope<'a> {
|
2019-08-15 17:47:15 +00:00
|
|
|
|
/// Creates a parent scope with the passed argument used as the module scope component,
|
|
|
|
|
/// and other scope components set to default empty values.
|
|
|
|
|
pub fn module(module: Module<'a>) -> ParentScope<'a> {
|
2020-03-13 22:06:36 +00:00
|
|
|
|
ParentScope {
|
|
|
|
|
module,
|
|
|
|
|
expansion: ExpnId::root(),
|
|
|
|
|
macro_rules: MacroRulesScope::Empty,
|
|
|
|
|
derives: &[],
|
|
|
|
|
}
|
2019-08-12 20:19:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Clean up "pattern doesn't bind x" messages
Group "missing variable bind" spans in `or` matches and clarify wording
for the two possible cases: when a variable from the first pattern is
not in any of the subsequent patterns, and when a variable in any of the
other patterns is not in the first one.
Before:
```
error[E0408]: variable `a` from pattern #1 is not bound in pattern #2
--> file.rs:10:23
|
10 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); }
| ^^^^^^^^^^^ pattern doesn't bind `a`
error[E0408]: variable `b` from pattern #2 is not bound in pattern #1
--> file.rs:10:32
|
10 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); }
| ^ pattern doesn't bind `b`
error[E0408]: variable `a` from pattern #1 is not bound in pattern #3
--> file.rs:10:37
|
10 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); }
| ^^^^^^^^ pattern doesn't bind `a`
error[E0408]: variable `d` from pattern #1 is not bound in pattern #3
--> file.rs:10:37
|
10 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); }
| ^^^^^^^^ pattern doesn't bind `d`
error[E0408]: variable `c` from pattern #3 is not bound in pattern #1
--> file.rs:10:43
|
10 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); }
| ^ pattern doesn't bind `c`
error[E0408]: variable `d` from pattern #1 is not bound in pattern #4
--> file.rs:10:48
|
10 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); }
| ^^^^^^^^ pattern doesn't bind `d`
error: aborting due to 6 previous errors
```
After:
```
error[E0408]: variable `a` is not bound in all patterns
--> file.rs:20:37
|
20 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => {
intln!("{:?}", a); }
| - ^^^^^^^^^^^ ^^^^^^^^ - variable
t in all patterns
| | | |
| | | pattern doesn't bind `a`
| | pattern doesn't bind `a`
| variable not in all patterns
error[E0408]: variable `d` is not bound in all patterns
--> file.rs:20:37
|
20 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => {
intln!("{:?}", a); }
| - - ^^^^^^^^ ^^^^^^^^ pattern
esn't bind `d`
| | | |
| | | pattern doesn't bind `d`
| | variable not in all patterns
| variable not in all patterns
error[E0408]: variable `b` is not bound in all patterns
--> file.rs:20:37
|
20 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => {
intln!("{:?}", a); }
| ^^^^^^^^^^^ - ^^^^^^^^ ^^^^^^^^ pattern
esn't bind `b`
| | | |
| | | pattern doesn't bind `b`
| | variable not in all patterns
| pattern doesn't bind `b`
error[E0408]: variable `c` is not bound in all patterns
--> file.rs:20:48
|
20 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => {
intln!("{:?}", a); }
| ^^^^^^^^^^^ ^^^^^^^^^^^ - ^^^^^^^^ pattern
esn't bind `c`
| | | |
| | | variable not in all
tterns
| | pattern doesn't bind `c`
| pattern doesn't bind `c`
error: aborting due to 4 previous errors
```
* Have only one presentation for binding consistency errors
* Point to same binding in multiple patterns when possible
* Check inconsistent bindings in all arms
* Simplify wording of diagnostic message
* Sort emition and spans of binding errors for deterministic output
2017-02-10 01:54:56 +00:00
|
|
|
|
#[derive(Eq)]
|
|
|
|
|
struct BindingError {
|
2020-04-19 11:00:18 +00:00
|
|
|
|
name: Symbol,
|
2017-03-05 23:19:05 +00:00
|
|
|
|
origin: BTreeSet<Span>,
|
|
|
|
|
target: BTreeSet<Span>,
|
2019-12-24 22:38:22 +00:00
|
|
|
|
could_be_path: bool,
|
Clean up "pattern doesn't bind x" messages
Group "missing variable bind" spans in `or` matches and clarify wording
for the two possible cases: when a variable from the first pattern is
not in any of the subsequent patterns, and when a variable in any of the
other patterns is not in the first one.
Before:
```
error[E0408]: variable `a` from pattern #1 is not bound in pattern #2
--> file.rs:10:23
|
10 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); }
| ^^^^^^^^^^^ pattern doesn't bind `a`
error[E0408]: variable `b` from pattern #2 is not bound in pattern #1
--> file.rs:10:32
|
10 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); }
| ^ pattern doesn't bind `b`
error[E0408]: variable `a` from pattern #1 is not bound in pattern #3
--> file.rs:10:37
|
10 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); }
| ^^^^^^^^ pattern doesn't bind `a`
error[E0408]: variable `d` from pattern #1 is not bound in pattern #3
--> file.rs:10:37
|
10 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); }
| ^^^^^^^^ pattern doesn't bind `d`
error[E0408]: variable `c` from pattern #3 is not bound in pattern #1
--> file.rs:10:43
|
10 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); }
| ^ pattern doesn't bind `c`
error[E0408]: variable `d` from pattern #1 is not bound in pattern #4
--> file.rs:10:48
|
10 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); }
| ^^^^^^^^ pattern doesn't bind `d`
error: aborting due to 6 previous errors
```
After:
```
error[E0408]: variable `a` is not bound in all patterns
--> file.rs:20:37
|
20 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => {
intln!("{:?}", a); }
| - ^^^^^^^^^^^ ^^^^^^^^ - variable
t in all patterns
| | | |
| | | pattern doesn't bind `a`
| | pattern doesn't bind `a`
| variable not in all patterns
error[E0408]: variable `d` is not bound in all patterns
--> file.rs:20:37
|
20 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => {
intln!("{:?}", a); }
| - - ^^^^^^^^ ^^^^^^^^ pattern
esn't bind `d`
| | | |
| | | pattern doesn't bind `d`
| | variable not in all patterns
| variable not in all patterns
error[E0408]: variable `b` is not bound in all patterns
--> file.rs:20:37
|
20 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => {
intln!("{:?}", a); }
| ^^^^^^^^^^^ - ^^^^^^^^ ^^^^^^^^ pattern
esn't bind `b`
| | | |
| | | pattern doesn't bind `b`
| | variable not in all patterns
| pattern doesn't bind `b`
error[E0408]: variable `c` is not bound in all patterns
--> file.rs:20:48
|
20 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => {
intln!("{:?}", a); }
| ^^^^^^^^^^^ ^^^^^^^^^^^ - ^^^^^^^^ pattern
esn't bind `c`
| | | |
| | | variable not in all
tterns
| | pattern doesn't bind `c`
| pattern doesn't bind `c`
error: aborting due to 4 previous errors
```
* Have only one presentation for binding consistency errors
* Point to same binding in multiple patterns when possible
* Check inconsistent bindings in all arms
* Simplify wording of diagnostic message
* Sort emition and spans of binding errors for deterministic output
2017-02-10 01:54:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PartialOrd for BindingError {
|
|
|
|
|
fn partial_cmp(&self, other: &BindingError) -> Option<cmp::Ordering> {
|
|
|
|
|
Some(self.cmp(other))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PartialEq for BindingError {
|
|
|
|
|
fn eq(&self, other: &BindingError) -> bool {
|
|
|
|
|
self.name == other.name
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Ord for BindingError {
|
|
|
|
|
fn cmp(&self, other: &BindingError) -> cmp::Ordering {
|
|
|
|
|
self.name.cmp(&other.name)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-17 01:05:29 +00:00
|
|
|
|
enum ResolutionError<'a> {
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Error E0401: can't use type or const parameters from outer function.
|
2019-10-05 15:55:58 +00:00
|
|
|
|
GenericParamsFromOuterFunction(Res, HasGenericParams),
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Error E0403: the name is already used for a type or const parameter in this generic
|
|
|
|
|
/// parameter list.
|
2020-04-19 11:00:18 +00:00
|
|
|
|
NameAlreadyUsedInParameterList(Symbol, Span),
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Error E0407: method is not a member of trait.
|
2020-04-19 11:00:18 +00:00
|
|
|
|
MethodNotMemberOfTrait(Symbol, &'a str),
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Error E0437: type is not a member of trait.
|
2020-04-19 11:00:18 +00:00
|
|
|
|
TypeNotMemberOfTrait(Symbol, &'a str),
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Error E0438: const is not a member of trait.
|
2020-04-19 11:00:18 +00:00
|
|
|
|
ConstNotMemberOfTrait(Symbol, &'a str),
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Error E0408: variable `{}` is not bound in all patterns.
|
Clean up "pattern doesn't bind x" messages
Group "missing variable bind" spans in `or` matches and clarify wording
for the two possible cases: when a variable from the first pattern is
not in any of the subsequent patterns, and when a variable in any of the
other patterns is not in the first one.
Before:
```
error[E0408]: variable `a` from pattern #1 is not bound in pattern #2
--> file.rs:10:23
|
10 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); }
| ^^^^^^^^^^^ pattern doesn't bind `a`
error[E0408]: variable `b` from pattern #2 is not bound in pattern #1
--> file.rs:10:32
|
10 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); }
| ^ pattern doesn't bind `b`
error[E0408]: variable `a` from pattern #1 is not bound in pattern #3
--> file.rs:10:37
|
10 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); }
| ^^^^^^^^ pattern doesn't bind `a`
error[E0408]: variable `d` from pattern #1 is not bound in pattern #3
--> file.rs:10:37
|
10 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); }
| ^^^^^^^^ pattern doesn't bind `d`
error[E0408]: variable `c` from pattern #3 is not bound in pattern #1
--> file.rs:10:43
|
10 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); }
| ^ pattern doesn't bind `c`
error[E0408]: variable `d` from pattern #1 is not bound in pattern #4
--> file.rs:10:48
|
10 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); }
| ^^^^^^^^ pattern doesn't bind `d`
error: aborting due to 6 previous errors
```
After:
```
error[E0408]: variable `a` is not bound in all patterns
--> file.rs:20:37
|
20 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => {
intln!("{:?}", a); }
| - ^^^^^^^^^^^ ^^^^^^^^ - variable
t in all patterns
| | | |
| | | pattern doesn't bind `a`
| | pattern doesn't bind `a`
| variable not in all patterns
error[E0408]: variable `d` is not bound in all patterns
--> file.rs:20:37
|
20 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => {
intln!("{:?}", a); }
| - - ^^^^^^^^ ^^^^^^^^ pattern
esn't bind `d`
| | | |
| | | pattern doesn't bind `d`
| | variable not in all patterns
| variable not in all patterns
error[E0408]: variable `b` is not bound in all patterns
--> file.rs:20:37
|
20 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => {
intln!("{:?}", a); }
| ^^^^^^^^^^^ - ^^^^^^^^ ^^^^^^^^ pattern
esn't bind `b`
| | | |
| | | pattern doesn't bind `b`
| | variable not in all patterns
| pattern doesn't bind `b`
error[E0408]: variable `c` is not bound in all patterns
--> file.rs:20:48
|
20 | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => {
intln!("{:?}", a); }
| ^^^^^^^^^^^ ^^^^^^^^^^^ - ^^^^^^^^ pattern
esn't bind `c`
| | | |
| | | variable not in all
tterns
| | pattern doesn't bind `c`
| pattern doesn't bind `c`
error: aborting due to 4 previous errors
```
* Have only one presentation for binding consistency errors
* Point to same binding in multiple patterns when possible
* Check inconsistent bindings in all arms
* Simplify wording of diagnostic message
* Sort emition and spans of binding errors for deterministic output
2017-02-10 01:54:56 +00:00
|
|
|
|
VariableNotBoundInPattern(&'a BindingError),
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Error E0409: variable `{}` is bound in inconsistent ways within the same match arm.
|
2020-04-19 11:00:18 +00:00
|
|
|
|
VariableBoundWithDifferentMode(Symbol, Span),
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Error E0415: identifier is bound more than once in this parameter list.
|
2020-07-08 10:03:37 +00:00
|
|
|
|
IdentifierBoundMoreThanOnceInParameterList(Symbol),
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Error E0416: identifier is bound more than once in the same pattern.
|
2020-07-08 10:03:37 +00:00
|
|
|
|
IdentifierBoundMoreThanOnceInSamePattern(Symbol),
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Error E0426: use of undeclared label.
|
2020-07-08 10:03:37 +00:00
|
|
|
|
UndeclaredLabel { name: Symbol, suggestion: Option<LabelSuggestion> },
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Error E0429: `self` imports are only allowed within a `{ }` list.
|
2020-05-03 16:54:21 +00:00
|
|
|
|
SelfImportsOnlyAllowedWithin { root: bool, span_with_rename: Span },
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Error E0430: `self` import can only appear once in the list.
|
2015-07-14 14:32:43 +00:00
|
|
|
|
SelfImportCanOnlyAppearOnceInTheList,
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Error E0431: `self` import can only appear in an import list with a non-empty prefix.
|
2015-07-14 14:32:43 +00:00
|
|
|
|
SelfImportOnlyInImportListWithNonEmptyPrefix,
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Error E0433: failed to resolve.
|
2019-01-16 20:30:41 +00:00
|
|
|
|
FailedToResolve { label: String, suggestion: Option<Suggestion> },
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Error E0434: can't capture dynamic environment in a fn item.
|
2015-07-14 14:32:43 +00:00
|
|
|
|
CannotCaptureDynamicEnvironmentInFnItem,
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Error E0435: attempt to use a non-constant value in a constant.
|
2015-07-14 14:32:43 +00:00
|
|
|
|
AttemptToUseNonConstantValueInConstant,
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Error E0530: `X` bindings cannot shadow `Y`s.
|
2020-07-08 10:03:37 +00:00
|
|
|
|
BindingShadowsSomethingUnacceptable(&'static str, Symbol, &'a NameBinding<'a>),
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Error E0128: type parameters with a default cannot use forward-declared identifiers.
|
2019-02-05 15:50:55 +00:00
|
|
|
|
ForwardDeclaredTyParam, // FIXME(const_generics:defaults)
|
2020-07-08 20:16:18 +00:00
|
|
|
|
/// ERROR E0770: the type of const parameters must not depend on other generic parameters.
|
2020-07-18 20:35:50 +00:00
|
|
|
|
ParamInTyOfConstParam(Symbol),
|
2020-07-18 21:42:10 +00:00
|
|
|
|
/// constant values inside of type parameter defaults must not depend on generic parameters.
|
|
|
|
|
ParamInAnonConstInTyDefault(Symbol),
|
2020-07-28 13:55:42 +00:00
|
|
|
|
/// generic parameters must not be used inside of non trivial constant values.
|
|
|
|
|
///
|
|
|
|
|
/// This error is only emitted when using `min_const_generics`.
|
|
|
|
|
ParamInNonTrivialAnonConst(Symbol),
|
2019-09-27 13:21:02 +00:00
|
|
|
|
/// Error E0735: type parameters with a default cannot use `Self`
|
|
|
|
|
SelfInTyParamDefault,
|
2020-06-25 14:16:38 +00:00
|
|
|
|
/// Error E0767: use of unreachable label
|
2020-07-08 10:03:37 +00:00
|
|
|
|
UnreachableLabel { name: Symbol, definition_span: Span, suggestion: Option<LabelSuggestion> },
|
2015-12-10 23:00:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-06 22:49:21 +00:00
|
|
|
|
enum VisResolutionError<'a> {
|
|
|
|
|
Relative2018(Span, &'a ast::Path),
|
|
|
|
|
AncestorOnly(Span),
|
|
|
|
|
FailedToResolve(Span, String, Option<Suggestion>),
|
|
|
|
|
ExpectedFound(Span, String, Res),
|
|
|
|
|
Indeterminate(Span),
|
|
|
|
|
ModuleOnly(Span),
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-13 18:12:29 +00:00
|
|
|
|
/// A minimal representation of a path segment. We use this in resolve because we synthesize 'path
|
|
|
|
|
/// segments' which don't have the rest of an AST or HIR `PathSegment`.
|
2018-09-12 03:21:50 +00:00
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
|
pub struct Segment {
|
|
|
|
|
ident: Ident,
|
|
|
|
|
id: Option<NodeId>,
|
2020-06-13 18:12:29 +00:00
|
|
|
|
/// Signals whether this `PathSegment` has generic arguments. Used to avoid providing
|
|
|
|
|
/// nonsensical suggestions.
|
2020-06-17 23:29:03 +00:00
|
|
|
|
has_generic_args: bool,
|
2018-09-12 03:21:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Segment {
|
|
|
|
|
fn from_path(path: &Path) -> Vec<Segment> {
|
|
|
|
|
path.segments.iter().map(|s| s.into()).collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn from_ident(ident: Ident) -> Segment {
|
2020-06-17 23:29:03 +00:00
|
|
|
|
Segment { ident, id: None, has_generic_args: false }
|
2018-09-12 03:21:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn names_to_string(segments: &[Segment]) -> String {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
names_to_string(&segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>())
|
2018-09-12 03:21:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> From<&'a ast::PathSegment> for Segment {
|
|
|
|
|
fn from(seg: &'a ast::PathSegment) -> Segment {
|
2020-06-17 23:29:03 +00:00
|
|
|
|
Segment { ident: seg.ident, id: Some(seg.id), has_generic_args: seg.args.is_some() }
|
2018-09-12 03:21:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-20 18:59:29 +00:00
|
|
|
|
struct UsePlacementFinder {
|
|
|
|
|
target_module: NodeId,
|
2017-08-17 09:03:59 +00:00
|
|
|
|
span: Option<Span>,
|
2017-08-18 10:46:28 +00:00
|
|
|
|
found_use: bool,
|
2017-08-17 09:03:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-20 18:59:29 +00:00
|
|
|
|
impl UsePlacementFinder {
|
|
|
|
|
fn check(krate: &Crate, target_module: NodeId) -> (Option<Span>, bool) {
|
|
|
|
|
let mut finder = UsePlacementFinder { target_module, span: None, found_use: false };
|
|
|
|
|
visit::walk_crate(&mut finder, krate);
|
|
|
|
|
(finder.span, finder.found_use)
|
2017-11-16 12:14:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-20 18:59:29 +00:00
|
|
|
|
impl<'tcx> Visitor<'tcx> for UsePlacementFinder {
|
2017-08-17 09:03:59 +00:00
|
|
|
|
fn visit_mod(
|
|
|
|
|
&mut self,
|
|
|
|
|
module: &'tcx ast::Mod,
|
|
|
|
|
_: Span,
|
|
|
|
|
_: &[ast::Attribute],
|
|
|
|
|
node_id: NodeId,
|
|
|
|
|
) {
|
|
|
|
|
if self.span.is_some() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-06-20 18:59:29 +00:00
|
|
|
|
if node_id != self.target_module {
|
2017-08-17 09:03:59 +00:00
|
|
|
|
visit::walk_mod(self, module);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// find a use statement
|
|
|
|
|
for item in &module.items {
|
2019-09-26 16:51:36 +00:00
|
|
|
|
match item.kind {
|
2017-08-17 09:03:59 +00:00
|
|
|
|
ItemKind::Use(..) => {
|
|
|
|
|
// don't suggest placing a use before the prelude
|
|
|
|
|
// import or other generated ones
|
2019-08-10 22:08:30 +00:00
|
|
|
|
if !item.span.from_expansion() {
|
2018-03-10 14:45:47 +00:00
|
|
|
|
self.span = Some(item.span.shrink_to_lo());
|
2017-08-18 10:46:28 +00:00
|
|
|
|
self.found_use = true;
|
2017-08-17 09:03:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2019-12-24 22:38:22 +00:00
|
|
|
|
}
|
2017-08-17 09:03:59 +00:00
|
|
|
|
// don't place use before extern crate
|
|
|
|
|
ItemKind::ExternCrate(_) => {}
|
|
|
|
|
// but place them before the first other item
|
2019-12-24 22:38:22 +00:00
|
|
|
|
_ => {
|
|
|
|
|
if self.span.map_or(true, |span| item.span < span) {
|
|
|
|
|
if !item.span.from_expansion() {
|
|
|
|
|
// don't insert between attributes and an item
|
|
|
|
|
if item.attrs.is_empty() {
|
|
|
|
|
self.span = Some(item.span.shrink_to_lo());
|
|
|
|
|
} else {
|
|
|
|
|
// find the first attribute on the item
|
|
|
|
|
for attr in &item.attrs {
|
|
|
|
|
if self.span.map_or(true, |span| attr.span < span) {
|
|
|
|
|
self.span = Some(attr.span.shrink_to_lo());
|
|
|
|
|
}
|
2017-08-31 13:45:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-24 22:38:22 +00:00
|
|
|
|
}
|
2017-08-17 09:03:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-18 17:01:33 +00:00
|
|
|
|
/// An intermediate resolution result.
|
|
|
|
|
///
|
2019-04-20 16:36:05 +00:00
|
|
|
|
/// This refers to the thing referred by a name. The difference between `Res` and `Item` is that
|
|
|
|
|
/// items are visible in their whole block, while `Res`es only from the place they are defined
|
2018-02-18 17:01:33 +00:00
|
|
|
|
/// forward.
|
2019-07-14 02:20:28 +00:00
|
|
|
|
#[derive(Debug)]
|
2016-03-12 08:03:13 +00:00
|
|
|
|
enum LexicalScopeBinding<'a> {
|
|
|
|
|
Item(&'a NameBinding<'a>),
|
2019-04-20 16:36:05 +00:00
|
|
|
|
Res(Res),
|
2016-03-12 08:03:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-12 08:17:56 +00:00
|
|
|
|
impl<'a> LexicalScopeBinding<'a> {
|
2019-04-20 16:36:05 +00:00
|
|
|
|
fn res(self) -> Res {
|
2016-12-04 01:18:11 +00:00
|
|
|
|
match self {
|
2019-04-20 16:36:05 +00:00
|
|
|
|
LexicalScopeBinding::Item(binding) => binding.res(),
|
|
|
|
|
LexicalScopeBinding::Res(res) => res,
|
2016-12-04 01:18:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-12 08:17:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-09 13:29:22 +00:00
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
2018-11-08 22:29:07 +00:00
|
|
|
|
enum ModuleOrUniformRoot<'a> {
|
2018-08-09 13:29:22 +00:00
|
|
|
|
/// Regular module.
|
|
|
|
|
Module(Module<'a>),
|
|
|
|
|
|
2018-11-24 21:25:03 +00:00
|
|
|
|
/// Virtual module that denotes resolution in crate root with fallback to extern prelude.
|
|
|
|
|
CrateRootAndExternPrelude,
|
|
|
|
|
|
2018-11-24 16:14:05 +00:00
|
|
|
|
/// Virtual module that denotes resolution in extern prelude.
|
2019-01-13 13:18:00 +00:00
|
|
|
|
/// Used for paths starting with `::` on 2018 edition.
|
2018-11-24 16:14:05 +00:00
|
|
|
|
ExternPrelude,
|
|
|
|
|
|
|
|
|
|
/// Virtual module that denotes resolution in current scope.
|
|
|
|
|
/// Used only for resolving single-segment imports. The reason it exists is that import paths
|
|
|
|
|
/// are always split into two parts, the first of which should be some kind of module.
|
|
|
|
|
CurrentScope,
|
2018-08-09 13:29:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-28 21:15:19 +00:00
|
|
|
|
impl ModuleOrUniformRoot<'_> {
|
|
|
|
|
fn same_def(lhs: Self, rhs: Self) -> bool {
|
|
|
|
|
match (lhs, rhs) {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
(ModuleOrUniformRoot::Module(lhs), ModuleOrUniformRoot::Module(rhs)) => {
|
|
|
|
|
lhs.def_id() == rhs.def_id()
|
|
|
|
|
}
|
|
|
|
|
(
|
|
|
|
|
ModuleOrUniformRoot::CrateRootAndExternPrelude,
|
|
|
|
|
ModuleOrUniformRoot::CrateRootAndExternPrelude,
|
|
|
|
|
)
|
|
|
|
|
| (ModuleOrUniformRoot::ExternPrelude, ModuleOrUniformRoot::ExternPrelude)
|
|
|
|
|
| (ModuleOrUniformRoot::CurrentScope, ModuleOrUniformRoot::CurrentScope) => true,
|
2018-11-10 15:58:37 +00:00
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 05:21:11 +00:00
|
|
|
|
#[derive(Clone, Debug)]
|
2016-11-25 06:07:21 +00:00
|
|
|
|
enum PathResult<'a> {
|
2018-08-09 13:29:22 +00:00
|
|
|
|
Module(ModuleOrUniformRoot<'a>),
|
2019-05-04 12:18:58 +00:00
|
|
|
|
NonModule(PartialRes),
|
2016-11-25 06:07:21 +00:00
|
|
|
|
Indeterminate,
|
2019-01-16 20:30:41 +00:00
|
|
|
|
Failed {
|
|
|
|
|
span: Span,
|
|
|
|
|
label: String,
|
|
|
|
|
suggestion: Option<Suggestion>,
|
|
|
|
|
is_error_from_last_segment: bool,
|
|
|
|
|
},
|
2016-11-25 06:07:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-18 09:45:06 +00:00
|
|
|
|
enum ModuleKind {
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// An anonymous module; e.g., just a block.
|
2018-03-24 16:07:58 +00:00
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// fn main() {
|
|
|
|
|
/// fn f() {} // (1)
|
|
|
|
|
/// { // This is an anonymous module
|
|
|
|
|
/// f(); // This resolves to (2) as we are inside the block.
|
|
|
|
|
/// fn f() {} // (2)
|
|
|
|
|
/// }
|
|
|
|
|
/// f(); // Resolves to (1)
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2016-09-18 09:45:06 +00:00
|
|
|
|
Block(NodeId),
|
2018-03-24 16:07:58 +00:00
|
|
|
|
/// Any module with a name.
|
|
|
|
|
///
|
|
|
|
|
/// This could be:
|
2018-02-18 17:01:33 +00:00
|
|
|
|
///
|
2018-03-24 16:07:58 +00:00
|
|
|
|
/// * A normal module ‒ either `mod from_file;` or `mod from_block { }`.
|
|
|
|
|
/// * A trait or an enum (it implicitly contains associated types, methods and variant
|
|
|
|
|
/// constructors).
|
2020-04-19 11:00:18 +00:00
|
|
|
|
Def(DefKind, DefId, Symbol),
|
2012-05-22 17:54:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-07 17:56:41 +00:00
|
|
|
|
impl ModuleKind {
|
|
|
|
|
/// Get name of the module.
|
2020-04-19 11:00:18 +00:00
|
|
|
|
pub fn name(&self) -> Option<Symbol> {
|
2019-04-07 17:56:41 +00:00
|
|
|
|
match self {
|
|
|
|
|
ModuleKind::Block(..) => None,
|
2019-04-20 16:46:19 +00:00
|
|
|
|
ModuleKind::Def(.., name) => Some(*name),
|
2019-04-07 17:56:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 20:04:26 +00:00
|
|
|
|
/// A key that identifies a binding in a given `Module`.
|
|
|
|
|
///
|
|
|
|
|
/// Multiple bindings in the same module can have the same key (in a valid
|
|
|
|
|
/// program) if all but one of them come from glob imports.
|
2020-03-17 15:45:02 +00:00
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
2019-09-09 20:04:26 +00:00
|
|
|
|
struct BindingKey {
|
2020-03-13 22:36:46 +00:00
|
|
|
|
/// The identifier for the binding, aways the `normalize_to_macros_2_0` version of the
|
2019-09-09 20:04:26 +00:00
|
|
|
|
/// identifier.
|
|
|
|
|
ident: Ident,
|
|
|
|
|
ns: Namespace,
|
|
|
|
|
/// 0 if ident is not `_`, otherwise a value that's unique to the specific
|
|
|
|
|
/// `_` in the expanded AST that introduced this binding.
|
|
|
|
|
disambiguator: u32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Resolutions<'a> = RefCell<FxIndexMap<BindingKey, &'a RefCell<NameResolution<'a>>>>;
|
2019-07-29 18:19:50 +00:00
|
|
|
|
|
2012-07-04 21:53:12 +00:00
|
|
|
|
/// One node in the tree of modules.
|
2016-11-26 12:47:52 +00:00
|
|
|
|
pub struct ModuleData<'a> {
|
2016-09-18 09:45:06 +00:00
|
|
|
|
parent: Option<Module<'a>>,
|
|
|
|
|
kind: ModuleKind,
|
2016-02-02 20:21:24 +00:00
|
|
|
|
|
2016-12-20 08:32:15 +00:00
|
|
|
|
// The def id of the closest normal module (`mod`) ancestor (including this module).
|
|
|
|
|
normal_ancestor_id: DefId,
|
2016-08-22 00:24:11 +00:00
|
|
|
|
|
2019-07-29 18:19:50 +00:00
|
|
|
|
// Mapping between names and their (possibly in-progress) resolutions in this module.
|
|
|
|
|
// Resolutions in modules from other crates are not populated until accessed.
|
|
|
|
|
lazy_resolutions: Resolutions<'a>,
|
|
|
|
|
// True if this is a module from other crate that needs to be populated on access.
|
|
|
|
|
populate_on_access: Cell<bool>,
|
2012-05-22 17:54:12 +00:00
|
|
|
|
|
2016-11-11 10:51:15 +00:00
|
|
|
|
// Macro invocations that can expand into items in this module.
|
2019-08-17 17:49:00 +00:00
|
|
|
|
unexpanded_invocations: RefCell<FxHashSet<ExpnId>>,
|
2016-11-11 10:51:15 +00:00
|
|
|
|
|
2016-09-19 05:25:17 +00:00
|
|
|
|
no_implicit_prelude: bool,
|
2016-02-06 23:43:04 +00:00
|
|
|
|
|
2020-03-07 15:49:13 +00:00
|
|
|
|
glob_importers: RefCell<Vec<&'a Import<'a>>>,
|
|
|
|
|
globs: RefCell<Vec<&'a Import<'a>>>,
|
2015-08-05 19:47:01 +00:00
|
|
|
|
|
2016-04-18 00:00:18 +00:00
|
|
|
|
// Used to memoize the traits in this module for faster searches through all traits in scope.
|
2016-11-29 02:07:12 +00:00
|
|
|
|
traits: RefCell<Option<Box<[(Ident, &'a NameBinding<'a>)]>>>,
|
2016-04-18 00:00:18 +00:00
|
|
|
|
|
2017-05-10 11:19:29 +00:00
|
|
|
|
/// Span of the module itself. Used for error reporting.
|
|
|
|
|
span: Span,
|
2017-03-22 08:39:51 +00:00
|
|
|
|
|
2019-07-15 22:04:05 +00:00
|
|
|
|
expansion: ExpnId,
|
2012-05-22 17:54:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-19 00:09:55 +00:00
|
|
|
|
type Module<'a> = &'a ModuleData<'a>;
|
2016-01-11 21:19:29 +00:00
|
|
|
|
|
2016-11-26 12:47:52 +00:00
|
|
|
|
impl<'a> ModuleData<'a> {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
fn new(
|
|
|
|
|
parent: Option<Module<'a>>,
|
|
|
|
|
kind: ModuleKind,
|
|
|
|
|
normal_ancestor_id: DefId,
|
|
|
|
|
expansion: ExpnId,
|
|
|
|
|
span: Span,
|
|
|
|
|
) -> Self {
|
2016-11-26 12:47:52 +00:00
|
|
|
|
ModuleData {
|
2017-08-07 05:54:09 +00:00
|
|
|
|
parent,
|
|
|
|
|
kind,
|
|
|
|
|
normal_ancestor_id,
|
2019-07-29 18:19:50 +00:00
|
|
|
|
lazy_resolutions: Default::default(),
|
|
|
|
|
populate_on_access: Cell::new(!normal_ancestor_id.is_local()),
|
2019-08-17 17:49:00 +00:00
|
|
|
|
unexpanded_invocations: Default::default(),
|
2016-09-19 05:25:17 +00:00
|
|
|
|
no_implicit_prelude: false,
|
2016-02-16 03:54:14 +00:00
|
|
|
|
glob_importers: RefCell::new(Vec::new()),
|
2017-12-24 03:28:33 +00:00
|
|
|
|
globs: RefCell::new(Vec::new()),
|
2016-04-18 00:00:18 +00:00
|
|
|
|
traits: RefCell::new(None),
|
2017-08-07 05:54:09 +00:00
|
|
|
|
span,
|
|
|
|
|
expansion,
|
2016-01-11 21:19:29 +00:00
|
|
|
|
}
|
2012-09-05 22:58:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-29 18:19:50 +00:00
|
|
|
|
fn for_each_child<R, F>(&'a self, resolver: &mut R, mut f: F)
|
2019-12-24 22:38:22 +00:00
|
|
|
|
where
|
|
|
|
|
R: AsMut<Resolver<'a>>,
|
|
|
|
|
F: FnMut(&mut R, Ident, Namespace, &'a NameBinding<'a>),
|
2019-07-29 18:19:50 +00:00
|
|
|
|
{
|
2019-09-09 20:04:26 +00:00
|
|
|
|
for (key, name_resolution) in resolver.as_mut().resolutions(self).borrow().iter() {
|
2020-04-24 20:58:41 +00:00
|
|
|
|
if let Some(binding) = name_resolution.borrow().binding {
|
|
|
|
|
f(resolver, key.ident, key.ns, binding);
|
|
|
|
|
}
|
2016-02-07 23:58:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 17:21:19 +00:00
|
|
|
|
/// This modifies `self` in place. The traits will be stored in `self.traits`.
|
|
|
|
|
fn ensure_traits<R>(&'a self, resolver: &mut R)
|
|
|
|
|
where
|
|
|
|
|
R: AsMut<Resolver<'a>>,
|
|
|
|
|
{
|
|
|
|
|
let mut traits = self.traits.borrow_mut();
|
|
|
|
|
if traits.is_none() {
|
|
|
|
|
let mut collected_traits = Vec::new();
|
|
|
|
|
self.for_each_child(resolver, |_, name, ns, binding| {
|
|
|
|
|
if ns != TypeNS {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
match binding.res() {
|
|
|
|
|
Res::Def(DefKind::Trait | DefKind::TraitAlias, _) => {
|
|
|
|
|
collected_traits.push((name, binding))
|
|
|
|
|
}
|
|
|
|
|
_ => (),
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
*traits = Some(collected_traits.into_boxed_slice());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-20 16:36:05 +00:00
|
|
|
|
fn res(&self) -> Option<Res> {
|
2016-09-18 09:45:06 +00:00
|
|
|
|
match self.kind {
|
2019-04-20 16:36:05 +00:00
|
|
|
|
ModuleKind::Def(kind, def_id, _) => Some(Res::Def(kind, def_id)),
|
2019-04-20 16:46:19 +00:00
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-16 07:59:50 +00:00
|
|
|
|
fn def_id(&self) -> Option<DefId> {
|
2019-04-20 16:46:19 +00:00
|
|
|
|
match self.kind {
|
|
|
|
|
ModuleKind::Def(_, def_id, _) => Some(def_id),
|
|
|
|
|
_ => None,
|
|
|
|
|
}
|
2015-11-16 07:59:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-09 23:19:53 +00:00
|
|
|
|
// `self` resolves to the first module ancestor that `is_normal`.
|
2015-11-16 07:59:50 +00:00
|
|
|
|
fn is_normal(&self) -> bool {
|
2016-09-18 09:45:06 +00:00
|
|
|
|
match self.kind {
|
2019-04-20 16:46:19 +00:00
|
|
|
|
ModuleKind::Def(DefKind::Mod, _, _) => true,
|
2015-11-16 07:59:50 +00:00
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_trait(&self) -> bool {
|
2016-09-18 09:45:06 +00:00
|
|
|
|
match self.kind {
|
2019-04-20 16:46:19 +00:00
|
|
|
|
ModuleKind::Def(DefKind::Trait, _, _) => true,
|
2015-11-16 07:59:50 +00:00
|
|
|
|
_ => false,
|
2013-08-31 16:13:04 +00:00
|
|
|
|
}
|
2012-09-05 22:58:43 +00:00
|
|
|
|
}
|
2016-10-22 22:08:08 +00:00
|
|
|
|
|
2017-03-27 05:22:18 +00:00
|
|
|
|
fn nearest_item_scope(&'a self) -> Module<'a> {
|
2019-09-05 15:04:58 +00:00
|
|
|
|
match self.kind {
|
2020-04-17 00:38:52 +00:00
|
|
|
|
ModuleKind::Def(DefKind::Enum | DefKind::Trait, ..) => {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
self.parent.expect("enum or trait module without a parent")
|
|
|
|
|
}
|
2019-09-05 15:04:58 +00:00
|
|
|
|
_ => self,
|
|
|
|
|
}
|
2017-03-27 05:22:18 +00:00
|
|
|
|
}
|
2018-09-27 01:49:40 +00:00
|
|
|
|
|
|
|
|
|
fn is_ancestor_of(&self, mut other: &Self) -> bool {
|
|
|
|
|
while !ptr::eq(self, other) {
|
|
|
|
|
if let Some(parent) = other.parent {
|
|
|
|
|
other = parent;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
true
|
|
|
|
|
}
|
2015-08-06 10:47:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-26 12:47:52 +00:00
|
|
|
|
impl<'a> fmt::Debug for ModuleData<'a> {
|
2019-02-06 17:15:23 +00:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-04-20 16:36:05 +00:00
|
|
|
|
write!(f, "{:?}", self.res())
|
2014-11-28 02:41:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-14 01:05:01 +00:00
|
|
|
|
/// Records a possibly-private value, type, or module definition.
|
2016-02-15 05:18:55 +00:00
|
|
|
|
#[derive(Clone, Debug)]
|
2016-01-14 01:42:45 +00:00
|
|
|
|
pub struct NameBinding<'a> {
|
2016-02-07 21:34:23 +00:00
|
|
|
|
kind: NameBindingKind<'a>,
|
2018-12-29 15:15:29 +00:00
|
|
|
|
ambiguity: Option<(&'a NameBinding<'a>, AmbiguityKind)>,
|
2019-07-15 22:04:05 +00:00
|
|
|
|
expansion: ExpnId,
|
2016-04-27 01:13:15 +00:00
|
|
|
|
span: Span,
|
2016-04-09 23:19:53 +00:00
|
|
|
|
vis: ty::Visibility,
|
2012-08-18 00:55:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-28 02:34:01 +00:00
|
|
|
|
pub trait ToNameBinding<'a> {
|
2016-11-29 02:53:00 +00:00
|
|
|
|
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a>;
|
2016-07-28 02:34:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-29 02:53:00 +00:00
|
|
|
|
impl<'a> ToNameBinding<'a> for &'a NameBinding<'a> {
|
|
|
|
|
fn to_name_binding(self, _: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
|
2016-07-28 02:34:01 +00:00
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-15 05:18:55 +00:00
|
|
|
|
#[derive(Clone, Debug)]
|
2016-02-07 21:34:23 +00:00
|
|
|
|
enum NameBindingKind<'a> {
|
2019-04-20 16:36:05 +00:00
|
|
|
|
Res(Res, /* is_macro_export */ bool),
|
2016-01-11 21:19:29 +00:00
|
|
|
|
Module(Module<'a>),
|
2020-03-07 16:02:32 +00:00
|
|
|
|
Import { binding: &'a NameBinding<'a>, import: &'a Import<'a>, used: Cell<bool> },
|
2012-09-08 02:04:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-29 12:34:40 +00:00
|
|
|
|
impl<'a> NameBindingKind<'a> {
|
|
|
|
|
/// Is this a name binding of a import?
|
|
|
|
|
fn is_import(&self) -> bool {
|
|
|
|
|
match *self {
|
|
|
|
|
NameBindingKind::Import { .. } => true,
|
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-12 10:29:00 +00:00
|
|
|
|
struct PrivacyError<'a> {
|
|
|
|
|
ident: Ident,
|
|
|
|
|
binding: &'a NameBinding<'a>,
|
|
|
|
|
dedup_span: Span,
|
|
|
|
|
}
|
2016-02-25 04:40:46 +00:00
|
|
|
|
|
2017-08-17 09:03:59 +00:00
|
|
|
|
struct UseError<'a> {
|
|
|
|
|
err: DiagnosticBuilder<'a>,
|
2020-06-02 18:16:23 +00:00
|
|
|
|
/// Candidates which user could `use` to access the missing type.
|
2017-08-17 09:03:59 +00:00
|
|
|
|
candidates: Vec<ImportSuggestion>,
|
2020-06-02 18:16:23 +00:00
|
|
|
|
/// The `DefId` of the module to place the use-statements in.
|
2020-05-24 22:39:39 +00:00
|
|
|
|
def_id: DefId,
|
2020-06-02 18:16:23 +00:00
|
|
|
|
/// Whether the diagnostic should say "instead" (as in `consider importing ... instead`).
|
|
|
|
|
instead: bool,
|
|
|
|
|
/// Extra free-form suggestion.
|
2020-01-22 07:01:21 +00:00
|
|
|
|
suggestion: Option<(Span, &'static str, String, Applicability)>,
|
2017-08-17 09:03:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-04 22:11:59 +00:00
|
|
|
|
#[derive(Clone, Copy, PartialEq, Debug)]
|
|
|
|
|
enum AmbiguityKind {
|
|
|
|
|
Import,
|
|
|
|
|
BuiltinAttr,
|
|
|
|
|
DeriveHelper,
|
2020-03-13 22:23:24 +00:00
|
|
|
|
MacroRulesVsModularized,
|
2018-11-04 22:11:59 +00:00
|
|
|
|
GlobVsOuter,
|
|
|
|
|
GlobVsGlob,
|
|
|
|
|
GlobVsExpanded,
|
|
|
|
|
MoreExpandedVsOuter,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AmbiguityKind {
|
|
|
|
|
fn descr(self) -> &'static str {
|
|
|
|
|
match self {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
AmbiguityKind::Import => "name vs any other name during import resolution",
|
|
|
|
|
AmbiguityKind::BuiltinAttr => "built-in attribute vs any other name",
|
|
|
|
|
AmbiguityKind::DeriveHelper => "derive helper attribute vs any other name",
|
2020-03-13 22:23:24 +00:00
|
|
|
|
AmbiguityKind::MacroRulesVsModularized => {
|
|
|
|
|
"`macro_rules` vs non-`macro_rules` from other module"
|
|
|
|
|
}
|
2019-12-24 22:38:22 +00:00
|
|
|
|
AmbiguityKind::GlobVsOuter => {
|
|
|
|
|
"glob import vs any other name from outer scope during import/macro resolution"
|
|
|
|
|
}
|
|
|
|
|
AmbiguityKind::GlobVsGlob => "glob import vs glob import in the same module",
|
|
|
|
|
AmbiguityKind::GlobVsExpanded => {
|
2018-11-04 22:11:59 +00:00
|
|
|
|
"glob import vs macro-expanded name in the same \
|
2019-12-24 22:38:22 +00:00
|
|
|
|
module during import/macro resolution"
|
|
|
|
|
}
|
|
|
|
|
AmbiguityKind::MoreExpandedVsOuter => {
|
2018-11-04 22:11:59 +00:00
|
|
|
|
"macro-expanded name vs less macro-expanded name \
|
2019-12-24 22:38:22 +00:00
|
|
|
|
from outer scope during import/macro resolution"
|
|
|
|
|
}
|
2018-11-04 22:11:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Miscellaneous bits of metadata for better ambiguity error reporting.
|
|
|
|
|
#[derive(Clone, Copy, PartialEq)]
|
|
|
|
|
enum AmbiguityErrorMisc {
|
2018-11-25 13:08:43 +00:00
|
|
|
|
SuggestCrate,
|
2018-11-04 22:11:59 +00:00
|
|
|
|
SuggestSelf,
|
|
|
|
|
FromPrelude,
|
|
|
|
|
None,
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-06 03:47:11 +00:00
|
|
|
|
struct AmbiguityError<'a> {
|
2018-11-04 22:11:59 +00:00
|
|
|
|
kind: AmbiguityKind,
|
2018-09-07 23:51:20 +00:00
|
|
|
|
ident: Ident,
|
2016-09-06 03:47:11 +00:00
|
|
|
|
b1: &'a NameBinding<'a>,
|
|
|
|
|
b2: &'a NameBinding<'a>,
|
2018-11-04 22:11:59 +00:00
|
|
|
|
misc1: AmbiguityErrorMisc,
|
|
|
|
|
misc2: AmbiguityErrorMisc,
|
2016-09-06 03:47:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-14 01:42:45 +00:00
|
|
|
|
impl<'a> NameBinding<'a> {
|
2016-11-27 00:23:54 +00:00
|
|
|
|
fn module(&self) -> Option<Module<'a>> {
|
2016-02-07 21:34:23 +00:00
|
|
|
|
match self.kind {
|
2016-11-27 00:23:54 +00:00
|
|
|
|
NameBindingKind::Module(module) => Some(module),
|
2016-02-07 21:34:23 +00:00
|
|
|
|
NameBindingKind::Import { binding, .. } => binding.module(),
|
2016-11-27 00:23:54 +00:00
|
|
|
|
_ => None,
|
2013-05-13 23:13:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-20 16:36:05 +00:00
|
|
|
|
fn res(&self) -> Res {
|
2016-02-07 21:34:23 +00:00
|
|
|
|
match self.kind {
|
2019-04-20 16:36:05 +00:00
|
|
|
|
NameBindingKind::Res(res, _) => res,
|
|
|
|
|
NameBindingKind::Module(module) => module.res().unwrap(),
|
|
|
|
|
NameBindingKind::Import { binding, .. } => binding.res(),
|
2012-10-16 01:04:15 +00:00
|
|
|
|
}
|
2012-05-22 17:54:12 +00:00
|
|
|
|
}
|
2012-10-15 21:56:42 +00:00
|
|
|
|
|
2018-12-29 15:15:29 +00:00
|
|
|
|
fn is_ambiguity(&self) -> bool {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
self.ambiguity.is_some()
|
|
|
|
|
|| match self.kind {
|
|
|
|
|
NameBindingKind::Import { binding, .. } => binding.is_ambiguity(),
|
|
|
|
|
_ => false,
|
|
|
|
|
}
|
2016-11-27 10:27:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-21 16:31:49 +00:00
|
|
|
|
fn is_possibly_imported_variant(&self) -> bool {
|
|
|
|
|
match self.kind {
|
|
|
|
|
NameBindingKind::Import { binding, .. } => binding.is_possibly_imported_variant(),
|
|
|
|
|
_ => self.is_variant(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-15 21:55:04 +00:00
|
|
|
|
// We sometimes need to treat variants as `pub` for backwards compatibility.
|
2016-04-11 05:35:18 +00:00
|
|
|
|
fn pseudo_vis(&self) -> ty::Visibility {
|
2019-04-20 16:36:05 +00:00
|
|
|
|
if self.is_variant() && self.res().def_id().is_local() {
|
2017-11-29 19:20:49 +00:00
|
|
|
|
ty::Visibility::Public
|
|
|
|
|
} else {
|
|
|
|
|
self.vis
|
|
|
|
|
}
|
2016-04-11 05:35:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_variant(&self) -> bool {
|
|
|
|
|
match self.kind {
|
2020-04-17 00:38:52 +00:00
|
|
|
|
NameBindingKind::Res(
|
|
|
|
|
Res::Def(DefKind::Variant | DefKind::Ctor(CtorOf::Variant, ..), _),
|
|
|
|
|
_,
|
|
|
|
|
) => true,
|
2016-04-11 05:35:18 +00:00
|
|
|
|
_ => false,
|
|
|
|
|
}
|
2015-11-16 02:10:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-29 22:21:36 +00:00
|
|
|
|
fn is_extern_crate(&self) -> bool {
|
2016-10-28 03:40:58 +00:00
|
|
|
|
match self.kind {
|
|
|
|
|
NameBindingKind::Import {
|
2020-03-07 16:02:32 +00:00
|
|
|
|
import: &Import { kind: ImportKind::ExternCrate { .. }, .. },
|
2019-12-24 22:38:22 +00:00
|
|
|
|
..
|
2016-10-28 03:40:58 +00:00
|
|
|
|
} => true,
|
2019-12-24 22:38:22 +00:00
|
|
|
|
NameBindingKind::Module(&ModuleData {
|
|
|
|
|
kind: ModuleKind::Def(DefKind::Mod, def_id, _),
|
|
|
|
|
..
|
|
|
|
|
}) => def_id.index == CRATE_DEF_INDEX,
|
2016-10-28 03:40:58 +00:00
|
|
|
|
_ => false,
|
|
|
|
|
}
|
2016-01-29 22:21:36 +00:00
|
|
|
|
}
|
2016-02-07 21:34:23 +00:00
|
|
|
|
|
|
|
|
|
fn is_import(&self) -> bool {
|
|
|
|
|
match self.kind {
|
|
|
|
|
NameBindingKind::Import { .. } => true,
|
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-17 01:57:09 +00:00
|
|
|
|
|
|
|
|
|
fn is_glob_import(&self) -> bool {
|
|
|
|
|
match self.kind {
|
2020-03-07 16:02:32 +00:00
|
|
|
|
NameBindingKind::Import { import, .. } => import.is_glob(),
|
2016-04-17 01:57:09 +00:00
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_importable(&self) -> bool {
|
2019-04-20 16:36:05 +00:00
|
|
|
|
match self.res() {
|
2020-04-17 00:38:52 +00:00
|
|
|
|
Res::Def(DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy, _) => false,
|
2016-04-17 01:57:09 +00:00
|
|
|
|
_ => true,
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-18 01:55:51 +00:00
|
|
|
|
|
|
|
|
|
fn is_macro_def(&self) -> bool {
|
|
|
|
|
match self.kind {
|
2019-04-20 16:36:05 +00:00
|
|
|
|
NameBindingKind::Res(Res::Def(DefKind::Macro(..), _), _) => true,
|
2017-03-18 01:55:51 +00:00
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-21 11:11:08 +00:00
|
|
|
|
|
2018-09-03 22:14:58 +00:00
|
|
|
|
fn macro_kind(&self) -> Option<MacroKind> {
|
2019-07-11 23:29:28 +00:00
|
|
|
|
self.res().macro_kind()
|
2018-09-03 22:14:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-02 01:57:56 +00:00
|
|
|
|
// Suppose that we resolved macro invocation with `invoc_parent_expansion` to binding `binding`
|
|
|
|
|
// at some expansion round `max(invoc, binding)` when they both emerged from macros.
|
2018-08-28 01:07:31 +00:00
|
|
|
|
// Then this function returns `true` if `self` may emerge from a macro *after* that
|
|
|
|
|
// in some later round and screw up our previously found resolution.
|
2018-09-07 23:50:57 +00:00
|
|
|
|
// See more detailed explanation in
|
|
|
|
|
// https://github.com/rust-lang/rust/pull/53778#issuecomment-419224049
|
2019-07-15 22:04:05 +00:00
|
|
|
|
fn may_appear_after(&self, invoc_parent_expansion: ExpnId, binding: &NameBinding<'_>) -> bool {
|
2018-09-02 01:57:56 +00:00
|
|
|
|
// self > max(invoc, binding) => !(self <= invoc || self <= binding)
|
2018-08-29 00:23:28 +00:00
|
|
|
|
// Expansions are partially ordered, so "may appear after" is an inversion of
|
|
|
|
|
// "certainly appears before or simultaneously" and includes unordered cases.
|
|
|
|
|
let self_parent_expansion = self.expansion;
|
|
|
|
|
let other_parent_expansion = binding.expansion;
|
|
|
|
|
let certainly_before_other_or_simultaneously =
|
|
|
|
|
other_parent_expansion.is_descendant_of(self_parent_expansion);
|
|
|
|
|
let certainly_before_invoc_or_simultaneously =
|
|
|
|
|
invoc_parent_expansion.is_descendant_of(self_parent_expansion);
|
|
|
|
|
!(certainly_before_other_or_simultaneously || certainly_before_invoc_or_simultaneously)
|
2018-08-28 01:07:31 +00:00
|
|
|
|
}
|
2012-08-18 00:55:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-04 21:53:12 +00:00
|
|
|
|
/// Interns the names of the primitive types.
|
2018-02-18 17:01:33 +00:00
|
|
|
|
///
|
|
|
|
|
/// All other types are defined somewhere and possibly imported, but the primitive ones need
|
|
|
|
|
/// special handling, since they have no place of origin.
|
2013-10-02 12:33:01 +00:00
|
|
|
|
struct PrimitiveTypeTable {
|
2020-04-19 11:00:18 +00:00
|
|
|
|
primitive_types: FxHashMap<Symbol, PrimTy>,
|
2012-09-08 02:04:40 +00:00
|
|
|
|
}
|
2012-05-22 17:54:12 +00:00
|
|
|
|
|
2013-05-31 22:17:22 +00:00
|
|
|
|
impl PrimitiveTypeTable {
|
2014-05-28 19:36:05 +00:00
|
|
|
|
fn new() -> PrimitiveTypeTable {
|
2019-06-13 20:14:58 +00:00
|
|
|
|
let mut table = FxHashMap::default();
|
|
|
|
|
|
|
|
|
|
table.insert(sym::bool, Bool);
|
|
|
|
|
table.insert(sym::char, Char);
|
|
|
|
|
table.insert(sym::f32, Float(FloatTy::F32));
|
|
|
|
|
table.insert(sym::f64, Float(FloatTy::F64));
|
|
|
|
|
table.insert(sym::isize, Int(IntTy::Isize));
|
|
|
|
|
table.insert(sym::i8, Int(IntTy::I8));
|
|
|
|
|
table.insert(sym::i16, Int(IntTy::I16));
|
|
|
|
|
table.insert(sym::i32, Int(IntTy::I32));
|
|
|
|
|
table.insert(sym::i64, Int(IntTy::I64));
|
|
|
|
|
table.insert(sym::i128, Int(IntTy::I128));
|
|
|
|
|
table.insert(sym::str, Str);
|
|
|
|
|
table.insert(sym::usize, Uint(UintTy::Usize));
|
|
|
|
|
table.insert(sym::u8, Uint(UintTy::U8));
|
|
|
|
|
table.insert(sym::u16, Uint(UintTy::U16));
|
|
|
|
|
table.insert(sym::u32, Uint(UintTy::U32));
|
|
|
|
|
table.insert(sym::u64, Uint(UintTy::U64));
|
|
|
|
|
table.insert(sym::u128, Uint(UintTy::U128));
|
|
|
|
|
Self { primitive_types: table }
|
2012-05-22 17:54:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-09 19:11:00 +00:00
|
|
|
|
#[derive(Debug, Default, Clone)]
|
2018-09-28 22:31:54 +00:00
|
|
|
|
pub struct ExternPreludeEntry<'a> {
|
|
|
|
|
extern_crate_item: Option<&'a NameBinding<'a>>,
|
|
|
|
|
pub introduced_by_item: bool,
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-31 04:04:01 +00:00
|
|
|
|
/// Used for better errors for E0773
|
|
|
|
|
enum BuiltinMacroState {
|
|
|
|
|
NotYetSeen(SyntaxExtension),
|
|
|
|
|
AlreadySeen(Span),
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-04 21:53:12 +00:00
|
|
|
|
/// The main resolver class.
|
2018-02-18 17:01:33 +00:00
|
|
|
|
///
|
|
|
|
|
/// This is the visitor that walks the whole crate.
|
2018-12-10 04:37:10 +00:00
|
|
|
|
pub struct Resolver<'a> {
|
2014-03-05 14:36:01 +00:00
|
|
|
|
session: &'a Session,
|
2012-05-22 17:54:12 +00:00
|
|
|
|
|
2019-10-19 23:55:39 +00:00
|
|
|
|
definitions: Definitions,
|
2014-11-23 09:29:41 +00:00
|
|
|
|
|
2019-10-19 23:55:39 +00:00
|
|
|
|
graph_root: Module<'a>,
|
2012-05-22 17:54:12 +00:00
|
|
|
|
|
2016-06-05 09:56:05 +00:00
|
|
|
|
prelude: Option<Module<'a>>,
|
2019-10-19 23:55:39 +00:00
|
|
|
|
extern_prelude: FxHashMap<Ident, ExternPreludeEntry<'a>>,
|
2016-06-05 09:56:05 +00:00
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// N.B., this is used only for better diagnostics, not name resolution itself.
|
2017-03-18 02:10:13 +00:00
|
|
|
|
has_self: FxHashSet<DefId>,
|
2014-05-06 23:37:32 +00:00
|
|
|
|
|
2018-04-14 01:05:01 +00:00
|
|
|
|
/// Names of fields of an item `DefId` accessible with dot syntax.
|
|
|
|
|
/// Used for hints during error reporting.
|
2020-04-19 11:00:18 +00:00
|
|
|
|
field_names: FxHashMap<DefId, Vec<Spanned<Symbol>>>,
|
2012-07-11 22:00:40 +00:00
|
|
|
|
|
2018-04-14 01:05:01 +00:00
|
|
|
|
/// All imports known to succeed or fail.
|
2020-03-07 15:49:13 +00:00
|
|
|
|
determined_imports: Vec<&'a Import<'a>>,
|
2016-08-15 08:19:09 +00:00
|
|
|
|
|
2018-04-14 01:05:01 +00:00
|
|
|
|
/// All non-determined imports.
|
2020-03-07 15:49:13 +00:00
|
|
|
|
indeterminate_imports: Vec<&'a Import<'a>>,
|
2012-05-22 17:54:12 +00:00
|
|
|
|
|
2018-12-01 19:14:37 +00:00
|
|
|
|
/// FIXME: Refactor things so that these fields are passed through arguments and not resolver.
|
|
|
|
|
/// We are resolving a last import segment during import validation.
|
2018-11-10 15:58:37 +00:00
|
|
|
|
last_import_segment: bool,
|
2018-12-01 19:14:37 +00:00
|
|
|
|
/// This binding should be ignored during in-module resolution, so that we don't get
|
|
|
|
|
/// "self-confirming" import resolutions during import validation.
|
2020-07-08 13:36:52 +00:00
|
|
|
|
unusable_binding: Option<&'a NameBinding<'a>>,
|
2018-11-10 15:58:37 +00:00
|
|
|
|
|
2018-04-14 01:05:01 +00:00
|
|
|
|
/// The idents for the primitive types.
|
2014-04-14 08:30:59 +00:00
|
|
|
|
primitive_type_table: PrimitiveTypeTable,
|
2012-05-22 17:54:12 +00:00
|
|
|
|
|
2019-05-04 12:18:58 +00:00
|
|
|
|
/// Resolutions for nodes that have a single resolution.
|
|
|
|
|
partial_res_map: NodeMap<PartialRes>,
|
|
|
|
|
/// Resolutions for import nodes, which have multiple resolutions in different namespaces.
|
|
|
|
|
import_res_map: NodeMap<PerNS<Option<Res>>>,
|
2019-05-04 14:22:00 +00:00
|
|
|
|
/// Resolutions for labels (node IDs of their corresponding blocks or loops).
|
|
|
|
|
label_res_map: NodeMap<NodeId>,
|
2019-05-04 12:18:58 +00:00
|
|
|
|
|
2019-10-13 22:08:13 +00:00
|
|
|
|
/// `CrateNum` resolutions of `extern crate` items.
|
2020-05-24 11:18:22 +00:00
|
|
|
|
extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
|
2020-06-07 10:14:47 +00:00
|
|
|
|
export_map: ExportMap<LocalDefId>,
|
2020-06-14 21:35:29 +00:00
|
|
|
|
trait_map: NodeMap<Vec<TraitCandidate>>,
|
2013-04-30 05:15:17 +00:00
|
|
|
|
|
2018-04-14 01:05:01 +00:00
|
|
|
|
/// A map from nodes to anonymous modules.
|
|
|
|
|
/// Anonymous modules are pseudo-modules that are implicitly created around items
|
|
|
|
|
/// contained within blocks.
|
|
|
|
|
///
|
|
|
|
|
/// For example, if we have this:
|
|
|
|
|
///
|
|
|
|
|
/// fn f() {
|
|
|
|
|
/// fn g() {
|
|
|
|
|
/// ...
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// There will be an anonymous module created around `g` with the ID of the
|
|
|
|
|
/// entry block for `f`.
|
2016-12-20 08:32:15 +00:00
|
|
|
|
block_map: NodeMap<Module<'a>>,
|
2019-08-25 19:58:03 +00:00
|
|
|
|
/// A fake module that contains no definition and no prelude. Used so that
|
|
|
|
|
/// some AST passes can generate identifiers that only resolve to local or
|
|
|
|
|
/// language items.
|
|
|
|
|
empty_module: Module<'a>,
|
2020-04-07 23:29:50 +00:00
|
|
|
|
module_map: FxHashMap<LocalDefId, Module<'a>>,
|
2019-10-06 11:30:46 +00:00
|
|
|
|
extern_module_map: FxHashMap<DefId, Module<'a>>,
|
2018-09-27 01:49:40 +00:00
|
|
|
|
binding_parent_modules: FxHashMap<PtrKey<'a, NameBinding<'a>>, Module<'a>>,
|
2019-09-09 20:04:26 +00:00
|
|
|
|
underscore_disambiguator: u32,
|
2016-04-17 20:41:57 +00:00
|
|
|
|
|
2019-01-06 00:22:52 +00:00
|
|
|
|
/// Maps glob imports to the names of items actually imported.
|
2020-05-24 11:18:22 +00:00
|
|
|
|
glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
|
2014-11-23 09:29:41 +00:00
|
|
|
|
|
2016-11-08 03:02:55 +00:00
|
|
|
|
used_imports: FxHashSet<(NodeId, Namespace)>,
|
2020-05-24 11:18:22 +00:00
|
|
|
|
maybe_unused_trait_imports: FxHashSet<LocalDefId>,
|
|
|
|
|
maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,
|
2015-05-14 11:40:16 +00:00
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Privacy errors are delayed until the end in order to deduplicate them.
|
2016-02-25 04:40:46 +00:00
|
|
|
|
privacy_errors: Vec<PrivacyError<'a>>,
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Ambiguity errors are delayed for deduplication.
|
2016-09-06 03:47:11 +00:00
|
|
|
|
ambiguity_errors: Vec<AmbiguityError<'a>>,
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// `use` injections are delayed for better placement and deduplication.
|
2017-08-17 09:03:59 +00:00
|
|
|
|
use_injections: Vec<UseError<'a>>,
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Crate-local macro expanded `macro_export` referred to by a module-relative path.
|
2018-08-11 11:33:43 +00:00
|
|
|
|
macro_expanded_macro_export_errors: BTreeSet<(Span, Span)>,
|
2016-01-11 21:19:29 +00:00
|
|
|
|
|
|
|
|
|
arenas: &'a ResolverArenas<'a>,
|
2016-08-22 04:05:49 +00:00
|
|
|
|
dummy_binding: &'a NameBinding<'a>,
|
2016-09-05 03:46:05 +00:00
|
|
|
|
|
2019-10-20 00:28:36 +00:00
|
|
|
|
crate_loader: CrateLoader<'a>,
|
2017-03-22 08:39:51 +00:00
|
|
|
|
macro_names: FxHashSet<Ident>,
|
2020-08-31 04:04:01 +00:00
|
|
|
|
builtin_macros: FxHashMap<Symbol, BuiltinMacroState>,
|
2019-11-03 17:28:20 +00:00
|
|
|
|
registered_attrs: FxHashSet<Ident>,
|
|
|
|
|
registered_tools: FxHashSet<Ident>,
|
2020-04-19 11:00:18 +00:00
|
|
|
|
macro_use_prelude: FxHashMap<Symbol, &'a NameBinding<'a>>,
|
|
|
|
|
all_macros: FxHashMap<Symbol, Res>,
|
2018-02-27 16:11:14 +00:00
|
|
|
|
macro_map: FxHashMap<DefId, Lrc<SyntaxExtension>>,
|
2019-07-02 22:44:04 +00:00
|
|
|
|
dummy_ext_bang: Lrc<SyntaxExtension>,
|
|
|
|
|
dummy_ext_derive: Lrc<SyntaxExtension>,
|
2019-06-17 09:29:56 +00:00
|
|
|
|
non_macro_attrs: [Lrc<SyntaxExtension>; 2],
|
2020-05-24 11:18:22 +00:00
|
|
|
|
local_macro_def_scopes: FxHashMap<LocalDefId, Module<'a>>,
|
2019-08-25 19:58:03 +00:00
|
|
|
|
ast_transform_scopes: FxHashMap<ExpnId, Module<'a>>,
|
2020-05-24 22:39:39 +00:00
|
|
|
|
unused_macros: FxHashMap<LocalDefId, (NodeId, Span)>,
|
|
|
|
|
proc_macro_stubs: FxHashSet<LocalDefId>,
|
2019-08-12 18:52:37 +00:00
|
|
|
|
/// Traces collected during macro resolution and validated when it's complete.
|
2019-12-24 22:38:22 +00:00
|
|
|
|
single_segment_macro_resolutions:
|
|
|
|
|
Vec<(Ident, MacroKind, ParentScope<'a>, Option<&'a NameBinding<'a>>)>,
|
|
|
|
|
multi_segment_macro_resolutions:
|
|
|
|
|
Vec<(Vec<Segment>, Span, MacroKind, ParentScope<'a>, Option<Res>)>,
|
2019-08-12 18:52:37 +00:00
|
|
|
|
builtin_attrs: Vec<(Ident, ParentScope<'a>)>,
|
2019-10-30 10:13:00 +00:00
|
|
|
|
/// `derive(Copy)` marks items they are applied to so they are treated specially later.
|
2019-08-03 01:22:44 +00:00
|
|
|
|
/// Derive macros cannot modify the item themselves and have to store the markers in the global
|
|
|
|
|
/// context, so they attach the markers to derive container IDs using this resolver table.
|
2019-11-04 09:09:58 +00:00
|
|
|
|
containers_deriving_copy: FxHashSet<ExpnId>,
|
2019-08-12 20:39:49 +00:00
|
|
|
|
/// Parent scopes in which the macros were invoked.
|
|
|
|
|
/// FIXME: `derives` are missing in these parent scopes and need to be taken from elsewhere.
|
|
|
|
|
invocation_parent_scopes: FxHashMap<ExpnId, ParentScope<'a>>,
|
2020-03-13 22:06:36 +00:00
|
|
|
|
/// `macro_rules` scopes *produced* by expanding the macro invocations,
|
2019-08-12 20:39:49 +00:00
|
|
|
|
/// include all the `macro_rules` items and other invocations generated by them.
|
2020-03-13 22:06:36 +00:00
|
|
|
|
output_macro_rules_scopes: FxHashMap<ExpnId, MacroRulesScope<'a>>,
|
2019-10-03 22:53:20 +00:00
|
|
|
|
/// Helper attributes that are in scope for the given expansion.
|
|
|
|
|
helper_attrs: FxHashMap<ExpnId, Vec<Ident>>,
|
2016-10-28 07:30:23 +00:00
|
|
|
|
|
2018-04-14 01:05:01 +00:00
|
|
|
|
/// Avoid duplicated errors for "name already defined".
|
2020-04-19 11:00:18 +00:00
|
|
|
|
name_already_seen: FxHashMap<Symbol, Span>,
|
2017-01-09 09:31:14 +00:00
|
|
|
|
|
2020-03-07 15:49:13 +00:00
|
|
|
|
potentially_unused_imports: Vec<&'a Import<'a>>,
|
2017-01-20 15:53:49 +00:00
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Table for mapping struct IDs into struct constructor IDs,
|
2018-04-14 01:05:01 +00:00
|
|
|
|
/// it's not used during normal resolution, only for better error reporting.
|
2020-09-08 22:14:09 +00:00
|
|
|
|
/// Also includes of list of each fields visibility
|
|
|
|
|
struct_constructors: DefIdMap<(Res, ty::Visibility, Vec<ty::Visibility>)>,
|
2017-07-18 18:41:21 +00:00
|
|
|
|
|
2019-06-22 13:18:05 +00:00
|
|
|
|
/// Features enabled for this crate.
|
2020-04-19 11:00:18 +00:00
|
|
|
|
active_features: FxHashSet<Symbol>,
|
2019-09-09 12:26:25 +00:00
|
|
|
|
|
|
|
|
|
/// Stores enum visibilities to properly build a reduced graph
|
|
|
|
|
/// when visiting the correspondent variants.
|
|
|
|
|
variant_vis: DefIdMap<ty::Visibility>,
|
2019-10-25 13:15:33 +00:00
|
|
|
|
|
2020-01-05 08:40:16 +00:00
|
|
|
|
lint_buffer: LintBuffer,
|
2019-11-03 22:38:02 +00:00
|
|
|
|
|
|
|
|
|
next_node_id: NodeId,
|
2020-06-20 18:59:29 +00:00
|
|
|
|
|
|
|
|
|
def_id_to_span: IndexVec<LocalDefId, Span>,
|
|
|
|
|
|
|
|
|
|
node_id_to_def_id: FxHashMap<ast::NodeId, LocalDefId>,
|
|
|
|
|
def_id_to_node_id: IndexVec<LocalDefId, ast::NodeId>,
|
|
|
|
|
|
|
|
|
|
/// Indices of unnamed struct or variant fields with unresolved attributes.
|
|
|
|
|
placeholder_field_indices: FxHashMap<NodeId, usize>,
|
|
|
|
|
/// When collecting definitions from an AST fragment produced by a macro invocation `ExpnId`
|
|
|
|
|
/// we know what parent node that fragment should be attached to thanks to this table.
|
|
|
|
|
invocation_parents: FxHashMap<ExpnId, LocalDefId>,
|
2020-06-21 22:49:06 +00:00
|
|
|
|
|
|
|
|
|
next_disambiguator: FxHashMap<(LocalDefId, DefPathData), u32>,
|
2016-01-11 21:19:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Nothing really interesting here; it just provides memory for the rest of the crate.
|
2018-10-16 14:57:53 +00:00
|
|
|
|
#[derive(Default)]
|
2016-06-22 01:54:34 +00:00
|
|
|
|
pub struct ResolverArenas<'a> {
|
2020-06-02 17:19:49 +00:00
|
|
|
|
modules: TypedArena<ModuleData<'a>>,
|
2016-04-17 20:23:10 +00:00
|
|
|
|
local_modules: RefCell<Vec<Module<'a>>>,
|
2020-06-02 17:19:49 +00:00
|
|
|
|
name_bindings: TypedArena<NameBinding<'a>>,
|
|
|
|
|
imports: TypedArena<Import<'a>>,
|
|
|
|
|
name_resolutions: TypedArena<RefCell<NameResolution<'a>>>,
|
|
|
|
|
macro_rules_bindings: TypedArena<MacroRulesBinding<'a>>,
|
|
|
|
|
ast_paths: TypedArena<ast::Path>,
|
2020-09-08 22:14:09 +00:00
|
|
|
|
pattern_spans: TypedArena<Span>,
|
2016-02-15 02:22:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> ResolverArenas<'a> {
|
2016-11-26 12:47:52 +00:00
|
|
|
|
fn alloc_module(&'a self, module: ModuleData<'a>) -> Module<'a> {
|
2016-04-17 20:23:10 +00:00
|
|
|
|
let module = self.modules.alloc(module);
|
|
|
|
|
if module.def_id().map(|def_id| def_id.is_local()).unwrap_or(true) {
|
|
|
|
|
self.local_modules.borrow_mut().push(module);
|
|
|
|
|
}
|
|
|
|
|
module
|
|
|
|
|
}
|
2019-02-06 17:15:23 +00:00
|
|
|
|
fn local_modules(&'a self) -> std::cell::Ref<'a, Vec<Module<'a>>> {
|
2016-04-17 20:23:10 +00:00
|
|
|
|
self.local_modules.borrow()
|
2016-02-15 05:18:55 +00:00
|
|
|
|
}
|
|
|
|
|
fn alloc_name_binding(&'a self, name_binding: NameBinding<'a>) -> &'a NameBinding<'a> {
|
|
|
|
|
self.name_bindings.alloc(name_binding)
|
|
|
|
|
}
|
2020-03-07 16:02:32 +00:00
|
|
|
|
fn alloc_import(&'a self, import: Import<'a>) -> &'a Import<'_> {
|
|
|
|
|
self.imports.alloc(import)
|
2016-02-15 02:22:59 +00:00
|
|
|
|
}
|
2016-03-30 22:21:56 +00:00
|
|
|
|
fn alloc_name_resolution(&'a self) -> &'a RefCell<NameResolution<'a>> {
|
|
|
|
|
self.name_resolutions.alloc(Default::default())
|
|
|
|
|
}
|
2020-03-13 22:06:36 +00:00
|
|
|
|
fn alloc_macro_rules_binding(
|
|
|
|
|
&'a self,
|
|
|
|
|
binding: MacroRulesBinding<'a>,
|
|
|
|
|
) -> &'a MacroRulesBinding<'a> {
|
|
|
|
|
self.macro_rules_bindings.alloc(binding)
|
2016-10-06 08:04:30 +00:00
|
|
|
|
}
|
2019-08-12 22:39:10 +00:00
|
|
|
|
fn alloc_ast_paths(&'a self, paths: &[ast::Path]) -> &'a [ast::Path] {
|
|
|
|
|
self.ast_paths.alloc_from_iter(paths.iter().cloned())
|
|
|
|
|
}
|
2020-09-08 22:14:09 +00:00
|
|
|
|
fn alloc_pattern_spans(&'a self, spans: impl Iterator<Item = Span>) -> &'a [Span] {
|
|
|
|
|
self.pattern_spans.alloc_from_iter(spans)
|
|
|
|
|
}
|
2012-09-08 02:04:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-29 18:19:50 +00:00
|
|
|
|
impl<'a> AsMut<Resolver<'a>> for Resolver<'a> {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
fn as_mut(&mut self) -> &mut Resolver<'a> {
|
|
|
|
|
self
|
|
|
|
|
}
|
2019-07-29 18:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 14:37:48 +00:00
|
|
|
|
impl<'a, 'b> DefIdTree for &'a Resolver<'b> {
|
2016-12-20 08:32:15 +00:00
|
|
|
|
fn parent(self, id: DefId) -> Option<DefId> {
|
2019-11-03 12:36:59 +00:00
|
|
|
|
match id.as_local() {
|
|
|
|
|
Some(id) => self.definitions.def_key(id).parent,
|
|
|
|
|
None => self.cstore().def_key(id).parent,
|
2019-12-24 22:38:22 +00:00
|
|
|
|
}
|
|
|
|
|
.map(|index| DefId { index, ..id })
|
2016-04-27 02:29:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-24 16:07:58 +00:00
|
|
|
|
/// This interface is used through the AST→HIR step, to embed full paths into the HIR. After that
|
|
|
|
|
/// the resolver is no longer needed as all the relevant information is inline.
|
2020-06-20 18:59:29 +00:00
|
|
|
|
impl ResolverAstLowering for Resolver<'_> {
|
2020-01-06 06:34:52 +00:00
|
|
|
|
fn def_key(&mut self, id: DefId) -> DefKey {
|
2019-11-03 12:36:59 +00:00
|
|
|
|
if let Some(id) = id.as_local() {
|
|
|
|
|
self.definitions().def_key(id)
|
|
|
|
|
} else {
|
|
|
|
|
self.cstore().def_key(id)
|
|
|
|
|
}
|
2020-01-06 06:34:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 08:23:44 +00:00
|
|
|
|
fn item_generics_num_lifetimes(&self, def_id: DefId, sess: &Session) -> usize {
|
|
|
|
|
self.cstore().item_generics_num_lifetimes(def_id, sess)
|
2019-10-20 00:28:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-04 12:18:58 +00:00
|
|
|
|
fn get_partial_res(&mut self, id: NodeId) -> Option<PartialRes> {
|
|
|
|
|
self.partial_res_map.get(&id).cloned()
|
2018-01-01 07:31:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-04 12:18:58 +00:00
|
|
|
|
fn get_import_res(&mut self, id: NodeId) -> PerNS<Option<Res>> {
|
|
|
|
|
self.import_res_map.get(&id).cloned().unwrap_or_default()
|
2018-01-01 07:31:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-04 14:22:00 +00:00
|
|
|
|
fn get_label_res(&mut self, id: NodeId) -> Option<NodeId> {
|
|
|
|
|
self.label_res_map.get(&id).cloned()
|
2018-06-13 16:44:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-01 07:31:19 +00:00
|
|
|
|
fn definitions(&mut self) -> &mut Definitions {
|
|
|
|
|
&mut self.definitions
|
|
|
|
|
}
|
2019-08-03 01:22:44 +00:00
|
|
|
|
|
2020-01-05 08:40:16 +00:00
|
|
|
|
fn lint_buffer(&mut self) -> &mut LintBuffer {
|
2019-10-25 13:20:18 +00:00
|
|
|
|
&mut self.lint_buffer
|
|
|
|
|
}
|
2019-11-03 22:38:02 +00:00
|
|
|
|
|
|
|
|
|
fn next_node_id(&mut self) -> NodeId {
|
|
|
|
|
self.next_node_id()
|
|
|
|
|
}
|
2020-06-12 17:13:10 +00:00
|
|
|
|
|
|
|
|
|
fn trait_map(&self) -> &NodeMap<Vec<TraitCandidate>> {
|
|
|
|
|
&self.trait_map
|
|
|
|
|
}
|
2020-06-20 18:59:29 +00:00
|
|
|
|
|
|
|
|
|
fn opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId> {
|
|
|
|
|
self.node_id_to_def_id.get(&node).copied()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn local_def_id(&self, node: NodeId) -> LocalDefId {
|
|
|
|
|
self.opt_local_def_id(node).unwrap_or_else(|| panic!("no entry for node id: `{:?}`", node))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Adds a definition with a parent definition.
|
2020-06-21 14:49:38 +00:00
|
|
|
|
fn create_def(
|
2020-06-20 18:59:29 +00:00
|
|
|
|
&mut self,
|
|
|
|
|
parent: LocalDefId,
|
|
|
|
|
node_id: ast::NodeId,
|
|
|
|
|
data: DefPathData,
|
|
|
|
|
expn_id: ExpnId,
|
|
|
|
|
span: Span,
|
|
|
|
|
) -> LocalDefId {
|
|
|
|
|
assert!(
|
|
|
|
|
!self.node_id_to_def_id.contains_key(&node_id),
|
|
|
|
|
"adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}",
|
|
|
|
|
node_id,
|
|
|
|
|
data,
|
|
|
|
|
self.definitions.def_key(self.node_id_to_def_id[&node_id]),
|
|
|
|
|
);
|
|
|
|
|
|
2020-06-21 22:49:06 +00:00
|
|
|
|
// Find the next free disambiguator for this key.
|
|
|
|
|
let next_disambiguator = &mut self.next_disambiguator;
|
|
|
|
|
let next_disambiguator = |parent, data| {
|
|
|
|
|
let next_disamb = next_disambiguator.entry((parent, data)).or_insert(0);
|
|
|
|
|
let disambiguator = *next_disamb;
|
|
|
|
|
*next_disamb = next_disamb.checked_add(1).expect("disambiguator overflow");
|
|
|
|
|
disambiguator
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let def_id = self.definitions.create_def(parent, data, expn_id, next_disambiguator);
|
2020-06-20 18:59:29 +00:00
|
|
|
|
|
|
|
|
|
assert_eq!(self.def_id_to_span.push(span), def_id);
|
|
|
|
|
|
|
|
|
|
// Some things for which we allocate `LocalDefId`s don't correspond to
|
|
|
|
|
// anything in the AST, so they don't have a `NodeId`. For these cases
|
|
|
|
|
// we don't need a mapping from `NodeId` to `LocalDefId`.
|
|
|
|
|
if node_id != ast::DUMMY_NODE_ID {
|
2020-06-21 14:49:38 +00:00
|
|
|
|
debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id);
|
2020-06-20 18:59:29 +00:00
|
|
|
|
self.node_id_to_def_id.insert(node_id, def_id);
|
|
|
|
|
}
|
|
|
|
|
assert_eq!(self.def_id_to_node_id.push(node_id), def_id);
|
|
|
|
|
|
|
|
|
|
def_id
|
|
|
|
|
}
|
2018-01-01 07:31:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-10 04:37:10 +00:00
|
|
|
|
impl<'a> Resolver<'a> {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
pub fn new(
|
|
|
|
|
session: &'a Session,
|
|
|
|
|
krate: &Crate,
|
|
|
|
|
crate_name: &str,
|
|
|
|
|
metadata_loader: &'a MetadataLoaderDyn,
|
|
|
|
|
arenas: &'a ResolverArenas<'a>,
|
|
|
|
|
) -> Resolver<'a> {
|
2016-12-20 08:32:15 +00:00
|
|
|
|
let root_def_id = DefId::local(CRATE_DEF_INDEX);
|
2019-12-24 22:38:22 +00:00
|
|
|
|
let root_module_kind = ModuleKind::Def(DefKind::Mod, root_def_id, kw::Invalid);
|
2016-11-26 12:47:52 +00:00
|
|
|
|
let graph_root = arenas.alloc_module(ModuleData {
|
2020-07-30 01:27:50 +00:00
|
|
|
|
no_implicit_prelude: session.contains_name(&krate.attrs, sym::no_implicit_prelude),
|
2019-07-15 22:04:05 +00:00
|
|
|
|
..ModuleData::new(None, root_module_kind, root_def_id, ExpnId::root(), krate.span)
|
2016-09-19 05:25:17 +00:00
|
|
|
|
});
|
2019-12-24 22:38:22 +00:00
|
|
|
|
let empty_module_kind = ModuleKind::Def(DefKind::Mod, root_def_id, kw::Invalid);
|
2019-08-25 19:58:03 +00:00
|
|
|
|
let empty_module = arenas.alloc_module(ModuleData {
|
|
|
|
|
no_implicit_prelude: true,
|
|
|
|
|
..ModuleData::new(
|
|
|
|
|
Some(graph_root),
|
|
|
|
|
empty_module_kind,
|
|
|
|
|
root_def_id,
|
|
|
|
|
ExpnId::root(),
|
|
|
|
|
DUMMY_SP,
|
|
|
|
|
)
|
|
|
|
|
});
|
2018-10-16 08:44:26 +00:00
|
|
|
|
let mut module_map = FxHashMap::default();
|
2020-04-07 23:29:50 +00:00
|
|
|
|
module_map.insert(LocalDefId { local_def_index: CRATE_DEF_INDEX }, graph_root);
|
2014-05-28 19:36:05 +00:00
|
|
|
|
|
2020-06-21 14:49:38 +00:00
|
|
|
|
let definitions = Definitions::new(crate_name, session.local_crate_disambiguator());
|
|
|
|
|
let root = definitions.get_root_def();
|
2020-06-20 18:59:29 +00:00
|
|
|
|
|
|
|
|
|
let mut def_id_to_span = IndexVec::default();
|
|
|
|
|
assert_eq!(def_id_to_span.push(rustc_span::DUMMY_SP), root);
|
|
|
|
|
let mut def_id_to_node_id = IndexVec::default();
|
|
|
|
|
assert_eq!(def_id_to_node_id.push(CRATE_NODE_ID), root);
|
|
|
|
|
let mut node_id_to_def_id = FxHashMap::default();
|
|
|
|
|
node_id_to_def_id.insert(CRATE_NODE_ID, root);
|
|
|
|
|
|
|
|
|
|
let mut invocation_parents = FxHashMap::default();
|
|
|
|
|
invocation_parents.insert(ExpnId::root(), root);
|
2016-09-14 09:55:20 +00:00
|
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
|
let mut extern_prelude: FxHashMap<Ident, ExternPreludeEntry<'_>> = session
|
|
|
|
|
.opts
|
|
|
|
|
.externs
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|(_, entry)| entry.add_prelude)
|
|
|
|
|
.map(|(name, _)| (Ident::from_str(name), Default::default()))
|
|
|
|
|
.collect();
|
2018-10-13 16:07:17 +00:00
|
|
|
|
|
2020-07-30 01:27:50 +00:00
|
|
|
|
if !session.contains_name(&krate.attrs, sym::no_core) {
|
2019-08-10 23:20:18 +00:00
|
|
|
|
extern_prelude.insert(Ident::with_dummy_span(sym::core), Default::default());
|
2020-07-30 01:27:50 +00:00
|
|
|
|
if !session.contains_name(&krate.attrs, sym::no_std) {
|
2019-08-10 23:20:18 +00:00
|
|
|
|
extern_prelude.insert(Ident::with_dummy_span(sym::std), Default::default());
|
2018-10-13 18:24:50 +00:00
|
|
|
|
if session.rust_2018() {
|
2019-08-10 23:20:18 +00:00
|
|
|
|
extern_prelude.insert(Ident::with_dummy_span(sym::meta), Default::default());
|
2018-10-13 18:24:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-13 16:07:17 +00:00
|
|
|
|
|
2019-11-03 17:28:20 +00:00
|
|
|
|
let (registered_attrs, registered_tools) =
|
|
|
|
|
macros::registered_attrs_and_tools(session, &krate.attrs);
|
|
|
|
|
|
2019-08-12 20:39:49 +00:00
|
|
|
|
let mut invocation_parent_scopes = FxHashMap::default();
|
2019-08-15 17:47:15 +00:00
|
|
|
|
invocation_parent_scopes.insert(ExpnId::root(), ParentScope::module(graph_root));
|
2016-09-19 07:27:20 +00:00
|
|
|
|
|
2019-06-22 13:18:05 +00:00
|
|
|
|
let features = session.features_untracked();
|
2019-07-02 22:44:04 +00:00
|
|
|
|
let non_macro_attr =
|
|
|
|
|
|mark_used| Lrc::new(SyntaxExtension::non_macro_attr(mark_used, session.edition()));
|
2019-06-17 09:29:56 +00:00
|
|
|
|
|
2014-05-28 19:36:05 +00:00
|
|
|
|
Resolver {
|
2017-08-07 05:54:09 +00:00
|
|
|
|
session,
|
2014-05-28 19:36:05 +00:00
|
|
|
|
|
2017-08-07 05:54:09 +00:00
|
|
|
|
definitions,
|
2014-11-23 09:29:41 +00:00
|
|
|
|
|
2014-05-28 19:36:05 +00:00
|
|
|
|
// The outermost module has def ID 0; this is not reflected in the
|
|
|
|
|
// AST.
|
2017-08-07 05:54:09 +00:00
|
|
|
|
graph_root,
|
2016-06-05 09:56:05 +00:00
|
|
|
|
prelude: None,
|
2018-10-13 16:07:17 +00:00
|
|
|
|
extern_prelude,
|
2014-05-28 19:36:05 +00:00
|
|
|
|
|
2018-10-16 08:44:26 +00:00
|
|
|
|
has_self: FxHashSet::default(),
|
|
|
|
|
field_names: FxHashMap::default(),
|
2014-05-28 19:36:05 +00:00
|
|
|
|
|
2016-08-15 08:19:09 +00:00
|
|
|
|
determined_imports: Vec::new(),
|
2016-08-17 00:52:18 +00:00
|
|
|
|
indeterminate_imports: Vec::new(),
|
2014-05-28 19:36:05 +00:00
|
|
|
|
|
2018-11-10 15:58:37 +00:00
|
|
|
|
last_import_segment: false,
|
2020-07-08 13:36:52 +00:00
|
|
|
|
unusable_binding: None,
|
2014-05-28 19:36:05 +00:00
|
|
|
|
|
|
|
|
|
primitive_type_table: PrimitiveTypeTable::new(),
|
|
|
|
|
|
2019-05-04 12:18:58 +00:00
|
|
|
|
partial_res_map: Default::default(),
|
|
|
|
|
import_res_map: Default::default(),
|
2019-05-04 14:22:00 +00:00
|
|
|
|
label_res_map: Default::default(),
|
2019-10-13 22:08:13 +00:00
|
|
|
|
extern_crate_map: Default::default(),
|
2018-10-16 08:44:26 +00:00
|
|
|
|
export_map: FxHashMap::default(),
|
2018-07-21 19:15:11 +00:00
|
|
|
|
trait_map: Default::default(),
|
2019-09-09 20:04:26 +00:00
|
|
|
|
underscore_disambiguator: 0,
|
2019-08-25 19:58:03 +00:00
|
|
|
|
empty_module,
|
2017-08-07 05:54:09 +00:00
|
|
|
|
module_map,
|
2018-07-21 19:15:11 +00:00
|
|
|
|
block_map: Default::default(),
|
2018-10-16 08:44:26 +00:00
|
|
|
|
extern_module_map: FxHashMap::default(),
|
|
|
|
|
binding_parent_modules: FxHashMap::default(),
|
2019-08-25 19:58:03 +00:00
|
|
|
|
ast_transform_scopes: FxHashMap::default(),
|
2014-05-28 19:36:05 +00:00
|
|
|
|
|
2018-07-21 19:15:11 +00:00
|
|
|
|
glob_map: Default::default(),
|
2015-05-14 11:40:16 +00:00
|
|
|
|
|
2018-10-16 08:44:26 +00:00
|
|
|
|
used_imports: FxHashSet::default(),
|
2018-07-21 19:15:11 +00:00
|
|
|
|
maybe_unused_trait_imports: Default::default(),
|
2017-06-24 08:48:27 +00:00
|
|
|
|
maybe_unused_extern_crates: Vec::new(),
|
2016-04-19 13:43:10 +00:00
|
|
|
|
|
2016-02-25 04:40:46 +00:00
|
|
|
|
privacy_errors: Vec::new(),
|
2016-08-22 08:30:07 +00:00
|
|
|
|
ambiguity_errors: Vec::new(),
|
2017-08-17 09:03:59 +00:00
|
|
|
|
use_injections: Vec::new(),
|
2018-08-11 11:33:43 +00:00
|
|
|
|
macro_expanded_macro_export_errors: BTreeSet::new(),
|
2016-01-11 21:19:29 +00:00
|
|
|
|
|
2017-08-07 05:54:09 +00:00
|
|
|
|
arenas,
|
2016-08-22 04:05:49 +00:00
|
|
|
|
dummy_binding: arenas.alloc_name_binding(NameBinding {
|
2019-04-20 16:36:05 +00:00
|
|
|
|
kind: NameBindingKind::Res(Res::Err, false),
|
2018-12-29 15:15:29 +00:00
|
|
|
|
ambiguity: None,
|
2019-07-15 22:04:05 +00:00
|
|
|
|
expansion: ExpnId::root(),
|
2016-08-22 04:05:49 +00:00
|
|
|
|
span: DUMMY_SP,
|
|
|
|
|
vis: ty::Visibility::Public,
|
|
|
|
|
}),
|
2017-01-09 09:31:14 +00:00
|
|
|
|
|
2019-10-20 00:28:36 +00:00
|
|
|
|
crate_loader: CrateLoader::new(session, metadata_loader, crate_name),
|
2018-10-16 08:44:26 +00:00
|
|
|
|
macro_names: FxHashSet::default(),
|
2019-06-20 08:52:31 +00:00
|
|
|
|
builtin_macros: Default::default(),
|
2019-11-03 17:28:20 +00:00
|
|
|
|
registered_attrs,
|
|
|
|
|
registered_tools,
|
2018-10-16 08:44:26 +00:00
|
|
|
|
macro_use_prelude: FxHashMap::default(),
|
|
|
|
|
all_macros: FxHashMap::default(),
|
|
|
|
|
macro_map: FxHashMap::default(),
|
2019-07-02 22:44:04 +00:00
|
|
|
|
dummy_ext_bang: Lrc::new(SyntaxExtension::dummy_bang(session.edition())),
|
|
|
|
|
dummy_ext_derive: Lrc::new(SyntaxExtension::dummy_derive(session.edition())),
|
2019-06-17 09:29:56 +00:00
|
|
|
|
non_macro_attrs: [non_macro_attr(false), non_macro_attr(true)],
|
2019-08-12 20:39:49 +00:00
|
|
|
|
invocation_parent_scopes,
|
2020-03-13 22:06:36 +00:00
|
|
|
|
output_macro_rules_scopes: Default::default(),
|
2019-10-03 22:53:20 +00:00
|
|
|
|
helper_attrs: Default::default(),
|
2018-10-16 08:44:26 +00:00
|
|
|
|
local_macro_def_scopes: FxHashMap::default(),
|
|
|
|
|
name_already_seen: FxHashMap::default(),
|
2017-01-14 07:35:54 +00:00
|
|
|
|
potentially_unused_imports: Vec::new(),
|
2018-07-21 19:15:11 +00:00
|
|
|
|
struct_constructors: Default::default(),
|
2019-06-20 08:52:31 +00:00
|
|
|
|
unused_macros: Default::default(),
|
2019-07-02 22:44:04 +00:00
|
|
|
|
proc_macro_stubs: Default::default(),
|
2019-08-12 18:52:37 +00:00
|
|
|
|
single_segment_macro_resolutions: Default::default(),
|
|
|
|
|
multi_segment_macro_resolutions: Default::default(),
|
|
|
|
|
builtin_attrs: Default::default(),
|
2019-11-04 09:09:58 +00:00
|
|
|
|
containers_deriving_copy: Default::default(),
|
2019-12-24 22:38:22 +00:00
|
|
|
|
active_features: features
|
|
|
|
|
.declared_lib_features
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(feat, ..)| *feat)
|
|
|
|
|
.chain(features.declared_lang_features.iter().map(|(feat, ..)| *feat))
|
|
|
|
|
.collect(),
|
2019-10-25 13:15:33 +00:00
|
|
|
|
variant_vis: Default::default(),
|
2020-01-05 08:40:16 +00:00
|
|
|
|
lint_buffer: LintBuffer::default(),
|
2019-11-03 22:38:02 +00:00
|
|
|
|
next_node_id: NodeId::from_u32(1),
|
2020-06-20 18:59:29 +00:00
|
|
|
|
def_id_to_span,
|
|
|
|
|
node_id_to_def_id,
|
|
|
|
|
def_id_to_node_id,
|
|
|
|
|
placeholder_field_indices: Default::default(),
|
|
|
|
|
invocation_parents,
|
2020-06-21 22:49:06 +00:00
|
|
|
|
next_disambiguator: Default::default(),
|
2019-11-03 22:38:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn next_node_id(&mut self) -> NodeId {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
let next = self
|
|
|
|
|
.next_node_id
|
|
|
|
|
.as_usize()
|
2019-11-04 13:22:52 +00:00
|
|
|
|
.checked_add(1)
|
|
|
|
|
.expect("input too large; ran out of NodeIds");
|
|
|
|
|
self.next_node_id = ast::NodeId::from_usize(next);
|
|
|
|
|
self.next_node_id
|
2016-01-11 21:19:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-05 08:40:16 +00:00
|
|
|
|
pub fn lint_buffer(&mut self) -> &mut LintBuffer {
|
2019-10-25 17:41:51 +00:00
|
|
|
|
&mut self.lint_buffer
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-22 01:54:34 +00:00
|
|
|
|
pub fn arenas() -> ResolverArenas<'a> {
|
2018-10-16 14:57:53 +00:00
|
|
|
|
Default::default()
|
2014-05-28 19:36:05 +00:00
|
|
|
|
}
|
2012-05-22 17:54:12 +00:00
|
|
|
|
|
2019-10-20 00:19:12 +00:00
|
|
|
|
pub fn into_outputs(self) -> ResolverOutputs {
|
2020-05-20 20:52:30 +00:00
|
|
|
|
let definitions = self.definitions;
|
2020-05-24 11:18:22 +00:00
|
|
|
|
let extern_crate_map = self.extern_crate_map;
|
2020-06-07 10:14:47 +00:00
|
|
|
|
let export_map = self.export_map;
|
2020-05-24 11:18:22 +00:00
|
|
|
|
let maybe_unused_trait_imports = self.maybe_unused_trait_imports;
|
|
|
|
|
let maybe_unused_extern_crates = self.maybe_unused_extern_crates;
|
|
|
|
|
let glob_map = self.glob_map;
|
2019-10-20 00:19:12 +00:00
|
|
|
|
ResolverOutputs {
|
2020-05-20 20:52:30 +00:00
|
|
|
|
definitions: definitions,
|
2019-10-20 00:28:36 +00:00
|
|
|
|
cstore: Box::new(self.crate_loader.into_cstore()),
|
2020-05-20 23:08:49 +00:00
|
|
|
|
extern_crate_map,
|
2020-05-20 21:54:12 +00:00
|
|
|
|
export_map,
|
2020-05-20 22:18:45 +00:00
|
|
|
|
glob_map,
|
2020-05-20 22:01:48 +00:00
|
|
|
|
maybe_unused_trait_imports,
|
2020-05-20 22:11:56 +00:00
|
|
|
|
maybe_unused_extern_crates,
|
2019-12-24 22:38:22 +00:00
|
|
|
|
extern_prelude: self
|
|
|
|
|
.extern_prelude
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(ident, entry)| (ident.name, entry.introduced_by_item))
|
|
|
|
|
.collect(),
|
2019-10-20 00:19:12 +00:00
|
|
|
|
}
|
2019-10-19 23:55:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-20 00:19:12 +00:00
|
|
|
|
pub fn clone_outputs(&self) -> ResolverOutputs {
|
|
|
|
|
ResolverOutputs {
|
|
|
|
|
definitions: self.definitions.clone(),
|
2019-10-20 00:28:36 +00:00
|
|
|
|
cstore: Box::new(self.cstore().clone()),
|
2020-05-24 11:18:22 +00:00
|
|
|
|
extern_crate_map: self.extern_crate_map.clone(),
|
2020-06-07 10:14:47 +00:00
|
|
|
|
export_map: self.export_map.clone(),
|
2020-05-24 11:18:22 +00:00
|
|
|
|
glob_map: self.glob_map.clone(),
|
|
|
|
|
maybe_unused_trait_imports: self.maybe_unused_trait_imports.clone(),
|
|
|
|
|
maybe_unused_extern_crates: self.maybe_unused_extern_crates.clone(),
|
2019-12-24 22:38:22 +00:00
|
|
|
|
extern_prelude: self
|
|
|
|
|
.extern_prelude
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(ident, entry)| (ident.name, entry.introduced_by_item))
|
|
|
|
|
.collect(),
|
2019-10-20 00:19:12 +00:00
|
|
|
|
}
|
2019-10-19 23:55:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-20 00:28:36 +00:00
|
|
|
|
pub fn cstore(&self) -> &CStore {
|
|
|
|
|
self.crate_loader.cstore()
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-17 09:29:56 +00:00
|
|
|
|
fn non_macro_attr(&self, mark_used: bool) -> Lrc<SyntaxExtension> {
|
|
|
|
|
self.non_macro_attrs[mark_used as usize].clone()
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-02 22:44:04 +00:00
|
|
|
|
fn dummy_ext(&self, macro_kind: MacroKind) -> Lrc<SyntaxExtension> {
|
|
|
|
|
match macro_kind {
|
|
|
|
|
MacroKind::Bang => self.dummy_ext_bang.clone(),
|
|
|
|
|
MacroKind::Derive => self.dummy_ext_derive.clone(),
|
|
|
|
|
MacroKind::Attr => self.non_macro_attr(true),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-18 17:01:33 +00:00
|
|
|
|
/// Runs the function on each namespace.
|
2018-04-29 23:20:14 +00:00
|
|
|
|
fn per_ns<F: FnMut(&mut Self, Namespace)>(&mut self, mut f: F) {
|
|
|
|
|
f(self, TypeNS);
|
|
|
|
|
f(self, ValueNS);
|
2018-05-14 00:22:52 +00:00
|
|
|
|
f(self, MacroNS);
|
2016-11-10 06:19:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 23:13:36 +00:00
|
|
|
|
fn is_builtin_macro(&mut self, res: Res) -> bool {
|
|
|
|
|
self.get_macro(res).map_or(false, |ext| ext.is_builtin)
|
2019-06-20 08:52:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-28 07:07:44 +00:00
|
|
|
|
fn macro_def(&self, mut ctxt: SyntaxContext) -> DefId {
|
|
|
|
|
loop {
|
2020-05-22 20:57:25 +00:00
|
|
|
|
match ctxt.outer_expn().expn_data().macro_def_id {
|
|
|
|
|
Some(def_id) => return def_id,
|
2017-11-28 07:07:44 +00:00
|
|
|
|
None => ctxt.remove_mark(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-22 01:54:34 +00:00
|
|
|
|
/// Entry point to crate resolution.
|
|
|
|
|
pub fn resolve_crate(&mut self, krate: &Crate) {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
let _prof_timer = self.session.prof.generic_activity("resolve_crate");
|
2019-10-08 12:05:41 +00:00
|
|
|
|
|
2019-08-08 11:06:42 +00:00
|
|
|
|
ImportResolver { r: self }.finalize_imports();
|
2019-08-12 18:52:37 +00:00
|
|
|
|
self.finalize_macro_resolutions();
|
2017-08-17 09:03:59 +00:00
|
|
|
|
|
2019-08-07 23:39:02 +00:00
|
|
|
|
self.late_resolve_crate(krate);
|
2016-06-22 01:54:34 +00:00
|
|
|
|
|
2019-08-08 20:32:58 +00:00
|
|
|
|
self.check_unused(krate);
|
2017-08-17 09:03:59 +00:00
|
|
|
|
self.report_errors(krate);
|
2016-09-16 02:52:09 +00:00
|
|
|
|
self.crate_loader.postprocess(krate);
|
2016-06-22 01:54:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 17:06:45 +00:00
|
|
|
|
fn get_traits_in_module_containing_item(
|
|
|
|
|
&mut self,
|
|
|
|
|
ident: Ident,
|
|
|
|
|
ns: Namespace,
|
|
|
|
|
module: Module<'a>,
|
|
|
|
|
found_traits: &mut Vec<TraitCandidate>,
|
|
|
|
|
parent_scope: &ParentScope<'a>,
|
|
|
|
|
) {
|
|
|
|
|
assert!(ns == TypeNS || ns == ValueNS);
|
2020-08-08 17:21:19 +00:00
|
|
|
|
module.ensure_traits(self);
|
|
|
|
|
let traits = module.traits.borrow();
|
2020-08-08 17:06:45 +00:00
|
|
|
|
|
|
|
|
|
for &(trait_name, binding) in traits.as_ref().unwrap().iter() {
|
|
|
|
|
// Traits have pseudo-modules that can be used to search for the given ident.
|
|
|
|
|
if let Some(module) = binding.module() {
|
|
|
|
|
let mut ident = ident;
|
|
|
|
|
if ident.span.glob_adjust(module.expansion, binding.span).is_none() {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if self
|
|
|
|
|
.resolve_ident_in_module_unadjusted(
|
|
|
|
|
ModuleOrUniformRoot::Module(module),
|
|
|
|
|
ident,
|
|
|
|
|
ns,
|
|
|
|
|
parent_scope,
|
|
|
|
|
false,
|
|
|
|
|
module.span,
|
|
|
|
|
)
|
|
|
|
|
.is_ok()
|
|
|
|
|
{
|
|
|
|
|
let import_ids = self.find_transitive_imports(&binding.kind, trait_name);
|
|
|
|
|
let trait_def_id = module.def_id().unwrap();
|
|
|
|
|
found_traits.push(TraitCandidate { def_id: trait_def_id, import_ids });
|
|
|
|
|
}
|
|
|
|
|
} else if let Res::Def(DefKind::TraitAlias, _) = binding.res() {
|
|
|
|
|
// For now, just treat all trait aliases as possible candidates, since we don't
|
|
|
|
|
// know if the ident is somewhere in the transitive bounds.
|
|
|
|
|
let import_ids = self.find_transitive_imports(&binding.kind, trait_name);
|
|
|
|
|
let trait_def_id = binding.res().def_id();
|
|
|
|
|
found_traits.push(TraitCandidate { def_id: trait_def_id, import_ids });
|
|
|
|
|
} else {
|
|
|
|
|
bug!("candidate is not trait or trait alias?")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn find_transitive_imports(
|
|
|
|
|
&mut self,
|
|
|
|
|
mut kind: &NameBindingKind<'_>,
|
|
|
|
|
trait_name: Ident,
|
|
|
|
|
) -> SmallVec<[LocalDefId; 1]> {
|
|
|
|
|
let mut import_ids = smallvec![];
|
|
|
|
|
while let NameBindingKind::Import { import, binding, .. } = kind {
|
|
|
|
|
let id = self.local_def_id(import.id);
|
|
|
|
|
self.maybe_unused_trait_imports.insert(id);
|
|
|
|
|
self.add_to_glob_map(&import, trait_name);
|
|
|
|
|
import_ids.push(id);
|
|
|
|
|
kind = &binding.kind;
|
|
|
|
|
}
|
|
|
|
|
import_ids
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-10 11:58:41 +00:00
|
|
|
|
fn new_module(
|
|
|
|
|
&self,
|
|
|
|
|
parent: Module<'a>,
|
|
|
|
|
kind: ModuleKind,
|
|
|
|
|
normal_ancestor_id: DefId,
|
2019-07-15 22:42:58 +00:00
|
|
|
|
expn_id: ExpnId,
|
2017-05-10 11:58:41 +00:00
|
|
|
|
span: Span,
|
|
|
|
|
) -> Module<'a> {
|
2019-07-15 22:42:58 +00:00
|
|
|
|
let module = ModuleData::new(Some(parent), kind, normal_ancestor_id, expn_id, span);
|
2017-03-22 08:39:51 +00:00
|
|
|
|
self.arenas.alloc_module(module)
|
2016-01-29 22:21:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 20:04:26 +00:00
|
|
|
|
fn new_key(&mut self, ident: Ident, ns: Namespace) -> BindingKey {
|
2020-03-13 22:36:46 +00:00
|
|
|
|
let ident = ident.normalize_to_macros_2_0();
|
2019-09-09 20:04:26 +00:00
|
|
|
|
let disambiguator = if ident.name == kw::Underscore {
|
|
|
|
|
self.underscore_disambiguator += 1;
|
|
|
|
|
self.underscore_disambiguator
|
|
|
|
|
} else {
|
|
|
|
|
0
|
|
|
|
|
};
|
|
|
|
|
BindingKey { ident, ns, disambiguator }
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-15 22:18:14 +00:00
|
|
|
|
fn resolutions(&mut self, module: Module<'a>) -> &'a Resolutions<'a> {
|
|
|
|
|
if module.populate_on_access.get() {
|
|
|
|
|
module.populate_on_access.set(false);
|
2019-08-16 18:19:43 +00:00
|
|
|
|
self.build_reduced_graph_external(module);
|
2019-08-15 22:18:14 +00:00
|
|
|
|
}
|
|
|
|
|
&module.lazy_resolutions
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
|
fn resolution(
|
|
|
|
|
&mut self,
|
|
|
|
|
module: Module<'a>,
|
|
|
|
|
key: BindingKey,
|
|
|
|
|
) -> &'a RefCell<NameResolution<'a>> {
|
|
|
|
|
*self
|
|
|
|
|
.resolutions(module)
|
|
|
|
|
.borrow_mut()
|
|
|
|
|
.entry(key)
|
|
|
|
|
.or_insert_with(|| self.arenas.alloc_name_resolution())
|
2019-08-15 22:18:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
|
fn record_use(
|
|
|
|
|
&mut self,
|
|
|
|
|
ident: Ident,
|
|
|
|
|
ns: Namespace,
|
|
|
|
|
used_binding: &'a NameBinding<'a>,
|
|
|
|
|
is_lexical_scope: bool,
|
|
|
|
|
) {
|
2018-12-29 15:15:29 +00:00
|
|
|
|
if let Some((b2, kind)) = used_binding.ambiguity {
|
|
|
|
|
self.ambiguity_errors.push(AmbiguityError {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
kind,
|
|
|
|
|
ident,
|
|
|
|
|
b1: used_binding,
|
|
|
|
|
b2,
|
2018-12-29 15:15:29 +00:00
|
|
|
|
misc1: AmbiguityErrorMisc::None,
|
|
|
|
|
misc2: AmbiguityErrorMisc::None,
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-03-07 16:02:32 +00:00
|
|
|
|
if let NameBindingKind::Import { import, binding, ref used } = used_binding.kind {
|
2018-12-29 15:15:29 +00:00
|
|
|
|
// Avoid marking `extern crate` items that refer to a name from extern prelude,
|
|
|
|
|
// but not introduce it, as used if they are accessed from lexical scope.
|
|
|
|
|
if is_lexical_scope {
|
2020-03-13 22:36:46 +00:00
|
|
|
|
if let Some(entry) = self.extern_prelude.get(&ident.normalize_to_macros_2_0()) {
|
2018-12-29 15:15:29 +00:00
|
|
|
|
if let Some(crate_item) = entry.extern_crate_item {
|
|
|
|
|
if ptr::eq(used_binding, crate_item) && !entry.introduced_by_item {
|
|
|
|
|
return;
|
2018-11-13 23:17:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-05 05:27:58 +00:00
|
|
|
|
}
|
2018-12-29 15:15:29 +00:00
|
|
|
|
used.set(true);
|
2020-03-07 16:02:32 +00:00
|
|
|
|
import.used.set(true);
|
|
|
|
|
self.used_imports.insert((import.id, ns));
|
|
|
|
|
self.add_to_glob_map(&import, ident);
|
2018-12-29 15:15:29 +00:00
|
|
|
|
self.record_use(ident, ns, binding, false);
|
2016-09-05 04:55:12 +00:00
|
|
|
|
}
|
2016-07-29 16:04:45 +00:00
|
|
|
|
}
|
2014-11-23 09:29:41 +00:00
|
|
|
|
|
2019-01-06 00:22:52 +00:00
|
|
|
|
#[inline]
|
2020-03-07 16:02:32 +00:00
|
|
|
|
fn add_to_glob_map(&mut self, import: &Import<'_>, ident: Ident) {
|
|
|
|
|
if import.is_glob() {
|
2020-06-20 18:59:29 +00:00
|
|
|
|
let def_id = self.local_def_id(import.id);
|
2020-05-24 11:18:22 +00:00
|
|
|
|
self.glob_map.entry(def_id).or_default().insert(ident.name);
|
2016-07-29 16:04:45 +00:00
|
|
|
|
}
|
2014-11-23 09:29:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 20:05:35 +00:00
|
|
|
|
/// A generic scope visitor.
|
|
|
|
|
/// Visits scopes in order to resolve some identifier in them or perform other actions.
|
|
|
|
|
/// If the callback returns `Some` result, we stop visiting scopes and return it.
|
|
|
|
|
fn visit_scopes<T>(
|
|
|
|
|
&mut self,
|
|
|
|
|
scope_set: ScopeSet,
|
|
|
|
|
parent_scope: &ParentScope<'a>,
|
2019-07-14 20:04:51 +00:00
|
|
|
|
ident: Ident,
|
2019-06-20 08:52:31 +00:00
|
|
|
|
mut visitor: impl FnMut(&mut Self, Scope<'a>, /*use_prelude*/ bool, Ident) -> Option<T>,
|
2019-07-11 20:05:35 +00:00
|
|
|
|
) -> Option<T> {
|
|
|
|
|
// General principles:
|
|
|
|
|
// 1. Not controlled (user-defined) names should have higher priority than controlled names
|
|
|
|
|
// built into the language or standard library. This way we can add new names into the
|
|
|
|
|
// language or standard library without breaking user code.
|
|
|
|
|
// 2. "Closed set" below means new names cannot appear after the current resolution attempt.
|
|
|
|
|
// Places to search (in order of decreasing priority):
|
|
|
|
|
// (Type NS)
|
|
|
|
|
// 1. FIXME: Ribs (type parameters), there's no necessary infrastructure yet
|
|
|
|
|
// (open set, not controlled).
|
|
|
|
|
// 2. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
|
|
|
|
|
// (open, not controlled).
|
|
|
|
|
// 3. Extern prelude (open, the open part is from macro expansions, not controlled).
|
|
|
|
|
// 4. Tool modules (closed, controlled right now, but not in the future).
|
|
|
|
|
// 5. Standard library prelude (de-facto closed, controlled).
|
|
|
|
|
// 6. Language prelude (closed, controlled).
|
|
|
|
|
// (Value NS)
|
|
|
|
|
// 1. FIXME: Ribs (local variables), there's no necessary infrastructure yet
|
|
|
|
|
// (open set, not controlled).
|
|
|
|
|
// 2. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
|
|
|
|
|
// (open, not controlled).
|
|
|
|
|
// 3. Standard library prelude (de-facto closed, controlled).
|
|
|
|
|
// (Macro NS)
|
|
|
|
|
// 1-3. Derive helpers (open, not controlled). All ambiguities with other names
|
|
|
|
|
// are currently reported as errors. They should be higher in priority than preludes
|
|
|
|
|
// and probably even names in modules according to the "general principles" above. They
|
|
|
|
|
// also should be subject to restricted shadowing because are effectively produced by
|
|
|
|
|
// derives (you need to resolve the derive first to add helpers into scope), but they
|
|
|
|
|
// should be available before the derive is expanded for compatibility.
|
|
|
|
|
// It's mess in general, so we are being conservative for now.
|
2020-03-13 22:23:24 +00:00
|
|
|
|
// 1-3. `macro_rules` (open, not controlled), loop through `macro_rules` scopes. Have higher
|
2019-07-11 20:05:35 +00:00
|
|
|
|
// priority than prelude macros, but create ambiguities with macros in modules.
|
|
|
|
|
// 1-3. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
|
|
|
|
|
// (open, not controlled). Have higher priority than prelude macros, but create
|
|
|
|
|
// ambiguities with `macro_rules`.
|
|
|
|
|
// 4. `macro_use` prelude (open, the open part is from macro expansions, not controlled).
|
|
|
|
|
// 4a. User-defined prelude from macro-use
|
|
|
|
|
// (open, the open part is from macro expansions, not controlled).
|
2019-06-20 08:52:31 +00:00
|
|
|
|
// 4b. "Standard library prelude" part implemented through `macro-use` (closed, controlled).
|
|
|
|
|
// 4c. Standard library prelude (de-facto closed, controlled).
|
2019-07-11 20:05:35 +00:00
|
|
|
|
// 6. Language prelude: builtin attributes (closed, controlled).
|
|
|
|
|
|
2019-07-14 20:04:51 +00:00
|
|
|
|
let rust_2015 = ident.span.rust_2015();
|
2019-10-03 22:53:20 +00:00
|
|
|
|
let (ns, macro_kind, is_absolute_path) = match scope_set {
|
|
|
|
|
ScopeSet::All(ns, _) => (ns, None, false),
|
|
|
|
|
ScopeSet::AbsolutePath(ns) => (ns, None, true),
|
|
|
|
|
ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind), false),
|
2019-07-11 20:05:35 +00:00
|
|
|
|
};
|
2019-08-12 18:55:42 +00:00
|
|
|
|
// Jump out of trait or enum modules, they do not act as scopes.
|
|
|
|
|
let module = parent_scope.module.nearest_item_scope();
|
2019-07-11 20:05:35 +00:00
|
|
|
|
let mut scope = match ns {
|
|
|
|
|
_ if is_absolute_path => Scope::CrateRoot,
|
2019-08-12 18:55:42 +00:00
|
|
|
|
TypeNS | ValueNS => Scope::Module(module),
|
2019-10-03 22:53:20 +00:00
|
|
|
|
MacroNS => Scope::DeriveHelpers(parent_scope.expansion),
|
2019-07-11 20:05:35 +00:00
|
|
|
|
};
|
2020-03-13 22:36:46 +00:00
|
|
|
|
let mut ident = ident.normalize_to_macros_2_0();
|
2019-08-12 18:55:42 +00:00
|
|
|
|
let mut use_prelude = !module.no_implicit_prelude;
|
2019-07-11 20:05:35 +00:00
|
|
|
|
|
|
|
|
|
loop {
|
2019-07-14 20:04:51 +00:00
|
|
|
|
let visit = match scope {
|
2019-10-03 22:53:20 +00:00
|
|
|
|
// Derive helpers are not in scope when resolving derives in the same container.
|
2019-12-24 22:38:22 +00:00
|
|
|
|
Scope::DeriveHelpers(expn_id) => {
|
|
|
|
|
!(expn_id == parent_scope.expansion && macro_kind == Some(MacroKind::Derive))
|
|
|
|
|
}
|
2019-10-03 22:44:57 +00:00
|
|
|
|
Scope::DeriveHelpersCompat => true,
|
2019-07-14 20:04:51 +00:00
|
|
|
|
Scope::MacroRules(..) => true,
|
|
|
|
|
Scope::CrateRoot => true,
|
|
|
|
|
Scope::Module(..) => true,
|
2019-11-04 13:47:03 +00:00
|
|
|
|
Scope::RegisteredAttrs => use_prelude,
|
2019-07-14 20:04:51 +00:00
|
|
|
|
Scope::MacroUsePrelude => use_prelude || rust_2015,
|
|
|
|
|
Scope::BuiltinAttrs => true,
|
|
|
|
|
Scope::ExternPrelude => use_prelude || is_absolute_path,
|
|
|
|
|
Scope::ToolPrelude => use_prelude,
|
2019-06-20 08:52:31 +00:00
|
|
|
|
Scope::StdLibPrelude => use_prelude || ns == MacroNS,
|
2019-07-14 20:04:51 +00:00
|
|
|
|
Scope::BuiltinTypes => true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if visit {
|
2019-06-20 08:52:31 +00:00
|
|
|
|
if let break_result @ Some(..) = visitor(self, scope, use_prelude, ident) {
|
2019-07-14 20:04:51 +00:00
|
|
|
|
return break_result;
|
|
|
|
|
}
|
2019-07-11 20:05:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scope = match scope {
|
2019-10-03 22:53:20 +00:00
|
|
|
|
Scope::DeriveHelpers(expn_id) if expn_id != ExpnId::root() => {
|
|
|
|
|
// Derive helpers are not visible to code generated by bang or derive macros.
|
|
|
|
|
let expn_data = expn_id.expn_data();
|
|
|
|
|
match expn_data.kind {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
ExpnKind::Root
|
2020-04-17 00:38:52 +00:00
|
|
|
|
| ExpnKind::Macro(MacroKind::Bang | MacroKind::Derive, _) => {
|
|
|
|
|
Scope::DeriveHelpersCompat
|
|
|
|
|
}
|
2019-10-03 22:53:20 +00:00
|
|
|
|
_ => Scope::DeriveHelpers(expn_data.parent),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Scope::DeriveHelpers(..) => Scope::DeriveHelpersCompat,
|
2020-03-13 22:06:36 +00:00
|
|
|
|
Scope::DeriveHelpersCompat => Scope::MacroRules(parent_scope.macro_rules),
|
|
|
|
|
Scope::MacroRules(macro_rules_scope) => match macro_rules_scope {
|
|
|
|
|
MacroRulesScope::Binding(binding) => {
|
|
|
|
|
Scope::MacroRules(binding.parent_macro_rules_scope)
|
|
|
|
|
}
|
|
|
|
|
MacroRulesScope::Invocation(invoc_id) => Scope::MacroRules(
|
|
|
|
|
self.output_macro_rules_scopes
|
2019-12-24 22:38:22 +00:00
|
|
|
|
.get(&invoc_id)
|
|
|
|
|
.cloned()
|
2020-03-13 22:06:36 +00:00
|
|
|
|
.unwrap_or(self.invocation_parent_scopes[&invoc_id].macro_rules),
|
2019-07-11 20:05:35 +00:00
|
|
|
|
),
|
2020-03-13 22:06:36 +00:00
|
|
|
|
MacroRulesScope::Empty => Scope::Module(module),
|
2019-12-24 22:38:22 +00:00
|
|
|
|
},
|
2019-07-11 20:05:35 +00:00
|
|
|
|
Scope::CrateRoot => match ns {
|
|
|
|
|
TypeNS => {
|
2019-07-15 22:04:05 +00:00
|
|
|
|
ident.span.adjust(ExpnId::root());
|
2019-07-11 20:05:35 +00:00
|
|
|
|
Scope::ExternPrelude
|
|
|
|
|
}
|
|
|
|
|
ValueNS | MacroNS => break,
|
2019-12-24 22:38:22 +00:00
|
|
|
|
},
|
2019-07-11 20:05:35 +00:00
|
|
|
|
Scope::Module(module) => {
|
2019-07-14 20:04:51 +00:00
|
|
|
|
use_prelude = !module.no_implicit_prelude;
|
2019-07-11 20:05:35 +00:00
|
|
|
|
match self.hygienic_lexical_parent(module, &mut ident.span) {
|
|
|
|
|
Some(parent_module) => Scope::Module(parent_module),
|
|
|
|
|
None => {
|
2019-07-15 22:04:05 +00:00
|
|
|
|
ident.span.adjust(ExpnId::root());
|
2019-07-11 20:05:35 +00:00
|
|
|
|
match ns {
|
|
|
|
|
TypeNS => Scope::ExternPrelude,
|
|
|
|
|
ValueNS => Scope::StdLibPrelude,
|
2019-11-03 17:28:20 +00:00
|
|
|
|
MacroNS => Scope::RegisteredAttrs,
|
2019-07-11 20:05:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-03 17:28:20 +00:00
|
|
|
|
Scope::RegisteredAttrs => Scope::MacroUsePrelude,
|
2019-07-11 20:05:35 +00:00
|
|
|
|
Scope::MacroUsePrelude => Scope::StdLibPrelude,
|
2019-11-12 18:22:16 +00:00
|
|
|
|
Scope::BuiltinAttrs => break, // nowhere else to search
|
2019-07-11 20:05:35 +00:00
|
|
|
|
Scope::ExternPrelude if is_absolute_path => break,
|
|
|
|
|
Scope::ExternPrelude => Scope::ToolPrelude,
|
|
|
|
|
Scope::ToolPrelude => Scope::StdLibPrelude,
|
|
|
|
|
Scope::StdLibPrelude => match ns {
|
|
|
|
|
TypeNS => Scope::BuiltinTypes,
|
|
|
|
|
ValueNS => break, // nowhere else to search
|
2019-06-20 08:52:31 +00:00
|
|
|
|
MacroNS => Scope::BuiltinAttrs,
|
2019-12-24 22:38:22 +00:00
|
|
|
|
},
|
2019-07-11 20:05:35 +00:00
|
|
|
|
Scope::BuiltinTypes => break, // nowhere else to search
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-12 08:03:13 +00:00
|
|
|
|
/// This resolves the identifier `ident` in the namespace `ns` in the current lexical scope.
|
|
|
|
|
/// More specifically, we proceed up the hierarchy of scopes and return the binding for
|
|
|
|
|
/// `ident` in the first scope that defines it (or None if no scopes define it).
|
|
|
|
|
///
|
|
|
|
|
/// A block's items are above its local variables in the scope hierarchy, regardless of where
|
|
|
|
|
/// the items are defined in the block. For example,
|
|
|
|
|
/// ```rust
|
|
|
|
|
/// fn f() {
|
|
|
|
|
/// g(); // Since there are no local variables in scope yet, this resolves to the item.
|
|
|
|
|
/// let g = || {};
|
|
|
|
|
/// fn g() {}
|
|
|
|
|
/// g(); // This resolves to the local variable `g` since it shadows the item.
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2016-03-11 23:28:22 +00:00
|
|
|
|
///
|
2013-03-01 18:44:43 +00:00
|
|
|
|
/// Invariant: This must only be called during main resolution, not during
|
|
|
|
|
/// import resolution.
|
2019-12-24 22:38:22 +00:00
|
|
|
|
fn resolve_ident_in_lexical_scope(
|
|
|
|
|
&mut self,
|
|
|
|
|
mut ident: Ident,
|
|
|
|
|
ns: Namespace,
|
|
|
|
|
parent_scope: &ParentScope<'a>,
|
|
|
|
|
record_used_id: Option<NodeId>,
|
|
|
|
|
path_span: Span,
|
|
|
|
|
ribs: &[Rib<'a>],
|
|
|
|
|
) -> Option<LexicalScopeBinding<'a>> {
|
2019-02-28 22:43:53 +00:00
|
|
|
|
assert!(ns == TypeNS || ns == ValueNS);
|
2019-05-11 14:41:37 +00:00
|
|
|
|
if ident.name == kw::Invalid {
|
2019-04-20 16:36:05 +00:00
|
|
|
|
return Some(LexicalScopeBinding::Res(Res::Err));
|
2018-12-16 17:23:27 +00:00
|
|
|
|
}
|
2020-03-13 22:36:46 +00:00
|
|
|
|
let (general_span, normalized_span) = if ident.name == kw::SelfUpper {
|
2019-01-12 22:59:51 +00:00
|
|
|
|
// FIXME(jseyfried) improve `Self` hygiene
|
2019-08-10 22:44:55 +00:00
|
|
|
|
let empty_span = ident.span.with_ctxt(SyntaxContext::root());
|
2019-07-28 12:34:03 +00:00
|
|
|
|
(empty_span, empty_span)
|
2019-01-12 22:59:51 +00:00
|
|
|
|
} else if ns == TypeNS {
|
2020-03-13 22:36:46 +00:00
|
|
|
|
let normalized_span = ident.span.normalize_to_macros_2_0();
|
|
|
|
|
(normalized_span, normalized_span)
|
2018-06-24 16:54:23 +00:00
|
|
|
|
} else {
|
2020-03-13 22:36:46 +00:00
|
|
|
|
(ident.span.normalize_to_macro_rules(), ident.span.normalize_to_macros_2_0())
|
2019-01-12 22:59:51 +00:00
|
|
|
|
};
|
2019-07-28 12:34:03 +00:00
|
|
|
|
ident.span = general_span;
|
2020-03-13 22:36:46 +00:00
|
|
|
|
let normalized_ident = Ident { span: normalized_span, ..ident };
|
2016-03-12 08:03:13 +00:00
|
|
|
|
|
2016-03-11 23:28:22 +00:00
|
|
|
|
// Walk backwards up the ribs in scope.
|
2018-12-16 17:23:27 +00:00
|
|
|
|
let record_used = record_used_id.is_some();
|
2017-03-22 08:39:51 +00:00
|
|
|
|
let mut module = self.graph_root;
|
2019-12-24 22:38:22 +00:00
|
|
|
|
for i in (0..ribs.len()).rev() {
|
2019-08-05 18:18:50 +00:00
|
|
|
|
debug!("walk rib\n{:?}", ribs[i].bindings);
|
2019-07-28 12:34:03 +00:00
|
|
|
|
// Use the rib kind to determine whether we are resolving parameters
|
2020-03-13 22:36:46 +00:00
|
|
|
|
// (macro 2.0 hygiene) or local variables (`macro_rules` hygiene).
|
|
|
|
|
let rib_ident = if ribs[i].kind.contains_params() { normalized_ident } else { ident };
|
2019-08-05 18:18:50 +00:00
|
|
|
|
if let Some(res) = ribs[i].bindings.get(&rib_ident).cloned() {
|
2016-03-12 08:03:13 +00:00
|
|
|
|
// The ident resolves to a type parameter or local variable.
|
2019-12-24 22:38:22 +00:00
|
|
|
|
return Some(LexicalScopeBinding::Res(self.validate_res_from_ribs(
|
|
|
|
|
i,
|
|
|
|
|
rib_ident,
|
|
|
|
|
res,
|
|
|
|
|
record_used,
|
|
|
|
|
path_span,
|
|
|
|
|
ribs,
|
|
|
|
|
)));
|
2012-05-22 17:54:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-05 18:18:50 +00:00
|
|
|
|
module = match ribs[i].kind {
|
2017-03-22 08:39:51 +00:00
|
|
|
|
ModuleRibKind(module) => module,
|
2018-03-17 23:57:23 +00:00
|
|
|
|
MacroDefinition(def) if def == self.macro_def(ident.span.ctxt()) => {
|
2017-03-22 08:39:51 +00:00
|
|
|
|
// If an invocation of this macro created `ident`, give up on `ident`
|
|
|
|
|
// and switch to `ident`'s source from the macro definition.
|
2018-03-17 23:57:23 +00:00
|
|
|
|
ident.span.remove_mark();
|
2019-12-24 22:38:22 +00:00
|
|
|
|
continue;
|
2012-05-22 17:54:12 +00:00
|
|
|
|
}
|
2017-03-22 08:39:51 +00:00
|
|
|
|
_ => continue,
|
|
|
|
|
};
|
2016-03-12 08:03:13 +00:00
|
|
|
|
|
2017-03-22 08:39:51 +00:00
|
|
|
|
let item = self.resolve_ident_in_module_unadjusted(
|
2018-08-09 13:29:22 +00:00
|
|
|
|
ModuleOrUniformRoot::Module(module),
|
|
|
|
|
ident,
|
|
|
|
|
ns,
|
2019-08-05 18:18:50 +00:00
|
|
|
|
parent_scope,
|
2018-08-09 13:29:22 +00:00
|
|
|
|
record_used,
|
|
|
|
|
path_span,
|
2017-03-22 08:39:51 +00:00
|
|
|
|
);
|
|
|
|
|
if let Ok(binding) = item {
|
|
|
|
|
// The ident resolves to an item.
|
|
|
|
|
return Some(LexicalScopeBinding::Item(binding));
|
2012-05-22 17:54:12 +00:00
|
|
|
|
}
|
2016-06-26 03:32:45 +00:00
|
|
|
|
|
2017-03-22 08:39:51 +00:00
|
|
|
|
match module.kind {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
ModuleKind::Block(..) => {} // We can see through blocks
|
2017-03-22 08:39:51 +00:00
|
|
|
|
_ => break,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-13 22:36:46 +00:00
|
|
|
|
ident = normalized_ident;
|
2018-08-20 00:35:52 +00:00
|
|
|
|
let mut poisoned = None;
|
2017-03-22 08:39:51 +00:00
|
|
|
|
loop {
|
2018-08-20 00:35:52 +00:00
|
|
|
|
let opt_module = if let Some(node_id) = record_used_id {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
self.hygienic_lexical_parent_with_compatibility_fallback(
|
|
|
|
|
module,
|
|
|
|
|
&mut ident.span,
|
|
|
|
|
node_id,
|
|
|
|
|
&mut poisoned,
|
|
|
|
|
)
|
2018-07-07 20:07:06 +00:00
|
|
|
|
} else {
|
2018-08-20 00:35:52 +00:00
|
|
|
|
self.hygienic_lexical_parent(module, &mut ident.span)
|
2018-07-07 20:07:06 +00:00
|
|
|
|
};
|
|
|
|
|
module = unwrap_or!(opt_module, break);
|
2019-08-12 22:39:10 +00:00
|
|
|
|
let adjusted_parent_scope = &ParentScope { module, ..*parent_scope };
|
2017-03-22 08:39:51 +00:00
|
|
|
|
let result = self.resolve_ident_in_module_unadjusted(
|
2018-08-09 13:29:22 +00:00
|
|
|
|
ModuleOrUniformRoot::Module(module),
|
|
|
|
|
ident,
|
|
|
|
|
ns,
|
2019-08-05 18:18:50 +00:00
|
|
|
|
adjusted_parent_scope,
|
2018-08-09 13:29:22 +00:00
|
|
|
|
record_used,
|
|
|
|
|
path_span,
|
2017-03-22 08:39:51 +00:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
match result {
|
2018-07-07 20:07:06 +00:00
|
|
|
|
Ok(binding) => {
|
2018-07-21 18:14:22 +00:00
|
|
|
|
if let Some(node_id) = poisoned {
|
2019-10-25 13:15:33 +00:00
|
|
|
|
self.lint_buffer.buffer_lint_with_diagnostic(
|
2018-07-07 20:07:06 +00:00
|
|
|
|
lint::builtin::PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
|
2020-01-05 08:40:16 +00:00
|
|
|
|
node_id,
|
|
|
|
|
ident.span,
|
2018-07-07 20:07:06 +00:00
|
|
|
|
&format!("cannot find {} `{}` in this scope", ns.descr(), ident),
|
2020-01-05 08:40:16 +00:00
|
|
|
|
BuiltinLintDiagnostics::ProcMacroDeriveResolutionFallback(ident.span),
|
2018-07-07 20:07:06 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
2019-12-24 22:38:22 +00:00
|
|
|
|
return Some(LexicalScopeBinding::Item(binding));
|
2018-07-07 20:07:06 +00:00
|
|
|
|
}
|
2018-07-22 23:52:51 +00:00
|
|
|
|
Err(Determined) => continue,
|
2019-12-24 22:38:22 +00:00
|
|
|
|
Err(Undetermined) => {
|
|
|
|
|
span_bug!(ident.span, "undetermined resolution during main resolution pass")
|
|
|
|
|
}
|
2017-03-22 08:39:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-08 12:34:35 +00:00
|
|
|
|
if !module.no_implicit_prelude {
|
2019-07-15 22:04:05 +00:00
|
|
|
|
ident.span.adjust(ExpnId::root());
|
2018-09-28 22:31:54 +00:00
|
|
|
|
if ns == TypeNS {
|
2018-11-17 18:08:00 +00:00
|
|
|
|
if let Some(binding) = self.extern_prelude_get(ident, !record_used) {
|
2018-09-28 22:31:54 +00:00
|
|
|
|
return Some(LexicalScopeBinding::Item(binding));
|
|
|
|
|
}
|
2019-11-03 17:28:20 +00:00
|
|
|
|
if let Some(ident) = self.registered_tools.get(&ident) {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
let binding =
|
|
|
|
|
(Res::ToolMod, ty::Visibility::Public, ident.span, ExpnId::root())
|
|
|
|
|
.to_name_binding(self.arenas);
|
2019-11-03 17:28:20 +00:00
|
|
|
|
return Some(LexicalScopeBinding::Item(binding));
|
|
|
|
|
}
|
2018-07-22 23:52:51 +00:00
|
|
|
|
}
|
2018-04-08 12:34:35 +00:00
|
|
|
|
if let Some(prelude) = self.prelude {
|
2018-08-09 13:29:22 +00:00
|
|
|
|
if let Ok(binding) = self.resolve_ident_in_module_unadjusted(
|
|
|
|
|
ModuleOrUniformRoot::Module(prelude),
|
|
|
|
|
ident,
|
|
|
|
|
ns,
|
2019-08-05 18:18:50 +00:00
|
|
|
|
parent_scope,
|
2018-08-09 13:29:22 +00:00
|
|
|
|
false,
|
|
|
|
|
path_span,
|
|
|
|
|
) {
|
2018-04-08 12:34:35 +00:00
|
|
|
|
return Some(LexicalScopeBinding::Item(binding));
|
|
|
|
|
}
|
2017-03-22 08:39:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-08 12:34:35 +00:00
|
|
|
|
|
2019-11-18 19:22:00 +00:00
|
|
|
|
if ns == TypeNS {
|
|
|
|
|
if let Some(prim_ty) = self.primitive_type_table.primitive_types.get(&ident.name) {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
let binding =
|
|
|
|
|
(Res::PrimTy(*prim_ty), ty::Visibility::Public, DUMMY_SP, ExpnId::root())
|
|
|
|
|
.to_name_binding(self.arenas);
|
2019-11-18 19:22:00 +00:00
|
|
|
|
return Some(LexicalScopeBinding::Item(binding));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-08 12:34:35 +00:00
|
|
|
|
None
|
2017-03-22 08:39:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
|
fn hygienic_lexical_parent(
|
|
|
|
|
&mut self,
|
|
|
|
|
module: Module<'a>,
|
|
|
|
|
span: &mut Span,
|
|
|
|
|
) -> Option<Module<'a>> {
|
2019-07-15 23:59:53 +00:00
|
|
|
|
if !module.expansion.outer_expn_is_descendant_of(span.ctxt()) {
|
2018-03-17 23:57:23 +00:00
|
|
|
|
return Some(self.macro_def_scope(span.remove_mark()));
|
2017-03-22 08:39:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let ModuleKind::Block(..) = module.kind {
|
2019-09-07 14:33:50 +00:00
|
|
|
|
return Some(module.parent.unwrap().nearest_item_scope());
|
2017-03-22 08:39:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-07 20:07:06 +00:00
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
|
fn hygienic_lexical_parent_with_compatibility_fallback(
|
|
|
|
|
&mut self,
|
|
|
|
|
module: Module<'a>,
|
|
|
|
|
span: &mut Span,
|
|
|
|
|
node_id: NodeId,
|
|
|
|
|
poisoned: &mut Option<NodeId>,
|
|
|
|
|
) -> Option<Module<'a>> {
|
2018-07-07 20:07:06 +00:00
|
|
|
|
if let module @ Some(..) = self.hygienic_lexical_parent(module, span) {
|
2018-08-20 00:35:52 +00:00
|
|
|
|
return module;
|
2018-07-07 20:07:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We need to support the next case under a deprecation warning
|
|
|
|
|
// ```
|
|
|
|
|
// struct MyStruct;
|
|
|
|
|
// ---- begin: this comes from a proc macro derive
|
|
|
|
|
// mod implementation_details {
|
|
|
|
|
// // Note that `MyStruct` is not in scope here.
|
|
|
|
|
// impl SomeTrait for MyStruct { ... }
|
|
|
|
|
// }
|
|
|
|
|
// ---- end
|
|
|
|
|
// ```
|
|
|
|
|
// So we have to fall back to the module's parent during lexical resolution in this case.
|
|
|
|
|
if let Some(parent) = module.parent {
|
|
|
|
|
// Inner module is inside the macro, parent module is outside of the macro.
|
2019-12-24 22:38:22 +00:00
|
|
|
|
if module.expansion != parent.expansion
|
|
|
|
|
&& module.expansion.is_descendant_of(parent.expansion)
|
|
|
|
|
{
|
2018-07-07 20:07:06 +00:00
|
|
|
|
// The macro is a proc macro derive
|
2020-05-22 20:57:25 +00:00
|
|
|
|
if let Some(def_id) = module.expansion.expn_data().macro_def_id {
|
2019-08-21 22:29:34 +00:00
|
|
|
|
if let Some(ext) = self.get_macro_by_def_id(def_id) {
|
|
|
|
|
if !ext.is_builtin && ext.macro_kind() == MacroKind::Derive {
|
|
|
|
|
if parent.expansion.outer_expn_is_descendant_of(span.ctxt()) {
|
|
|
|
|
*poisoned = Some(node_id);
|
|
|
|
|
return module.parent;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-07 20:07:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-26 03:32:45 +00:00
|
|
|
|
}
|
2012-05-22 17:54:12 +00:00
|
|
|
|
}
|
2016-03-08 21:44:19 +00:00
|
|
|
|
|
2018-08-20 00:35:52 +00:00
|
|
|
|
None
|
2016-03-12 08:03:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-08 22:29:07 +00:00
|
|
|
|
fn resolve_ident_in_module(
|
|
|
|
|
&mut self,
|
|
|
|
|
module: ModuleOrUniformRoot<'a>,
|
2018-11-24 12:07:03 +00:00
|
|
|
|
ident: Ident,
|
2018-11-08 22:29:07 +00:00
|
|
|
|
ns: Namespace,
|
2019-08-05 18:18:50 +00:00
|
|
|
|
parent_scope: &ParentScope<'a>,
|
2018-11-08 22:29:07 +00:00
|
|
|
|
record_used: bool,
|
2019-12-24 22:38:22 +00:00
|
|
|
|
path_span: Span,
|
2018-11-08 22:29:07 +00:00
|
|
|
|
) -> Result<&'a NameBinding<'a>, Determinacy> {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
self.resolve_ident_in_module_ext(module, ident, ns, parent_scope, record_used, path_span)
|
|
|
|
|
.map_err(|(determinacy, _)| determinacy)
|
2018-11-24 12:07:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn resolve_ident_in_module_ext(
|
|
|
|
|
&mut self,
|
|
|
|
|
module: ModuleOrUniformRoot<'a>,
|
|
|
|
|
mut ident: Ident,
|
|
|
|
|
ns: Namespace,
|
2019-08-05 18:18:50 +00:00
|
|
|
|
parent_scope: &ParentScope<'a>,
|
2018-11-24 12:07:03 +00:00
|
|
|
|
record_used: bool,
|
2019-12-24 22:38:22 +00:00
|
|
|
|
path_span: Span,
|
2018-11-24 12:07:03 +00:00
|
|
|
|
) -> Result<&'a NameBinding<'a>, (Determinacy, Weak)> {
|
2019-08-05 18:18:50 +00:00
|
|
|
|
let tmp_parent_scope;
|
|
|
|
|
let mut adjusted_parent_scope = parent_scope;
|
2018-11-08 22:29:07 +00:00
|
|
|
|
match module {
|
2019-08-05 18:18:50 +00:00
|
|
|
|
ModuleOrUniformRoot::Module(m) => {
|
2020-03-13 22:36:46 +00:00
|
|
|
|
if let Some(def) = ident.span.normalize_to_macros_2_0_and_adjust(m.expansion) {
|
2019-08-05 18:18:50 +00:00
|
|
|
|
tmp_parent_scope =
|
2019-08-12 22:39:10 +00:00
|
|
|
|
ParentScope { module: self.macro_def_scope(def), ..*parent_scope };
|
2019-08-05 18:18:50 +00:00
|
|
|
|
adjusted_parent_scope = &tmp_parent_scope;
|
2018-11-08 22:29:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-24 16:14:05 +00:00
|
|
|
|
ModuleOrUniformRoot::ExternPrelude => {
|
2020-03-13 22:36:46 +00:00
|
|
|
|
ident.span.normalize_to_macros_2_0_and_adjust(ExpnId::root());
|
2018-08-09 13:29:22 +00:00
|
|
|
|
}
|
2019-12-24 22:38:22 +00:00
|
|
|
|
ModuleOrUniformRoot::CrateRootAndExternPrelude | ModuleOrUniformRoot::CurrentScope => {
|
2018-11-24 12:07:03 +00:00
|
|
|
|
// No adjustments
|
|
|
|
|
}
|
2017-03-22 08:39:51 +00:00
|
|
|
|
}
|
2020-03-21 23:20:58 +00:00
|
|
|
|
self.resolve_ident_in_module_unadjusted_ext(
|
2019-12-24 22:38:22 +00:00
|
|
|
|
module,
|
|
|
|
|
ident,
|
|
|
|
|
ns,
|
|
|
|
|
adjusted_parent_scope,
|
|
|
|
|
false,
|
|
|
|
|
record_used,
|
|
|
|
|
path_span,
|
2020-03-21 23:20:58 +00:00
|
|
|
|
)
|
2017-03-22 08:39:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-24 16:12:00 +00:00
|
|
|
|
fn resolve_crate_root(&mut self, ident: Ident) -> Module<'a> {
|
2020-03-17 15:45:02 +00:00
|
|
|
|
debug!("resolve_crate_root({:?})", ident);
|
2018-06-24 16:12:00 +00:00
|
|
|
|
let mut ctxt = ident.span.ctxt();
|
2019-05-11 14:41:37 +00:00
|
|
|
|
let mark = if ident.name == kw::DollarCrate {
|
2017-11-29 09:05:31 +00:00
|
|
|
|
// When resolving `$crate` from a `macro_rules!` invoked in a `macro`,
|
|
|
|
|
// we don't want to pretend that the `macro_rules!` definition is in the `macro`
|
2020-03-13 22:36:46 +00:00
|
|
|
|
// as described in `SyntaxContext::apply_mark`, so we ignore prepended opaque marks.
|
2018-06-29 00:45:47 +00:00
|
|
|
|
// FIXME: This is only a guess and it doesn't work correctly for `macro_rules!`
|
|
|
|
|
// definitions actually produced by `macro` and `macro` definitions produced by
|
|
|
|
|
// `macro_rules!`, but at least such configurations are not stable yet.
|
2020-03-13 22:36:46 +00:00
|
|
|
|
ctxt = ctxt.normalize_to_macro_rules();
|
2020-03-17 15:45:02 +00:00
|
|
|
|
debug!(
|
|
|
|
|
"resolve_crate_root: marks={:?}",
|
|
|
|
|
ctxt.marks().into_iter().map(|(i, t)| (i.expn_data(), t)).collect::<Vec<_>>()
|
|
|
|
|
);
|
2018-06-29 00:45:47 +00:00
|
|
|
|
let mut iter = ctxt.marks().into_iter().rev().peekable();
|
|
|
|
|
let mut result = None;
|
2020-03-13 22:36:46 +00:00
|
|
|
|
// Find the last opaque mark from the end if it exists.
|
2018-06-30 16:53:46 +00:00
|
|
|
|
while let Some(&(mark, transparency)) = iter.peek() {
|
|
|
|
|
if transparency == Transparency::Opaque {
|
2018-06-29 00:45:47 +00:00
|
|
|
|
result = Some(mark);
|
|
|
|
|
iter.next();
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-17 15:45:02 +00:00
|
|
|
|
debug!(
|
|
|
|
|
"resolve_crate_root: found opaque mark {:?} {:?}",
|
|
|
|
|
result,
|
|
|
|
|
result.map(|r| r.expn_data())
|
|
|
|
|
);
|
2020-03-13 22:23:24 +00:00
|
|
|
|
// Then find the last semi-transparent mark from the end if it exists.
|
2018-06-30 16:53:46 +00:00
|
|
|
|
for (mark, transparency) in iter {
|
|
|
|
|
if transparency == Transparency::SemiTransparent {
|
2018-06-29 00:45:47 +00:00
|
|
|
|
result = Some(mark);
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-17 15:45:02 +00:00
|
|
|
|
debug!(
|
|
|
|
|
"resolve_crate_root: found semi-transparent mark {:?} {:?}",
|
|
|
|
|
result,
|
|
|
|
|
result.map(|r| r.expn_data())
|
|
|
|
|
);
|
2018-06-29 00:45:47 +00:00
|
|
|
|
result
|
2017-11-29 09:05:31 +00:00
|
|
|
|
} else {
|
2020-03-17 15:45:02 +00:00
|
|
|
|
debug!("resolve_crate_root: not DollarCrate");
|
2020-03-13 22:36:46 +00:00
|
|
|
|
ctxt = ctxt.normalize_to_macros_2_0();
|
2019-07-15 22:04:05 +00:00
|
|
|
|
ctxt.adjust(ExpnId::root())
|
2017-11-29 09:05:31 +00:00
|
|
|
|
};
|
|
|
|
|
let module = match mark {
|
2017-03-22 08:39:51 +00:00
|
|
|
|
Some(def) => self.macro_def_scope(def),
|
2020-03-17 15:45:02 +00:00
|
|
|
|
None => {
|
|
|
|
|
debug!(
|
|
|
|
|
"resolve_crate_root({:?}): found no mark (ident.span = {:?})",
|
|
|
|
|
ident, ident.span
|
|
|
|
|
);
|
|
|
|
|
return self.graph_root;
|
|
|
|
|
}
|
2017-03-22 08:39:51 +00:00
|
|
|
|
};
|
2020-03-17 15:45:02 +00:00
|
|
|
|
let module = self.get_module(DefId { index: CRATE_DEF_INDEX, ..module.normal_ancestor_id });
|
|
|
|
|
debug!(
|
|
|
|
|
"resolve_crate_root({:?}): got module {:?} ({:?}) (ident.span = {:?})",
|
|
|
|
|
ident,
|
|
|
|
|
module,
|
|
|
|
|
module.kind.name(),
|
|
|
|
|
ident.span
|
|
|
|
|
);
|
|
|
|
|
module
|
2017-03-22 08:39:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn resolve_self(&mut self, ctxt: &mut SyntaxContext, module: Module<'a>) -> Module<'a> {
|
|
|
|
|
let mut module = self.get_module(module.normal_ancestor_id);
|
2020-03-13 22:36:46 +00:00
|
|
|
|
while module.span.ctxt().normalize_to_macros_2_0() != *ctxt {
|
2017-03-22 08:39:51 +00:00
|
|
|
|
let parent = module.parent.unwrap_or_else(|| self.macro_def_scope(ctxt.remove_mark()));
|
|
|
|
|
module = self.get_module(parent.normal_ancestor_id);
|
2016-10-23 02:44:36 +00:00
|
|
|
|
}
|
2017-03-22 08:39:51 +00:00
|
|
|
|
module
|
2016-10-23 02:44:36 +00:00
|
|
|
|
}
|
2019-08-05 18:18:50 +00:00
|
|
|
|
|
|
|
|
|
fn resolve_path(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &[Segment],
|
|
|
|
|
opt_ns: Option<Namespace>, // `None` indicates a module path in import
|
2019-08-07 23:39:02 +00:00
|
|
|
|
parent_scope: &ParentScope<'a>,
|
2019-08-05 18:18:50 +00:00
|
|
|
|
record_used: bool,
|
|
|
|
|
path_span: Span,
|
|
|
|
|
crate_lint: CrateLint,
|
|
|
|
|
) -> PathResult<'a> {
|
2019-08-07 23:39:02 +00:00
|
|
|
|
self.resolve_path_with_ribs(
|
2019-12-24 22:38:22 +00:00
|
|
|
|
path,
|
|
|
|
|
opt_ns,
|
|
|
|
|
parent_scope,
|
|
|
|
|
record_used,
|
|
|
|
|
path_span,
|
|
|
|
|
crate_lint,
|
|
|
|
|
None,
|
2019-08-05 18:18:50 +00:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-07 23:39:02 +00:00
|
|
|
|
fn resolve_path_with_ribs(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &[Segment],
|
|
|
|
|
opt_ns: Option<Namespace>, // `None` indicates a module path in import
|
|
|
|
|
parent_scope: &ParentScope<'a>,
|
|
|
|
|
record_used: bool,
|
|
|
|
|
path_span: Span,
|
|
|
|
|
crate_lint: CrateLint,
|
2019-08-08 23:16:45 +00:00
|
|
|
|
ribs: Option<&PerNS<Vec<Rib<'a>>>>,
|
2019-08-07 23:39:02 +00:00
|
|
|
|
) -> PathResult<'a> {
|
|
|
|
|
let mut module = None;
|
|
|
|
|
let mut allow_super = true;
|
|
|
|
|
let mut second_binding = None;
|
2018-11-17 17:00:00 +00:00
|
|
|
|
|
2019-08-07 23:39:02 +00:00
|
|
|
|
debug!(
|
|
|
|
|
"resolve_path(path={:?}, opt_ns={:?}, record_used={:?}, \
|
|
|
|
|
path_span={:?}, crate_lint={:?})",
|
2019-12-24 22:38:22 +00:00
|
|
|
|
path, opt_ns, record_used, path_span, crate_lint,
|
2019-08-07 23:39:02 +00:00
|
|
|
|
);
|
2018-12-30 17:07:43 +00:00
|
|
|
|
|
2020-06-17 23:29:03 +00:00
|
|
|
|
for (i, &Segment { ident, id, has_generic_args: _ }) in path.iter().enumerate() {
|
2019-08-07 23:39:02 +00:00
|
|
|
|
debug!("resolve_path ident {} {:?} {:?}", i, ident, id);
|
|
|
|
|
let record_segment_res = |this: &mut Self, res| {
|
|
|
|
|
if record_used {
|
|
|
|
|
if let Some(id) = id {
|
|
|
|
|
if !this.partial_res_map.contains_key(&id) {
|
|
|
|
|
assert!(id != ast::DUMMY_NODE_ID, "Trying to resolve dummy id");
|
|
|
|
|
this.record_partial_res(id, PartialRes::new(res));
|
2018-12-30 17:07:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-17 17:00:00 +00:00
|
|
|
|
}
|
2019-08-07 23:39:02 +00:00
|
|
|
|
};
|
2012-05-22 17:54:12 +00:00
|
|
|
|
|
2019-08-07 23:39:02 +00:00
|
|
|
|
let is_last = i == path.len() - 1;
|
|
|
|
|
let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS };
|
|
|
|
|
let name = ident.name;
|
2017-10-02 13:48:57 +00:00
|
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
|
allow_super &= ns == TypeNS && (name == kw::SelfLower || name == kw::Super);
|
2018-08-14 09:54:43 +00:00
|
|
|
|
|
|
|
|
|
if ns == TypeNS {
|
2019-05-11 14:41:37 +00:00
|
|
|
|
if allow_super && name == kw::Super {
|
2020-03-13 22:36:46 +00:00
|
|
|
|
let mut ctxt = ident.span.ctxt().normalize_to_macros_2_0();
|
2018-08-14 09:54:43 +00:00
|
|
|
|
let self_module = match i {
|
2019-08-05 18:18:50 +00:00
|
|
|
|
0 => Some(self.resolve_self(&mut ctxt, parent_scope.module)),
|
2018-08-14 09:54:43 +00:00
|
|
|
|
_ => match module {
|
|
|
|
|
Some(ModuleOrUniformRoot::Module(module)) => Some(module),
|
|
|
|
|
_ => None,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
if let Some(self_module) = self_module {
|
|
|
|
|
if let Some(parent) = self_module.parent {
|
|
|
|
|
module = Some(ModuleOrUniformRoot::Module(
|
2019-12-24 22:38:22 +00:00
|
|
|
|
self.resolve_self(&mut ctxt, parent),
|
|
|
|
|
));
|
2018-08-14 09:54:43 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-10 14:36:22 +00:00
|
|
|
|
let msg = "there are too many leading `super` keywords".to_string();
|
2019-01-16 20:30:41 +00:00
|
|
|
|
return PathResult::Failed {
|
|
|
|
|
span: ident.span,
|
|
|
|
|
label: msg,
|
|
|
|
|
suggestion: None,
|
|
|
|
|
is_error_from_last_segment: false,
|
|
|
|
|
};
|
2016-11-25 06:07:21 +00:00
|
|
|
|
}
|
2018-08-09 13:29:22 +00:00
|
|
|
|
if i == 0 {
|
2019-05-11 14:41:37 +00:00
|
|
|
|
if name == kw::SelfLower {
|
2020-03-13 22:36:46 +00:00
|
|
|
|
let mut ctxt = ident.span.ctxt().normalize_to_macros_2_0();
|
2018-08-14 09:54:43 +00:00
|
|
|
|
module = Some(ModuleOrUniformRoot::Module(
|
2019-12-24 22:38:22 +00:00
|
|
|
|
self.resolve_self(&mut ctxt, parent_scope.module),
|
|
|
|
|
));
|
2018-08-14 09:54:43 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-05-11 14:41:37 +00:00
|
|
|
|
if name == kw::PathRoot && ident.span.rust_2018() {
|
2018-11-24 16:14:05 +00:00
|
|
|
|
module = Some(ModuleOrUniformRoot::ExternPrelude);
|
2018-08-09 13:29:22 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-12-24 22:38:22 +00:00
|
|
|
|
if name == kw::PathRoot && ident.span.rust_2015() && self.session.rust_2018() {
|
2018-11-24 21:25:03 +00:00
|
|
|
|
// `::a::b` from 2015 macro on 2018 global edition
|
|
|
|
|
module = Some(ModuleOrUniformRoot::CrateRootAndExternPrelude);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-12-24 22:38:22 +00:00
|
|
|
|
if name == kw::PathRoot || name == kw::Crate || name == kw::DollarCrate {
|
2018-08-14 09:54:43 +00:00
|
|
|
|
// `::a::b`, `crate::a::b` or `$crate::a::b`
|
2019-12-24 22:38:22 +00:00
|
|
|
|
module = Some(ModuleOrUniformRoot::Module(self.resolve_crate_root(ident)));
|
2018-08-14 09:54:43 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-11-04 20:56:45 +00:00
|
|
|
|
}
|
2016-12-05 03:51:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-19 14:05:29 +00:00
|
|
|
|
// Report special messages for path segment keywords in wrong positions.
|
2018-08-14 09:54:43 +00:00
|
|
|
|
if ident.is_path_segment_keyword() && i != 0 {
|
2019-05-11 14:41:37 +00:00
|
|
|
|
let name_str = if name == kw::PathRoot {
|
2018-07-28 12:40:32 +00:00
|
|
|
|
"crate root".to_string()
|
2017-11-19 14:05:29 +00:00
|
|
|
|
} else {
|
|
|
|
|
format!("`{}`", name)
|
|
|
|
|
};
|
2019-05-11 14:41:37 +00:00
|
|
|
|
let label = if i == 1 && path[0].ident.name == kw::PathRoot {
|
2017-11-19 14:05:29 +00:00
|
|
|
|
format!("global paths cannot start with {}", name_str)
|
|
|
|
|
} else {
|
|
|
|
|
format!("{} in paths can only be used in start position", name_str)
|
|
|
|
|
};
|
2019-01-16 20:30:41 +00:00
|
|
|
|
return PathResult::Failed {
|
|
|
|
|
span: ident.span,
|
|
|
|
|
label,
|
|
|
|
|
suggestion: None,
|
|
|
|
|
is_error_from_last_segment: false,
|
|
|
|
|
};
|
2017-11-19 14:05:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 07:33:34 +00:00
|
|
|
|
enum FindBindingResult<'a> {
|
|
|
|
|
Binding(Result<&'a NameBinding<'a>, Determinacy>),
|
|
|
|
|
PathResult(PathResult<'a>),
|
|
|
|
|
}
|
|
|
|
|
let find_binding_in_ns = |this: &mut Self, ns| {
|
|
|
|
|
let binding = if let Some(module) = module {
|
|
|
|
|
this.resolve_ident_in_module(
|
|
|
|
|
module,
|
|
|
|
|
ident,
|
|
|
|
|
ns,
|
|
|
|
|
parent_scope,
|
|
|
|
|
record_used,
|
|
|
|
|
path_span,
|
|
|
|
|
)
|
|
|
|
|
} else if ribs.is_none() || opt_ns.is_none() || opt_ns == Some(MacroNS) {
|
|
|
|
|
let scopes = ScopeSet::All(ns, opt_ns.is_none());
|
|
|
|
|
this.early_resolve_ident_in_lexical_scope(
|
|
|
|
|
ident,
|
|
|
|
|
scopes,
|
|
|
|
|
parent_scope,
|
|
|
|
|
record_used,
|
|
|
|
|
record_used,
|
|
|
|
|
path_span,
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
let record_used_id = if record_used {
|
|
|
|
|
crate_lint.node_id().or(Some(CRATE_NODE_ID))
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
match this.resolve_ident_in_lexical_scope(
|
|
|
|
|
ident,
|
|
|
|
|
ns,
|
|
|
|
|
parent_scope,
|
|
|
|
|
record_used_id,
|
|
|
|
|
path_span,
|
|
|
|
|
&ribs.unwrap()[ns],
|
|
|
|
|
) {
|
|
|
|
|
// we found a locally-imported or available item/module
|
|
|
|
|
Some(LexicalScopeBinding::Item(binding)) => Ok(binding),
|
|
|
|
|
// we found a local variable or type param
|
|
|
|
|
Some(LexicalScopeBinding::Res(res))
|
|
|
|
|
if opt_ns == Some(TypeNS) || opt_ns == Some(ValueNS) =>
|
|
|
|
|
{
|
|
|
|
|
record_segment_res(this, res);
|
|
|
|
|
return FindBindingResult::PathResult(PathResult::NonModule(
|
|
|
|
|
PartialRes::with_unresolved_segments(res, path.len() - 1),
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
_ => Err(Determinacy::determined(record_used)),
|
2016-11-25 06:07:21 +00:00
|
|
|
|
}
|
2020-04-22 07:33:34 +00:00
|
|
|
|
};
|
|
|
|
|
FindBindingResult::Binding(binding)
|
|
|
|
|
};
|
|
|
|
|
let binding = match find_binding_in_ns(self, ns) {
|
|
|
|
|
FindBindingResult::PathResult(x) => return x,
|
|
|
|
|
FindBindingResult::Binding(binding) => binding,
|
2016-11-25 06:07:21 +00:00
|
|
|
|
};
|
|
|
|
|
match binding {
|
|
|
|
|
Ok(binding) => {
|
2018-05-11 17:02:17 +00:00
|
|
|
|
if i == 1 {
|
|
|
|
|
second_binding = Some(binding);
|
|
|
|
|
}
|
2019-04-20 16:36:05 +00:00
|
|
|
|
let res = binding.res();
|
|
|
|
|
let maybe_assoc = opt_ns != Some(MacroNS) && PathSource::Type.is_expected(res);
|
2016-11-27 00:23:54 +00:00
|
|
|
|
if let Some(next_module) = binding.module() {
|
2018-08-09 13:29:22 +00:00
|
|
|
|
module = Some(ModuleOrUniformRoot::Module(next_module));
|
2019-04-20 16:36:05 +00:00
|
|
|
|
record_segment_res(self, res);
|
|
|
|
|
} else if res == Res::ToolMod && i + 1 != path.len() {
|
2018-12-12 22:43:44 +00:00
|
|
|
|
if binding.is_import() {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
self.session
|
|
|
|
|
.struct_span_err(
|
|
|
|
|
ident.span,
|
|
|
|
|
"cannot use a tool module through an import",
|
|
|
|
|
)
|
|
|
|
|
.span_note(binding.span, "the tool module imported here")
|
|
|
|
|
.emit();
|
2018-12-12 22:43:44 +00:00
|
|
|
|
}
|
2019-04-20 16:36:05 +00:00
|
|
|
|
let res = Res::NonMacroAttr(NonMacroAttrKind::Tool);
|
2019-05-04 12:18:58 +00:00
|
|
|
|
return PathResult::NonModule(PartialRes::new(res));
|
2019-04-20 16:36:05 +00:00
|
|
|
|
} else if res == Res::Err {
|
2019-05-04 12:18:58 +00:00
|
|
|
|
return PathResult::NonModule(PartialRes::new(Res::Err));
|
2017-01-08 13:38:40 +00:00
|
|
|
|
} else if opt_ns.is_some() && (is_last || maybe_assoc) {
|
2018-05-11 17:02:17 +00:00
|
|
|
|
self.lint_if_path_starts_with_module(
|
2018-05-22 15:10:17 +00:00
|
|
|
|
crate_lint,
|
2018-05-11 17:02:17 +00:00
|
|
|
|
path,
|
|
|
|
|
path_span,
|
|
|
|
|
second_binding,
|
|
|
|
|
);
|
2019-05-04 12:18:58 +00:00
|
|
|
|
return PathResult::NonModule(PartialRes::with_unresolved_segments(
|
2019-12-24 22:38:22 +00:00
|
|
|
|
res,
|
|
|
|
|
path.len() - i - 1,
|
2017-02-18 19:11:42 +00:00
|
|
|
|
));
|
2016-11-25 06:07:21 +00:00
|
|
|
|
} else {
|
2019-04-01 20:22:12 +00:00
|
|
|
|
let label = format!(
|
|
|
|
|
"`{}` is {} {}, not a module",
|
|
|
|
|
ident,
|
2019-04-20 16:36:05 +00:00
|
|
|
|
res.article(),
|
2019-05-04 12:22:00 +00:00
|
|
|
|
res.descr(),
|
2019-04-01 20:22:12 +00:00
|
|
|
|
);
|
|
|
|
|
|
2019-01-16 20:30:41 +00:00
|
|
|
|
return PathResult::Failed {
|
|
|
|
|
span: ident.span,
|
2019-04-01 20:22:12 +00:00
|
|
|
|
label,
|
2019-01-16 20:30:41 +00:00
|
|
|
|
suggestion: None,
|
|
|
|
|
is_error_from_last_segment: is_last,
|
|
|
|
|
};
|
2016-11-25 06:07:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Err(Undetermined) => return PathResult::Indeterminate,
|
|
|
|
|
Err(Determined) => {
|
2018-08-09 13:29:22 +00:00
|
|
|
|
if let Some(ModuleOrUniformRoot::Module(module)) = module {
|
2016-11-25 06:07:21 +00:00
|
|
|
|
if opt_ns.is_some() && !module.is_normal() {
|
2019-05-04 12:18:58 +00:00
|
|
|
|
return PathResult::NonModule(PartialRes::with_unresolved_segments(
|
2019-12-24 22:38:22 +00:00
|
|
|
|
module.res().unwrap(),
|
|
|
|
|
path.len() - i,
|
2017-02-18 19:11:42 +00:00
|
|
|
|
));
|
2016-11-25 06:07:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-20 16:36:05 +00:00
|
|
|
|
let module_res = match module {
|
|
|
|
|
Some(ModuleOrUniformRoot::Module(module)) => module.res(),
|
2018-08-09 13:29:22 +00:00
|
|
|
|
_ => None,
|
|
|
|
|
};
|
2019-04-20 16:36:05 +00:00
|
|
|
|
let (label, suggestion) = if module_res == self.graph_root.res() {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
let is_mod = |res| match res {
|
|
|
|
|
Res::Def(DefKind::Mod, _) => true,
|
|
|
|
|
_ => false,
|
2019-04-20 15:26:26 +00:00
|
|
|
|
};
|
2020-08-29 13:47:39 +00:00
|
|
|
|
// Don't look up import candidates if this is a speculative resolve
|
|
|
|
|
let mut candidates = if record_used {
|
|
|
|
|
self.lookup_import_candidates(ident, TypeNS, parent_scope, is_mod)
|
|
|
|
|
} else {
|
|
|
|
|
Vec::new()
|
|
|
|
|
};
|
2018-03-30 09:23:27 +00:00
|
|
|
|
candidates.sort_by_cached_key(|c| {
|
2019-10-08 20:17:46 +00:00
|
|
|
|
(c.path.segments.len(), pprust::path_to_string(&c.path))
|
2018-03-30 09:23:27 +00:00
|
|
|
|
});
|
2016-11-25 06:07:21 +00:00
|
|
|
|
if let Some(candidate) = candidates.get(0) {
|
2019-01-16 20:30:41 +00:00
|
|
|
|
(
|
|
|
|
|
String::from("unresolved import"),
|
|
|
|
|
Some((
|
2019-10-08 20:17:46 +00:00
|
|
|
|
vec![(ident.span, pprust::path_to_string(&candidate.path))],
|
2019-01-16 20:30:41 +00:00
|
|
|
|
String::from("a similar path exists"),
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
)),
|
|
|
|
|
)
|
2018-12-30 00:35:57 +00:00
|
|
|
|
} else {
|
2020-03-14 12:28:17 +00:00
|
|
|
|
(format!("maybe a missing crate `{}`?", ident), None)
|
2016-11-25 06:07:21 +00:00
|
|
|
|
}
|
|
|
|
|
} else if i == 0 {
|
2020-08-27 12:27:14 +00:00
|
|
|
|
if ident
|
|
|
|
|
.name
|
|
|
|
|
.with(|n| n.chars().next().map_or(false, |c| c.is_ascii_uppercase()))
|
|
|
|
|
{
|
|
|
|
|
(format!("use of undeclared type `{}`", ident), None)
|
|
|
|
|
} else {
|
|
|
|
|
(format!("use of undeclared crate or module `{}`", ident), None)
|
|
|
|
|
}
|
2016-11-25 06:07:21 +00:00
|
|
|
|
} else {
|
2020-04-22 07:33:34 +00:00
|
|
|
|
let mut msg =
|
|
|
|
|
format!("could not find `{}` in `{}`", ident, path[i - 1].ident);
|
2020-04-26 02:28:33 +00:00
|
|
|
|
if ns == TypeNS || ns == ValueNS {
|
|
|
|
|
let ns_to_try = if ns == TypeNS { ValueNS } else { TypeNS };
|
|
|
|
|
if let FindBindingResult::Binding(Ok(binding)) =
|
|
|
|
|
find_binding_in_ns(self, ns_to_try)
|
2020-04-22 07:33:34 +00:00
|
|
|
|
{
|
2020-04-26 02:28:33 +00:00
|
|
|
|
let mut found = |what| {
|
|
|
|
|
msg = format!(
|
|
|
|
|
"expected {}, found {} `{}` in `{}`",
|
|
|
|
|
ns.descr(),
|
|
|
|
|
what,
|
|
|
|
|
ident,
|
|
|
|
|
path[i - 1].ident
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
if binding.module().is_some() {
|
|
|
|
|
found("module")
|
|
|
|
|
} else {
|
|
|
|
|
match binding.res() {
|
|
|
|
|
def::Res::<NodeId>::Def(kind, id) => found(kind.descr(id)),
|
|
|
|
|
_ => found(ns_to_try.descr()),
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-22 07:33:34 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
(msg, None)
|
2019-01-16 20:30:41 +00:00
|
|
|
|
};
|
|
|
|
|
return PathResult::Failed {
|
|
|
|
|
span: ident.span,
|
|
|
|
|
label,
|
|
|
|
|
suggestion,
|
|
|
|
|
is_error_from_last_segment: is_last,
|
2016-11-25 06:07:21 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-20 09:29:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-22 15:10:17 +00:00
|
|
|
|
self.lint_if_path_starts_with_module(crate_lint, path, path_span, second_binding);
|
2018-05-11 17:02:17 +00:00
|
|
|
|
|
2018-11-03 19:02:36 +00:00
|
|
|
|
PathResult::Module(match module {
|
|
|
|
|
Some(module) => module,
|
2018-11-24 16:14:05 +00:00
|
|
|
|
None if path.is_empty() => ModuleOrUniformRoot::CurrentScope,
|
2018-11-03 19:02:36 +00:00
|
|
|
|
_ => span_bug!(path_span, "resolve_path: non-empty path `{:?}` has no module", path),
|
|
|
|
|
})
|
2015-11-03 18:44:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-22 15:10:17 +00:00
|
|
|
|
fn lint_if_path_starts_with_module(
|
2019-10-25 13:15:33 +00:00
|
|
|
|
&mut self,
|
2018-05-22 15:10:17 +00:00
|
|
|
|
crate_lint: CrateLint,
|
2018-09-12 03:21:50 +00:00
|
|
|
|
path: &[Segment],
|
2018-05-22 15:10:17 +00:00
|
|
|
|
path_span: Span,
|
2019-02-06 17:15:23 +00:00
|
|
|
|
second_binding: Option<&NameBinding<'_>>,
|
2018-05-22 15:10:17 +00:00
|
|
|
|
) {
|
|
|
|
|
let (diag_id, diag_span) = match crate_lint {
|
|
|
|
|
CrateLint::No => return,
|
|
|
|
|
CrateLint::SimplePath(id) => (id, path_span),
|
|
|
|
|
CrateLint::UsePath { root_id, root_span } => (root_id, root_span),
|
2018-05-22 23:01:09 +00:00
|
|
|
|
CrateLint::QPathTrait { qpath_id, qpath_span } => (qpath_id, qpath_span),
|
2018-05-11 17:02:17 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let first_name = match path.get(0) {
|
2018-11-18 00:25:59 +00:00
|
|
|
|
// In the 2018 edition this lint is a hard error, so nothing to do
|
2018-11-28 19:52:58 +00:00
|
|
|
|
Some(seg) if seg.ident.span.rust_2015() && self.session.rust_2015() => seg.ident.name,
|
2018-11-18 00:25:59 +00:00
|
|
|
|
_ => return,
|
2018-05-11 17:02:17 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// We're only interested in `use` paths which should start with
|
2019-01-13 13:18:00 +00:00
|
|
|
|
// `{{root}}` currently.
|
2019-05-11 14:41:37 +00:00
|
|
|
|
if first_name != kw::PathRoot {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
return;
|
2018-05-11 17:02:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match path.get(1) {
|
|
|
|
|
// If this import looks like `crate::...` it's already good
|
2019-05-11 14:41:37 +00:00
|
|
|
|
Some(Segment { ident, .. }) if ident.name == kw::Crate => return,
|
2018-05-11 17:02:17 +00:00
|
|
|
|
// Otherwise go below to see if it's an extern crate
|
|
|
|
|
Some(_) => {}
|
2018-12-02 00:35:55 +00:00
|
|
|
|
// If the path has length one (and it's `PathRoot` most likely)
|
2018-05-11 17:02:17 +00:00
|
|
|
|
// then we don't know whether we're gonna be importing a crate or an
|
|
|
|
|
// item in our crate. Defer this lint to elsewhere
|
|
|
|
|
None => return,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the first element of our path was actually resolved to an
|
|
|
|
|
// `ExternCrate` (also used for `crate::...`) then no need to issue a
|
|
|
|
|
// warning, this looks all good!
|
|
|
|
|
if let Some(binding) = second_binding {
|
2020-03-07 16:02:32 +00:00
|
|
|
|
if let NameBindingKind::Import { import, .. } = binding.kind {
|
|
|
|
|
// Careful: we still want to rewrite paths from renamed extern crates.
|
|
|
|
|
if let ImportKind::ExternCrate { source: None, .. } = import.kind {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
return;
|
2018-05-11 17:02:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-05 08:40:16 +00:00
|
|
|
|
let diag = BuiltinLintDiagnostics::AbsPathWithModule(diag_span);
|
2019-10-25 13:15:33 +00:00
|
|
|
|
self.lint_buffer.buffer_lint_with_diagnostic(
|
2018-05-18 22:13:53 +00:00
|
|
|
|
lint::builtin::ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE,
|
2019-12-24 22:38:22 +00:00
|
|
|
|
diag_id,
|
|
|
|
|
diag_span,
|
2018-05-11 17:02:17 +00:00
|
|
|
|
"absolute paths must start with `self`, `super`, \
|
2019-12-24 22:38:22 +00:00
|
|
|
|
`crate`, or an external crate name in the 2018 edition",
|
|
|
|
|
diag,
|
|
|
|
|
);
|
2018-05-11 17:02:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 20:31:01 +00:00
|
|
|
|
// Validate a local resolution (from ribs).
|
2019-05-28 15:06:01 +00:00
|
|
|
|
fn validate_res_from_ribs(
|
|
|
|
|
&mut self,
|
|
|
|
|
rib_index: usize,
|
2019-09-27 13:21:02 +00:00
|
|
|
|
rib_ident: Ident,
|
2019-05-28 15:06:01 +00:00
|
|
|
|
res: Res,
|
|
|
|
|
record_used: bool,
|
|
|
|
|
span: Span,
|
2019-08-05 18:18:50 +00:00
|
|
|
|
all_ribs: &[Rib<'a>],
|
2019-05-28 15:06:01 +00:00
|
|
|
|
) -> Res {
|
|
|
|
|
debug!("validate_res_from_ribs({:?})", res);
|
2019-08-05 18:18:50 +00:00
|
|
|
|
let ribs = &all_ribs[rib_index + 1..];
|
2017-01-25 20:01:11 +00:00
|
|
|
|
|
|
|
|
|
// An invalid forward use of a type parameter from a previous default.
|
2019-08-05 18:18:50 +00:00
|
|
|
|
if let ForwardTyParamBanRibKind = all_ribs[rib_index].kind {
|
2017-05-12 09:21:11 +00:00
|
|
|
|
if record_used {
|
2019-09-27 13:21:02 +00:00
|
|
|
|
let res_error = if rib_ident.name == kw::SelfUpper {
|
|
|
|
|
ResolutionError::SelfInTyParamDefault
|
|
|
|
|
} else {
|
|
|
|
|
ResolutionError::ForwardDeclaredTyParam
|
|
|
|
|
};
|
|
|
|
|
self.report_error(span, res_error);
|
2017-01-25 20:01:11 +00:00
|
|
|
|
}
|
2019-04-20 16:36:05 +00:00
|
|
|
|
assert_eq!(res, Res::Err);
|
|
|
|
|
return Res::Err;
|
2017-01-25 20:01:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-20 16:36:05 +00:00
|
|
|
|
match res {
|
2019-05-28 20:31:01 +00:00
|
|
|
|
Res::Local(_) => {
|
2019-02-07 15:03:12 +00:00
|
|
|
|
use ResolutionError::*;
|
|
|
|
|
let mut res_err = None;
|
|
|
|
|
|
2015-11-03 18:44:23 +00:00
|
|
|
|
for rib in ribs {
|
|
|
|
|
match rib.kind {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
NormalRibKind
|
2020-06-25 14:16:38 +00:00
|
|
|
|
| ClosureOrAsyncRibKind
|
2019-12-24 22:38:22 +00:00
|
|
|
|
| ModuleRibKind(..)
|
|
|
|
|
| MacroDefinition(..)
|
|
|
|
|
| ForwardTyParamBanRibKind => {
|
2015-11-03 18:44:23 +00:00
|
|
|
|
// Nothing to do. Continue.
|
|
|
|
|
}
|
2019-10-05 15:55:58 +00:00
|
|
|
|
ItemRibKind(_) | FnItemRibKind | AssocItemRibKind => {
|
2015-11-03 18:44:23 +00:00
|
|
|
|
// This was an attempt to access an upvar inside a
|
|
|
|
|
// named function item. This is not allowed, so we
|
|
|
|
|
// report an error.
|
2017-05-12 09:21:11 +00:00
|
|
|
|
if record_used {
|
2019-02-07 15:03:12 +00:00
|
|
|
|
// We don't immediately trigger a resolve error, because
|
|
|
|
|
// we want certain other resolution errors (namely those
|
|
|
|
|
// emitted for `ConstantItemRibKind` below) to take
|
|
|
|
|
// precedence.
|
|
|
|
|
res_err = Some(CannotCaptureDynamicEnvironmentInFnItem);
|
2016-11-30 22:35:25 +00:00
|
|
|
|
}
|
2015-11-03 18:44:23 +00:00
|
|
|
|
}
|
2020-07-28 13:55:42 +00:00
|
|
|
|
ConstantItemRibKind(_) => {
|
2015-11-03 18:44:23 +00:00
|
|
|
|
// Still doesn't deal with upvars
|
2017-05-12 09:21:11 +00:00
|
|
|
|
if record_used {
|
2019-08-08 20:32:58 +00:00
|
|
|
|
self.report_error(span, AttemptToUseNonConstantValueInConstant);
|
2016-11-30 22:35:25 +00:00
|
|
|
|
}
|
2019-04-20 16:36:05 +00:00
|
|
|
|
return Res::Err;
|
2015-11-03 18:44:23 +00:00
|
|
|
|
}
|
2020-07-08 20:16:18 +00:00
|
|
|
|
ConstParamTyRibKind => {
|
|
|
|
|
if record_used {
|
2020-07-18 20:35:50 +00:00
|
|
|
|
self.report_error(span, ParamInTyOfConstParam(rib_ident.name));
|
2020-07-08 20:16:18 +00:00
|
|
|
|
}
|
|
|
|
|
return Res::Err;
|
|
|
|
|
}
|
2015-11-03 18:44:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-07 15:03:12 +00:00
|
|
|
|
if let Some(res_err) = res_err {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
self.report_error(span, res_err);
|
|
|
|
|
return Res::Err;
|
2019-02-07 15:03:12 +00:00
|
|
|
|
}
|
2015-11-03 18:44:23 +00:00
|
|
|
|
}
|
2019-04-20 16:36:05 +00:00
|
|
|
|
Res::Def(DefKind::TyParam, _) | Res::SelfTy(..) => {
|
2020-07-18 21:42:10 +00:00
|
|
|
|
let mut in_ty_param_default = false;
|
2015-11-03 18:44:23 +00:00
|
|
|
|
for rib in ribs {
|
2019-10-05 15:55:58 +00:00
|
|
|
|
let has_generic_params = match rib.kind {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
NormalRibKind
|
2020-06-25 14:16:38 +00:00
|
|
|
|
| ClosureOrAsyncRibKind
|
2019-12-24 22:38:22 +00:00
|
|
|
|
| AssocItemRibKind
|
|
|
|
|
| ModuleRibKind(..)
|
2020-07-18 21:42:10 +00:00
|
|
|
|
| MacroDefinition(..) => {
|
2015-11-03 18:44:23 +00:00
|
|
|
|
// Nothing to do. Continue.
|
2019-10-05 15:55:58 +00:00
|
|
|
|
continue;
|
2015-11-03 18:44:23 +00:00
|
|
|
|
}
|
2020-07-18 21:42:10 +00:00
|
|
|
|
|
|
|
|
|
// We only forbid constant items if we are inside of type defaults,
|
|
|
|
|
// for example `struct Foo<T, U = [u8; std::mem::size_of::<T>()]>`
|
|
|
|
|
ForwardTyParamBanRibKind => {
|
|
|
|
|
in_ty_param_default = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-07-28 13:55:42 +00:00
|
|
|
|
ConstantItemRibKind(trivial) => {
|
|
|
|
|
// HACK(min_const_generics): We currently only allow `N` or `{ N }`.
|
|
|
|
|
if !trivial && self.session.features_untracked().min_const_generics {
|
|
|
|
|
if record_used {
|
|
|
|
|
self.report_error(
|
|
|
|
|
span,
|
|
|
|
|
ResolutionError::ParamInNonTrivialAnonConst(rib_ident.name),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return Res::Err;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-18 21:42:10 +00:00
|
|
|
|
if in_ty_param_default {
|
|
|
|
|
if record_used {
|
|
|
|
|
self.report_error(
|
|
|
|
|
span,
|
|
|
|
|
ResolutionError::ParamInAnonConstInTyDefault(
|
|
|
|
|
rib_ident.name,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return Res::Err;
|
|
|
|
|
} else {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-05 15:55:58 +00:00
|
|
|
|
// This was an attempt to use a type parameter outside its scope.
|
|
|
|
|
ItemRibKind(has_generic_params) => has_generic_params,
|
|
|
|
|
FnItemRibKind => HasGenericParams::Yes,
|
2020-07-08 20:16:18 +00:00
|
|
|
|
ConstParamTyRibKind => {
|
|
|
|
|
if record_used {
|
2020-07-16 09:10:22 +00:00
|
|
|
|
self.report_error(
|
|
|
|
|
span,
|
2020-07-18 20:35:50 +00:00
|
|
|
|
ResolutionError::ParamInTyOfConstParam(rib_ident.name),
|
2020-07-16 09:10:22 +00:00
|
|
|
|
);
|
2020-07-08 20:16:18 +00:00
|
|
|
|
}
|
|
|
|
|
return Res::Err;
|
|
|
|
|
}
|
2019-10-05 15:55:58 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if record_used {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
self.report_error(
|
|
|
|
|
span,
|
|
|
|
|
ResolutionError::GenericParamsFromOuterFunction(
|
|
|
|
|
res,
|
|
|
|
|
has_generic_params,
|
|
|
|
|
),
|
|
|
|
|
);
|
2015-11-03 18:44:23 +00:00
|
|
|
|
}
|
2019-10-05 15:55:58 +00:00
|
|
|
|
return Res::Err;
|
2019-02-07 13:59:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-20 16:36:05 +00:00
|
|
|
|
Res::Def(DefKind::ConstParam, _) => {
|
2019-03-22 01:49:42 +00:00
|
|
|
|
let mut ribs = ribs.iter().peekable();
|
|
|
|
|
if let Some(Rib { kind: FnItemRibKind, .. }) = ribs.peek() {
|
|
|
|
|
// When declaring const parameters inside function signatures, the first rib
|
|
|
|
|
// is always a `FnItemRibKind`. In this case, we can skip it, to avoid it
|
|
|
|
|
// (spuriously) conflicting with the const param.
|
|
|
|
|
ribs.next();
|
|
|
|
|
}
|
2020-07-18 21:42:10 +00:00
|
|
|
|
|
|
|
|
|
let mut in_ty_param_default = false;
|
2019-03-22 01:49:42 +00:00
|
|
|
|
for rib in ribs {
|
2019-10-05 15:55:58 +00:00
|
|
|
|
let has_generic_params = match rib.kind {
|
2020-07-08 20:16:18 +00:00
|
|
|
|
NormalRibKind
|
|
|
|
|
| ClosureOrAsyncRibKind
|
|
|
|
|
| AssocItemRibKind
|
|
|
|
|
| ModuleRibKind(..)
|
2020-07-18 21:42:10 +00:00
|
|
|
|
| MacroDefinition(..) => continue,
|
|
|
|
|
|
|
|
|
|
// We only forbid constant items if we are inside of type defaults,
|
|
|
|
|
// for example `struct Foo<T, U = [u8; std::mem::size_of::<T>()]>`
|
|
|
|
|
ForwardTyParamBanRibKind => {
|
|
|
|
|
in_ty_param_default = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-07-28 13:55:42 +00:00
|
|
|
|
ConstantItemRibKind(trivial) => {
|
|
|
|
|
// HACK(min_const_generics): We currently only allow `N` or `{ N }`.
|
|
|
|
|
if !trivial && self.session.features_untracked().min_const_generics {
|
|
|
|
|
if record_used {
|
|
|
|
|
self.report_error(
|
|
|
|
|
span,
|
|
|
|
|
ResolutionError::ParamInNonTrivialAnonConst(rib_ident.name),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return Res::Err;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-18 21:42:10 +00:00
|
|
|
|
if in_ty_param_default {
|
|
|
|
|
if record_used {
|
|
|
|
|
self.report_error(
|
|
|
|
|
span,
|
|
|
|
|
ResolutionError::ParamInAnonConstInTyDefault(
|
|
|
|
|
rib_ident.name,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return Res::Err;
|
|
|
|
|
} else {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-05 15:55:58 +00:00
|
|
|
|
ItemRibKind(has_generic_params) => has_generic_params,
|
|
|
|
|
FnItemRibKind => HasGenericParams::Yes,
|
2020-07-08 20:16:18 +00:00
|
|
|
|
ConstParamTyRibKind => {
|
|
|
|
|
if record_used {
|
2020-07-16 09:10:22 +00:00
|
|
|
|
self.report_error(
|
|
|
|
|
span,
|
2020-07-18 20:35:50 +00:00
|
|
|
|
ResolutionError::ParamInTyOfConstParam(rib_ident.name),
|
2020-07-16 09:10:22 +00:00
|
|
|
|
);
|
2020-07-08 20:16:18 +00:00
|
|
|
|
}
|
|
|
|
|
return Res::Err;
|
|
|
|
|
}
|
2019-10-05 15:55:58 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// This was an attempt to use a const parameter outside its scope.
|
|
|
|
|
if record_used {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
self.report_error(
|
|
|
|
|
span,
|
|
|
|
|
ResolutionError::GenericParamsFromOuterFunction(
|
|
|
|
|
res,
|
|
|
|
|
has_generic_params,
|
|
|
|
|
),
|
|
|
|
|
);
|
2019-02-07 13:59:59 +00:00
|
|
|
|
}
|
2019-10-05 15:55:58 +00:00
|
|
|
|
return Res::Err;
|
2015-11-03 18:44:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
2019-04-20 16:36:05 +00:00
|
|
|
|
res
|
2012-05-22 17:54:12 +00:00
|
|
|
|
}
|
2014-04-22 16:06:43 +00:00
|
|
|
|
|
2019-05-04 12:18:58 +00:00
|
|
|
|
fn record_partial_res(&mut self, node_id: NodeId, resolution: PartialRes) {
|
2019-04-20 16:36:05 +00:00
|
|
|
|
debug!("(recording res) recording {:?} for {}", resolution, node_id);
|
2019-05-04 12:18:58 +00:00
|
|
|
|
if let Some(prev_res) = self.partial_res_map.insert(node_id, resolution) {
|
2016-04-24 03:26:10 +00:00
|
|
|
|
panic!("path resolved multiple times ({:?} before, {:?} now)", prev_res, resolution);
|
2014-09-18 21:05:52 +00:00
|
|
|
|
}
|
2012-05-22 17:54:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-20 00:33:06 +00:00
|
|
|
|
fn is_accessible_from(&self, vis: ty::Visibility, module: Module<'a>) -> bool {
|
2016-12-20 08:32:15 +00:00
|
|
|
|
vis.is_accessible_from(module.normal_ancestor_id, self)
|
2016-08-20 00:33:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-27 01:49:40 +00:00
|
|
|
|
fn set_binding_parent_module(&mut self, binding: &'a NameBinding<'a>, module: Module<'a>) {
|
|
|
|
|
if let Some(old_module) = self.binding_parent_modules.insert(PtrKey(binding), module) {
|
|
|
|
|
if !ptr::eq(module, old_module) {
|
|
|
|
|
span_bug!(binding.span, "parent module is reset for binding");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-13 22:23:24 +00:00
|
|
|
|
fn disambiguate_macro_rules_vs_modularized(
|
2018-09-27 01:49:40 +00:00
|
|
|
|
&self,
|
2020-03-13 22:23:24 +00:00
|
|
|
|
macro_rules: &'a NameBinding<'a>,
|
|
|
|
|
modularized: &'a NameBinding<'a>,
|
2018-09-27 01:49:40 +00:00
|
|
|
|
) -> bool {
|
2020-03-13 22:36:46 +00:00
|
|
|
|
// Some non-controversial subset of ambiguities "modularized macro name" vs "macro_rules"
|
2018-09-27 01:49:40 +00:00
|
|
|
|
// is disambiguated to mitigate regressions from macro modularization.
|
|
|
|
|
// Scoping for `macro_rules` behaves like scoping for `let` at module level, in general.
|
2019-12-24 22:38:22 +00:00
|
|
|
|
match (
|
2020-03-13 22:23:24 +00:00
|
|
|
|
self.binding_parent_modules.get(&PtrKey(macro_rules)),
|
|
|
|
|
self.binding_parent_modules.get(&PtrKey(modularized)),
|
2019-12-24 22:38:22 +00:00
|
|
|
|
) {
|
2020-03-13 22:23:24 +00:00
|
|
|
|
(Some(macro_rules), Some(modularized)) => {
|
|
|
|
|
macro_rules.normal_ancestor_id == modularized.normal_ancestor_id
|
|
|
|
|
&& modularized.is_ancestor_of(macro_rules)
|
2019-12-24 22:38:22 +00:00
|
|
|
|
}
|
2018-09-27 01:49:40 +00:00
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-17 09:03:59 +00:00
|
|
|
|
fn report_errors(&mut self, krate: &Crate) {
|
|
|
|
|
self.report_with_use_injections(krate);
|
2016-08-22 08:30:07 +00:00
|
|
|
|
|
2018-08-11 11:33:43 +00:00
|
|
|
|
for &(span_use, span_def) in &self.macro_expanded_macro_export_errors {
|
|
|
|
|
let msg = "macro-expanded `macro_export` macros from the current crate \
|
|
|
|
|
cannot be referred to by absolute paths";
|
2019-10-25 13:15:33 +00:00
|
|
|
|
self.lint_buffer.buffer_lint_with_diagnostic(
|
2018-08-23 23:51:41 +00:00
|
|
|
|
lint::builtin::MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
|
2020-01-05 08:40:16 +00:00
|
|
|
|
CRATE_NODE_ID,
|
|
|
|
|
span_use,
|
|
|
|
|
msg,
|
|
|
|
|
BuiltinLintDiagnostics::MacroExpandedMacroExportsAccessedByAbsolutePaths(span_def),
|
2018-08-23 23:51:41 +00:00
|
|
|
|
);
|
2018-08-11 11:33:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-04 22:11:59 +00:00
|
|
|
|
for ambiguity_error in &self.ambiguity_errors {
|
2018-11-10 15:58:37 +00:00
|
|
|
|
self.report_ambiguity_error(ambiguity_error);
|
2016-08-22 08:30:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-10 15:58:37 +00:00
|
|
|
|
let mut reported_spans = FxHashSet::default();
|
2020-01-12 10:29:00 +00:00
|
|
|
|
for error in &self.privacy_errors {
|
|
|
|
|
if reported_spans.insert(error.dedup_span) {
|
|
|
|
|
self.report_privacy_error(error);
|
2018-10-27 17:21:34 +00:00
|
|
|
|
}
|
2016-02-25 04:40:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-05-22 17:54:12 +00:00
|
|
|
|
|
2017-08-17 09:03:59 +00:00
|
|
|
|
fn report_with_use_injections(&mut self, krate: &Crate) {
|
2020-06-02 18:16:23 +00:00
|
|
|
|
for UseError { mut err, candidates, def_id, instead, suggestion } in
|
2020-01-22 07:01:21 +00:00
|
|
|
|
self.use_injections.drain(..)
|
|
|
|
|
{
|
2020-06-20 18:59:29 +00:00
|
|
|
|
let (span, found_use) = if let Some(def_id) = def_id.as_local() {
|
|
|
|
|
UsePlacementFinder::check(krate, self.def_id_to_node_id[def_id])
|
|
|
|
|
} else {
|
|
|
|
|
(None, false)
|
|
|
|
|
};
|
2017-08-17 09:03:59 +00:00
|
|
|
|
if !candidates.is_empty() {
|
2020-06-02 18:16:23 +00:00
|
|
|
|
diagnostics::show_candidates(&mut err, span, &candidates, instead, found_use);
|
2020-04-08 01:07:26 +00:00
|
|
|
|
} else if let Some((span, msg, sugg, appl)) = suggestion {
|
2020-01-22 07:01:21 +00:00
|
|
|
|
err.span_suggestion(span, msg, sugg, appl);
|
|
|
|
|
}
|
2017-08-17 09:03:59 +00:00
|
|
|
|
err.emit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
|
fn report_conflict<'b>(
|
|
|
|
|
&mut self,
|
|
|
|
|
parent: Module<'_>,
|
|
|
|
|
ident: Ident,
|
|
|
|
|
ns: Namespace,
|
|
|
|
|
new_binding: &NameBinding<'b>,
|
|
|
|
|
old_binding: &NameBinding<'b>,
|
|
|
|
|
) {
|
2016-03-16 05:20:58 +00:00
|
|
|
|
// Error on the second of two conflicting names
|
2017-07-31 20:04:34 +00:00
|
|
|
|
if old_binding.span.lo() > new_binding.span.lo() {
|
2017-05-18 03:29:58 +00:00
|
|
|
|
return self.report_conflict(parent, ident, ns, old_binding, new_binding);
|
2016-03-16 05:20:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-18 09:45:06 +00:00
|
|
|
|
let container = match parent.kind {
|
2020-03-16 15:01:03 +00:00
|
|
|
|
ModuleKind::Def(kind, _, _) => kind.descr(parent.def_id().unwrap()),
|
2016-09-18 09:45:06 +00:00
|
|
|
|
ModuleKind::Block(..) => "block",
|
2016-03-16 05:20:58 +00:00
|
|
|
|
};
|
|
|
|
|
|
2017-05-18 03:29:58 +00:00
|
|
|
|
let old_noun = match old_binding.is_import() {
|
|
|
|
|
true => "import",
|
|
|
|
|
false => "definition",
|
2016-03-16 05:20:58 +00:00
|
|
|
|
};
|
|
|
|
|
|
2017-05-18 03:29:58 +00:00
|
|
|
|
let new_participle = match new_binding.is_import() {
|
|
|
|
|
true => "imported",
|
|
|
|
|
false => "defined",
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-09 18:42:37 +00:00
|
|
|
|
let (name, span) =
|
|
|
|
|
(ident.name, self.session.source_map().guess_head_span(new_binding.span));
|
2016-10-28 07:30:23 +00:00
|
|
|
|
|
|
|
|
|
if let Some(s) = self.name_already_seen.get(&name) {
|
|
|
|
|
if s == &span {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-18 03:29:58 +00:00
|
|
|
|
let old_kind = match (ns, old_binding.module()) {
|
|
|
|
|
(ValueNS, _) => "value",
|
|
|
|
|
(MacroNS, _) => "macro",
|
|
|
|
|
(TypeNS, _) if old_binding.is_extern_crate() => "extern crate",
|
|
|
|
|
(TypeNS, Some(module)) if module.is_normal() => "module",
|
|
|
|
|
(TypeNS, Some(module)) if module.is_trait() => "trait",
|
|
|
|
|
(TypeNS, _) => "type",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let msg = format!("the name `{}` is defined multiple times", name);
|
|
|
|
|
|
|
|
|
|
let mut err = match (old_binding.is_extern_crate(), new_binding.is_extern_crate()) {
|
2016-12-12 08:17:47 +00:00
|
|
|
|
(true, true) => struct_span_err!(self.session, span, E0259, "{}", msg),
|
2017-05-18 03:29:58 +00:00
|
|
|
|
(true, _) | (_, true) => match new_binding.is_import() && old_binding.is_import() {
|
2016-12-12 08:17:47 +00:00
|
|
|
|
true => struct_span_err!(self.session, span, E0254, "{}", msg),
|
|
|
|
|
false => struct_span_err!(self.session, span, E0260, "{}", msg),
|
2016-08-29 10:50:08 +00:00
|
|
|
|
},
|
2017-05-18 03:29:58 +00:00
|
|
|
|
_ => match (old_binding.is_import(), new_binding.is_import()) {
|
2016-12-12 08:17:47 +00:00
|
|
|
|
(false, false) => struct_span_err!(self.session, span, E0428, "{}", msg),
|
|
|
|
|
(true, true) => struct_span_err!(self.session, span, E0252, "{}", msg),
|
|
|
|
|
_ => struct_span_err!(self.session, span, E0255, "{}", msg),
|
2016-03-16 05:20:58 +00:00
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
|
err.note(&format!(
|
|
|
|
|
"`{}` must be defined only once in the {} namespace of this {}",
|
|
|
|
|
name,
|
|
|
|
|
ns.descr(),
|
|
|
|
|
container
|
|
|
|
|
));
|
2017-05-18 03:29:58 +00:00
|
|
|
|
|
|
|
|
|
err.span_label(span, format!("`{}` re{} here", name, new_participle));
|
2018-11-25 21:05:06 +00:00
|
|
|
|
err.span_label(
|
2020-03-09 18:42:37 +00:00
|
|
|
|
self.session.source_map().guess_head_span(old_binding.span),
|
2018-11-25 21:05:06 +00:00
|
|
|
|
format!("previous {} of the {} `{}` here", old_noun, old_kind, name),
|
|
|
|
|
);
|
2017-05-18 03:29:58 +00:00
|
|
|
|
|
2017-10-31 17:31:48 +00:00
|
|
|
|
// See https://github.com/rust-lang/rust/issues/32354
|
2019-01-29 12:34:40 +00:00
|
|
|
|
use NameBindingKind::Import;
|
2020-03-07 16:02:32 +00:00
|
|
|
|
let import = match (&new_binding.kind, &old_binding.kind) {
|
2019-01-29 12:34:40 +00:00
|
|
|
|
// If there are two imports where one or both have attributes then prefer removing the
|
|
|
|
|
// import without attributes.
|
2020-03-07 16:02:32 +00:00
|
|
|
|
(Import { import: new, .. }, Import { import: old, .. })
|
2019-12-24 22:38:22 +00:00
|
|
|
|
if {
|
|
|
|
|
!new_binding.span.is_dummy()
|
|
|
|
|
&& !old_binding.span.is_dummy()
|
|
|
|
|
&& (new.has_attributes || old.has_attributes)
|
|
|
|
|
} =>
|
|
|
|
|
{
|
2019-01-29 12:34:40 +00:00
|
|
|
|
if old.has_attributes {
|
|
|
|
|
Some((new, new_binding.span, true))
|
|
|
|
|
} else {
|
|
|
|
|
Some((old, old_binding.span, true))
|
|
|
|
|
}
|
2019-12-24 22:38:22 +00:00
|
|
|
|
}
|
2019-01-29 12:34:40 +00:00
|
|
|
|
// Otherwise prioritize the new binding.
|
2020-03-07 16:02:32 +00:00
|
|
|
|
(Import { import, .. }, other) if !new_binding.span.is_dummy() => {
|
|
|
|
|
Some((import, new_binding.span, other.is_import()))
|
2019-12-24 22:38:22 +00:00
|
|
|
|
}
|
2020-03-07 16:02:32 +00:00
|
|
|
|
(other, Import { import, .. }) if !old_binding.span.is_dummy() => {
|
|
|
|
|
Some((import, old_binding.span, other.is_import()))
|
2019-12-24 22:38:22 +00:00
|
|
|
|
}
|
2019-01-25 21:36:50 +00:00
|
|
|
|
_ => None,
|
|
|
|
|
};
|
2017-10-31 17:31:48 +00:00
|
|
|
|
|
2019-01-29 12:34:40 +00:00
|
|
|
|
// Check if the target of the use for both bindings is the same.
|
2019-04-20 16:36:05 +00:00
|
|
|
|
let duplicate = new_binding.res().opt_def_id() == old_binding.res().opt_def_id();
|
2019-01-29 12:34:40 +00:00
|
|
|
|
let has_dummy_span = new_binding.span.is_dummy() || old_binding.span.is_dummy();
|
2019-12-24 22:38:22 +00:00
|
|
|
|
let from_item =
|
|
|
|
|
self.extern_prelude.get(&ident).map(|entry| entry.introduced_by_item).unwrap_or(true);
|
2019-01-29 12:34:40 +00:00
|
|
|
|
// Only suggest removing an import if both bindings are to the same def, if both spans
|
|
|
|
|
// aren't dummy spans. Further, if both bindings are imports, then the ident must have
|
|
|
|
|
// been introduced by a item.
|
2019-12-24 22:38:22 +00:00
|
|
|
|
let should_remove_import = duplicate
|
|
|
|
|
&& !has_dummy_span
|
|
|
|
|
&& ((new_binding.is_extern_crate() || old_binding.is_extern_crate()) || from_item);
|
2019-01-29 12:34:40 +00:00
|
|
|
|
|
2020-03-07 16:02:32 +00:00
|
|
|
|
match import {
|
|
|
|
|
Some((import, span, true)) if should_remove_import && import.is_nested() => {
|
|
|
|
|
self.add_suggestion_for_duplicate_nested_use(&mut err, import, span)
|
2019-12-24 22:38:22 +00:00
|
|
|
|
}
|
2020-03-07 16:02:32 +00:00
|
|
|
|
Some((import, _, true)) if should_remove_import && !import.is_glob() => {
|
2019-01-29 12:34:40 +00:00
|
|
|
|
// Simple case - remove the entire import. Due to the above match arm, this can
|
|
|
|
|
// only be a single use so just remove it entirely.
|
2019-03-11 16:50:50 +00:00
|
|
|
|
err.tool_only_span_suggestion(
|
2020-03-07 16:02:32 +00:00
|
|
|
|
import.use_span_with_attributes,
|
2019-01-29 12:34:40 +00:00
|
|
|
|
"remove unnecessary import",
|
|
|
|
|
String::new(),
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
);
|
2019-12-24 22:38:22 +00:00
|
|
|
|
}
|
2020-03-07 16:02:32 +00:00
|
|
|
|
Some((import, span, _)) => {
|
|
|
|
|
self.add_suggestion_for_rename_of_use(&mut err, name, import, span)
|
2019-12-24 22:38:22 +00:00
|
|
|
|
}
|
|
|
|
|
_ => {}
|
2019-01-29 12:34:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err.emit();
|
|
|
|
|
self.name_already_seen.insert(name, span);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// This function adds a suggestion to change the binding name of a new import that conflicts
|
|
|
|
|
/// with an existing import.
|
|
|
|
|
///
|
2020-05-01 20:28:15 +00:00
|
|
|
|
/// ```text,ignore (diagnostic)
|
2019-01-29 12:34:40 +00:00
|
|
|
|
/// help: you can use `as` to change the binding name of the import
|
|
|
|
|
/// |
|
|
|
|
|
/// LL | use foo::bar as other_bar;
|
|
|
|
|
/// | ^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
/// ```
|
|
|
|
|
fn add_suggestion_for_rename_of_use(
|
|
|
|
|
&self,
|
|
|
|
|
err: &mut DiagnosticBuilder<'_>,
|
2020-04-19 11:00:18 +00:00
|
|
|
|
name: Symbol,
|
2020-03-07 16:02:32 +00:00
|
|
|
|
import: &Import<'_>,
|
2019-01-29 12:34:40 +00:00
|
|
|
|
binding_span: Span,
|
|
|
|
|
) {
|
|
|
|
|
let suggested_name = if name.as_str().chars().next().unwrap().is_uppercase() {
|
|
|
|
|
format!("Other{}", name)
|
|
|
|
|
} else {
|
|
|
|
|
format!("other_{}", name)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut suggestion = None;
|
2020-03-07 16:02:32 +00:00
|
|
|
|
match import.kind {
|
2020-03-07 15:49:13 +00:00
|
|
|
|
ImportKind::Single { type_ns_only: true, .. } => {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
suggestion = Some(format!("self as {}", suggested_name))
|
|
|
|
|
}
|
2020-03-07 15:49:13 +00:00
|
|
|
|
ImportKind::Single { source, .. } => {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
if let Some(pos) =
|
|
|
|
|
source.span.hi().0.checked_sub(binding_span.lo().0).map(|pos| pos as usize)
|
|
|
|
|
{
|
|
|
|
|
if let Ok(snippet) = self.session.source_map().span_to_snippet(binding_span) {
|
2019-01-29 12:34:40 +00:00
|
|
|
|
if pos <= snippet.len() {
|
|
|
|
|
suggestion = Some(format!(
|
|
|
|
|
"{} as {}{}",
|
|
|
|
|
&snippet[..pos],
|
|
|
|
|
suggested_name,
|
2020-02-26 12:03:46 +00:00
|
|
|
|
if snippet.ends_with(';') { ";" } else { "" }
|
2019-01-29 12:34:40 +00:00
|
|
|
|
))
|
2019-01-25 21:36:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-07 15:49:13 +00:00
|
|
|
|
ImportKind::ExternCrate { source, target, .. } => {
|
2019-01-29 12:34:40 +00:00
|
|
|
|
suggestion = Some(format!(
|
|
|
|
|
"extern crate {} as {};",
|
|
|
|
|
source.unwrap_or(target.name),
|
|
|
|
|
suggested_name,
|
2019-12-24 22:38:22 +00:00
|
|
|
|
))
|
|
|
|
|
}
|
2019-01-29 12:34:40 +00:00
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let rename_msg = "you can use `as` to change the binding name of the import";
|
|
|
|
|
if let Some(suggestion) = suggestion {
|
|
|
|
|
err.span_suggestion(
|
|
|
|
|
binding_span,
|
|
|
|
|
rename_msg,
|
|
|
|
|
suggestion,
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
err.span_label(binding_span, rename_msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-29 01:48:54 +00:00
|
|
|
|
|
2019-01-29 12:34:40 +00:00
|
|
|
|
/// This function adds a suggestion to remove a unnecessary binding from an import that is
|
|
|
|
|
/// nested. In the following example, this function will be invoked to remove the `a` binding
|
|
|
|
|
/// in the second use statement:
|
|
|
|
|
///
|
|
|
|
|
/// ```ignore (diagnostic)
|
|
|
|
|
/// use issue_52891::a;
|
|
|
|
|
/// use issue_52891::{d, a, e};
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// The following suggestion will be added:
|
|
|
|
|
///
|
|
|
|
|
/// ```ignore (diagnostic)
|
|
|
|
|
/// use issue_52891::{d, a, e};
|
|
|
|
|
/// ^-- help: remove unnecessary import
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// If the nested use contains only one import then the suggestion will remove the entire
|
|
|
|
|
/// line.
|
|
|
|
|
///
|
2020-03-07 16:02:32 +00:00
|
|
|
|
/// It is expected that the provided import is nested - this isn't checked by the
|
2019-01-29 12:34:40 +00:00
|
|
|
|
/// function. If this invariant is not upheld, this function's behaviour will be unexpected
|
|
|
|
|
/// as characters expected by span manipulations won't be present.
|
|
|
|
|
fn add_suggestion_for_duplicate_nested_use(
|
|
|
|
|
&self,
|
|
|
|
|
err: &mut DiagnosticBuilder<'_>,
|
2020-03-07 16:02:32 +00:00
|
|
|
|
import: &Import<'_>,
|
2019-01-29 12:34:40 +00:00
|
|
|
|
binding_span: Span,
|
|
|
|
|
) {
|
2020-03-07 16:02:32 +00:00
|
|
|
|
assert!(import.is_nested());
|
2019-01-29 12:34:40 +00:00
|
|
|
|
let message = "remove unnecessary import";
|
|
|
|
|
|
|
|
|
|
// Two examples will be used to illustrate the span manipulations we're doing:
|
|
|
|
|
//
|
|
|
|
|
// - Given `use issue_52891::{d, a, e};` where `a` is a duplicate then `binding_span` is
|
2020-03-07 16:02:32 +00:00
|
|
|
|
// `a` and `import.use_span` is `issue_52891::{d, a, e};`.
|
2019-01-29 12:34:40 +00:00
|
|
|
|
// - Given `use issue_52891::{d, e, a};` where `a` is a duplicate then `binding_span` is
|
2020-03-07 16:02:32 +00:00
|
|
|
|
// `a` and `import.use_span` is `issue_52891::{d, e, a};`.
|
2019-01-29 12:34:40 +00:00
|
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
|
let (found_closing_brace, span) =
|
2020-03-07 16:02:32 +00:00
|
|
|
|
find_span_of_binding_until_next_binding(self.session, binding_span, import.use_span);
|
2019-01-29 12:34:40 +00:00
|
|
|
|
|
|
|
|
|
// If there was a closing brace then identify the span to remove any trailing commas from
|
|
|
|
|
// previous imports.
|
|
|
|
|
if found_closing_brace {
|
2019-04-07 21:18:13 +00:00
|
|
|
|
if let Some(span) = extend_span_to_previous_binding(self.session, span) {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
err.tool_only_span_suggestion(
|
|
|
|
|
span,
|
|
|
|
|
message,
|
|
|
|
|
String::new(),
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
);
|
2019-04-07 21:18:13 +00:00
|
|
|
|
} else {
|
|
|
|
|
// Remove the entire line if we cannot extend the span back, this indicates a
|
|
|
|
|
// `issue_52891::{self}` case.
|
2019-12-24 22:38:22 +00:00
|
|
|
|
err.span_suggestion(
|
2020-03-07 16:02:32 +00:00
|
|
|
|
import.use_span_with_attributes,
|
2019-12-24 22:38:22 +00:00
|
|
|
|
message,
|
|
|
|
|
String::new(),
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
);
|
2017-10-31 17:31:48 +00:00
|
|
|
|
}
|
2019-04-07 21:18:13 +00:00
|
|
|
|
|
|
|
|
|
return;
|
2017-10-31 17:31:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-29 12:34:40 +00:00
|
|
|
|
err.span_suggestion(span, message, String::new(), Applicability::MachineApplicable);
|
2016-03-16 05:20:58 +00:00
|
|
|
|
}
|
2018-09-28 22:31:54 +00:00
|
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
|
fn extern_prelude_get(
|
|
|
|
|
&mut self,
|
|
|
|
|
ident: Ident,
|
|
|
|
|
speculative: bool,
|
|
|
|
|
) -> Option<&'a NameBinding<'a>> {
|
2018-11-08 22:29:07 +00:00
|
|
|
|
if ident.is_path_segment_keyword() {
|
|
|
|
|
// Make sure `self`, `super` etc produce an error when passed to here.
|
|
|
|
|
return None;
|
|
|
|
|
}
|
2020-03-13 22:36:46 +00:00
|
|
|
|
self.extern_prelude.get(&ident.normalize_to_macros_2_0()).cloned().and_then(|entry| {
|
2018-09-28 22:31:54 +00:00
|
|
|
|
if let Some(binding) = entry.extern_crate_item {
|
2019-01-12 21:58:45 +00:00
|
|
|
|
if !speculative && entry.introduced_by_item {
|
|
|
|
|
self.record_use(ident, TypeNS, binding, false);
|
|
|
|
|
}
|
2018-09-28 22:31:54 +00:00
|
|
|
|
Some(binding)
|
|
|
|
|
} else {
|
|
|
|
|
let crate_id = if !speculative {
|
|
|
|
|
self.crate_loader.process_path_extern(ident.name, ident.span)
|
|
|
|
|
} else {
|
2020-07-05 07:39:15 +00:00
|
|
|
|
self.crate_loader.maybe_process_path_extern(ident.name)?
|
2018-09-28 22:31:54 +00:00
|
|
|
|
};
|
|
|
|
|
let crate_root = self.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX });
|
2019-12-24 22:38:22 +00:00
|
|
|
|
Some(
|
|
|
|
|
(crate_root, ty::Visibility::Public, DUMMY_SP, ExpnId::root())
|
|
|
|
|
.to_name_binding(self.arenas),
|
|
|
|
|
)
|
2018-09-28 22:31:54 +00:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2015-03-15 21:44:19 +00:00
|
|
|
|
|
2020-08-23 21:47:59 +00:00
|
|
|
|
/// This is equivalent to `get_traits_in_module_containing_item`, but without filtering by the associated item.
|
|
|
|
|
///
|
|
|
|
|
/// This is used by rustdoc for intra-doc links.
|
2020-08-08 17:37:44 +00:00
|
|
|
|
pub fn traits_in_scope(&mut self, module_id: DefId) -> Vec<TraitCandidate> {
|
|
|
|
|
let module = self.get_module(module_id);
|
|
|
|
|
module.ensure_traits(self);
|
|
|
|
|
let traits = module.traits.borrow();
|
|
|
|
|
let to_candidate =
|
|
|
|
|
|this: &mut Self, &(trait_name, binding): &(Ident, &NameBinding<'_>)| TraitCandidate {
|
|
|
|
|
def_id: binding.res().def_id(),
|
|
|
|
|
import_ids: this.find_transitive_imports(&binding.kind, trait_name),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut candidates: Vec<_> =
|
|
|
|
|
traits.as_ref().unwrap().iter().map(|x| to_candidate(self, x)).collect();
|
|
|
|
|
|
|
|
|
|
if let Some(prelude) = self.prelude {
|
|
|
|
|
if !module.no_implicit_prelude {
|
|
|
|
|
prelude.ensure_traits(self);
|
|
|
|
|
candidates.extend(
|
|
|
|
|
prelude.traits.borrow().as_ref().unwrap().iter().map(|x| to_candidate(self, x)),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
candidates
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-07 23:39:02 +00:00
|
|
|
|
/// Rustdoc uses this to resolve things in a recoverable way. `ResolutionError<'a>`
|
|
|
|
|
/// isn't something that can be returned because it can't be made to live that long,
|
|
|
|
|
/// and also it's a private type. Fortunately rustdoc doesn't need to know the error,
|
|
|
|
|
/// just that an error occurred.
|
2019-08-08 23:16:45 +00:00
|
|
|
|
// FIXME(Manishearth): intra-doc links won't get warned of epoch changes.
|
|
|
|
|
pub fn resolve_str_path_error(
|
2019-12-24 22:38:22 +00:00
|
|
|
|
&mut self,
|
|
|
|
|
span: Span,
|
|
|
|
|
path_str: &str,
|
|
|
|
|
ns: Namespace,
|
2020-06-06 17:09:05 +00:00
|
|
|
|
module_id: DefId,
|
2019-08-08 23:16:45 +00:00
|
|
|
|
) -> Result<(ast::Path, Res), ()> {
|
2019-08-07 23:39:02 +00:00
|
|
|
|
let path = if path_str.starts_with("::") {
|
|
|
|
|
ast::Path {
|
|
|
|
|
span,
|
2019-08-10 23:20:18 +00:00
|
|
|
|
segments: iter::once(Ident::with_dummy_span(kw::PathRoot))
|
2020-03-27 20:55:15 +00:00
|
|
|
|
.chain(path_str.split("::").skip(1).map(Ident::from_str))
|
2019-08-07 23:39:02 +00:00
|
|
|
|
.map(|i| self.new_ast_path_segment(i))
|
|
|
|
|
.collect(),
|
2020-08-21 22:51:23 +00:00
|
|
|
|
tokens: None,
|
2019-08-07 23:39:02 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
ast::Path {
|
|
|
|
|
span,
|
|
|
|
|
segments: path_str
|
|
|
|
|
.split("::")
|
|
|
|
|
.map(Ident::from_str)
|
|
|
|
|
.map(|i| self.new_ast_path_segment(i))
|
|
|
|
|
.collect(),
|
2020-08-21 22:51:23 +00:00
|
|
|
|
tokens: None,
|
2019-08-07 23:39:02 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
2020-06-06 17:09:05 +00:00
|
|
|
|
let module = self.get_module(module_id);
|
2019-08-15 17:47:15 +00:00
|
|
|
|
let parent_scope = &ParentScope::module(module);
|
2019-08-08 23:16:45 +00:00
|
|
|
|
let res = self.resolve_ast_path(&path, ns, parent_scope).map_err(|_| ())?;
|
2019-08-07 23:39:02 +00:00
|
|
|
|
Ok((path, res))
|
|
|
|
|
}
|
2016-11-30 22:35:25 +00:00
|
|
|
|
|
2019-08-08 23:16:45 +00:00
|
|
|
|
// Resolve a path passed from rustdoc or HIR lowering.
|
|
|
|
|
fn resolve_ast_path(
|
2019-08-07 23:39:02 +00:00
|
|
|
|
&mut self,
|
|
|
|
|
path: &ast::Path,
|
2019-08-08 23:16:45 +00:00
|
|
|
|
ns: Namespace,
|
|
|
|
|
parent_scope: &ParentScope<'a>,
|
2019-08-07 23:39:02 +00:00
|
|
|
|
) -> Result<Res, (Span, ResolutionError<'a>)> {
|
2019-08-08 23:16:45 +00:00
|
|
|
|
match self.resolve_path(
|
2019-12-24 22:38:22 +00:00
|
|
|
|
&Segment::from_path(path),
|
|
|
|
|
Some(ns),
|
|
|
|
|
parent_scope,
|
2020-08-29 13:47:39 +00:00
|
|
|
|
false,
|
2019-12-24 22:38:22 +00:00
|
|
|
|
path.span,
|
|
|
|
|
CrateLint::No,
|
2019-08-08 23:16:45 +00:00
|
|
|
|
) {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
PathResult::Module(ModuleOrUniformRoot::Module(module)) => Ok(module.res().unwrap()),
|
|
|
|
|
PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => {
|
|
|
|
|
Ok(path_res.base_res())
|
|
|
|
|
}
|
|
|
|
|
PathResult::NonModule(..) => Err((
|
|
|
|
|
path.span,
|
|
|
|
|
ResolutionError::FailedToResolve {
|
2019-08-07 23:39:02 +00:00
|
|
|
|
label: String::from("type-relative paths are not supported in this context"),
|
|
|
|
|
suggestion: None,
|
2019-12-24 22:38:22 +00:00
|
|
|
|
},
|
|
|
|
|
)),
|
2019-08-07 23:39:02 +00:00
|
|
|
|
PathResult::Module(..) | PathResult::Indeterminate => unreachable!(),
|
|
|
|
|
PathResult::Failed { span, label, suggestion, .. } => {
|
2019-12-24 22:38:22 +00:00
|
|
|
|
Err((span, ResolutionError::FailedToResolve { label, suggestion }))
|
2019-08-07 23:39:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-03 22:38:02 +00:00
|
|
|
|
fn new_ast_path_segment(&mut self, ident: Ident) -> ast::PathSegment {
|
2019-08-07 23:39:02 +00:00
|
|
|
|
let mut seg = ast::PathSegment::from_ident(ident);
|
2019-11-03 22:38:02 +00:00
|
|
|
|
seg.id = self.next_node_id();
|
2019-08-07 23:39:02 +00:00
|
|
|
|
seg
|
|
|
|
|
}
|
2019-10-19 23:55:39 +00:00
|
|
|
|
|
|
|
|
|
// For rustdoc.
|
|
|
|
|
pub fn graph_root(&self) -> Module<'a> {
|
|
|
|
|
self.graph_root
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// For rustdoc.
|
2020-04-19 11:00:18 +00:00
|
|
|
|
pub fn all_macros(&self) -> &FxHashMap<Symbol, Res> {
|
2019-10-19 23:55:39 +00:00
|
|
|
|
&self.all_macros
|
|
|
|
|
}
|
2020-06-20 18:59:29 +00:00
|
|
|
|
|
|
|
|
|
/// Retrieves the span of the given `DefId` if `DefId` is in the local crate.
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn opt_span(&self, def_id: DefId) -> Option<Span> {
|
|
|
|
|
if let Some(def_id) = def_id.as_local() { Some(self.def_id_to_span[def_id]) } else { None }
|
|
|
|
|
}
|
2016-11-30 22:35:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-19 11:00:18 +00:00
|
|
|
|
fn names_to_string(names: &[Symbol]) -> String {
|
2015-03-15 21:44:19 +00:00
|
|
|
|
let mut result = String::new();
|
2019-12-24 22:38:22 +00:00
|
|
|
|
for (i, name) in names.iter().filter(|name| **name != kw::PathRoot).enumerate() {
|
2016-12-05 03:51:11 +00:00
|
|
|
|
if i > 0 {
|
|
|
|
|
result.push_str("::");
|
|
|
|
|
}
|
2019-11-24 01:08:04 +00:00
|
|
|
|
if Ident::with_dummy_span(*name).is_raw_guess() {
|
2019-11-20 22:50:13 +00:00
|
|
|
|
result.push_str("r#");
|
|
|
|
|
}
|
2019-09-14 20:10:12 +00:00
|
|
|
|
result.push_str(&name.as_str());
|
2015-10-26 19:31:11 +00:00
|
|
|
|
}
|
2015-03-15 21:44:19 +00:00
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-30 22:35:25 +00:00
|
|
|
|
fn path_names_to_string(path: &Path) -> String {
|
2019-11-20 22:50:13 +00:00
|
|
|
|
names_to_string(&path.segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>())
|
2015-03-15 21:44:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A somewhat inefficient routine to obtain the name of a module.
|
2019-02-06 17:15:23 +00:00
|
|
|
|
fn module_to_string(module: Module<'_>) -> Option<String> {
|
2015-03-15 21:44:19 +00:00
|
|
|
|
let mut names = Vec::new();
|
|
|
|
|
|
2020-04-19 11:00:18 +00:00
|
|
|
|
fn collect_mod(names: &mut Vec<Symbol>, module: Module<'_>) {
|
2019-04-20 16:46:19 +00:00
|
|
|
|
if let ModuleKind::Def(.., name) = module.kind {
|
2016-09-18 09:45:06 +00:00
|
|
|
|
if let Some(parent) = module.parent {
|
2019-09-14 20:10:12 +00:00
|
|
|
|
names.push(name);
|
2016-09-18 09:45:06 +00:00
|
|
|
|
collect_mod(names, parent);
|
2015-03-15 21:44:19 +00:00
|
|
|
|
}
|
2016-09-18 09:45:06 +00:00
|
|
|
|
} else {
|
2020-04-19 11:00:18 +00:00
|
|
|
|
names.push(Symbol::intern("<opaque>"));
|
2016-10-01 07:38:47 +00:00
|
|
|
|
collect_mod(names, module.parent.unwrap());
|
2015-03-15 21:44:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
collect_mod(&mut names, module);
|
|
|
|
|
|
2015-03-24 23:53:34 +00:00
|
|
|
|
if names.is_empty() {
|
2018-01-16 19:47:14 +00:00
|
|
|
|
return None;
|
2015-03-15 21:44:19 +00:00
|
|
|
|
}
|
2019-09-14 20:10:12 +00:00
|
|
|
|
names.reverse();
|
|
|
|
|
Some(names_to_string(&names))
|
2015-03-15 21:44:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-22 22:43:02 +00:00
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
2018-05-22 15:10:17 +00:00
|
|
|
|
enum CrateLint {
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Do not issue the lint.
|
2018-05-22 15:10:17 +00:00
|
|
|
|
No,
|
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// This lint applies to some arbitrary path; e.g., `impl ::foo::Bar`.
|
|
|
|
|
/// In this case, we can take the span of that path.
|
2018-05-22 15:10:17 +00:00
|
|
|
|
SimplePath(NodeId),
|
|
|
|
|
|
|
|
|
|
/// This lint comes from a `use` statement. In this case, what we
|
|
|
|
|
/// care about really is the *root* `use` statement; e.g., if we
|
|
|
|
|
/// have nested things like `use a::{b, c}`, we care about the
|
|
|
|
|
/// `use a` part.
|
|
|
|
|
UsePath { root_id: NodeId, root_span: Span },
|
2018-05-22 23:01:09 +00:00
|
|
|
|
|
|
|
|
|
/// This is the "trait item" from a fully qualified path. For example,
|
|
|
|
|
/// we might be resolving `X::Y::Z` from a path like `<T as X::Y>::Z`.
|
|
|
|
|
/// The `path_span` is the span of the to the trait itself (`X::Y`).
|
|
|
|
|
QPathTrait { qpath_id: NodeId, qpath_span: Span },
|
2018-05-22 15:10:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-21 18:14:22 +00:00
|
|
|
|
impl CrateLint {
|
|
|
|
|
fn node_id(&self) -> Option<NodeId> {
|
|
|
|
|
match *self {
|
|
|
|
|
CrateLint::No => None,
|
2019-12-24 22:38:22 +00:00
|
|
|
|
CrateLint::SimplePath(id)
|
|
|
|
|
| CrateLint::UsePath { root_id: id, .. }
|
|
|
|
|
| CrateLint::QPathTrait { qpath_id: id, .. } => Some(id),
|
2018-07-21 18:14:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-29 09:48:52 +00:00
|
|
|
|
|
2020-07-05 20:00:14 +00:00
|
|
|
|
pub fn provide(providers: &mut Providers) {
|
2020-02-24 18:44:55 +00:00
|
|
|
|
late::lifetimes::provide(providers);
|
2019-12-29 09:48:52 +00:00
|
|
|
|
}
|