2016-09-07 23:21:59 +00:00
|
|
|
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2018-11-05 01:11:59 +03:00
|
|
|
use {AmbiguityError, AmbiguityKind, AmbiguityErrorMisc};
|
2018-11-24 19:14:05 +03:00
|
|
|
use {CrateLint, Resolver, ResolutionError, ScopeSet, Weak};
|
|
|
|
use {Module, ModuleKind, NameBinding, NameBindingKind, PathResult, Segment, ToNameBinding};
|
2018-11-18 14:41:06 +03:00
|
|
|
use {is_known_tool, resolve_error};
|
2018-08-09 16:29:22 +03:00
|
|
|
use ModuleOrUniformRoot;
|
2018-11-24 19:14:05 +03:00
|
|
|
use Namespace::*;
|
2018-07-29 14:51:17 +03:00
|
|
|
use build_reduced_graph::{BuildReducedGraphVisitor, IsMacroExport};
|
2016-11-10 10:11:25 +00:00
|
|
|
use resolve_imports::ImportResolver;
|
2018-09-11 11:18:58 +02:00
|
|
|
use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX, DefIndex,
|
|
|
|
CrateNum, DefIndexAddressSpace};
|
2018-05-14 03:22:52 +03:00
|
|
|
use rustc::hir::def::{Def, NonMacroAttrKind};
|
2016-09-23 21:13:59 +00:00
|
|
|
use rustc::hir::map::{self, DefCollector};
|
2017-05-11 10:26:07 +02:00
|
|
|
use rustc::{ty, lint};
|
2016-11-29 02:07:12 +00:00
|
|
|
use syntax::ast::{self, Name, Ident};
|
2018-08-03 02:30:03 +03:00
|
|
|
use syntax::attr;
|
2016-09-07 23:21:59 +00:00
|
|
|
use syntax::errors::DiagnosticBuilder;
|
2018-09-15 23:57:07 +03:00
|
|
|
use syntax::ext::base::{self, Determinacy};
|
2017-03-01 08:44:05 +00:00
|
|
|
use syntax::ext::base::{MacroKind, SyntaxExtension, Resolver as SyntaxResolver};
|
2018-09-19 01:46:18 +03:00
|
|
|
use syntax::ext::expand::{AstFragment, Invocation, InvocationKind};
|
2018-06-30 19:53:46 +03:00
|
|
|
use syntax::ext::hygiene::{self, Mark};
|
2016-09-21 06:25:09 +00:00
|
|
|
use syntax::ext::tt::macro_rules;
|
2018-07-23 02:52:51 +03:00
|
|
|
use syntax::feature_gate::{self, feature_err, emit_feature_err, is_builtin_attr_name, GateIssue};
|
2018-08-11 16:40:08 +03:00
|
|
|
use syntax::feature_gate::EXPLAIN_DERIVE_UNDERSCORE;
|
2016-12-31 17:55:59 +10:30
|
|
|
use syntax::fold::{self, Folder};
|
2017-03-08 23:13:35 +00:00
|
|
|
use syntax::parse::parser::PathStyle;
|
|
|
|
use syntax::parse::token::{self, Token};
|
2016-11-08 03:16:54 +00:00
|
|
|
use syntax::ptr::P;
|
2017-02-02 00:33:42 +00:00
|
|
|
use syntax::symbol::{Symbol, keywords};
|
2018-09-08 18:07:02 -07:00
|
|
|
use syntax::tokenstream::{TokenStream, TokenTree, Delimited, DelimSpan};
|
2016-09-07 23:21:59 +00:00
|
|
|
use syntax::util::lev_distance::find_best_match_for_name;
|
2016-11-07 22:08:26 +00:00
|
|
|
use syntax_pos::{Span, DUMMY_SP};
|
2018-08-19 15:01:33 -04:00
|
|
|
use errors::Applicability;
|
2016-09-07 23:21:59 +00:00
|
|
|
|
2017-03-01 08:44:05 +00:00
|
|
|
use std::cell::Cell;
|
2018-11-25 16:08:43 +03:00
|
|
|
use std::{mem, ptr};
|
2018-02-27 17:11:14 +01:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2017-03-01 08:44:05 +00:00
|
|
|
|
2018-10-22 01:28:59 +03:00
|
|
|
#[derive(Clone, Debug)]
|
2016-10-03 23:48:19 +00:00
|
|
|
pub struct InvocationData<'a> {
|
2018-08-29 04:48:02 +03:00
|
|
|
def_index: DefIndex,
|
2018-08-31 22:53:08 +03:00
|
|
|
/// Module in which the macro was invoked.
|
|
|
|
crate module: Cell<Module<'a>>,
|
|
|
|
/// Legacy scope in which the macro was invoked.
|
|
|
|
/// The invocation path is resolved in this scope.
|
2018-08-29 04:48:02 +03:00
|
|
|
crate parent_legacy_scope: Cell<LegacyScope<'a>>,
|
2018-08-31 22:53:08 +03:00
|
|
|
/// Legacy scope *produced* by expanding this macro invocation,
|
|
|
|
/// includes all the macro_rules items, other invocations, etc generated by it.
|
2018-11-08 00:39:07 +03:00
|
|
|
/// `None` if the macro is not expanded yet.
|
|
|
|
crate output_legacy_scope: Cell<Option<LegacyScope<'a>>>,
|
2016-09-14 09:55:20 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 23:48:19 +00:00
|
|
|
impl<'a> InvocationData<'a> {
|
2016-09-14 21:03:09 +00:00
|
|
|
pub fn root(graph_root: Module<'a>) -> Self {
|
2016-10-03 23:48:19 +00:00
|
|
|
InvocationData {
|
2016-09-16 08:50:34 +00:00
|
|
|
module: Cell::new(graph_root),
|
2016-09-14 09:55:20 +00:00
|
|
|
def_index: CRATE_DEF_INDEX,
|
2018-08-29 04:48:02 +03:00
|
|
|
parent_legacy_scope: Cell::new(LegacyScope::Empty),
|
2018-11-08 00:39:07 +03:00
|
|
|
output_legacy_scope: Cell::new(Some(LegacyScope::Empty)),
|
2016-09-14 09:55:20 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 22:53:08 +03:00
|
|
|
/// Binding produced by a `macro_rules` item.
|
|
|
|
/// Not modularized, can shadow previous legacy bindings, etc.
|
2018-10-22 01:28:59 +03:00
|
|
|
#[derive(Debug)]
|
2018-08-29 04:48:02 +03:00
|
|
|
pub struct LegacyBinding<'a> {
|
|
|
|
binding: &'a NameBinding<'a>,
|
2018-08-31 22:53:08 +03:00
|
|
|
/// Legacy scope into which the `macro_rules` item was planted.
|
|
|
|
parent_legacy_scope: LegacyScope<'a>,
|
2018-08-29 04:48:02 +03:00
|
|
|
ident: Ident,
|
|
|
|
}
|
|
|
|
|
2018-08-31 22:53:08 +03:00
|
|
|
/// Scope introduced by a `macro_rules!` macro.
|
|
|
|
/// Starts at the macro's definition and ends at the end of the macro's parent module
|
|
|
|
/// (named or unnamed), or even further if it escapes with `#[macro_use]`.
|
|
|
|
/// Some macro invocations need to introduce legacy scopes too because they
|
|
|
|
/// potentially can expand into macro definitions.
|
2018-10-22 01:28:59 +03:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2016-10-06 08:04:30 +00:00
|
|
|
pub enum LegacyScope<'a> {
|
2018-08-31 22:53:08 +03:00
|
|
|
/// Created when invocation data is allocated in the arena,
|
|
|
|
/// must be replaced with a proper scope later.
|
|
|
|
Uninitialized,
|
|
|
|
/// Empty "root" scope at the crate start containing no names.
|
2016-10-06 08:04:30 +00:00
|
|
|
Empty,
|
2018-08-31 22:53:08 +03:00
|
|
|
/// Scope introduced by a `macro_rules!` macro definition.
|
2016-10-06 08:04:30 +00:00
|
|
|
Binding(&'a LegacyBinding<'a>),
|
2018-08-31 22:53:08 +03:00
|
|
|
/// Scope introduced by a macro invocation that can potentially
|
|
|
|
/// create a `macro_rules!` macro definition.
|
|
|
|
Invocation(&'a InvocationData<'a>),
|
2018-08-18 02:38:51 +03:00
|
|
|
}
|
|
|
|
|
2018-10-22 01:28:59 +03:00
|
|
|
/// Everything you need to resolve a macro or import path.
|
|
|
|
#[derive(Clone, Debug)]
|
2018-09-13 01:41:07 +03:00
|
|
|
pub struct ParentScope<'a> {
|
|
|
|
crate module: Module<'a>,
|
|
|
|
crate expansion: Mark,
|
|
|
|
crate legacy: LegacyScope<'a>,
|
|
|
|
crate derives: Vec<ast::Path>,
|
|
|
|
}
|
|
|
|
|
2018-09-08 22:19:53 +03:00
|
|
|
// Macro namespace is separated into two sub-namespaces, one for bang macros and
|
|
|
|
// one for attribute-like macros (attributes, derives).
|
|
|
|
// We ignore resolutions from one sub-namespace when searching names in scope for another.
|
2018-11-08 00:39:07 +03:00
|
|
|
fn sub_namespace_match(candidate: Option<MacroKind>, requirement: Option<MacroKind>) -> bool {
|
2018-09-08 22:19:53 +03:00
|
|
|
#[derive(PartialEq)]
|
|
|
|
enum SubNS { Bang, AttrLike }
|
|
|
|
let sub_ns = |kind| match kind {
|
|
|
|
MacroKind::Bang => Some(SubNS::Bang),
|
|
|
|
MacroKind::Attr | MacroKind::Derive => Some(SubNS::AttrLike),
|
|
|
|
MacroKind::ProcMacroStub => None,
|
|
|
|
};
|
|
|
|
let requirement = requirement.and_then(|kind| sub_ns(kind));
|
|
|
|
let candidate = candidate.and_then(|kind| sub_ns(kind));
|
|
|
|
// "No specific sub-namespace" means "matches anything" for both requirements and candidates.
|
2018-11-08 00:39:07 +03:00
|
|
|
candidate.is_none() || requirement.is_none() || candidate == requirement
|
2018-09-11 00:28:35 +03:00
|
|
|
}
|
|
|
|
|
2018-07-31 15:23:31 -06:00
|
|
|
impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
|
2016-09-05 00:10:27 +00:00
|
|
|
fn next_node_id(&mut self) -> ast::NodeId {
|
|
|
|
self.session.next_node_id()
|
|
|
|
}
|
|
|
|
|
2016-09-23 07:23:01 +00:00
|
|
|
fn get_module_scope(&mut self, id: ast::NodeId) -> Mark {
|
2017-03-22 08:39:51 +00:00
|
|
|
let mark = Mark::fresh(Mark::root());
|
2016-12-20 08:32:15 +00:00
|
|
|
let module = self.module_map[&self.definitions.local_def_id(id)];
|
2016-10-03 23:48:19 +00:00
|
|
|
self.invocations.insert(mark, self.arenas.alloc_invocation_data(InvocationData {
|
2016-09-16 08:50:34 +00:00
|
|
|
module: Cell::new(module),
|
2016-09-23 07:23:01 +00:00
|
|
|
def_index: module.def_id().unwrap().index,
|
2018-08-29 04:48:02 +03:00
|
|
|
parent_legacy_scope: Cell::new(LegacyScope::Empty),
|
2018-11-08 00:39:07 +03:00
|
|
|
output_legacy_scope: Cell::new(Some(LegacyScope::Empty)),
|
2016-09-16 08:50:34 +00:00
|
|
|
}));
|
2016-09-23 07:23:01 +00:00
|
|
|
mark
|
|
|
|
}
|
|
|
|
|
2016-11-08 03:16:54 +00:00
|
|
|
fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item> {
|
2018-07-31 15:23:31 -06:00
|
|
|
struct EliminateCrateVar<'b, 'a: 'b, 'crateloader: 'a>(
|
|
|
|
&'b mut Resolver<'a, 'crateloader>, Span
|
|
|
|
);
|
2016-11-08 03:16:54 +00:00
|
|
|
|
2018-07-31 15:23:31 -06:00
|
|
|
impl<'a, 'b, 'crateloader> Folder for EliminateCrateVar<'a, 'b, 'crateloader> {
|
2018-06-11 19:44:48 -04:00
|
|
|
fn fold_path(&mut self, path: ast::Path) -> ast::Path {
|
|
|
|
match self.fold_qpath(None, path) {
|
|
|
|
(None, path) => path,
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_qpath(&mut self, mut qself: Option<ast::QSelf>, mut path: ast::Path)
|
|
|
|
-> (Option<ast::QSelf>, ast::Path) {
|
|
|
|
qself = qself.map(|ast::QSelf { ty, path_span, position }| {
|
|
|
|
ast::QSelf {
|
|
|
|
ty: self.fold_ty(ty),
|
|
|
|
path_span: self.new_span(path_span),
|
|
|
|
position,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-24 19:12:00 +03:00
|
|
|
if path.segments[0].ident.name == keywords::DollarCrate.name() {
|
|
|
|
let module = self.0.resolve_crate_root(path.segments[0].ident);
|
2018-03-18 03:53:41 +03:00
|
|
|
path.segments[0].ident.name = keywords::CrateRoot.name();
|
2016-12-05 03:51:11 +00:00
|
|
|
if !module.is_local() {
|
2018-03-19 03:54:56 +03:00
|
|
|
let span = path.segments[0].ident.span;
|
2016-12-05 03:51:11 +00:00
|
|
|
path.segments.insert(1, match module.kind {
|
2017-03-08 20:30:06 +03:00
|
|
|
ModuleKind::Def(_, name) => ast::PathSegment::from_ident(
|
2018-03-19 03:54:56 +03:00
|
|
|
ast::Ident::with_empty_ctxt(name).with_span_pos(span)
|
2017-03-08 20:30:06 +03:00
|
|
|
),
|
2016-11-08 03:16:54 +00:00
|
|
|
_ => unreachable!(),
|
2018-06-11 19:44:48 -04:00
|
|
|
});
|
|
|
|
if let Some(qself) = &mut qself {
|
|
|
|
qself.position += 1;
|
|
|
|
}
|
2016-11-08 03:16:54 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-11 19:44:48 -04:00
|
|
|
(qself, path)
|
2016-11-08 03:16:54 +00:00
|
|
|
}
|
2016-12-31 16:02:06 +10:30
|
|
|
|
|
|
|
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
|
|
|
|
fold::noop_fold_mac(mac, self)
|
|
|
|
}
|
2016-11-08 03:16:54 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 11:42:16 +02:00
|
|
|
let ret = EliminateCrateVar(self, item.span).fold_item(item);
|
|
|
|
assert!(ret.len() == 1);
|
|
|
|
ret.into_iter().next().unwrap()
|
2016-11-08 03:16:54 +00:00
|
|
|
}
|
|
|
|
|
2016-12-22 06:03:19 +00:00
|
|
|
fn is_whitelisted_legacy_custom_derive(&self, name: Name) -> bool {
|
|
|
|
self.whitelisted_legacy_custom_derives.contains(&name)
|
|
|
|
}
|
|
|
|
|
2018-06-20 02:08:08 +03:00
|
|
|
fn visit_ast_fragment_with_placeholders(&mut self, mark: Mark, fragment: &AstFragment,
|
|
|
|
derives: &[Mark]) {
|
2016-10-06 08:04:30 +00:00
|
|
|
let invocation = self.invocations[&mark];
|
2018-06-20 02:08:08 +03:00
|
|
|
self.collect_def_ids(mark, invocation, fragment);
|
2016-10-06 08:04:30 +00:00
|
|
|
|
|
|
|
self.current_module = invocation.module.get();
|
2016-11-11 10:51:15 +00:00
|
|
|
self.current_module.unresolved_invocations.borrow_mut().remove(&mark);
|
2017-02-02 07:01:15 +00:00
|
|
|
self.current_module.unresolved_invocations.borrow_mut().extend(derives);
|
2018-10-17 11:36:19 +02:00
|
|
|
self.invocations.extend(derives.iter().map(|&derive| (derive, invocation)));
|
2016-10-06 08:04:30 +00:00
|
|
|
let mut visitor = BuildReducedGraphVisitor {
|
|
|
|
resolver: self,
|
2018-08-31 22:53:08 +03:00
|
|
|
current_legacy_scope: invocation.parent_legacy_scope.get(),
|
2016-10-11 03:42:06 +00:00
|
|
|
expansion: mark,
|
2016-10-06 08:04:30 +00:00
|
|
|
};
|
2018-06-20 02:08:08 +03:00
|
|
|
fragment.visit_with(&mut visitor);
|
2018-11-08 00:39:07 +03:00
|
|
|
invocation.output_legacy_scope.set(Some(visitor.current_legacy_scope));
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|
|
|
|
|
2018-02-27 17:11:14 +01:00
|
|
|
fn add_builtin(&mut self, ident: ast::Ident, ext: Lrc<SyntaxExtension>) {
|
2016-10-28 06:52:45 +00:00
|
|
|
let def_id = DefId {
|
2018-09-11 11:18:58 +02:00
|
|
|
krate: CrateNum::BuiltinMacros,
|
2018-01-02 06:36:12 -05:00
|
|
|
index: DefIndex::from_array_index(self.macro_map.len(),
|
|
|
|
DefIndexAddressSpace::Low),
|
2016-10-28 06:52:45 +00:00
|
|
|
};
|
2017-02-23 20:12:33 +10:30
|
|
|
let kind = ext.kind();
|
2016-10-28 06:52:45 +00:00
|
|
|
self.macro_map.insert(def_id, ext);
|
2016-11-07 22:08:26 +00:00
|
|
|
let binding = self.arenas.alloc_name_binding(NameBinding {
|
2018-07-29 14:51:17 +03:00
|
|
|
kind: NameBindingKind::Def(Def::Macro(def_id, kind), false),
|
2016-11-07 22:08:26 +00:00
|
|
|
span: DUMMY_SP,
|
2018-11-11 20:28:56 +03:00
|
|
|
vis: ty::Visibility::Public,
|
2016-11-07 22:23:26 +00:00
|
|
|
expansion: Mark::root(),
|
2016-11-07 22:08:26 +00:00
|
|
|
});
|
2018-09-04 01:14:58 +03:00
|
|
|
if self.builtin_macros.insert(ident.name, binding).is_some() {
|
|
|
|
self.session.span_err(ident.span,
|
|
|
|
&format!("built-in macro `{}` was already defined", ident));
|
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 10:11:25 +00:00
|
|
|
fn resolve_imports(&mut self) {
|
|
|
|
ImportResolver { resolver: self }.resolve_imports()
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:01:15 +00:00
|
|
|
// Resolves attribute and derive legacy macros from `#![plugin(..)]`.
|
2018-04-17 23:19:21 -07:00
|
|
|
fn find_legacy_attr_invoc(&mut self, attrs: &mut Vec<ast::Attribute>, allow_derive: bool)
|
2017-02-02 07:01:15 +00:00
|
|
|
-> Option<ast::Attribute> {
|
2018-09-15 23:57:07 +03:00
|
|
|
if !allow_derive {
|
|
|
|
return None;
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|
2017-02-02 00:33:42 +00:00
|
|
|
|
|
|
|
// Check for legacy derives
|
|
|
|
for i in 0..attrs.len() {
|
2018-01-30 14:53:01 +09:00
|
|
|
let name = attrs[i].name();
|
2017-03-03 09:23:59 +00:00
|
|
|
|
|
|
|
if name == "derive" {
|
2017-04-03 22:23:32 +00:00
|
|
|
let result = attrs[i].parse_list(&self.session.parse_sess, |parser| {
|
2018-01-30 14:53:01 +09:00
|
|
|
parser.parse_path_allowing_meta(PathStyle::Mod)
|
2017-04-03 22:23:32 +00:00
|
|
|
});
|
|
|
|
|
2017-03-08 23:13:35 +00:00
|
|
|
let mut traits = match result {
|
|
|
|
Ok(traits) => traits,
|
|
|
|
Err(mut e) => {
|
|
|
|
e.cancel();
|
|
|
|
continue
|
|
|
|
}
|
2017-02-02 00:33:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for j in 0..traits.len() {
|
2017-03-08 23:13:35 +00:00
|
|
|
if traits[j].segments.len() > 1 {
|
|
|
|
continue
|
|
|
|
}
|
2018-03-18 03:53:41 +03:00
|
|
|
let trait_name = traits[j].segments[0].ident.name;
|
2017-03-08 23:13:35 +00:00
|
|
|
let legacy_name = Symbol::intern(&format!("derive_{}", trait_name));
|
2018-09-04 01:14:58 +03:00
|
|
|
if !self.builtin_macros.contains_key(&legacy_name) {
|
2017-02-02 00:33:42 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
let span = traits.remove(j).span;
|
|
|
|
self.gate_legacy_custom_derive(legacy_name, span);
|
|
|
|
if traits.is_empty() {
|
|
|
|
attrs.remove(i);
|
|
|
|
} else {
|
2018-10-17 11:36:19 +02:00
|
|
|
let mut tokens = Vec::with_capacity(traits.len() - 1);
|
2017-03-20 05:03:10 +00:00
|
|
|
for (j, path) in traits.iter().enumerate() {
|
|
|
|
if j > 0 {
|
2017-03-08 23:13:35 +00:00
|
|
|
tokens.push(TokenTree::Token(attrs[i].span, Token::Comma).into());
|
|
|
|
}
|
2018-10-17 11:36:19 +02:00
|
|
|
tokens.reserve((path.segments.len() * 2).saturating_sub(1));
|
2017-03-20 05:03:10 +00:00
|
|
|
for (k, segment) in path.segments.iter().enumerate() {
|
|
|
|
if k > 0 {
|
2017-03-08 23:13:35 +00:00
|
|
|
tokens.push(TokenTree::Token(path.span, Token::ModSep).into());
|
|
|
|
}
|
2018-03-18 03:53:41 +03:00
|
|
|
let tok = Token::from_ast_ident(segment.ident);
|
2017-03-08 23:13:35 +00:00
|
|
|
tokens.push(TokenTree::Token(path.span, tok).into());
|
|
|
|
}
|
|
|
|
}
|
2018-09-08 18:07:02 -07:00
|
|
|
let delim_span = DelimSpan::from_single(attrs[i].span);
|
|
|
|
attrs[i].tokens = TokenTree::Delimited(delim_span, Delimited {
|
2017-03-08 23:13:35 +00:00
|
|
|
delim: token::Paren,
|
|
|
|
tts: TokenStream::concat(tokens).into(),
|
|
|
|
}).into();
|
2017-02-02 00:33:42 +00:00
|
|
|
}
|
|
|
|
return Some(ast::Attribute {
|
2018-03-19 03:54:56 +03:00
|
|
|
path: ast::Path::from_ident(Ident::new(legacy_name, span)),
|
2017-03-03 09:23:59 +00:00
|
|
|
tokens: TokenStream::empty(),
|
2017-02-02 00:33:42 +00:00
|
|
|
id: attr::mk_attr_id(),
|
|
|
|
style: ast::AttrStyle::Outer,
|
|
|
|
is_sugared_doc: false,
|
2017-08-06 22:54:09 -07:00
|
|
|
span,
|
2017-02-02 00:33:42 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-07 23:21:59 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2018-09-02 04:57:56 +03:00
|
|
|
fn resolve_macro_invocation(&mut self, invoc: &Invocation, invoc_id: Mark, force: bool)
|
2018-08-15 03:51:12 +03:00
|
|
|
-> Result<Option<Lrc<SyntaxExtension>>, Determinacy> {
|
2018-09-19 01:46:18 +03:00
|
|
|
let (path, kind, derives_in_scope, after_derive) = match invoc.kind {
|
2018-08-13 02:57:19 +03:00
|
|
|
InvocationKind::Attr { attr: None, .. } =>
|
|
|
|
return Ok(None),
|
2018-09-19 01:46:18 +03:00
|
|
|
InvocationKind::Attr { attr: Some(ref attr), ref traits, after_derive, .. } =>
|
|
|
|
(&attr.path, MacroKind::Attr, traits.clone(), after_derive),
|
2018-08-13 02:57:19 +03:00
|
|
|
InvocationKind::Bang { ref mac, .. } =>
|
2018-09-19 01:46:18 +03:00
|
|
|
(&mac.node.path, MacroKind::Bang, Vec::new(), false),
|
2018-08-13 02:57:19 +03:00
|
|
|
InvocationKind::Derive { ref path, .. } =>
|
2018-09-19 01:46:18 +03:00
|
|
|
(path, MacroKind::Derive, Vec::new(), false),
|
2017-03-01 23:48:16 +00:00
|
|
|
};
|
2018-08-11 16:40:08 +03:00
|
|
|
|
2018-09-13 01:41:07 +03:00
|
|
|
let parent_scope = self.invoc_parent_scope(invoc_id, derives_in_scope);
|
2018-11-14 02:20:59 +03:00
|
|
|
let (def, ext) = self.resolve_macro_to_def(path, kind, &parent_scope, true, force)?;
|
2018-08-15 03:51:12 +03:00
|
|
|
|
|
|
|
if let Def::Macro(def_id, _) = def {
|
2018-09-19 01:46:18 +03:00
|
|
|
if after_derive {
|
|
|
|
self.session.span_err(invoc.span(),
|
|
|
|
"macro attributes must be placed before `#[derive]`");
|
2018-09-16 17:15:07 +03:00
|
|
|
}
|
2018-08-15 03:51:12 +03:00
|
|
|
self.macro_defs.insert(invoc.expansion_data.mark, def_id);
|
|
|
|
let normal_module_def_id =
|
|
|
|
self.macro_def_scope(invoc.expansion_data.mark).normal_ancestor_id;
|
|
|
|
self.definitions.add_parent_module_of_macro_def(invoc.expansion_data.mark,
|
|
|
|
normal_module_def_id);
|
|
|
|
invoc.expansion_data.mark.set_default_transparency(ext.default_transparency());
|
2018-09-11 11:18:58 +02:00
|
|
|
invoc.expansion_data.mark.set_is_builtin(def_id.krate == CrateNum::BuiltinMacros);
|
2018-07-12 13:24:59 +03:00
|
|
|
}
|
2018-08-15 03:51:12 +03:00
|
|
|
|
2017-03-22 08:39:51 +00:00
|
|
|
Ok(Some(ext))
|
2017-03-01 23:48:16 +00:00
|
|
|
}
|
|
|
|
|
2018-09-02 04:57:56 +03:00
|
|
|
fn resolve_macro_path(&mut self, path: &ast::Path, kind: MacroKind, invoc_id: Mark,
|
2018-09-13 01:41:07 +03:00
|
|
|
derives_in_scope: Vec<ast::Path>, force: bool)
|
2018-08-15 03:51:12 +03:00
|
|
|
-> Result<Lrc<SyntaxExtension>, Determinacy> {
|
2018-09-13 01:41:07 +03:00
|
|
|
let parent_scope = self.invoc_parent_scope(invoc_id, derives_in_scope);
|
2018-11-14 02:20:59 +03:00
|
|
|
Ok(self.resolve_macro_to_def(path, kind, &parent_scope, false, force)?.1)
|
2017-05-11 10:26:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_unused_macros(&self) {
|
2017-05-15 07:35:19 +02:00
|
|
|
for did in self.unused_macros.iter() {
|
2017-05-12 08:10:52 +02:00
|
|
|
let id_span = match *self.macro_map[did] {
|
2018-06-24 19:24:51 +03:00
|
|
|
SyntaxExtension::NormalTT { def_info, .. } |
|
|
|
|
SyntaxExtension::DeclMacro { def_info, .. } => def_info,
|
2017-05-15 07:35:19 +02:00
|
|
|
_ => None,
|
|
|
|
};
|
2017-05-12 08:10:52 +02:00
|
|
|
if let Some((id, span)) = id_span {
|
2017-05-11 10:26:07 +02:00
|
|
|
let lint = lint::builtin::UNUSED_MACROS;
|
2017-07-26 21:51:09 -07:00
|
|
|
let msg = "unused macro definition";
|
|
|
|
self.session.buffer_lint(lint, id, span, msg);
|
2017-05-11 10:26:07 +02:00
|
|
|
} else {
|
|
|
|
bug!("attempted to create unused macro error, but span not available");
|
|
|
|
}
|
|
|
|
}
|
2017-03-01 23:48:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-31 15:23:31 -06:00
|
|
|
impl<'a, 'cl> Resolver<'a, 'cl> {
|
2018-11-09 01:29:07 +03:00
|
|
|
pub fn dummy_parent_scope(&self) -> ParentScope<'a> {
|
2018-09-13 01:41:07 +03:00
|
|
|
self.invoc_parent_scope(Mark::root(), Vec::new())
|
|
|
|
}
|
|
|
|
|
2018-11-09 01:29:07 +03:00
|
|
|
fn invoc_parent_scope(&self, invoc_id: Mark, derives: Vec<ast::Path>) -> ParentScope<'a> {
|
2018-09-13 01:41:07 +03:00
|
|
|
let invoc = self.invocations[&invoc_id];
|
|
|
|
ParentScope {
|
|
|
|
module: invoc.module.get().nearest_item_scope(),
|
|
|
|
expansion: invoc_id.parent(),
|
|
|
|
legacy: invoc.parent_legacy_scope.get(),
|
|
|
|
derives,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_macro_to_def(
|
|
|
|
&mut self,
|
|
|
|
path: &ast::Path,
|
|
|
|
kind: MacroKind,
|
|
|
|
parent_scope: &ParentScope<'a>,
|
2018-11-14 02:20:59 +03:00
|
|
|
trace: bool,
|
2018-09-13 01:41:07 +03:00
|
|
|
force: bool,
|
|
|
|
) -> Result<(Def, Lrc<SyntaxExtension>), Determinacy> {
|
2018-11-14 02:20:59 +03:00
|
|
|
let def = self.resolve_macro_to_def_inner(path, kind, parent_scope, trace, force);
|
2018-08-15 03:51:12 +03:00
|
|
|
|
|
|
|
// Report errors and enforce feature gates for the resolved macro.
|
2018-07-23 02:52:51 +03:00
|
|
|
if def != Err(Determinacy::Undetermined) {
|
|
|
|
// Do not report duplicated errors on every undetermined resolution.
|
2018-08-15 03:51:12 +03:00
|
|
|
for segment in &path.segments {
|
|
|
|
if let Some(args) = &segment.args {
|
|
|
|
self.session.span_err(args.span(), "generic arguments in macro path");
|
|
|
|
}
|
|
|
|
}
|
2018-07-23 02:52:51 +03:00
|
|
|
}
|
2018-08-15 03:51:12 +03:00
|
|
|
|
|
|
|
let def = def?;
|
|
|
|
|
|
|
|
match def {
|
|
|
|
Def::Macro(def_id, macro_kind) => {
|
|
|
|
self.unused_macros.remove(&def_id);
|
|
|
|
if macro_kind == MacroKind::ProcMacroStub {
|
|
|
|
let msg = "can't use a procedural macro from the same crate that defines it";
|
|
|
|
self.session.span_err(path.span, msg);
|
|
|
|
return Err(Determinacy::Determined);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Def::NonMacroAttr(attr_kind) => {
|
|
|
|
if kind == MacroKind::Attr {
|
|
|
|
let features = self.session.features_untracked();
|
|
|
|
if attr_kind == NonMacroAttrKind::Custom {
|
|
|
|
assert!(path.segments.len() == 1);
|
|
|
|
let name = path.segments[0].ident.name.as_str();
|
|
|
|
if name.starts_with("rustc_") {
|
|
|
|
if !features.rustc_attrs {
|
|
|
|
let msg = "unless otherwise specified, attributes with the prefix \
|
|
|
|
`rustc_` are reserved for internal compiler diagnostics";
|
|
|
|
feature_err(&self.session.parse_sess, "rustc_attrs", path.span,
|
|
|
|
GateIssue::Language, &msg).emit();
|
|
|
|
}
|
|
|
|
} else if name.starts_with("derive_") {
|
|
|
|
if !features.custom_derive {
|
|
|
|
feature_err(&self.session.parse_sess, "custom_derive", path.span,
|
|
|
|
GateIssue::Language, EXPLAIN_DERIVE_UNDERSCORE).emit();
|
|
|
|
}
|
|
|
|
} else if !features.custom_attribute {
|
|
|
|
let msg = format!("The attribute `{}` is currently unknown to the \
|
|
|
|
compiler and may have meaning added to it in the \
|
|
|
|
future", path);
|
|
|
|
feature_err(&self.session.parse_sess, "custom_attribute", path.span,
|
|
|
|
GateIssue::Language, &msg).emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Not only attributes, but anything in macro namespace can result in
|
|
|
|
// `Def::NonMacroAttr` definition (e.g. `inline!()`), so we must report
|
|
|
|
// an error for those cases.
|
|
|
|
let msg = format!("expected a macro, found {}", def.kind_name());
|
|
|
|
self.session.span_err(path.span, &msg);
|
|
|
|
return Err(Determinacy::Determined);
|
|
|
|
}
|
rustc: Tweak custom attribute capabilities
This commit starts to lay some groundwork for the stabilization of custom
attribute invocations and general procedural macros. It applies a number of
changes discussed on [internals] as well as a [recent issue][issue], namely:
* The path used to specify a custom attribute must be of length one and cannot
be a global path. This'll help future-proof us against any ambiguities and
give us more time to settle the precise syntax. In the meantime though a bare
identifier can be used and imported to invoke a custom attribute macro. A new
feature gate, `proc_macro_path_invoc`, was added to gate multi-segment paths
and absolute paths.
* The set of items which can be annotated by a custom procedural attribute has
been restricted. Statements, expressions, and modules are disallowed behind
two new feature gates: `proc_macro_expr` and `proc_macro_mod`.
* The input to procedural macro attributes has been restricted and adjusted.
Today an invocation like `#[foo(bar)]` will receive `(bar)` as the input token
stream, but after this PR it will only receive `bar` (the delimiters were
removed). Invocations like `#[foo]` are still allowed and will be invoked in
the same way as `#[foo()]`. This is a **breaking change** for all nightly
users as the syntax coming in to procedural macros will be tweaked slightly.
* Procedural macros (`foo!()` style) can only be expanded to item-like items by
default. A separate feature gate, `proc_macro_non_items`, is required to
expand to items like expressions, statements, etc.
Closes #50038
[internals]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
[issue]: https://github.com/rust-lang/rust/issues/50038
2018-04-20 07:50:39 -07:00
|
|
|
}
|
2018-10-30 00:21:39 +03:00
|
|
|
Def::Err => {
|
|
|
|
return Err(Determinacy::Determined);
|
|
|
|
}
|
2018-08-15 03:51:12 +03:00
|
|
|
_ => panic!("expected `Def::Macro` or `Def::NonMacroAttr`"),
|
rustc: Tweak custom attribute capabilities
This commit starts to lay some groundwork for the stabilization of custom
attribute invocations and general procedural macros. It applies a number of
changes discussed on [internals] as well as a [recent issue][issue], namely:
* The path used to specify a custom attribute must be of length one and cannot
be a global path. This'll help future-proof us against any ambiguities and
give us more time to settle the precise syntax. In the meantime though a bare
identifier can be used and imported to invoke a custom attribute macro. A new
feature gate, `proc_macro_path_invoc`, was added to gate multi-segment paths
and absolute paths.
* The set of items which can be annotated by a custom procedural attribute has
been restricted. Statements, expressions, and modules are disallowed behind
two new feature gates: `proc_macro_expr` and `proc_macro_mod`.
* The input to procedural macro attributes has been restricted and adjusted.
Today an invocation like `#[foo(bar)]` will receive `(bar)` as the input token
stream, but after this PR it will only receive `bar` (the delimiters were
removed). Invocations like `#[foo]` are still allowed and will be invoked in
the same way as `#[foo()]`. This is a **breaking change** for all nightly
users as the syntax coming in to procedural macros will be tweaked slightly.
* Procedural macros (`foo!()` style) can only be expanded to item-like items by
default. A separate feature gate, `proc_macro_non_items`, is required to
expand to items like expressions, statements, etc.
Closes #50038
[internals]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
[issue]: https://github.com/rust-lang/rust/issues/50038
2018-04-20 07:50:39 -07:00
|
|
|
}
|
2018-08-15 03:51:12 +03:00
|
|
|
|
|
|
|
Ok((def, self.get_macro(def)))
|
2017-07-25 00:33:15 +03:00
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
|
2018-09-13 01:41:07 +03:00
|
|
|
pub fn resolve_macro_to_def_inner(
|
|
|
|
&mut self,
|
|
|
|
path: &ast::Path,
|
|
|
|
kind: MacroKind,
|
|
|
|
parent_scope: &ParentScope<'a>,
|
2018-11-14 02:20:59 +03:00
|
|
|
trace: bool,
|
2018-09-13 01:41:07 +03:00
|
|
|
force: bool,
|
|
|
|
) -> Result<Def, Determinacy> {
|
2018-10-27 20:23:54 +03:00
|
|
|
let path_span = path.span;
|
2018-09-12 15:21:50 +12:00
|
|
|
let mut path = Segment::from_path(path);
|
2016-11-27 10:58:46 +00:00
|
|
|
|
2018-06-11 14:21:36 +03:00
|
|
|
// Possibly apply the macro helper hack
|
2018-05-14 03:22:52 +03:00
|
|
|
if kind == MacroKind::Bang && path.len() == 1 &&
|
2018-09-12 15:21:50 +12:00
|
|
|
path[0].ident.span.ctxt().outer().expn_info()
|
|
|
|
.map_or(false, |info| info.local_inner_macros) {
|
|
|
|
let root = Ident::new(keywords::DollarCrate.name(), path[0].ident.span);
|
|
|
|
path.insert(0, Segment::from_ident(root));
|
2018-06-11 14:21:36 +03:00
|
|
|
}
|
|
|
|
|
2016-12-05 03:51:11 +00:00
|
|
|
if path.len() > 1 {
|
2018-11-03 22:02:36 +03:00
|
|
|
let def = match self.resolve_path(&path, Some(MacroNS), parent_scope,
|
2018-10-27 20:23:54 +03:00
|
|
|
false, path_span, CrateLint::No) {
|
2018-11-10 18:58:37 +03:00
|
|
|
PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => {
|
|
|
|
Ok(path_res.base_def())
|
|
|
|
}
|
2016-11-27 10:58:46 +00:00
|
|
|
PathResult::Indeterminate if !force => return Err(Determinacy::Undetermined),
|
2018-11-10 18:58:37 +03:00
|
|
|
PathResult::NonModule(..) | PathResult::Indeterminate | PathResult::Failed(..) => {
|
2017-02-06 22:14:38 +10:30
|
|
|
self.found_unresolved_macro = true;
|
|
|
|
Err(Determinacy::Determined)
|
2018-11-10 18:58:37 +03:00
|
|
|
}
|
|
|
|
PathResult::Module(..) => unreachable!(),
|
2016-11-27 10:58:46 +00:00
|
|
|
};
|
2018-09-18 03:19:26 +03:00
|
|
|
|
2018-11-14 02:20:59 +03:00
|
|
|
if trace {
|
|
|
|
parent_scope.module.multi_segment_macro_resolutions.borrow_mut()
|
|
|
|
.push((path, path_span, kind, parent_scope.clone(), def.ok()));
|
|
|
|
}
|
2016-11-27 10:58:46 +00:00
|
|
|
|
2018-09-18 03:19:26 +03:00
|
|
|
def
|
2017-03-11 10:58:19 +00:00
|
|
|
} else {
|
2018-09-19 01:01:09 +03:00
|
|
|
let binding = self.early_resolve_ident_in_lexical_scope(
|
2018-11-24 19:14:05 +03:00
|
|
|
path[0].ident, ScopeSet::Macro(kind), parent_scope, false, force, path_span
|
2018-09-19 01:01:09 +03:00
|
|
|
);
|
|
|
|
match binding {
|
|
|
|
Ok(..) => {}
|
|
|
|
Err(Determinacy::Determined) => self.found_unresolved_macro = true,
|
2018-08-04 00:25:45 +03:00
|
|
|
Err(Determinacy::Undetermined) => return Err(Determinacy::Undetermined),
|
2018-09-19 01:01:09 +03:00
|
|
|
}
|
2016-11-10 10:29:36 +00:00
|
|
|
|
2018-11-14 02:20:59 +03:00
|
|
|
if trace {
|
|
|
|
parent_scope.module.single_segment_macro_resolutions.borrow_mut()
|
|
|
|
.push((path[0].ident, kind, parent_scope.clone(), binding.ok()));
|
|
|
|
}
|
2017-02-01 21:03:09 +10:30
|
|
|
|
2018-09-19 01:01:09 +03:00
|
|
|
binding.map(|binding| binding.def_ignoring_ambiguity())
|
2018-09-18 03:19:26 +03:00
|
|
|
}
|
2017-02-01 21:03:09 +10:30
|
|
|
}
|
2016-09-26 03:17:05 +00:00
|
|
|
|
2018-09-18 03:19:26 +03:00
|
|
|
// Resolve an identifier in lexical scope.
|
2018-07-23 02:52:51 +03:00
|
|
|
// This is a variation of `fn resolve_ident_in_lexical_scope` that can be run during
|
|
|
|
// expansion and import resolution (perhaps they can be merged in the future).
|
2018-09-18 03:19:26 +03:00
|
|
|
// The function is used for resolving initial segments of macro paths (e.g. `foo` in
|
2018-11-03 22:02:36 +03:00
|
|
|
// `foo::bar!(); or `foo!();`) and also for import paths on 2018 edition.
|
2018-09-18 03:19:26 +03:00
|
|
|
crate fn early_resolve_ident_in_lexical_scope(
|
2018-08-18 02:38:51 +03:00
|
|
|
&mut self,
|
2018-11-24 15:07:03 +03:00
|
|
|
orig_ident: Ident,
|
2018-11-24 19:14:05 +03:00
|
|
|
scope_set: ScopeSet,
|
2018-09-13 01:41:07 +03:00
|
|
|
parent_scope: &ParentScope<'a>,
|
2018-08-18 02:38:51 +03:00
|
|
|
record_used: bool,
|
|
|
|
force: bool,
|
2018-08-28 04:07:31 +03:00
|
|
|
path_span: Span,
|
2018-09-18 03:19:26 +03:00
|
|
|
) -> Result<&'a NameBinding<'a>, Determinacy> {
|
2018-07-23 02:52:51 +03:00
|
|
|
// 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.
|
2018-09-17 01:56:15 +03:00
|
|
|
// 2. "Closed set" below means new names cannot appear after the current resolution attempt.
|
2018-07-23 02:52:51 +03:00
|
|
|
// 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 (closed, 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).
|
2018-09-17 01:56:15 +03:00
|
|
|
// (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).
|
2018-07-23 02:52:51 +03:00
|
|
|
// (Macro NS)
|
2018-09-26 13:11:34 +03:00
|
|
|
// 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.
|
|
|
|
// 1-3. `macro_rules` (open, not controlled), loop through legacy scopes. Have higher
|
2018-09-18 03:19:26 +03:00
|
|
|
// priority than prelude macros, but create ambiguities with macros in modules.
|
2018-09-26 13:11:34 +03:00
|
|
|
// 1-3. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
|
2018-09-18 03:19:26 +03:00
|
|
|
// (open, not controlled). Have higher priority than prelude macros, but create
|
|
|
|
// ambiguities with `macro_rules`.
|
2018-09-26 13:11:34 +03:00
|
|
|
// 4. `macro_use` prelude (open, the open part is from macro expansions, not controlled).
|
|
|
|
// 4a. User-defined prelude from macro-use
|
2018-07-23 02:52:51 +03:00
|
|
|
// (open, the open part is from macro expansions, not controlled).
|
2018-09-26 13:11:34 +03:00
|
|
|
// 4b. Standard library prelude is currently implemented as `macro-use` (closed, controlled)
|
2018-09-18 03:19:26 +03:00
|
|
|
// 5. Language prelude: builtin macros (closed, controlled, except for legacy plugins).
|
|
|
|
// 6. Language prelude: builtin attributes (closed, controlled).
|
2018-09-26 13:11:34 +03:00
|
|
|
// 4-6. Legacy plugin helpers (open, not controlled). Similar to derive helpers,
|
2018-09-18 03:19:26 +03:00
|
|
|
// but introduced by legacy plugins using `register_attribute`. Priority is somewhere
|
|
|
|
// in prelude, not sure where exactly (creates ambiguities with any other prelude names).
|
|
|
|
|
|
|
|
enum WhereToResolve<'a> {
|
2018-09-26 13:11:34 +03:00
|
|
|
DeriveHelpers,
|
2018-09-18 03:19:26 +03:00
|
|
|
MacroRules(LegacyScope<'a>),
|
2018-11-24 15:07:03 +03:00
|
|
|
CrateRoot,
|
2018-09-18 03:19:26 +03:00
|
|
|
Module(Module<'a>),
|
|
|
|
MacroUsePrelude,
|
|
|
|
BuiltinMacros,
|
|
|
|
BuiltinAttrs,
|
|
|
|
LegacyPluginHelpers,
|
|
|
|
ExternPrelude,
|
|
|
|
ToolPrelude,
|
|
|
|
StdLibPrelude,
|
|
|
|
BuiltinTypes,
|
|
|
|
}
|
|
|
|
|
|
|
|
bitflags! {
|
|
|
|
struct Flags: u8 {
|
2018-11-25 16:08:43 +03:00
|
|
|
const MACRO_RULES = 1 << 0;
|
|
|
|
const MODULE = 1 << 1;
|
|
|
|
const PRELUDE = 1 << 2;
|
|
|
|
const MISC_SUGGEST_CRATE = 1 << 3;
|
|
|
|
const MISC_SUGGEST_SELF = 1 << 4;
|
|
|
|
const MISC_FROM_PRELUDE = 1 << 5;
|
2018-09-18 03:19:26 +03:00
|
|
|
}
|
|
|
|
}
|
2018-07-23 02:52:51 +03:00
|
|
|
|
2018-08-04 05:17:51 +03:00
|
|
|
assert!(force || !record_used); // `record_used` implies `force`
|
2018-11-24 15:07:03 +03:00
|
|
|
let mut ident = orig_ident.modern();
|
2018-07-23 02:52:51 +03:00
|
|
|
|
2018-11-09 01:29:07 +03:00
|
|
|
// Make sure `self`, `super` etc produce an error when passed to here.
|
|
|
|
if ident.is_path_segment_keyword() {
|
|
|
|
return Err(Determinacy::Determined);
|
|
|
|
}
|
|
|
|
|
2018-08-28 03:27:41 +03:00
|
|
|
// This is *the* result, resolution from the scope closest to the resolved identifier.
|
|
|
|
// However, sometimes this result is "weak" because it comes from a glob import or
|
|
|
|
// a macro expansion, and in this case it cannot shadow names from outer scopes, e.g.
|
|
|
|
// mod m { ... } // solution in outer scope
|
2018-07-23 02:52:51 +03:00
|
|
|
// {
|
2018-08-28 03:27:41 +03:00
|
|
|
// use prefix::*; // imports another `m` - innermost solution
|
|
|
|
// // weak, cannot shadow the outer `m`, need to report ambiguity error
|
2018-07-23 02:52:51 +03:00
|
|
|
// m::mac!();
|
|
|
|
// }
|
2018-08-28 03:27:41 +03:00
|
|
|
// So we have to save the innermost solution and continue searching in outer scopes
|
|
|
|
// to detect potential ambiguities.
|
2018-11-05 01:11:59 +03:00
|
|
|
let mut innermost_result: Option<(&NameBinding, Flags)> = None;
|
2018-07-23 02:52:51 +03:00
|
|
|
|
|
|
|
// Go through all the scopes and try to resolve the name.
|
2018-11-24 19:14:05 +03:00
|
|
|
let rust_2015 = orig_ident.span.rust_2015();
|
2018-11-25 00:25:03 +03:00
|
|
|
let (ns, macro_kind, is_import, is_absolute_path) = match scope_set {
|
|
|
|
ScopeSet::Import(ns) => (ns, None, true, false),
|
|
|
|
ScopeSet::AbsolutePath(ns) => (ns, None, false, true),
|
|
|
|
ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind), false, false),
|
|
|
|
ScopeSet::Module => (TypeNS, None, false, false),
|
2018-11-24 19:14:05 +03:00
|
|
|
};
|
2018-11-24 15:07:03 +03:00
|
|
|
let mut where_to_resolve = match ns {
|
2018-11-25 00:25:03 +03:00
|
|
|
_ if is_absolute_path || is_import && rust_2015 => WhereToResolve::CrateRoot,
|
2018-11-24 15:07:03 +03:00
|
|
|
TypeNS | ValueNS => WhereToResolve::Module(parent_scope.module),
|
|
|
|
MacroNS => WhereToResolve::DeriveHelpers,
|
2018-11-08 00:39:07 +03:00
|
|
|
};
|
2018-09-13 01:41:07 +03:00
|
|
|
let mut use_prelude = !parent_scope.module.no_implicit_prelude;
|
2018-11-08 00:39:07 +03:00
|
|
|
let mut determinacy = Determinacy::Determined;
|
2016-11-10 10:29:36 +00:00
|
|
|
loop {
|
2018-07-23 02:52:51 +03:00
|
|
|
let result = match where_to_resolve {
|
2018-09-26 13:11:34 +03:00
|
|
|
WhereToResolve::DeriveHelpers => {
|
|
|
|
let mut result = Err(Determinacy::Determined);
|
|
|
|
for derive in &parent_scope.derives {
|
|
|
|
let parent_scope = ParentScope { derives: Vec::new(), ..*parent_scope };
|
2018-11-08 00:39:07 +03:00
|
|
|
match self.resolve_macro_to_def(derive, MacroKind::Derive,
|
2018-11-14 02:20:59 +03:00
|
|
|
&parent_scope, true, force) {
|
2018-11-08 00:39:07 +03:00
|
|
|
Ok((_, ext)) => {
|
|
|
|
if let SyntaxExtension::ProcMacroDerive(_, helpers, _) = &*ext {
|
|
|
|
if helpers.contains(&ident.name) {
|
|
|
|
let binding =
|
|
|
|
(Def::NonMacroAttr(NonMacroAttrKind::DeriveHelper),
|
|
|
|
ty::Visibility::Public, derive.span, Mark::root())
|
|
|
|
.to_name_binding(self.arenas);
|
|
|
|
result = Ok((binding, Flags::empty()));
|
|
|
|
break;
|
|
|
|
}
|
2018-09-26 13:11:34 +03:00
|
|
|
}
|
|
|
|
}
|
2018-11-08 00:39:07 +03:00
|
|
|
Err(Determinacy::Determined) => {}
|
|
|
|
Err(Determinacy::Undetermined) =>
|
|
|
|
result = Err(Determinacy::Undetermined),
|
2018-09-26 13:11:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
2018-09-18 03:19:26 +03:00
|
|
|
WhereToResolve::MacroRules(legacy_scope) => match legacy_scope {
|
|
|
|
LegacyScope::Binding(legacy_binding) if ident == legacy_binding.ident =>
|
2018-11-05 01:11:59 +03:00
|
|
|
Ok((legacy_binding.binding, Flags::MACRO_RULES)),
|
2018-11-08 00:39:07 +03:00
|
|
|
LegacyScope::Invocation(invoc) if invoc.output_legacy_scope.get().is_none() =>
|
|
|
|
Err(Determinacy::Undetermined),
|
2018-09-18 03:19:26 +03:00
|
|
|
_ => Err(Determinacy::Determined),
|
|
|
|
}
|
2018-11-24 15:07:03 +03:00
|
|
|
WhereToResolve::CrateRoot => {
|
|
|
|
let root_ident = Ident::new(keywords::CrateRoot.name(), orig_ident.span);
|
|
|
|
let root_module = self.resolve_crate_root(root_ident);
|
|
|
|
let binding = self.resolve_ident_in_module_ext(
|
|
|
|
ModuleOrUniformRoot::Module(root_module),
|
|
|
|
orig_ident,
|
|
|
|
ns,
|
|
|
|
None,
|
|
|
|
record_used,
|
|
|
|
path_span,
|
|
|
|
);
|
|
|
|
match binding {
|
2018-11-25 16:08:43 +03:00
|
|
|
Ok(binding) => Ok((binding, Flags::MODULE | Flags::MISC_SUGGEST_CRATE)),
|
2018-11-24 15:07:03 +03:00
|
|
|
Err((Determinacy::Undetermined, Weak::No)) =>
|
|
|
|
return Err(Determinacy::determined(force)),
|
|
|
|
Err((Determinacy::Undetermined, Weak::Yes)) =>
|
|
|
|
Err(Determinacy::Undetermined),
|
|
|
|
Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
|
|
|
|
}
|
|
|
|
}
|
2018-07-23 02:52:51 +03:00
|
|
|
WhereToResolve::Module(module) => {
|
|
|
|
let orig_current_module = mem::replace(&mut self.current_module, module);
|
2018-11-08 00:39:07 +03:00
|
|
|
let binding = self.resolve_ident_in_module_unadjusted_ext(
|
2018-08-09 16:29:22 +03:00
|
|
|
ModuleOrUniformRoot::Module(module),
|
|
|
|
ident,
|
|
|
|
ns,
|
2018-11-09 01:29:07 +03:00
|
|
|
None,
|
2018-08-09 16:29:22 +03:00
|
|
|
true,
|
|
|
|
record_used,
|
|
|
|
path_span,
|
2018-07-23 02:52:51 +03:00
|
|
|
);
|
|
|
|
self.current_module = orig_current_module;
|
2018-11-08 00:39:07 +03:00
|
|
|
match binding {
|
|
|
|
Ok(binding) => {
|
2018-11-25 16:08:43 +03:00
|
|
|
let misc_flags = if ptr::eq(module, self.graph_root) {
|
|
|
|
Flags::MISC_SUGGEST_CRATE
|
|
|
|
} else if module.is_normal() {
|
2018-11-08 00:39:07 +03:00
|
|
|
Flags::MISC_SUGGEST_SELF
|
|
|
|
} else {
|
|
|
|
Flags::empty()
|
|
|
|
};
|
|
|
|
Ok((binding, Flags::MODULE | misc_flags))
|
|
|
|
}
|
2018-11-17 20:13:25 +03:00
|
|
|
Err((Determinacy::Undetermined, Weak::No)) =>
|
2018-11-08 00:39:07 +03:00
|
|
|
return Err(Determinacy::determined(force)),
|
2018-11-17 20:13:25 +03:00
|
|
|
Err((Determinacy::Undetermined, Weak::Yes)) =>
|
|
|
|
Err(Determinacy::Undetermined),
|
|
|
|
Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
|
2018-11-08 00:39:07 +03:00
|
|
|
}
|
2018-07-23 02:52:51 +03:00
|
|
|
}
|
2018-09-04 01:14:58 +03:00
|
|
|
WhereToResolve::MacroUsePrelude => {
|
2018-11-18 03:25:59 +03:00
|
|
|
if use_prelude || rust_2015 {
|
2018-11-08 00:39:07 +03:00
|
|
|
match self.macro_use_prelude.get(&ident.name).cloned() {
|
|
|
|
Some(binding) =>
|
|
|
|
Ok((binding, Flags::PRELUDE | Flags::MISC_FROM_PRELUDE)),
|
|
|
|
None => Err(Determinacy::determined(
|
|
|
|
self.graph_root.unresolved_invocations.borrow().is_empty()
|
|
|
|
))
|
2018-11-03 00:07:56 +03:00
|
|
|
}
|
2018-11-08 00:39:07 +03:00
|
|
|
} else {
|
|
|
|
Err(Determinacy::Determined)
|
2018-09-04 01:14:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
WhereToResolve::BuiltinMacros => {
|
|
|
|
match self.builtin_macros.get(&ident.name).cloned() {
|
2018-11-05 01:11:59 +03:00
|
|
|
Some(binding) => Ok((binding, Flags::PRELUDE)),
|
2018-07-23 02:52:51 +03:00
|
|
|
None => Err(Determinacy::Determined),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WhereToResolve::BuiltinAttrs => {
|
2018-09-08 22:19:53 +03:00
|
|
|
if is_builtin_attr_name(ident.name) {
|
2018-08-03 02:05:00 +03:00
|
|
|
let binding = (Def::NonMacroAttr(NonMacroAttrKind::Builtin),
|
2018-11-05 01:11:59 +03:00
|
|
|
ty::Visibility::Public, DUMMY_SP, Mark::root())
|
2018-08-03 02:05:00 +03:00
|
|
|
.to_name_binding(self.arenas);
|
2018-11-05 01:11:59 +03:00
|
|
|
Ok((binding, Flags::PRELUDE))
|
2018-07-23 02:52:51 +03:00
|
|
|
} else {
|
|
|
|
Err(Determinacy::Determined)
|
|
|
|
}
|
|
|
|
}
|
2018-09-15 23:46:54 +03:00
|
|
|
WhereToResolve::LegacyPluginHelpers => {
|
2018-11-18 03:25:59 +03:00
|
|
|
if (use_prelude || rust_2015) &&
|
2018-11-03 00:07:56 +03:00
|
|
|
self.session.plugin_attributes.borrow().iter()
|
2018-09-15 23:46:54 +03:00
|
|
|
.any(|(name, _)| ident.name == &**name) {
|
|
|
|
let binding = (Def::NonMacroAttr(NonMacroAttrKind::LegacyPluginHelper),
|
2018-11-05 01:11:59 +03:00
|
|
|
ty::Visibility::Public, DUMMY_SP, Mark::root())
|
2018-09-15 23:46:54 +03:00
|
|
|
.to_name_binding(self.arenas);
|
2018-11-05 01:11:59 +03:00
|
|
|
Ok((binding, Flags::PRELUDE))
|
2018-09-15 23:46:54 +03:00
|
|
|
} else {
|
|
|
|
Err(Determinacy::Determined)
|
|
|
|
}
|
|
|
|
}
|
2018-07-23 02:52:51 +03:00
|
|
|
WhereToResolve::ExternPrelude => {
|
2018-11-25 00:25:03 +03:00
|
|
|
if use_prelude || is_absolute_path {
|
2018-11-17 21:08:00 +03:00
|
|
|
match self.extern_prelude_get(ident, !record_used) {
|
2018-11-08 00:39:07 +03:00
|
|
|
Some(binding) => Ok((binding, Flags::PRELUDE)),
|
|
|
|
None => Err(Determinacy::determined(
|
|
|
|
self.graph_root.unresolved_invocations.borrow().is_empty()
|
|
|
|
)),
|
2018-09-29 01:31:54 +03:00
|
|
|
}
|
2018-11-08 00:39:07 +03:00
|
|
|
} else {
|
|
|
|
Err(Determinacy::Determined)
|
2018-07-23 02:52:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
WhereToResolve::ToolPrelude => {
|
|
|
|
if use_prelude && is_known_tool(ident.name) {
|
|
|
|
let binding = (Def::ToolMod, ty::Visibility::Public,
|
2018-11-05 01:11:59 +03:00
|
|
|
DUMMY_SP, Mark::root()).to_name_binding(self.arenas);
|
|
|
|
Ok((binding, Flags::PRELUDE))
|
2018-07-23 02:52:51 +03:00
|
|
|
} else {
|
|
|
|
Err(Determinacy::Determined)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WhereToResolve::StdLibPrelude => {
|
|
|
|
let mut result = Err(Determinacy::Determined);
|
|
|
|
if use_prelude {
|
|
|
|
if let Some(prelude) = self.prelude {
|
2018-08-09 16:29:22 +03:00
|
|
|
if let Ok(binding) = self.resolve_ident_in_module_unadjusted(
|
|
|
|
ModuleOrUniformRoot::Module(prelude),
|
|
|
|
ident,
|
|
|
|
ns,
|
|
|
|
false,
|
|
|
|
path_span,
|
|
|
|
) {
|
2018-11-05 01:11:59 +03:00
|
|
|
result = Ok((binding, Flags::PRELUDE | Flags::MISC_FROM_PRELUDE));
|
2018-07-23 02:52:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
2018-09-04 01:14:58 +03:00
|
|
|
WhereToResolve::BuiltinTypes => {
|
2018-09-18 03:19:26 +03:00
|
|
|
match self.primitive_type_table.primitive_types.get(&ident.name).cloned() {
|
|
|
|
Some(prim_ty) => {
|
|
|
|
let binding = (Def::PrimTy(prim_ty), ty::Visibility::Public,
|
2018-11-05 01:11:59 +03:00
|
|
|
DUMMY_SP, Mark::root()).to_name_binding(self.arenas);
|
|
|
|
Ok((binding, Flags::PRELUDE))
|
2018-09-18 03:19:26 +03:00
|
|
|
}
|
|
|
|
None => Err(Determinacy::Determined)
|
2018-07-23 02:52:51 +03:00
|
|
|
}
|
|
|
|
}
|
2017-03-11 10:58:19 +00:00
|
|
|
};
|
|
|
|
|
2018-07-23 02:52:51 +03:00
|
|
|
match result {
|
2018-11-08 00:39:07 +03:00
|
|
|
Ok((binding, flags)) if sub_namespace_match(binding.macro_kind(), macro_kind) => {
|
2017-05-12 11:21:11 +02:00
|
|
|
if !record_used {
|
2018-09-18 03:19:26 +03:00
|
|
|
return Ok(binding);
|
2017-05-12 11:21:11 +02:00
|
|
|
}
|
2018-07-23 02:52:51 +03:00
|
|
|
|
2018-11-05 01:11:59 +03:00
|
|
|
if let Some((innermost_binding, innermost_flags)) = innermost_result {
|
2018-08-28 03:27:41 +03:00
|
|
|
// Found another solution, if the first one was "weak", report an error.
|
2018-11-05 01:11:59 +03:00
|
|
|
let (def, innermost_def) = (binding.def(), innermost_binding.def());
|
|
|
|
if def != innermost_def {
|
|
|
|
let builtin = Def::NonMacroAttr(NonMacroAttrKind::Builtin);
|
|
|
|
let derive_helper = Def::NonMacroAttr(NonMacroAttrKind::DeriveHelper);
|
|
|
|
let legacy_helper =
|
|
|
|
Def::NonMacroAttr(NonMacroAttrKind::LegacyPluginHelper);
|
|
|
|
|
|
|
|
let ambiguity_error_kind = if is_import {
|
|
|
|
Some(AmbiguityKind::Import)
|
2018-11-25 00:25:03 +03:00
|
|
|
} else if is_absolute_path {
|
|
|
|
Some(AmbiguityKind::AbsolutePath)
|
2018-11-05 01:11:59 +03:00
|
|
|
} else if innermost_def == builtin || def == builtin {
|
|
|
|
Some(AmbiguityKind::BuiltinAttr)
|
|
|
|
} else if innermost_def == derive_helper || def == derive_helper {
|
|
|
|
Some(AmbiguityKind::DeriveHelper)
|
|
|
|
} else if innermost_def == legacy_helper &&
|
|
|
|
flags.contains(Flags::PRELUDE) ||
|
|
|
|
def == legacy_helper &&
|
|
|
|
innermost_flags.contains(Flags::PRELUDE) {
|
|
|
|
Some(AmbiguityKind::LegacyHelperVsPrelude)
|
|
|
|
} else if innermost_flags.contains(Flags::MACRO_RULES) &&
|
|
|
|
flags.contains(Flags::MODULE) &&
|
|
|
|
!self.disambiguate_legacy_vs_modern(innermost_binding,
|
2018-11-24 15:07:03 +03:00
|
|
|
binding) ||
|
|
|
|
flags.contains(Flags::MACRO_RULES) &&
|
|
|
|
innermost_flags.contains(Flags::MODULE) &&
|
|
|
|
!self.disambiguate_legacy_vs_modern(binding,
|
|
|
|
innermost_binding) {
|
2018-11-05 01:11:59 +03:00
|
|
|
Some(AmbiguityKind::LegacyVsModern)
|
|
|
|
} else if innermost_binding.is_glob_import() {
|
|
|
|
Some(AmbiguityKind::GlobVsOuter)
|
|
|
|
} else if innermost_binding.may_appear_after(parent_scope.expansion,
|
|
|
|
binding) {
|
|
|
|
Some(AmbiguityKind::MoreExpandedVsOuter)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
if let Some(kind) = ambiguity_error_kind {
|
2018-11-25 16:08:43 +03:00
|
|
|
let misc = |f: Flags| if f.contains(Flags::MISC_SUGGEST_CRATE) {
|
|
|
|
AmbiguityErrorMisc::SuggestCrate
|
|
|
|
} else if f.contains(Flags::MISC_SUGGEST_SELF) {
|
2018-11-05 01:11:59 +03:00
|
|
|
AmbiguityErrorMisc::SuggestSelf
|
|
|
|
} else if f.contains(Flags::MISC_FROM_PRELUDE) {
|
|
|
|
AmbiguityErrorMisc::FromPrelude
|
|
|
|
} else {
|
|
|
|
AmbiguityErrorMisc::None
|
|
|
|
};
|
|
|
|
self.ambiguity_errors.push(AmbiguityError {
|
|
|
|
kind,
|
2018-11-25 16:08:43 +03:00
|
|
|
ident: orig_ident,
|
2018-11-05 01:11:59 +03:00
|
|
|
b1: innermost_binding,
|
|
|
|
b2: binding,
|
|
|
|
misc1: misc(innermost_flags),
|
|
|
|
misc2: misc(flags),
|
|
|
|
});
|
|
|
|
return Ok(innermost_binding);
|
|
|
|
}
|
2016-11-27 10:58:46 +00:00
|
|
|
}
|
2018-08-28 03:27:41 +03:00
|
|
|
} else {
|
|
|
|
// Found the first solution.
|
2018-11-05 01:11:59 +03:00
|
|
|
innermost_result = Some((binding, flags));
|
2017-03-11 10:58:19 +00:00
|
|
|
}
|
2018-07-23 02:52:51 +03:00
|
|
|
}
|
2018-11-08 00:39:07 +03:00
|
|
|
Ok(..) | Err(Determinacy::Determined) => {}
|
|
|
|
Err(Determinacy::Undetermined) => determinacy = Determinacy::Undetermined
|
2016-11-10 10:29:36 +00:00
|
|
|
}
|
2018-11-08 00:39:07 +03:00
|
|
|
|
|
|
|
where_to_resolve = match where_to_resolve {
|
|
|
|
WhereToResolve::DeriveHelpers =>
|
|
|
|
WhereToResolve::MacroRules(parent_scope.legacy),
|
|
|
|
WhereToResolve::MacroRules(legacy_scope) => match legacy_scope {
|
|
|
|
LegacyScope::Binding(binding) => WhereToResolve::MacroRules(
|
|
|
|
binding.parent_legacy_scope
|
|
|
|
),
|
|
|
|
LegacyScope::Invocation(invoc) => WhereToResolve::MacroRules(
|
|
|
|
invoc.output_legacy_scope.get().unwrap_or(invoc.parent_legacy_scope.get())
|
|
|
|
),
|
|
|
|
LegacyScope::Empty => WhereToResolve::Module(parent_scope.module),
|
|
|
|
LegacyScope::Uninitialized => unreachable!(),
|
|
|
|
}
|
2018-11-25 00:25:03 +03:00
|
|
|
WhereToResolve::CrateRoot if is_import => match ns {
|
2018-11-24 15:07:03 +03:00
|
|
|
TypeNS | ValueNS => WhereToResolve::Module(parent_scope.module),
|
|
|
|
MacroNS => WhereToResolve::DeriveHelpers,
|
|
|
|
}
|
2018-11-25 00:25:03 +03:00
|
|
|
WhereToResolve::CrateRoot if is_absolute_path => match ns {
|
|
|
|
TypeNS => {
|
|
|
|
ident.span.adjust(Mark::root());
|
|
|
|
WhereToResolve::ExternPrelude
|
|
|
|
}
|
|
|
|
ValueNS | MacroNS => break,
|
|
|
|
}
|
|
|
|
WhereToResolve::CrateRoot => unreachable!(),
|
2018-11-08 00:39:07 +03:00
|
|
|
WhereToResolve::Module(module) => {
|
|
|
|
match self.hygienic_lexical_parent(module, &mut ident.span) {
|
|
|
|
Some(parent_module) => WhereToResolve::Module(parent_module),
|
|
|
|
None => {
|
|
|
|
use_prelude = !module.no_implicit_prelude;
|
|
|
|
match ns {
|
|
|
|
TypeNS => WhereToResolve::ExternPrelude,
|
|
|
|
ValueNS => WhereToResolve::StdLibPrelude,
|
|
|
|
MacroNS => WhereToResolve::MacroUsePrelude,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WhereToResolve::MacroUsePrelude => WhereToResolve::BuiltinMacros,
|
|
|
|
WhereToResolve::BuiltinMacros => WhereToResolve::BuiltinAttrs,
|
|
|
|
WhereToResolve::BuiltinAttrs => WhereToResolve::LegacyPluginHelpers,
|
|
|
|
WhereToResolve::LegacyPluginHelpers => break, // nowhere else to search
|
2018-11-25 00:25:03 +03:00
|
|
|
WhereToResolve::ExternPrelude if is_absolute_path => break,
|
2018-11-08 00:39:07 +03:00
|
|
|
WhereToResolve::ExternPrelude => WhereToResolve::ToolPrelude,
|
|
|
|
WhereToResolve::ToolPrelude => WhereToResolve::StdLibPrelude,
|
|
|
|
WhereToResolve::StdLibPrelude => match ns {
|
|
|
|
TypeNS => WhereToResolve::BuiltinTypes,
|
|
|
|
ValueNS => break, // nowhere else to search
|
|
|
|
MacroNS => unreachable!(),
|
|
|
|
}
|
|
|
|
WhereToResolve::BuiltinTypes => break, // nowhere else to search
|
|
|
|
};
|
|
|
|
|
|
|
|
continue;
|
2018-07-23 02:52:51 +03:00
|
|
|
}
|
2016-11-10 10:29:36 +00:00
|
|
|
|
2018-08-28 03:27:41 +03:00
|
|
|
// The first found solution was the only one, return it.
|
2018-11-12 03:58:39 +03:00
|
|
|
if let Some((binding, flags)) = innermost_result {
|
2018-11-24 15:07:03 +03:00
|
|
|
// We get to here only if there's no ambiguity, in ambiguous cases an error will
|
|
|
|
// be reported anyway, so there's no reason to report an additional feature error.
|
|
|
|
// The `binding` can actually be introduced by something other than `--extern`,
|
|
|
|
// but its `Def` should coincide with a crate passed with `--extern`
|
|
|
|
// (otherwise there would be ambiguity) and we can skip feature error in this case.
|
|
|
|
'ok: {
|
|
|
|
if !is_import || self.session.features_untracked().uniform_paths {
|
|
|
|
break 'ok;
|
|
|
|
}
|
|
|
|
if ns == TypeNS && use_prelude && self.extern_prelude_get(ident, true).is_some() {
|
|
|
|
break 'ok;
|
|
|
|
}
|
|
|
|
if rust_2015 {
|
|
|
|
let root_ident = Ident::new(keywords::CrateRoot.name(), orig_ident.span);
|
|
|
|
let root_module = self.resolve_crate_root(root_ident);
|
|
|
|
if self.resolve_ident_in_module_ext(ModuleOrUniformRoot::Module(root_module),
|
|
|
|
orig_ident, ns, None, false, path_span)
|
|
|
|
.is_ok() {
|
|
|
|
break 'ok;
|
2018-11-12 03:58:39 +03:00
|
|
|
}
|
|
|
|
}
|
2018-11-24 15:07:03 +03:00
|
|
|
|
|
|
|
let msg = "imports can only refer to extern crate names \
|
|
|
|
passed with `--extern` on stable channel";
|
|
|
|
let mut err = feature_err(&self.session.parse_sess, "uniform_paths",
|
|
|
|
ident.span, GateIssue::Language, msg);
|
|
|
|
|
|
|
|
let what = self.binding_description(binding, ident,
|
|
|
|
flags.contains(Flags::MISC_FROM_PRELUDE));
|
|
|
|
let note_msg = format!("this import refers to {what}", what = what);
|
|
|
|
if binding.span.is_dummy() {
|
|
|
|
err.note(¬e_msg);
|
|
|
|
} else {
|
|
|
|
err.span_note(binding.span, ¬e_msg);
|
|
|
|
err.span_label(binding.span, "not an extern crate passed with `--extern`");
|
|
|
|
}
|
|
|
|
err.emit();
|
2018-11-12 03:58:39 +03:00
|
|
|
}
|
|
|
|
|
2018-09-18 03:19:26 +03:00
|
|
|
return Ok(binding);
|
2016-11-10 10:29:36 +00:00
|
|
|
}
|
2018-07-23 02:52:51 +03:00
|
|
|
|
2018-11-08 00:39:07 +03:00
|
|
|
let determinacy = Determinacy::determined(determinacy == Determinacy::Determined || force);
|
2018-11-03 22:02:36 +03:00
|
|
|
if determinacy == Determinacy::Determined && macro_kind == Some(MacroKind::Attr) {
|
2018-08-04 05:17:51 +03:00
|
|
|
// For single-segment attributes interpret determinate "no resolution" as a custom
|
2018-09-11 00:28:35 +03:00
|
|
|
// attribute. (Lexical resolution implies the first segment and attr kind should imply
|
2018-08-04 05:17:51 +03:00
|
|
|
// the last segment, so we are certainly working with a single-segment attribute here.)
|
|
|
|
assert!(ns == MacroNS);
|
2018-08-04 00:25:45 +03:00
|
|
|
let binding = (Def::NonMacroAttr(NonMacroAttrKind::Custom),
|
|
|
|
ty::Visibility::Public, ident.span, Mark::root())
|
|
|
|
.to_name_binding(self.arenas);
|
2018-09-18 03:19:26 +03:00
|
|
|
Ok(binding)
|
2018-08-04 00:25:45 +03:00
|
|
|
} else {
|
|
|
|
Err(determinacy)
|
|
|
|
}
|
2016-11-10 10:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn finalize_current_module_macro_resolutions(&mut self) {
|
|
|
|
let module = self.current_module;
|
2018-10-27 20:23:54 +03:00
|
|
|
|
2018-11-18 14:41:06 +03:00
|
|
|
let check_consistency = |this: &mut Self, path: &[Segment], span,
|
2018-11-10 18:58:37 +03:00
|
|
|
kind: MacroKind, initial_def, def| {
|
|
|
|
if let Some(initial_def) = initial_def {
|
|
|
|
if def != initial_def && def != Def::Err && this.ambiguity_errors.is_empty() {
|
|
|
|
// Make sure compilation does not succeed if preferred macro resolution
|
|
|
|
// has changed after the macro had been expanded. In theory all such
|
|
|
|
// situations should be reported as ambiguity errors, so this is a bug.
|
|
|
|
span_bug!(span, "inconsistent resolution for a macro");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// It's possible that the macro was unresolved (indeterminate) and silently
|
|
|
|
// expanded into a dummy fragment for recovery during expansion.
|
|
|
|
// Now, post-expansion, the resolution may succeed, but we can't change the
|
|
|
|
// past and need to report an error.
|
|
|
|
// However, non-speculative `resolve_path` can successfully return private items
|
|
|
|
// even if speculative `resolve_path` returned nothing previously, so we skip this
|
|
|
|
// less informative error if the privacy error is reported elsewhere.
|
|
|
|
if this.privacy_errors.is_empty() {
|
|
|
|
let msg = format!("cannot determine resolution for the {} `{}`",
|
2018-11-18 14:41:06 +03:00
|
|
|
kind.descr(), Segment::names_to_string(path));
|
2018-11-10 18:58:37 +03:00
|
|
|
let msg_note = "import resolution is stuck, try simplifying macro imports";
|
|
|
|
this.session.struct_span_err(span, &msg).note(msg_note).emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-27 20:23:54 +03:00
|
|
|
let macro_resolutions =
|
2018-11-10 18:58:37 +03:00
|
|
|
mem::replace(&mut *module.multi_segment_macro_resolutions.borrow_mut(), Vec::new());
|
|
|
|
for (mut path, path_span, kind, parent_scope, initial_def) in macro_resolutions {
|
2018-10-27 20:23:54 +03:00
|
|
|
// FIXME: Path resolution will ICE if segment IDs present.
|
|
|
|
for seg in &mut path { seg.id = None; }
|
2018-11-03 22:02:36 +03:00
|
|
|
match self.resolve_path(&path, Some(MacroNS), &parent_scope,
|
2018-10-27 20:23:54 +03:00
|
|
|
true, path_span, CrateLint::No) {
|
2018-11-10 18:58:37 +03:00
|
|
|
PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => {
|
|
|
|
let def = path_res.base_def();
|
|
|
|
check_consistency(self, &path, path_span, kind, initial_def, def);
|
|
|
|
}
|
|
|
|
path_res @ PathResult::NonModule(..) | path_res @ PathResult::Failed(..) => {
|
|
|
|
let (span, msg) = if let PathResult::Failed(span, msg, ..) = path_res {
|
|
|
|
(span, msg)
|
|
|
|
} else {
|
|
|
|
(path_span, format!("partially resolved path in {} {}",
|
|
|
|
kind.article(), kind.descr()))
|
|
|
|
};
|
2016-11-27 10:58:46 +00:00
|
|
|
resolve_error(self, span, ResolutionError::FailedToResolve(&msg));
|
|
|
|
}
|
2018-11-10 18:58:37 +03:00
|
|
|
PathResult::Module(..) | PathResult::Indeterminate => unreachable!(),
|
2016-11-27 10:58:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-10 18:58:37 +03:00
|
|
|
let macro_resolutions =
|
|
|
|
mem::replace(&mut *module.single_segment_macro_resolutions.borrow_mut(), Vec::new());
|
|
|
|
for (ident, kind, parent_scope, initial_binding) in macro_resolutions {
|
2018-11-24 19:14:05 +03:00
|
|
|
match self.early_resolve_ident_in_lexical_scope(ident, ScopeSet::Macro(kind),
|
2018-11-10 18:58:37 +03:00
|
|
|
&parent_scope, true, true, ident.span) {
|
2018-09-18 03:19:26 +03:00
|
|
|
Ok(binding) => {
|
2018-11-10 18:58:37 +03:00
|
|
|
let initial_def = initial_binding.map(|initial_binding| {
|
2018-11-14 02:17:40 +03:00
|
|
|
self.record_use(ident, MacroNS, initial_binding, false);
|
2018-11-10 18:58:37 +03:00
|
|
|
initial_binding.def_ignoring_ambiguity()
|
|
|
|
});
|
|
|
|
let def = binding.def_ignoring_ambiguity();
|
2018-11-18 14:41:06 +03:00
|
|
|
let seg = Segment::from_ident(ident);
|
|
|
|
check_consistency(self, &[seg], ident.span, kind, initial_def, def);
|
2018-05-28 22:13:59 +03:00
|
|
|
}
|
2018-09-18 03:19:26 +03:00
|
|
|
Err(..) => {
|
2018-09-19 01:01:09 +03:00
|
|
|
assert!(initial_binding.is_none());
|
2018-05-28 22:13:59 +03:00
|
|
|
let bang = if kind == MacroKind::Bang { "!" } else { "" };
|
|
|
|
let msg =
|
|
|
|
format!("cannot find {} `{}{}` in this scope", kind.descr(), ident, bang);
|
2018-09-18 03:19:26 +03:00
|
|
|
let mut err = self.session.struct_span_err(ident.span, &msg);
|
|
|
|
self.suggest_macro_name(&ident.as_str(), kind, &mut err, ident.span);
|
2017-02-06 22:14:38 +10:30
|
|
|
err.emit();
|
2018-05-28 22:13:59 +03:00
|
|
|
}
|
2018-09-18 03:19:26 +03:00
|
|
|
}
|
2016-10-31 22:17:15 +00:00
|
|
|
}
|
2018-09-03 00:04:54 +03:00
|
|
|
|
2018-09-13 01:41:07 +03:00
|
|
|
let builtin_attrs = mem::replace(&mut *module.builtin_attrs.borrow_mut(), Vec::new());
|
|
|
|
for (ident, parent_scope) in builtin_attrs {
|
2018-11-05 01:00:31 +03:00
|
|
|
let _ = self.early_resolve_ident_in_lexical_scope(
|
2018-11-24 19:14:05 +03:00
|
|
|
ident, ScopeSet::Macro(MacroKind::Attr), &parent_scope, true, true, ident.span
|
2018-09-18 03:19:26 +03:00
|
|
|
);
|
2018-09-03 00:04:54 +03:00
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|
2016-09-21 06:25:09 +00:00
|
|
|
|
2017-02-06 22:14:38 +10:30
|
|
|
fn suggest_macro_name(&mut self, name: &str, kind: MacroKind,
|
2017-05-12 11:21:11 +02:00
|
|
|
err: &mut DiagnosticBuilder<'a>, span: Span) {
|
2017-02-23 20:18:20 +10:30
|
|
|
// First check if this is a locally-defined bang macro.
|
|
|
|
let suggestion = if let MacroKind::Bang = kind {
|
2017-03-22 08:39:51 +00:00
|
|
|
find_best_match_for_name(self.macro_names.iter().map(|ident| &ident.name), name, None)
|
2017-02-23 20:18:20 +10:30
|
|
|
} else {
|
|
|
|
None
|
2017-03-16 01:39:47 +00:00
|
|
|
// Then check global macros.
|
2017-02-23 20:18:20 +10:30
|
|
|
}.or_else(|| {
|
2018-09-04 01:14:58 +03:00
|
|
|
let names = self.builtin_macros.iter().chain(self.macro_use_prelude.iter())
|
|
|
|
.filter_map(|(name, binding)| {
|
|
|
|
if binding.macro_kind() == Some(kind) { Some(name) } else { None }
|
2017-02-23 20:18:20 +10:30
|
|
|
});
|
|
|
|
find_best_match_for_name(names, name, None)
|
|
|
|
// Then check modules.
|
|
|
|
}).or_else(|| {
|
|
|
|
let is_macro = |def| {
|
|
|
|
if let Def::Macro(_, def_kind) = def {
|
|
|
|
def_kind == kind
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
};
|
2018-03-18 16:47:09 +03:00
|
|
|
let ident = Ident::new(Symbol::intern(name), span);
|
2018-09-12 15:21:50 +12:00
|
|
|
self.lookup_typo_candidate(&[Segment::from_ident(ident)], MacroNS, is_macro, span)
|
2017-02-23 20:18:20 +10:30
|
|
|
});
|
|
|
|
|
2017-02-06 22:14:38 +10:30
|
|
|
if let Some(suggestion) = suggestion {
|
2016-09-07 23:21:59 +00:00
|
|
|
if suggestion != name {
|
2017-02-06 22:14:38 +10:30
|
|
|
if let MacroKind::Bang = kind {
|
2018-08-19 15:01:33 -04:00
|
|
|
err.span_suggestion_with_applicability(
|
|
|
|
span,
|
|
|
|
"you could try the macro",
|
|
|
|
suggestion.to_string(),
|
|
|
|
Applicability::MaybeIncorrect
|
|
|
|
);
|
2017-02-06 22:14:38 +10:30
|
|
|
} else {
|
2018-08-19 15:01:33 -04:00
|
|
|
err.span_suggestion_with_applicability(
|
|
|
|
span,
|
|
|
|
"try",
|
|
|
|
suggestion.to_string(),
|
|
|
|
Applicability::MaybeIncorrect
|
|
|
|
);
|
2017-02-06 22:14:38 +10:30
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
} else {
|
2017-05-04 14:17:23 +02:00
|
|
|
err.help("have you added the `#[macro_use]` on the module/import?");
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-21 06:25:09 +00:00
|
|
|
|
2017-03-24 23:03:15 +00:00
|
|
|
fn collect_def_ids(&mut self,
|
|
|
|
mark: Mark,
|
|
|
|
invocation: &'a InvocationData<'a>,
|
2018-06-20 02:08:08 +03:00
|
|
|
fragment: &AstFragment) {
|
2016-10-03 23:48:19 +00:00
|
|
|
let Resolver { ref mut invocations, arenas, graph_root, .. } = *self;
|
2018-05-17 21:28:50 +03:00
|
|
|
let InvocationData { def_index, .. } = *invocation;
|
2016-09-16 08:50:34 +00:00
|
|
|
|
2016-09-23 21:13:59 +00:00
|
|
|
let visit_macro_invoc = &mut |invoc: map::MacroInvocationData| {
|
2016-10-03 23:48:19 +00:00
|
|
|
invocations.entry(invoc.mark).or_insert_with(|| {
|
|
|
|
arenas.alloc_invocation_data(InvocationData {
|
2016-09-16 08:50:34 +00:00
|
|
|
def_index: invoc.def_index,
|
|
|
|
module: Cell::new(graph_root),
|
2018-08-31 22:53:08 +03:00
|
|
|
parent_legacy_scope: Cell::new(LegacyScope::Uninitialized),
|
2018-11-08 00:39:07 +03:00
|
|
|
output_legacy_scope: Cell::new(None),
|
2016-09-16 08:50:34 +00:00
|
|
|
})
|
2016-09-14 09:55:20 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-03-24 23:03:15 +00:00
|
|
|
let mut def_collector = DefCollector::new(&mut self.definitions, mark);
|
2016-09-14 09:55:20 +00:00
|
|
|
def_collector.visit_macro_invoc = Some(visit_macro_invoc);
|
2016-09-29 02:23:19 +00:00
|
|
|
def_collector.with_parent(def_index, |def_collector| {
|
2018-06-20 02:08:08 +03:00
|
|
|
fragment.visit_with(def_collector)
|
2016-09-23 21:13:59 +00:00
|
|
|
});
|
2016-09-14 09:55:20 +00:00
|
|
|
}
|
2016-12-01 11:20:04 +00:00
|
|
|
|
2017-03-18 01:55:51 +00:00
|
|
|
pub fn define_macro(&mut self,
|
|
|
|
item: &ast::Item,
|
|
|
|
expansion: Mark,
|
2018-08-29 04:48:02 +03:00
|
|
|
current_legacy_scope: &mut LegacyScope<'a>) {
|
2017-03-01 23:48:16 +00:00
|
|
|
self.local_macro_def_scopes.insert(item.id, self.current_module);
|
|
|
|
let ident = item.ident;
|
|
|
|
if ident.name == "macro_rules" {
|
2016-12-01 11:20:04 +00:00
|
|
|
self.session.span_err(item.span, "user-defined macros may not be named `macro_rules`");
|
|
|
|
}
|
|
|
|
|
2017-03-01 23:48:16 +00:00
|
|
|
let def_id = self.definitions.local_def_id(item.id);
|
2018-02-27 17:11:14 +01:00
|
|
|
let ext = Lrc::new(macro_rules::compile(&self.session.parse_sess,
|
2018-02-14 16:11:02 +01:00
|
|
|
&self.session.features_untracked(),
|
2018-05-13 03:51:46 +03:00
|
|
|
item, hygiene::default_edition()));
|
2017-03-01 23:48:16 +00:00
|
|
|
self.macro_map.insert(def_id, ext);
|
2016-12-01 11:20:04 +00:00
|
|
|
|
2017-03-18 01:55:51 +00:00
|
|
|
let def = match item.node { ast::ItemKind::MacroDef(ref def) => def, _ => unreachable!() };
|
|
|
|
if def.legacy {
|
2017-03-22 08:39:51 +00:00
|
|
|
let ident = ident.modern();
|
|
|
|
self.macro_names.insert(ident);
|
2018-01-06 17:23:33 +05:30
|
|
|
let def = Def::Macro(def_id, MacroKind::Bang);
|
2018-08-28 00:56:11 +03:00
|
|
|
let vis = ty::Visibility::Invisible; // Doesn't matter for legacy bindings
|
|
|
|
let binding = (def, vis, item.span, expansion).to_name_binding(self.arenas);
|
2018-09-27 04:49:40 +03:00
|
|
|
self.set_binding_parent_module(binding, self.current_module);
|
2018-08-29 04:48:02 +03:00
|
|
|
let legacy_binding = self.arenas.alloc_legacy_binding(LegacyBinding {
|
2018-08-31 22:53:08 +03:00
|
|
|
parent_legacy_scope: *current_legacy_scope, binding, ident
|
2018-08-29 04:48:02 +03:00
|
|
|
});
|
|
|
|
*current_legacy_scope = LegacyScope::Binding(legacy_binding);
|
2018-01-06 17:23:33 +05:30
|
|
|
self.all_macros.insert(ident.name, def);
|
2017-03-18 01:55:51 +00:00
|
|
|
if attr::contains_name(&item.attrs, "macro_export") {
|
2018-05-14 03:22:52 +03:00
|
|
|
let module = self.graph_root;
|
|
|
|
let vis = ty::Visibility::Public;
|
|
|
|
self.define(module, ident, MacroNS,
|
|
|
|
(def, vis, item.span, expansion, IsMacroExport));
|
2017-03-18 01:55:51 +00:00
|
|
|
} else {
|
2018-09-11 00:30:21 +03:00
|
|
|
if !attr::contains_name(&item.attrs, "rustc_doc_only_macro") {
|
|
|
|
self.check_reserved_macro_name(ident, MacroNS);
|
|
|
|
}
|
2017-03-18 01:55:51 +00:00
|
|
|
self.unused_macros.insert(def_id);
|
|
|
|
}
|
2017-05-11 10:26:07 +02:00
|
|
|
} else {
|
2017-03-18 01:55:51 +00:00
|
|
|
let module = self.current_module;
|
|
|
|
let def = Def::Macro(def_id, MacroKind::Bang);
|
|
|
|
let vis = self.resolve_visibility(&item.vis);
|
2017-05-31 17:03:41 +02:00
|
|
|
if vis != ty::Visibility::Public {
|
|
|
|
self.unused_macros.insert(def_id);
|
|
|
|
}
|
2017-03-18 01:55:51 +00:00
|
|
|
self.define(module, ident, MacroNS, (def, vis, item.span, expansion));
|
2016-12-01 11:20:04 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-09 01:31:14 -08:00
|
|
|
|
2017-02-02 00:33:42 +00:00
|
|
|
fn gate_legacy_custom_derive(&mut self, name: Symbol, span: Span) {
|
2018-02-14 16:11:02 +01:00
|
|
|
if !self.session.features_untracked().custom_derive {
|
2017-02-02 00:33:42 +00:00
|
|
|
let sess = &self.session.parse_sess;
|
|
|
|
let explain = feature_gate::EXPLAIN_CUSTOM_DERIVE;
|
|
|
|
emit_feature_err(sess, "custom_derive", span, GateIssue::Language, explain);
|
|
|
|
} else if !self.is_whitelisted_legacy_custom_derive(name) {
|
|
|
|
self.session.span_warn(span, feature_gate::EXPLAIN_DEPR_CUSTOM_DERIVE);
|
|
|
|
}
|
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
}
|