1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use {AmbiguityError, CrateLint, Resolver, ResolutionError, is_known_tool, resolve_error};
12 use {Module, ModuleKind, NameBinding, NameBindingKind, PathResult, Segment, ToNameBinding};
13 use ModuleOrUniformRoot;
14 use Namespace::{self, *};
15 use build_reduced_graph::{BuildReducedGraphVisitor, IsMacroExport};
16 use resolve_imports::ImportResolver;
17 use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX, DefIndex,
18 CrateNum, DefIndexAddressSpace};
19 use rustc::hir::def::{Def, NonMacroAttrKind};
20 use rustc::hir::map::{self, DefCollector};
21 use rustc::{ty, lint};
22 use syntax::ast::{self, Name, Ident};
24 use syntax::errors::DiagnosticBuilder;
25 use syntax::ext::base::{self, Determinacy};
26 use syntax::ext::base::{MacroKind, SyntaxExtension, Resolver as SyntaxResolver};
27 use syntax::ext::expand::{AstFragment, Invocation, InvocationKind};
28 use syntax::ext::hygiene::{self, Mark};
29 use syntax::ext::tt::macro_rules;
30 use syntax::feature_gate::{self, feature_err, emit_feature_err, is_builtin_attr_name, GateIssue};
31 use syntax::feature_gate::EXPLAIN_DERIVE_UNDERSCORE;
32 use syntax::fold::{self, Folder};
33 use syntax::parse::parser::PathStyle;
34 use syntax::parse::token::{self, Token};
36 use syntax::symbol::{Symbol, keywords};
37 use syntax::tokenstream::{TokenStream, TokenTree, Delimited, DelimSpan};
38 use syntax::util::lev_distance::find_best_match_for_name;
39 use syntax_pos::{Span, DUMMY_SP};
40 use errors::Applicability;
44 use rustc_data_structures::sync::Lrc;
46 #[derive(Clone, Debug)]
47 pub struct InvocationData<'a> {
49 /// Module in which the macro was invoked.
50 crate module: Cell<Module<'a>>,
51 /// Legacy scope in which the macro was invoked.
52 /// The invocation path is resolved in this scope.
53 crate parent_legacy_scope: Cell<LegacyScope<'a>>,
54 /// Legacy scope *produced* by expanding this macro invocation,
55 /// includes all the macro_rules items, other invocations, etc generated by it.
56 /// Set to the parent scope if the macro is not expanded yet (as if the macro produced nothing).
57 crate output_legacy_scope: Cell<LegacyScope<'a>>,
60 impl<'a> InvocationData<'a> {
61 pub fn root(graph_root: Module<'a>) -> Self {
63 module: Cell::new(graph_root),
64 def_index: CRATE_DEF_INDEX,
65 parent_legacy_scope: Cell::new(LegacyScope::Empty),
66 output_legacy_scope: Cell::new(LegacyScope::Empty),
71 /// Binding produced by a `macro_rules` item.
72 /// Not modularized, can shadow previous legacy bindings, etc.
74 pub struct LegacyBinding<'a> {
75 binding: &'a NameBinding<'a>,
76 /// Legacy scope into which the `macro_rules` item was planted.
77 parent_legacy_scope: LegacyScope<'a>,
81 /// Scope introduced by a `macro_rules!` macro.
82 /// Starts at the macro's definition and ends at the end of the macro's parent module
83 /// (named or unnamed), or even further if it escapes with `#[macro_use]`.
84 /// Some macro invocations need to introduce legacy scopes too because they
85 /// potentially can expand into macro definitions.
86 #[derive(Copy, Clone, Debug)]
87 pub enum LegacyScope<'a> {
88 /// Created when invocation data is allocated in the arena,
89 /// must be replaced with a proper scope later.
91 /// Empty "root" scope at the crate start containing no names.
93 /// Scope introduced by a `macro_rules!` macro definition.
94 Binding(&'a LegacyBinding<'a>),
95 /// Scope introduced by a macro invocation that can potentially
96 /// create a `macro_rules!` macro definition.
97 Invocation(&'a InvocationData<'a>),
100 /// Everything you need to resolve a macro or import path.
101 #[derive(Clone, Debug)]
102 pub struct ParentScope<'a> {
103 crate module: Module<'a>,
104 crate expansion: Mark,
105 crate legacy: LegacyScope<'a>,
106 crate derives: Vec<ast::Path>,
109 // Macro namespace is separated into two sub-namespaces, one for bang macros and
110 // one for attribute-like macros (attributes, derives).
111 // We ignore resolutions from one sub-namespace when searching names in scope for another.
112 fn sub_namespace_mismatch(requirement: Option<MacroKind>, candidate: Option<MacroKind>) -> bool {
114 enum SubNS { Bang, AttrLike }
115 let sub_ns = |kind| match kind {
116 MacroKind::Bang => Some(SubNS::Bang),
117 MacroKind::Attr | MacroKind::Derive => Some(SubNS::AttrLike),
118 MacroKind::ProcMacroStub => None,
120 let requirement = requirement.and_then(|kind| sub_ns(kind));
121 let candidate = candidate.and_then(|kind| sub_ns(kind));
122 // "No specific sub-namespace" means "matches anything" for both requirements and candidates.
123 candidate.is_some() && requirement.is_some() && candidate != requirement
126 impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
127 fn next_node_id(&mut self) -> ast::NodeId {
128 self.session.next_node_id()
131 fn get_module_scope(&mut self, id: ast::NodeId) -> Mark {
132 let mark = Mark::fresh(Mark::root());
133 let module = self.module_map[&self.definitions.local_def_id(id)];
134 self.invocations.insert(mark, self.arenas.alloc_invocation_data(InvocationData {
135 module: Cell::new(module),
136 def_index: module.def_id().unwrap().index,
137 parent_legacy_scope: Cell::new(LegacyScope::Empty),
138 output_legacy_scope: Cell::new(LegacyScope::Empty),
143 fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item> {
144 struct EliminateCrateVar<'b, 'a: 'b, 'crateloader: 'a>(
145 &'b mut Resolver<'a, 'crateloader>, Span
148 impl<'a, 'b, 'crateloader> Folder for EliminateCrateVar<'a, 'b, 'crateloader> {
149 fn fold_path(&mut self, path: ast::Path) -> ast::Path {
150 match self.fold_qpath(None, path) {
151 (None, path) => path,
156 fn fold_qpath(&mut self, mut qself: Option<ast::QSelf>, mut path: ast::Path)
157 -> (Option<ast::QSelf>, ast::Path) {
158 qself = qself.map(|ast::QSelf { ty, path_span, position }| {
160 ty: self.fold_ty(ty),
161 path_span: self.new_span(path_span),
166 if path.segments[0].ident.name == keywords::DollarCrate.name() {
167 let module = self.0.resolve_crate_root(path.segments[0].ident);
168 path.segments[0].ident.name = keywords::CrateRoot.name();
169 if !module.is_local() {
170 let span = path.segments[0].ident.span;
171 path.segments.insert(1, match module.kind {
172 ModuleKind::Def(_, name) => ast::PathSegment::from_ident(
173 ast::Ident::with_empty_ctxt(name).with_span_pos(span)
177 if let Some(qself) = &mut qself {
185 fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
186 fold::noop_fold_mac(mac, self)
190 let ret = EliminateCrateVar(self, item.span).fold_item(item);
191 assert!(ret.len() == 1);
192 ret.into_iter().next().unwrap()
195 fn is_whitelisted_legacy_custom_derive(&self, name: Name) -> bool {
196 self.whitelisted_legacy_custom_derives.contains(&name)
199 fn visit_ast_fragment_with_placeholders(&mut self, mark: Mark, fragment: &AstFragment,
201 let invocation = self.invocations[&mark];
202 self.collect_def_ids(mark, invocation, fragment);
204 self.current_module = invocation.module.get();
205 self.current_module.unresolved_invocations.borrow_mut().remove(&mark);
206 self.current_module.unresolved_invocations.borrow_mut().extend(derives);
207 self.invocations.extend(derives.iter().map(|&derive| (derive, invocation)));
208 let mut visitor = BuildReducedGraphVisitor {
210 current_legacy_scope: invocation.parent_legacy_scope.get(),
213 fragment.visit_with(&mut visitor);
214 invocation.output_legacy_scope.set(visitor.current_legacy_scope);
217 fn add_builtin(&mut self, ident: ast::Ident, ext: Lrc<SyntaxExtension>) {
219 krate: CrateNum::BuiltinMacros,
220 index: DefIndex::from_array_index(self.macro_map.len(),
221 DefIndexAddressSpace::Low),
223 let kind = ext.kind();
224 self.macro_map.insert(def_id, ext);
225 let binding = self.arenas.alloc_name_binding(NameBinding {
226 kind: NameBindingKind::Def(Def::Macro(def_id, kind), false),
228 vis: ty::Visibility::Invisible,
229 expansion: Mark::root(),
231 if self.builtin_macros.insert(ident.name, binding).is_some() {
232 self.session.span_err(ident.span,
233 &format!("built-in macro `{}` was already defined", ident));
237 fn resolve_imports(&mut self) {
238 ImportResolver { resolver: self }.resolve_imports()
241 // Resolves attribute and derive legacy macros from `#![plugin(..)]`.
242 fn find_legacy_attr_invoc(&mut self, attrs: &mut Vec<ast::Attribute>, allow_derive: bool)
243 -> Option<ast::Attribute> {
248 // Check for legacy derives
249 for i in 0..attrs.len() {
250 let name = attrs[i].name();
252 if name == "derive" {
253 let result = attrs[i].parse_list(&self.session.parse_sess, |parser| {
254 parser.parse_path_allowing_meta(PathStyle::Mod)
257 let mut traits = match result {
258 Ok(traits) => traits,
265 for j in 0..traits.len() {
266 if traits[j].segments.len() > 1 {
269 let trait_name = traits[j].segments[0].ident.name;
270 let legacy_name = Symbol::intern(&format!("derive_{}", trait_name));
271 if !self.builtin_macros.contains_key(&legacy_name) {
274 let span = traits.remove(j).span;
275 self.gate_legacy_custom_derive(legacy_name, span);
276 if traits.is_empty() {
279 let mut tokens = Vec::with_capacity(traits.len() - 1);
280 for (j, path) in traits.iter().enumerate() {
282 tokens.push(TokenTree::Token(attrs[i].span, Token::Comma).into());
284 tokens.reserve((path.segments.len() * 2).saturating_sub(1));
285 for (k, segment) in path.segments.iter().enumerate() {
287 tokens.push(TokenTree::Token(path.span, Token::ModSep).into());
289 let tok = Token::from_ast_ident(segment.ident);
290 tokens.push(TokenTree::Token(path.span, tok).into());
293 let delim_span = DelimSpan::from_single(attrs[i].span);
294 attrs[i].tokens = TokenTree::Delimited(delim_span, Delimited {
296 tts: TokenStream::concat(tokens).into(),
299 return Some(ast::Attribute {
300 path: ast::Path::from_ident(Ident::new(legacy_name, span)),
301 tokens: TokenStream::empty(),
302 id: attr::mk_attr_id(),
303 style: ast::AttrStyle::Outer,
304 is_sugared_doc: false,
314 fn resolve_macro_invocation(&mut self, invoc: &Invocation, invoc_id: Mark, force: bool)
315 -> Result<Option<Lrc<SyntaxExtension>>, Determinacy> {
316 let (path, kind, derives_in_scope, after_derive) = match invoc.kind {
317 InvocationKind::Attr { attr: None, .. } =>
319 InvocationKind::Attr { attr: Some(ref attr), ref traits, after_derive, .. } =>
320 (&attr.path, MacroKind::Attr, traits.clone(), after_derive),
321 InvocationKind::Bang { ref mac, .. } =>
322 (&mac.node.path, MacroKind::Bang, Vec::new(), false),
323 InvocationKind::Derive { ref path, .. } =>
324 (path, MacroKind::Derive, Vec::new(), false),
327 let parent_scope = self.invoc_parent_scope(invoc_id, derives_in_scope);
328 let (def, ext) = self.resolve_macro_to_def(path, kind, &parent_scope, force)?;
330 if let Def::Macro(def_id, _) = def {
332 self.session.span_err(invoc.span(),
333 "macro attributes must be placed before `#[derive]`");
335 self.macro_defs.insert(invoc.expansion_data.mark, def_id);
336 let normal_module_def_id =
337 self.macro_def_scope(invoc.expansion_data.mark).normal_ancestor_id;
338 self.definitions.add_parent_module_of_macro_def(invoc.expansion_data.mark,
339 normal_module_def_id);
340 invoc.expansion_data.mark.set_default_transparency(ext.default_transparency());
341 invoc.expansion_data.mark.set_is_builtin(def_id.krate == CrateNum::BuiltinMacros);
347 fn resolve_macro_path(&mut self, path: &ast::Path, kind: MacroKind, invoc_id: Mark,
348 derives_in_scope: Vec<ast::Path>, force: bool)
349 -> Result<Lrc<SyntaxExtension>, Determinacy> {
350 let parent_scope = self.invoc_parent_scope(invoc_id, derives_in_scope);
351 Ok(self.resolve_macro_to_def(path, kind, &parent_scope, force)?.1)
354 fn check_unused_macros(&self) {
355 for did in self.unused_macros.iter() {
356 let id_span = match *self.macro_map[did] {
357 SyntaxExtension::NormalTT { def_info, .. } |
358 SyntaxExtension::DeclMacro { def_info, .. } => def_info,
361 if let Some((id, span)) = id_span {
362 let lint = lint::builtin::UNUSED_MACROS;
363 let msg = "unused macro definition";
364 self.session.buffer_lint(lint, id, span, msg);
366 bug!("attempted to create unused macro error, but span not available");
372 impl<'a, 'cl> Resolver<'a, 'cl> {
373 pub fn dummy_parent_scope(&mut self) -> ParentScope<'a> {
374 self.invoc_parent_scope(Mark::root(), Vec::new())
377 fn invoc_parent_scope(&mut self, invoc_id: Mark, derives: Vec<ast::Path>) -> ParentScope<'a> {
378 let invoc = self.invocations[&invoc_id];
380 module: invoc.module.get().nearest_item_scope(),
381 expansion: invoc_id.parent(),
382 legacy: invoc.parent_legacy_scope.get(),
387 fn resolve_macro_to_def(
391 parent_scope: &ParentScope<'a>,
393 ) -> Result<(Def, Lrc<SyntaxExtension>), Determinacy> {
394 let def = self.resolve_macro_to_def_inner(path, kind, parent_scope, force);
396 // Report errors and enforce feature gates for the resolved macro.
397 if def != Err(Determinacy::Undetermined) {
398 // Do not report duplicated errors on every undetermined resolution.
399 for segment in &path.segments {
400 if let Some(args) = &segment.args {
401 self.session.span_err(args.span(), "generic arguments in macro path");
409 Def::Macro(def_id, macro_kind) => {
410 self.unused_macros.remove(&def_id);
411 if macro_kind == MacroKind::ProcMacroStub {
412 let msg = "can't use a procedural macro from the same crate that defines it";
413 self.session.span_err(path.span, msg);
414 return Err(Determinacy::Determined);
417 Def::NonMacroAttr(attr_kind) => {
418 if kind == MacroKind::Attr {
419 let features = self.session.features_untracked();
420 if attr_kind == NonMacroAttrKind::Custom {
421 assert!(path.segments.len() == 1);
422 let name = path.segments[0].ident.name.as_str();
423 if name.starts_with("rustc_") {
424 if !features.rustc_attrs {
425 let msg = "unless otherwise specified, attributes with the prefix \
426 `rustc_` are reserved for internal compiler diagnostics";
427 feature_err(&self.session.parse_sess, "rustc_attrs", path.span,
428 GateIssue::Language, &msg).emit();
430 } else if name.starts_with("derive_") {
431 if !features.custom_derive {
432 feature_err(&self.session.parse_sess, "custom_derive", path.span,
433 GateIssue::Language, EXPLAIN_DERIVE_UNDERSCORE).emit();
435 } else if !features.custom_attribute {
436 let msg = format!("The attribute `{}` is currently unknown to the \
437 compiler and may have meaning added to it in the \
439 feature_err(&self.session.parse_sess, "custom_attribute", path.span,
440 GateIssue::Language, &msg).emit();
444 // Not only attributes, but anything in macro namespace can result in
445 // `Def::NonMacroAttr` definition (e.g. `inline!()`), so we must report
446 // an error for those cases.
447 let msg = format!("expected a macro, found {}", def.kind_name());
448 self.session.span_err(path.span, &msg);
449 return Err(Determinacy::Determined);
452 _ => panic!("expected `Def::Macro` or `Def::NonMacroAttr`"),
455 Ok((def, self.get_macro(def)))
458 pub fn resolve_macro_to_def_inner(
462 parent_scope: &ParentScope<'a>,
464 ) -> Result<Def, Determinacy> {
465 let path_span = path.span;
466 let mut path = Segment::from_path(path);
468 // Possibly apply the macro helper hack
469 if kind == MacroKind::Bang && path.len() == 1 &&
470 path[0].ident.span.ctxt().outer().expn_info()
471 .map_or(false, |info| info.local_inner_macros) {
472 let root = Ident::new(keywords::DollarCrate.name(), path[0].ident.span);
473 path.insert(0, Segment::from_ident(root));
477 let def = match self.resolve_path(None, &path, Some(MacroNS), parent_scope,
478 false, path_span, CrateLint::No) {
479 PathResult::NonModule(path_res) => match path_res.base_def() {
480 Def::Err => Err(Determinacy::Determined),
482 if path_res.unresolved_segments() > 0 {
483 self.found_unresolved_macro = true;
484 self.session.span_err(path_span,
485 "fail to resolve non-ident macro path");
486 Err(Determinacy::Determined)
492 PathResult::Module(..) => unreachable!(),
493 PathResult::Indeterminate if !force => return Err(Determinacy::Undetermined),
495 self.found_unresolved_macro = true;
496 Err(Determinacy::Determined)
500 parent_scope.module.macro_resolutions.borrow_mut()
501 .push((path, parent_scope.clone(), path_span));
505 let binding = self.early_resolve_ident_in_lexical_scope(
506 path[0].ident, MacroNS, Some(kind), parent_scope, false, force, path_span
510 Err(Determinacy::Determined) => self.found_unresolved_macro = true,
511 Err(Determinacy::Undetermined) => return Err(Determinacy::Undetermined),
514 parent_scope.module.legacy_macro_resolutions.borrow_mut()
515 .push((path[0].ident, kind, parent_scope.clone(), binding.ok()));
517 binding.map(|binding| binding.def_ignoring_ambiguity())
521 // Resolve an identifier in lexical scope.
522 // This is a variation of `fn resolve_ident_in_lexical_scope` that can be run during
523 // expansion and import resolution (perhaps they can be merged in the future).
524 // The function is used for resolving initial segments of macro paths (e.g. `foo` in
525 // `foo::bar!(); or `foo!();`) and can be used for "uniform path" imports in the future.
526 crate fn early_resolve_ident_in_lexical_scope(
530 kind: Option<MacroKind>,
531 parent_scope: &ParentScope<'a>,
535 ) -> Result<&'a NameBinding<'a>, Determinacy> {
536 // General principles:
537 // 1. Not controlled (user-defined) names should have higher priority than controlled names
538 // built into the language or standard library. This way we can add new names into the
539 // language or standard library without breaking user code.
540 // 2. "Closed set" below means new names cannot appear after the current resolution attempt.
541 // Places to search (in order of decreasing priority):
543 // 1. FIXME: Ribs (type parameters), there's no necessary infrastructure yet
544 // (open set, not controlled).
545 // 2. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
546 // (open, not controlled).
547 // 3. Extern prelude (closed, not controlled).
548 // 4. Tool modules (closed, controlled right now, but not in the future).
549 // 5. Standard library prelude (de-facto closed, controlled).
550 // 6. Language prelude (closed, controlled).
552 // 1. FIXME: Ribs (local variables), there's no necessary infrastructure yet
553 // (open set, not controlled).
554 // 2. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
555 // (open, not controlled).
556 // 3. Standard library prelude (de-facto closed, controlled).
558 // 1-3. Derive helpers (open, not controlled). All ambiguities with other names
559 // are currently reported as errors. They should be higher in priority than preludes
560 // and probably even names in modules according to the "general principles" above. They
561 // also should be subject to restricted shadowing because are effectively produced by
562 // derives (you need to resolve the derive first to add helpers into scope), but they
563 // should be available before the derive is expanded for compatibility.
564 // It's mess in general, so we are being conservative for now.
565 // 1-3. `macro_rules` (open, not controlled), loop through legacy scopes. Have higher
566 // priority than prelude macros, but create ambiguities with macros in modules.
567 // 1-3. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
568 // (open, not controlled). Have higher priority than prelude macros, but create
569 // ambiguities with `macro_rules`.
570 // 4. `macro_use` prelude (open, the open part is from macro expansions, not controlled).
571 // 4a. User-defined prelude from macro-use
572 // (open, the open part is from macro expansions, not controlled).
573 // 4b. Standard library prelude is currently implemented as `macro-use` (closed, controlled)
574 // 5. Language prelude: builtin macros (closed, controlled, except for legacy plugins).
575 // 6. Language prelude: builtin attributes (closed, controlled).
576 // 4-6. Legacy plugin helpers (open, not controlled). Similar to derive helpers,
577 // but introduced by legacy plugins using `register_attribute`. Priority is somewhere
578 // in prelude, not sure where exactly (creates ambiguities with any other prelude names).
580 enum WhereToResolve<'a> {
582 MacroRules(LegacyScope<'a>),
596 const DERIVE_HELPERS = 1 << 0;
597 const MACRO_RULES = 1 << 1;
598 const MODULE = 1 << 2;
599 const PRELUDE = 1 << 3;
603 assert!(force || !record_used); // `record_used` implies `force`
604 ident = ident.modern();
606 // This is *the* result, resolution from the scope closest to the resolved identifier.
607 // However, sometimes this result is "weak" because it comes from a glob import or
608 // a macro expansion, and in this case it cannot shadow names from outer scopes, e.g.
609 // mod m { ... } // solution in outer scope
611 // use prefix::*; // imports another `m` - innermost solution
612 // // weak, cannot shadow the outer `m`, need to report ambiguity error
615 // So we have to save the innermost solution and continue searching in outer scopes
616 // to detect potential ambiguities.
617 let mut innermost_result: Option<(&NameBinding, Flags, /* conflicts with */ Flags)> = None;
619 // Go through all the scopes and try to resolve the name.
620 let mut where_to_resolve = WhereToResolve::DeriveHelpers;
621 let mut use_prelude = !parent_scope.module.no_implicit_prelude;
623 let result = match where_to_resolve {
624 WhereToResolve::DeriveHelpers => {
625 let mut result = Err(Determinacy::Determined);
626 for derive in &parent_scope.derives {
627 let parent_scope = ParentScope { derives: Vec::new(), ..*parent_scope };
628 if let Ok((_, ext)) = self.resolve_macro_to_def(derive, MacroKind::Derive,
629 &parent_scope, force) {
630 if let SyntaxExtension::ProcMacroDerive(_, helper_attrs, _) = &*ext {
631 if helper_attrs.contains(&ident.name) {
633 (Def::NonMacroAttr(NonMacroAttrKind::DeriveHelper),
634 ty::Visibility::Public, derive.span, Mark::root())
635 .to_name_binding(self.arenas);
636 result = Ok((binding, Flags::DERIVE_HELPERS, Flags::all()));
644 WhereToResolve::MacroRules(legacy_scope) => match legacy_scope {
645 LegacyScope::Binding(legacy_binding) if ident == legacy_binding.ident =>
646 Ok((legacy_binding.binding, Flags::MACRO_RULES, Flags::empty())),
647 _ => Err(Determinacy::Determined),
649 WhereToResolve::Module(module) => {
650 let orig_current_module = mem::replace(&mut self.current_module, module);
651 let binding = self.resolve_ident_in_module_unadjusted(
652 ModuleOrUniformRoot::Module(module),
659 self.current_module = orig_current_module;
660 binding.map(|binding| (binding, Flags::MODULE, Flags::empty()))
662 WhereToResolve::MacroUsePrelude => {
663 match self.macro_use_prelude.get(&ident.name).cloned() {
664 Some(binding) => Ok((binding, Flags::PRELUDE, Flags::empty())),
665 None => Err(Determinacy::Determined),
668 WhereToResolve::BuiltinMacros => {
669 match self.builtin_macros.get(&ident.name).cloned() {
670 Some(binding) => Ok((binding, Flags::PRELUDE, Flags::empty())),
671 None => Err(Determinacy::Determined),
674 WhereToResolve::BuiltinAttrs => {
675 if is_builtin_attr_name(ident.name) {
676 let binding = (Def::NonMacroAttr(NonMacroAttrKind::Builtin),
677 ty::Visibility::Public, ident.span, Mark::root())
678 .to_name_binding(self.arenas);
679 Ok((binding, Flags::PRELUDE, Flags::empty()))
681 Err(Determinacy::Determined)
684 WhereToResolve::LegacyPluginHelpers => {
685 if self.session.plugin_attributes.borrow().iter()
686 .any(|(name, _)| ident.name == &**name) {
687 let binding = (Def::NonMacroAttr(NonMacroAttrKind::LegacyPluginHelper),
688 ty::Visibility::Public, ident.span, Mark::root())
689 .to_name_binding(self.arenas);
690 Ok((binding, Flags::PRELUDE, Flags::PRELUDE))
692 Err(Determinacy::Determined)
695 WhereToResolve::ExternPrelude => {
696 let mut result = Err(Determinacy::Determined);
698 if let Some(binding) = self.extern_prelude_get(ident, !record_used,
699 innermost_result.is_some()) {
700 result = Ok((binding, Flags::PRELUDE, Flags::empty()));
705 WhereToResolve::ToolPrelude => {
706 if use_prelude && is_known_tool(ident.name) {
707 let binding = (Def::ToolMod, ty::Visibility::Public,
708 ident.span, Mark::root()).to_name_binding(self.arenas);
709 Ok((binding, Flags::PRELUDE, Flags::empty()))
711 Err(Determinacy::Determined)
714 WhereToResolve::StdLibPrelude => {
715 let mut result = Err(Determinacy::Determined);
717 if let Some(prelude) = self.prelude {
718 if let Ok(binding) = self.resolve_ident_in_module_unadjusted(
719 ModuleOrUniformRoot::Module(prelude),
726 result = Ok((binding, Flags::PRELUDE, Flags::empty()));
732 WhereToResolve::BuiltinTypes => {
733 match self.primitive_type_table.primitive_types.get(&ident.name).cloned() {
735 let binding = (Def::PrimTy(prim_ty), ty::Visibility::Public,
736 ident.span, Mark::root()).to_name_binding(self.arenas);
737 Ok((binding, Flags::PRELUDE, Flags::empty()))
739 None => Err(Determinacy::Determined)
744 macro_rules! continue_search { () => {
745 where_to_resolve = match where_to_resolve {
746 WhereToResolve::DeriveHelpers =>
747 WhereToResolve::MacroRules(parent_scope.legacy),
748 WhereToResolve::MacroRules(legacy_scope) => match legacy_scope {
749 LegacyScope::Binding(binding) =>
750 WhereToResolve::MacroRules(binding.parent_legacy_scope),
751 LegacyScope::Invocation(invocation) =>
752 WhereToResolve::MacroRules(invocation.output_legacy_scope.get()),
753 LegacyScope::Empty => WhereToResolve::Module(parent_scope.module),
754 LegacyScope::Uninitialized => unreachable!(),
756 WhereToResolve::Module(module) => {
757 match self.hygienic_lexical_parent(module, &mut ident.span) {
758 Some(parent_module) => WhereToResolve::Module(parent_module),
760 use_prelude = !module.no_implicit_prelude;
762 TypeNS => WhereToResolve::ExternPrelude,
763 ValueNS => WhereToResolve::StdLibPrelude,
764 MacroNS => WhereToResolve::MacroUsePrelude,
769 WhereToResolve::MacroUsePrelude => WhereToResolve::BuiltinMacros,
770 WhereToResolve::BuiltinMacros => WhereToResolve::BuiltinAttrs,
771 WhereToResolve::BuiltinAttrs => WhereToResolve::LegacyPluginHelpers,
772 WhereToResolve::LegacyPluginHelpers => break, // nowhere else to search
773 WhereToResolve::ExternPrelude => WhereToResolve::ToolPrelude,
774 WhereToResolve::ToolPrelude => WhereToResolve::StdLibPrelude,
775 WhereToResolve::StdLibPrelude => match ns {
776 TypeNS => WhereToResolve::BuiltinTypes,
777 ValueNS => break, // nowhere else to search
778 MacroNS => unreachable!(),
780 WhereToResolve::BuiltinTypes => break, // nowhere else to search
787 Ok((binding, flags, ambig_flags)) => {
788 if sub_namespace_mismatch(kind, binding.macro_kind()) {
796 if let Some((innermost_binding, innermost_flags, innermost_ambig_flags))
798 // Found another solution, if the first one was "weak", report an error.
799 if binding.def() != innermost_binding.def() &&
800 (innermost_binding.is_glob_import() ||
801 innermost_binding.may_appear_after(parent_scope.expansion, binding) ||
802 innermost_flags.intersects(ambig_flags) ||
803 flags.intersects(innermost_ambig_flags) ||
804 (innermost_flags.contains(Flags::MACRO_RULES) &&
805 flags.contains(Flags::MODULE) &&
806 !self.disambiguate_legacy_vs_modern(innermost_binding, binding))) {
807 self.ambiguity_errors.push(AmbiguityError {
809 b1: innermost_binding,
812 return Ok(innermost_binding);
815 // Found the first solution.
816 innermost_result = Some((binding, flags, ambig_flags));
821 Err(Determinacy::Determined) => {
824 Err(Determinacy::Undetermined) => return Err(Determinacy::determined(force)),
828 // The first found solution was the only one, return it.
829 if let Some((binding, ..)) = innermost_result {
833 let determinacy = Determinacy::determined(force);
834 if determinacy == Determinacy::Determined && kind == Some(MacroKind::Attr) {
835 // For single-segment attributes interpret determinate "no resolution" as a custom
836 // attribute. (Lexical resolution implies the first segment and attr kind should imply
837 // the last segment, so we are certainly working with a single-segment attribute here.)
838 assert!(ns == MacroNS);
839 let binding = (Def::NonMacroAttr(NonMacroAttrKind::Custom),
840 ty::Visibility::Public, ident.span, Mark::root())
841 .to_name_binding(self.arenas);
848 pub fn finalize_current_module_macro_resolutions(&mut self) {
849 let module = self.current_module;
851 let macro_resolutions =
852 mem::replace(&mut *module.macro_resolutions.borrow_mut(), Vec::new());
853 for (mut path, parent_scope, path_span) in macro_resolutions {
854 // FIXME: Path resolution will ICE if segment IDs present.
855 for seg in &mut path { seg.id = None; }
856 match self.resolve_path(None, &path, Some(MacroNS), &parent_scope,
857 true, path_span, CrateLint::No) {
858 PathResult::NonModule(_) => {},
859 PathResult::Failed(span, msg, _) => {
860 resolve_error(self, span, ResolutionError::FailedToResolve(&msg));
866 let legacy_macro_resolutions =
867 mem::replace(&mut *module.legacy_macro_resolutions.borrow_mut(), Vec::new());
868 for (ident, kind, parent_scope, initial_binding) in legacy_macro_resolutions {
869 let binding = self.early_resolve_ident_in_lexical_scope(
870 ident, MacroNS, Some(kind), &parent_scope, true, true, ident.span
874 let def = binding.def_ignoring_ambiguity();
875 if let Some(initial_binding) = initial_binding {
876 self.record_use(ident, MacroNS, initial_binding);
877 let initial_def = initial_binding.def_ignoring_ambiguity();
878 if self.ambiguity_errors.is_empty() &&
879 def != initial_def && def != Def::Err {
880 // Make sure compilation does not succeed if preferred macro resolution
881 // has changed after the macro had been expanded. In theory all such
882 // situations should be reported as ambiguity errors, so this is a bug.
883 span_bug!(ident.span, "inconsistent resolution for a macro");
886 // It's possible that the macro was unresolved (indeterminate) and silently
887 // expanded into a dummy fragment for recovery during expansion.
888 // Now, post-expansion, the resolution may succeed, but we can't change the
889 // past and need to report an error.
890 let msg = format!("cannot determine resolution for the {} `{}`",
891 kind.descr(), ident);
892 let msg_note = "import resolution is stuck, try simplifying macro imports";
893 self.session.struct_span_err(ident.span, &msg).note(msg_note).emit();
897 assert!(initial_binding.is_none());
898 let bang = if kind == MacroKind::Bang { "!" } else { "" };
900 format!("cannot find {} `{}{}` in this scope", kind.descr(), ident, bang);
901 let mut err = self.session.struct_span_err(ident.span, &msg);
902 self.suggest_macro_name(&ident.as_str(), kind, &mut err, ident.span);
908 let builtin_attrs = mem::replace(&mut *module.builtin_attrs.borrow_mut(), Vec::new());
909 for (ident, parent_scope) in builtin_attrs {
910 let binding = self.early_resolve_ident_in_lexical_scope(
911 ident, MacroNS, Some(MacroKind::Attr), &parent_scope, true, true, ident.span
913 if let Ok(binding) = binding {
914 if binding.def_ignoring_ambiguity() !=
915 Def::NonMacroAttr(NonMacroAttrKind::Builtin) {
916 let builtin_binding = (Def::NonMacroAttr(NonMacroAttrKind::Builtin),
917 ty::Visibility::Public, ident.span, Mark::root())
918 .to_name_binding(self.arenas);
919 self.report_ambiguity_error(ident, binding, builtin_binding);
925 fn suggest_macro_name(&mut self, name: &str, kind: MacroKind,
926 err: &mut DiagnosticBuilder<'a>, span: Span) {
927 // First check if this is a locally-defined bang macro.
928 let suggestion = if let MacroKind::Bang = kind {
929 find_best_match_for_name(self.macro_names.iter().map(|ident| &ident.name), name, None)
932 // Then check global macros.
934 let names = self.builtin_macros.iter().chain(self.macro_use_prelude.iter())
935 .filter_map(|(name, binding)| {
936 if binding.macro_kind() == Some(kind) { Some(name) } else { None }
938 find_best_match_for_name(names, name, None)
939 // Then check modules.
941 let is_macro = |def| {
942 if let Def::Macro(_, def_kind) = def {
948 let ident = Ident::new(Symbol::intern(name), span);
949 self.lookup_typo_candidate(&[Segment::from_ident(ident)], MacroNS, is_macro, span)
952 if let Some(suggestion) = suggestion {
953 if suggestion != name {
954 if let MacroKind::Bang = kind {
955 err.span_suggestion_with_applicability(
957 "you could try the macro",
958 suggestion.to_string(),
959 Applicability::MaybeIncorrect
962 err.span_suggestion_with_applicability(
965 suggestion.to_string(),
966 Applicability::MaybeIncorrect
970 err.help("have you added the `#[macro_use]` on the module/import?");
975 fn collect_def_ids(&mut self,
977 invocation: &'a InvocationData<'a>,
978 fragment: &AstFragment) {
979 let Resolver { ref mut invocations, arenas, graph_root, .. } = *self;
980 let InvocationData { def_index, .. } = *invocation;
982 let visit_macro_invoc = &mut |invoc: map::MacroInvocationData| {
983 invocations.entry(invoc.mark).or_insert_with(|| {
984 arenas.alloc_invocation_data(InvocationData {
985 def_index: invoc.def_index,
986 module: Cell::new(graph_root),
987 parent_legacy_scope: Cell::new(LegacyScope::Uninitialized),
988 output_legacy_scope: Cell::new(LegacyScope::Uninitialized),
993 let mut def_collector = DefCollector::new(&mut self.definitions, mark);
994 def_collector.visit_macro_invoc = Some(visit_macro_invoc);
995 def_collector.with_parent(def_index, |def_collector| {
996 fragment.visit_with(def_collector)
1000 pub fn define_macro(&mut self,
1003 current_legacy_scope: &mut LegacyScope<'a>) {
1004 self.local_macro_def_scopes.insert(item.id, self.current_module);
1005 let ident = item.ident;
1006 if ident.name == "macro_rules" {
1007 self.session.span_err(item.span, "user-defined macros may not be named `macro_rules`");
1010 let def_id = self.definitions.local_def_id(item.id);
1011 let ext = Lrc::new(macro_rules::compile(&self.session.parse_sess,
1012 &self.session.features_untracked(),
1013 item, hygiene::default_edition()));
1014 self.macro_map.insert(def_id, ext);
1016 let def = match item.node { ast::ItemKind::MacroDef(ref def) => def, _ => unreachable!() };
1018 let ident = ident.modern();
1019 self.macro_names.insert(ident);
1020 let def = Def::Macro(def_id, MacroKind::Bang);
1021 let vis = ty::Visibility::Invisible; // Doesn't matter for legacy bindings
1022 let binding = (def, vis, item.span, expansion).to_name_binding(self.arenas);
1023 self.set_binding_parent_module(binding, self.current_module);
1024 let legacy_binding = self.arenas.alloc_legacy_binding(LegacyBinding {
1025 parent_legacy_scope: *current_legacy_scope, binding, ident
1027 *current_legacy_scope = LegacyScope::Binding(legacy_binding);
1028 self.all_macros.insert(ident.name, def);
1029 if attr::contains_name(&item.attrs, "macro_export") {
1030 let module = self.graph_root;
1031 let vis = ty::Visibility::Public;
1032 self.define(module, ident, MacroNS,
1033 (def, vis, item.span, expansion, IsMacroExport));
1035 if !attr::contains_name(&item.attrs, "rustc_doc_only_macro") {
1036 self.check_reserved_macro_name(ident, MacroNS);
1038 self.unused_macros.insert(def_id);
1041 let module = self.current_module;
1042 let def = Def::Macro(def_id, MacroKind::Bang);
1043 let vis = self.resolve_visibility(&item.vis);
1044 if vis != ty::Visibility::Public {
1045 self.unused_macros.insert(def_id);
1047 self.define(module, ident, MacroNS, (def, vis, item.span, expansion));
1051 fn gate_legacy_custom_derive(&mut self, name: Symbol, span: Span) {
1052 if !self.session.features_untracked().custom_derive {
1053 let sess = &self.session.parse_sess;
1054 let explain = feature_gate::EXPLAIN_CUSTOM_DERIVE;
1055 emit_feature_err(sess, "custom_derive", span, GateIssue::Language, explain);
1056 } else if !self.is_whitelisted_legacy_custom_derive(name) {
1057 self.session.span_warn(span, feature_gate::EXPLAIN_DEPR_CUSTOM_DERIVE);