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, ToNameBinding};
13 use ModuleOrUniformRoot;
14 use Namespace::{self, TypeNS, MacroNS};
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, MultiModifier, MultiDecorator};
26 use syntax::ext::base::{MacroKind, SyntaxExtension, Resolver as SyntaxResolver};
27 use syntax::ext::expand::{AstFragment, Invocation, InvocationKind, TogetherWith};
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, Copy)]
47 crate struct FromPrelude(bool);
50 pub struct InvocationData<'a> {
52 /// Module in which the macro was invoked.
53 crate module: Cell<Module<'a>>,
54 /// Legacy scope in which the macro was invoked.
55 /// The invocation path is resolved in this scope.
56 crate parent_legacy_scope: Cell<LegacyScope<'a>>,
57 /// Legacy scope *produced* by expanding this macro invocation,
58 /// includes all the macro_rules items, other invocations, etc generated by it.
59 /// Set to the parent scope if the macro is not expanded yet (as if the macro produced nothing).
60 crate output_legacy_scope: Cell<LegacyScope<'a>>,
63 impl<'a> InvocationData<'a> {
64 pub fn root(graph_root: Module<'a>) -> Self {
66 module: Cell::new(graph_root),
67 def_index: CRATE_DEF_INDEX,
68 parent_legacy_scope: Cell::new(LegacyScope::Empty),
69 output_legacy_scope: Cell::new(LegacyScope::Empty),
74 /// Binding produced by a `macro_rules` item.
75 /// Not modularized, can shadow previous legacy bindings, etc.
76 pub struct LegacyBinding<'a> {
77 binding: &'a NameBinding<'a>,
78 /// Legacy scope into which the `macro_rules` item was planted.
79 parent_legacy_scope: LegacyScope<'a>,
83 /// Scope introduced by a `macro_rules!` macro.
84 /// Starts at the macro's definition and ends at the end of the macro's parent module
85 /// (named or unnamed), or even further if it escapes with `#[macro_use]`.
86 /// Some macro invocations need to introduce legacy scopes too because they
87 /// potentially can expand into macro definitions.
88 #[derive(Copy, Clone)]
89 pub enum LegacyScope<'a> {
90 /// Created when invocation data is allocated in the arena,
91 /// must be replaced with a proper scope later.
93 /// Empty "root" scope at the crate start containing no names.
95 /// Scope introduced by a `macro_rules!` macro definition.
96 Binding(&'a LegacyBinding<'a>),
97 /// Scope introduced by a macro invocation that can potentially
98 /// create a `macro_rules!` macro definition.
99 Invocation(&'a InvocationData<'a>),
102 /// Everything you need to resolve a macro path.
104 pub struct ParentScope<'a> {
105 crate module: Module<'a>,
106 crate expansion: Mark,
107 crate legacy: LegacyScope<'a>,
108 crate derives: Vec<ast::Path>,
111 // Macro namespace is separated into two sub-namespaces, one for bang macros and
112 // one for attribute-like macros (attributes, derives).
113 // We ignore resolutions from one sub-namespace when searching names in scope for another.
114 fn sub_namespace_mismatch(requirement: Option<MacroKind>, candidate: Option<MacroKind>) -> bool {
116 enum SubNS { Bang, AttrLike }
117 let sub_ns = |kind| match kind {
118 MacroKind::Bang => Some(SubNS::Bang),
119 MacroKind::Attr | MacroKind::Derive => Some(SubNS::AttrLike),
120 MacroKind::ProcMacroStub => None,
122 let requirement = requirement.and_then(|kind| sub_ns(kind));
123 let candidate = candidate.and_then(|kind| sub_ns(kind));
124 // "No specific sub-namespace" means "matches anything" for both requirements and candidates.
125 candidate.is_some() && requirement.is_some() && candidate != requirement
128 impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
129 fn next_node_id(&mut self) -> ast::NodeId {
130 self.session.next_node_id()
133 fn get_module_scope(&mut self, id: ast::NodeId) -> Mark {
134 let mark = Mark::fresh(Mark::root());
135 let module = self.module_map[&self.definitions.local_def_id(id)];
136 self.invocations.insert(mark, self.arenas.alloc_invocation_data(InvocationData {
137 module: Cell::new(module),
138 def_index: module.def_id().unwrap().index,
139 parent_legacy_scope: Cell::new(LegacyScope::Empty),
140 output_legacy_scope: Cell::new(LegacyScope::Empty),
145 fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item> {
146 struct EliminateCrateVar<'b, 'a: 'b, 'crateloader: 'a>(
147 &'b mut Resolver<'a, 'crateloader>, Span
150 impl<'a, 'b, 'crateloader> Folder for EliminateCrateVar<'a, 'b, 'crateloader> {
151 fn fold_path(&mut self, path: ast::Path) -> ast::Path {
152 match self.fold_qpath(None, path) {
153 (None, path) => path,
158 fn fold_qpath(&mut self, mut qself: Option<ast::QSelf>, mut path: ast::Path)
159 -> (Option<ast::QSelf>, ast::Path) {
160 qself = qself.map(|ast::QSelf { ty, path_span, position }| {
162 ty: self.fold_ty(ty),
163 path_span: self.new_span(path_span),
168 if path.segments[0].ident.name == keywords::DollarCrate.name() {
169 let module = self.0.resolve_crate_root(path.segments[0].ident);
170 path.segments[0].ident.name = keywords::CrateRoot.name();
171 if !module.is_local() {
172 let span = path.segments[0].ident.span;
173 path.segments.insert(1, match module.kind {
174 ModuleKind::Def(_, name) => ast::PathSegment::from_ident(
175 ast::Ident::with_empty_ctxt(name).with_span_pos(span)
179 if let Some(qself) = &mut qself {
187 fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
188 fold::noop_fold_mac(mac, self)
192 let ret = EliminateCrateVar(self, item.span).fold_item(item);
193 assert!(ret.len() == 1);
194 ret.into_iter().next().unwrap()
197 fn is_whitelisted_legacy_custom_derive(&self, name: Name) -> bool {
198 self.whitelisted_legacy_custom_derives.contains(&name)
201 fn visit_ast_fragment_with_placeholders(&mut self, mark: Mark, fragment: &AstFragment,
203 let invocation = self.invocations[&mark];
204 self.collect_def_ids(mark, invocation, fragment);
206 self.current_module = invocation.module.get();
207 self.current_module.unresolved_invocations.borrow_mut().remove(&mark);
208 self.current_module.unresolved_invocations.borrow_mut().extend(derives);
209 for &derive in derives {
210 self.invocations.insert(derive, invocation);
212 let mut visitor = BuildReducedGraphVisitor {
214 current_legacy_scope: invocation.parent_legacy_scope.get(),
217 fragment.visit_with(&mut visitor);
218 invocation.output_legacy_scope.set(visitor.current_legacy_scope);
221 fn add_builtin(&mut self, ident: ast::Ident, ext: Lrc<SyntaxExtension>) {
223 krate: CrateNum::BuiltinMacros,
224 index: DefIndex::from_array_index(self.macro_map.len(),
225 DefIndexAddressSpace::Low),
227 let kind = ext.kind();
228 self.macro_map.insert(def_id, ext);
229 let binding = self.arenas.alloc_name_binding(NameBinding {
230 kind: NameBindingKind::Def(Def::Macro(def_id, kind), false),
232 vis: ty::Visibility::Invisible,
233 expansion: Mark::root(),
235 if self.builtin_macros.insert(ident.name, binding).is_some() {
236 self.session.span_err(ident.span,
237 &format!("built-in macro `{}` was already defined", ident));
241 fn resolve_imports(&mut self) {
242 ImportResolver { resolver: self }.resolve_imports()
245 // Resolves attribute and derive legacy macros from `#![plugin(..)]`.
246 fn find_legacy_attr_invoc(&mut self, attrs: &mut Vec<ast::Attribute>, allow_derive: bool)
247 -> Option<ast::Attribute> {
248 for i in 0..attrs.len() {
249 let name = attrs[i].name();
251 if self.session.plugin_attributes.borrow().iter()
252 .any(|&(ref attr_nm, _)| name == &**attr_nm) {
253 attr::mark_known(&attrs[i]);
256 match self.builtin_macros.get(&name).cloned() {
257 Some(binding) => match *binding.get_macro(self) {
258 MultiModifier(..) | MultiDecorator(..) | SyntaxExtension::AttrProcMacro(..) => {
259 return Some(attrs.remove(i))
267 if !allow_derive { return None }
269 // Check for legacy derives
270 for i in 0..attrs.len() {
271 let name = attrs[i].name();
273 if name == "derive" {
274 let result = attrs[i].parse_list(&self.session.parse_sess, |parser| {
275 parser.parse_path_allowing_meta(PathStyle::Mod)
278 let mut traits = match result {
279 Ok(traits) => traits,
286 for j in 0..traits.len() {
287 if traits[j].segments.len() > 1 {
290 let trait_name = traits[j].segments[0].ident.name;
291 let legacy_name = Symbol::intern(&format!("derive_{}", trait_name));
292 if !self.builtin_macros.contains_key(&legacy_name) {
295 let span = traits.remove(j).span;
296 self.gate_legacy_custom_derive(legacy_name, span);
297 if traits.is_empty() {
300 let mut tokens = Vec::new();
301 for (j, path) in traits.iter().enumerate() {
303 tokens.push(TokenTree::Token(attrs[i].span, Token::Comma).into());
305 for (k, segment) in path.segments.iter().enumerate() {
307 tokens.push(TokenTree::Token(path.span, Token::ModSep).into());
309 let tok = Token::from_ast_ident(segment.ident);
310 tokens.push(TokenTree::Token(path.span, tok).into());
313 let delim_span = DelimSpan::from_single(attrs[i].span);
314 attrs[i].tokens = TokenTree::Delimited(delim_span, Delimited {
316 tts: TokenStream::concat(tokens).into(),
319 return Some(ast::Attribute {
320 path: ast::Path::from_ident(Ident::new(legacy_name, span)),
321 tokens: TokenStream::empty(),
322 id: attr::mk_attr_id(),
323 style: ast::AttrStyle::Outer,
324 is_sugared_doc: false,
334 fn resolve_macro_invocation(&mut self, invoc: &Invocation, invoc_id: Mark, force: bool)
335 -> Result<Option<Lrc<SyntaxExtension>>, Determinacy> {
336 let (path, kind, derives_in_scope, together_with) = match invoc.kind {
337 InvocationKind::Attr { attr: None, .. } =>
339 InvocationKind::Attr { attr: Some(ref attr), ref traits, together_with, .. } =>
340 (&attr.path, MacroKind::Attr, traits.clone(), together_with),
341 InvocationKind::Bang { ref mac, .. } =>
342 (&mac.node.path, MacroKind::Bang, Vec::new(), TogetherWith::None),
343 InvocationKind::Derive { ref path, .. } =>
344 (path, MacroKind::Derive, Vec::new(), TogetherWith::None),
347 let parent_scope = self.invoc_parent_scope(invoc_id, derives_in_scope);
348 let (def, ext) = self.resolve_macro_to_def(path, kind, &parent_scope, force)?;
350 if let Def::Macro(def_id, _) = def {
351 match together_with {
352 TogetherWith::Derive =>
353 self.session.span_err(invoc.span(),
354 "macro attributes must be placed before `#[derive]`"),
355 TogetherWith::TestBench if !self.session.features_untracked().plugin =>
356 self.session.span_err(invoc.span(),
357 "macro attributes cannot be used together with `#[test]` or `#[bench]`"),
360 self.macro_defs.insert(invoc.expansion_data.mark, def_id);
361 let normal_module_def_id =
362 self.macro_def_scope(invoc.expansion_data.mark).normal_ancestor_id;
363 self.definitions.add_parent_module_of_macro_def(invoc.expansion_data.mark,
364 normal_module_def_id);
365 invoc.expansion_data.mark.set_default_transparency(ext.default_transparency());
366 invoc.expansion_data.mark.set_is_builtin(def_id.krate == CrateNum::BuiltinMacros);
372 fn resolve_macro_path(&mut self, path: &ast::Path, kind: MacroKind, invoc_id: Mark,
373 derives_in_scope: Vec<ast::Path>, force: bool)
374 -> Result<Lrc<SyntaxExtension>, Determinacy> {
375 let parent_scope = self.invoc_parent_scope(invoc_id, derives_in_scope);
376 Ok(self.resolve_macro_to_def(path, kind, &parent_scope, force)?.1)
379 fn check_unused_macros(&self) {
380 for did in self.unused_macros.iter() {
381 let id_span = match *self.macro_map[did] {
382 SyntaxExtension::NormalTT { def_info, .. } |
383 SyntaxExtension::DeclMacro { def_info, .. } => def_info,
386 if let Some((id, span)) = id_span {
387 let lint = lint::builtin::UNUSED_MACROS;
388 let msg = "unused macro definition";
389 self.session.buffer_lint(lint, id, span, msg);
391 bug!("attempted to create unused macro error, but span not available");
397 impl<'a, 'cl> Resolver<'a, 'cl> {
398 pub fn dummy_parent_scope(&mut self) -> ParentScope<'a> {
399 self.invoc_parent_scope(Mark::root(), Vec::new())
402 fn invoc_parent_scope(&mut self, invoc_id: Mark, derives: Vec<ast::Path>) -> ParentScope<'a> {
403 let invoc = self.invocations[&invoc_id];
405 module: invoc.module.get().nearest_item_scope(),
406 expansion: invoc_id.parent(),
407 legacy: invoc.parent_legacy_scope.get(),
412 fn resolve_macro_to_def(
416 parent_scope: &ParentScope<'a>,
418 ) -> Result<(Def, Lrc<SyntaxExtension>), Determinacy> {
419 let def = self.resolve_macro_to_def_inner(path, kind, parent_scope, force);
421 // Report errors and enforce feature gates for the resolved macro.
422 if def != Err(Determinacy::Undetermined) {
423 // Do not report duplicated errors on every undetermined resolution.
424 for segment in &path.segments {
425 if let Some(args) = &segment.args {
426 self.session.span_err(args.span(), "generic arguments in macro path");
434 Def::Macro(def_id, macro_kind) => {
435 self.unused_macros.remove(&def_id);
436 if macro_kind == MacroKind::ProcMacroStub {
437 let msg = "can't use a procedural macro from the same crate that defines it";
438 self.session.span_err(path.span, msg);
439 return Err(Determinacy::Determined);
442 Def::NonMacroAttr(attr_kind) => {
443 if kind == MacroKind::Attr {
444 let features = self.session.features_untracked();
445 if attr_kind == NonMacroAttrKind::Custom {
446 assert!(path.segments.len() == 1);
447 let name = path.segments[0].ident.name.as_str();
448 if name.starts_with("rustc_") {
449 if !features.rustc_attrs {
450 let msg = "unless otherwise specified, attributes with the prefix \
451 `rustc_` are reserved for internal compiler diagnostics";
452 feature_err(&self.session.parse_sess, "rustc_attrs", path.span,
453 GateIssue::Language, &msg).emit();
455 } else if name.starts_with("derive_") {
456 if !features.custom_derive {
457 feature_err(&self.session.parse_sess, "custom_derive", path.span,
458 GateIssue::Language, EXPLAIN_DERIVE_UNDERSCORE).emit();
460 } else if !features.custom_attribute {
461 let msg = format!("The attribute `{}` is currently unknown to the \
462 compiler and may have meaning added to it in the \
464 feature_err(&self.session.parse_sess, "custom_attribute", path.span,
465 GateIssue::Language, &msg).emit();
469 // Not only attributes, but anything in macro namespace can result in
470 // `Def::NonMacroAttr` definition (e.g. `inline!()`), so we must report
471 // an error for those cases.
472 let msg = format!("expected a macro, found {}", def.kind_name());
473 self.session.span_err(path.span, &msg);
474 return Err(Determinacy::Determined);
477 _ => panic!("expected `Def::Macro` or `Def::NonMacroAttr`"),
480 Ok((def, self.get_macro(def)))
483 pub fn resolve_macro_to_def_inner(
487 parent_scope: &ParentScope<'a>,
489 ) -> Result<Def, Determinacy> {
490 let ast::Path { ref segments, span } = *path;
491 let mut path: Vec<_> = segments.iter().map(|seg| seg.ident).collect();
493 // Possibly apply the macro helper hack
494 if kind == MacroKind::Bang && path.len() == 1 &&
495 path[0].span.ctxt().outer().expn_info().map_or(false, |info| info.local_inner_macros) {
496 let root = Ident::new(keywords::DollarCrate.name(), path[0].span);
497 path.insert(0, root);
501 let def = match self.resolve_path_with_parent_scope(None, &path, Some(MacroNS),
502 parent_scope, false, span,
504 PathResult::NonModule(path_res) => match path_res.base_def() {
505 Def::Err => Err(Determinacy::Determined),
507 if path_res.unresolved_segments() > 0 {
508 self.found_unresolved_macro = true;
509 self.session.span_err(span, "fail to resolve non-ident macro path");
510 Err(Determinacy::Determined)
516 PathResult::Module(..) => unreachable!(),
517 PathResult::Indeterminate if !force => return Err(Determinacy::Undetermined),
519 self.found_unresolved_macro = true;
520 Err(Determinacy::Determined)
523 parent_scope.module.macro_resolutions.borrow_mut()
524 .push((path.into_boxed_slice(), span));
528 let result = if let Some(legacy_binding) = self.resolve_legacy_scope(path[0], Some(kind),
529 parent_scope, false) {
530 Ok(legacy_binding.def())
532 match self.resolve_lexical_macro_path_segment(path[0], MacroNS, Some(kind),
533 parent_scope, false, force, span) {
534 Ok((binding, _)) => Ok(binding.def_ignoring_ambiguity()),
535 Err(Determinacy::Undetermined) => return Err(Determinacy::Undetermined),
536 Err(Determinacy::Determined) => {
537 self.found_unresolved_macro = true;
538 Err(Determinacy::Determined)
543 parent_scope.module.legacy_macro_resolutions.borrow_mut()
544 .push((path[0], kind, parent_scope.clone(), result.ok()));
549 // Resolve the initial segment of a non-global macro path
550 // (e.g. `foo` in `foo::bar!(); or `foo!();`).
551 // This is a variation of `fn resolve_ident_in_lexical_scope` that can be run during
552 // expansion and import resolution (perhaps they can be merged in the future).
553 crate fn resolve_lexical_macro_path_segment(
557 kind: Option<MacroKind>,
558 parent_scope: &ParentScope<'a>,
562 ) -> Result<(&'a NameBinding<'a>, FromPrelude), Determinacy> {
563 // General principles:
564 // 1. Not controlled (user-defined) names should have higher priority than controlled names
565 // built into the language or standard library. This way we can add new names into the
566 // language or standard library without breaking user code.
567 // 2. "Closed set" below means new names can appear after the current resolution attempt.
568 // Places to search (in order of decreasing priority):
570 // 1. FIXME: Ribs (type parameters), there's no necessary infrastructure yet
571 // (open set, not controlled).
572 // 2. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
573 // (open, not controlled).
574 // 3. Extern prelude (closed, not controlled).
575 // 4. Tool modules (closed, controlled right now, but not in the future).
576 // 5. Standard library prelude (de-facto closed, controlled).
577 // 6. Language prelude (closed, controlled).
579 // 0. Derive helpers (open, not controlled). All ambiguities with other names
580 // are currently reported as errors. They should be higher in priority than preludes
581 // and probably even names in modules according to the "general principles" above. They
582 // also should be subject to restricted shadowing because are effectively produced by
583 // derives (you need to resolve the derive first to add helpers into scope), but they
584 // should be available before the derive is expanded for compatibility.
585 // It's mess in general, so we are being conservative for now.
586 // 1. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
587 // (open, not controlled).
588 // 2. `macro_use` prelude (open, the open part is from macro expansions, not controlled).
589 // 2a. User-defined prelude from macro-use
590 // (open, the open part is from macro expansions, not controlled).
591 // 2b. Standard library prelude is currently implemented as `macro-use` (closed, controlled)
592 // 3. Language prelude: builtin macros (closed, controlled, except for legacy plugins).
593 // 4. Language prelude: builtin attributes (closed, controlled).
595 assert!(ns == TypeNS || ns == MacroNS);
596 assert!(force || !record_used); // `record_used` implies `force`
597 ident = ident.modern();
599 // This is *the* result, resolution from the scope closest to the resolved identifier.
600 // However, sometimes this result is "weak" because it comes from a glob import or
601 // a macro expansion, and in this case it cannot shadow names from outer scopes, e.g.
602 // mod m { ... } // solution in outer scope
604 // use prefix::*; // imports another `m` - innermost solution
605 // // weak, cannot shadow the outer `m`, need to report ambiguity error
608 // So we have to save the innermost solution and continue searching in outer scopes
609 // to detect potential ambiguities.
610 let mut innermost_result: Option<(&NameBinding, FromPrelude)> = None;
612 enum WhereToResolve<'a> {
624 // Go through all the scopes and try to resolve the name.
625 let mut where_to_resolve = WhereToResolve::DeriveHelpers;
626 let mut use_prelude = !parent_scope.module.no_implicit_prelude;
628 let result = match where_to_resolve {
629 WhereToResolve::Module(module) => {
630 let orig_current_module = mem::replace(&mut self.current_module, module);
631 let binding = self.resolve_ident_in_module_unadjusted(
632 ModuleOrUniformRoot::Module(module),
639 self.current_module = orig_current_module;
640 binding.map(|binding| (binding, FromPrelude(false)))
642 WhereToResolve::MacroUsePrelude => {
643 match self.macro_use_prelude.get(&ident.name).cloned() {
644 Some(binding) => Ok((binding, FromPrelude(true))),
645 None => Err(Determinacy::Determined),
648 WhereToResolve::BuiltinMacros => {
649 match self.builtin_macros.get(&ident.name).cloned() {
650 Some(binding) => Ok((binding, FromPrelude(true))),
651 None => Err(Determinacy::Determined),
654 WhereToResolve::BuiltinAttrs => {
655 if is_builtin_attr_name(ident.name) {
656 let binding = (Def::NonMacroAttr(NonMacroAttrKind::Builtin),
657 ty::Visibility::Public, ident.span, Mark::root())
658 .to_name_binding(self.arenas);
659 Ok((binding, FromPrelude(true)))
661 Err(Determinacy::Determined)
664 WhereToResolve::DeriveHelpers => {
665 let mut result = Err(Determinacy::Determined);
666 for derive in &parent_scope.derives {
667 let parent_scope = ParentScope { derives: Vec::new(), ..*parent_scope };
668 if let Ok((_, ext)) = self.resolve_macro_to_def(derive, MacroKind::Derive,
669 &parent_scope, force) {
670 if let SyntaxExtension::ProcMacroDerive(_, helper_attrs, _) = &*ext {
671 if helper_attrs.contains(&ident.name) {
673 (Def::NonMacroAttr(NonMacroAttrKind::DeriveHelper),
674 ty::Visibility::Public, derive.span, Mark::root())
675 .to_name_binding(self.arenas);
676 result = Ok((binding, FromPrelude(false)));
684 WhereToResolve::ExternPrelude => {
685 if use_prelude && self.extern_prelude.contains(&ident.name) {
687 self.crate_loader.process_path_extern(ident.name, ident.span);
689 self.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX });
690 self.populate_module_if_necessary(crate_root);
692 let binding = (crate_root, ty::Visibility::Public,
693 ident.span, Mark::root()).to_name_binding(self.arenas);
694 Ok((binding, FromPrelude(true)))
696 Err(Determinacy::Determined)
699 WhereToResolve::ToolPrelude => {
700 if use_prelude && is_known_tool(ident.name) {
701 let binding = (Def::ToolMod, ty::Visibility::Public,
702 ident.span, Mark::root()).to_name_binding(self.arenas);
703 Ok((binding, FromPrelude(true)))
705 Err(Determinacy::Determined)
708 WhereToResolve::StdLibPrelude => {
709 let mut result = Err(Determinacy::Determined);
711 if let Some(prelude) = self.prelude {
712 if let Ok(binding) = self.resolve_ident_in_module_unadjusted(
713 ModuleOrUniformRoot::Module(prelude),
720 result = Ok((binding, FromPrelude(true)));
726 WhereToResolve::BuiltinTypes => {
727 if let Some(prim_ty) =
728 self.primitive_type_table.primitive_types.get(&ident.name).cloned() {
729 let binding = (Def::PrimTy(prim_ty), ty::Visibility::Public,
730 ident.span, Mark::root()).to_name_binding(self.arenas);
731 Ok((binding, FromPrelude(true)))
733 Err(Determinacy::Determined)
738 macro_rules! continue_search { () => {
739 where_to_resolve = match where_to_resolve {
740 WhereToResolve::Module(module) => {
741 match self.hygienic_lexical_parent(module, &mut ident.span) {
742 Some(parent_module) => WhereToResolve::Module(parent_module),
744 use_prelude = !module.no_implicit_prelude;
746 WhereToResolve::MacroUsePrelude
748 WhereToResolve::ExternPrelude
753 WhereToResolve::MacroUsePrelude => WhereToResolve::BuiltinMacros,
754 WhereToResolve::BuiltinMacros => WhereToResolve::BuiltinAttrs,
755 WhereToResolve::BuiltinAttrs => break, // nowhere else to search
756 WhereToResolve::DeriveHelpers => WhereToResolve::Module(parent_scope.module),
757 WhereToResolve::ExternPrelude => WhereToResolve::ToolPrelude,
758 WhereToResolve::ToolPrelude => WhereToResolve::StdLibPrelude,
759 WhereToResolve::StdLibPrelude => WhereToResolve::BuiltinTypes,
760 WhereToResolve::BuiltinTypes => break, // nowhere else to search
768 if sub_namespace_mismatch(kind, result.0.macro_kind()) {
776 if let Some(innermost_result) = innermost_result {
777 // Found another solution, if the first one was "weak", report an error.
778 let (def, innermost_def) = (result.0.def(), innermost_result.0.def());
779 if def != innermost_def &&
780 (innermost_result.0.is_glob_import() ||
781 innermost_result.0.may_appear_after(parent_scope.expansion, result.0) ||
782 innermost_def == Def::NonMacroAttr(NonMacroAttrKind::DeriveHelper) ||
783 def == Def::NonMacroAttr(NonMacroAttrKind::DeriveHelper)) {
784 self.ambiguity_errors.push(AmbiguityError {
786 b1: innermost_result.0,
789 return Ok(innermost_result);
792 // Found the first solution.
793 innermost_result = Some(result);
798 Err(Determinacy::Determined) => {
801 Err(Determinacy::Undetermined) => return Err(Determinacy::determined(force)),
805 // The first found solution was the only one, return it.
806 if let Some(innermost_result) = innermost_result {
807 return Ok(innermost_result);
810 let determinacy = Determinacy::determined(force);
811 if determinacy == Determinacy::Determined && kind == Some(MacroKind::Attr) {
812 // For single-segment attributes interpret determinate "no resolution" as a custom
813 // attribute. (Lexical resolution implies the first segment and attr kind should imply
814 // the last segment, so we are certainly working with a single-segment attribute here.)
815 assert!(ns == MacroNS);
816 let binding = (Def::NonMacroAttr(NonMacroAttrKind::Custom),
817 ty::Visibility::Public, ident.span, Mark::root())
818 .to_name_binding(self.arenas);
819 Ok((binding, FromPrelude(true)))
825 fn resolve_legacy_scope(
828 kind: Option<MacroKind>,
829 parent_scope: &ParentScope<'a>,
831 ) -> Option<&'a NameBinding<'a>> {
832 if sub_namespace_mismatch(kind, Some(MacroKind::Bang)) {
836 let ident = ident.modern();
838 // This is *the* result, resolution from the scope closest to the resolved identifier.
839 // However, sometimes this result is "weak" because it comes from a macro expansion,
840 // and in this case it cannot shadow names from outer scopes, e.g.
841 // macro_rules! m { ... } // solution in outer scope
843 // define_m!(); // generates another `macro_rules! m` - innermost solution
844 // // weak, cannot shadow the outer `m`, need to report ambiguity error
847 // So we have to save the innermost solution and continue searching in outer scopes
848 // to detect potential ambiguities.
849 let mut innermost_result: Option<&NameBinding> = None;
851 // Go through all the scopes and try to resolve the name.
852 let mut where_to_resolve = parent_scope.legacy;
854 let result = match where_to_resolve {
855 LegacyScope::Binding(legacy_binding) if ident == legacy_binding.ident =>
856 Some(legacy_binding.binding),
860 macro_rules! continue_search { () => {
861 where_to_resolve = match where_to_resolve {
862 LegacyScope::Empty => break, // nowhere else to search
863 LegacyScope::Binding(binding) => binding.parent_legacy_scope,
864 LegacyScope::Invocation(invocation) => invocation.output_legacy_scope.get(),
865 LegacyScope::Uninitialized => unreachable!(),
877 if let Some(innermost_result) = innermost_result {
878 // Found another solution, if the first one was "weak", report an error.
879 if result.def() != innermost_result.def() &&
880 innermost_result.may_appear_after(parent_scope.expansion, result) {
881 self.ambiguity_errors.push(AmbiguityError {
883 b1: innermost_result,
886 return Some(innermost_result);
889 // Found the first solution.
890 innermost_result = Some(result);
901 // The first found solution was the only one (or there was no solution at all), return it.
905 pub fn finalize_current_module_macro_resolutions(&mut self) {
906 let module = self.current_module;
907 for &(ref path, span) in module.macro_resolutions.borrow().iter() {
908 match self.resolve_path(None, &path, Some(MacroNS), true, span, CrateLint::No) {
909 PathResult::NonModule(_) => {},
910 PathResult::Failed(span, msg, _) => {
911 resolve_error(self, span, ResolutionError::FailedToResolve(&msg));
917 let legacy_macro_resolutions =
918 mem::replace(&mut *module.legacy_macro_resolutions.borrow_mut(), Vec::new());
919 for (ident, kind, parent_scope, def) in legacy_macro_resolutions {
920 let span = ident.span;
921 let legacy_resolution = self.resolve_legacy_scope(
922 ident, Some(kind), &parent_scope, true
924 let resolution = self.resolve_lexical_macro_path_segment(
925 ident, MacroNS, Some(kind), &parent_scope, true, true, span
928 let check_consistency = |this: &Self, new_def: Def| {
929 if let Some(def) = def {
930 if this.ambiguity_errors.is_empty() && new_def != def && new_def != Def::Err {
931 // Make sure compilation does not succeed if preferred macro resolution
932 // has changed after the macro had been expanded. In theory all such
933 // situations should be reported as ambiguity errors, so this is span-bug.
934 span_bug!(span, "inconsistent resolution for a macro");
937 // It's possible that the macro was unresolved (indeterminate) and silently
938 // expanded into a dummy fragment for recovery during expansion.
939 // Now, post-expansion, the resolution may succeed, but we can't change the
940 // past and need to report an error.
942 format!("cannot determine resolution for the {} `{}`", kind.descr(), ident);
943 let msg_note = "import resolution is stuck, try simplifying macro imports";
944 this.session.struct_span_err(span, &msg).note(msg_note).emit();
948 match (legacy_resolution, resolution) {
950 assert!(def.is_none());
951 let bang = if kind == MacroKind::Bang { "!" } else { "" };
953 format!("cannot find {} `{}{}` in this scope", kind.descr(), ident, bang);
954 let mut err = self.session.struct_span_err(span, &msg);
955 self.suggest_macro_name(&ident.as_str(), kind, &mut err, span);
958 (Some(legacy_binding), Ok((binding, FromPrelude(from_prelude))))
959 if legacy_binding.def() != binding.def_ignoring_ambiguity() &&
961 legacy_binding.may_appear_after(parent_scope.expansion, binding)) => {
962 self.report_ambiguity_error(ident, legacy_binding, binding);
964 // OK, non-macro-expanded legacy wins over prelude even if defs are different
965 // Also, legacy and modern can co-exist if their defs are same
966 (Some(legacy_binding), Ok(_)) |
967 // OK, unambiguous resolution
968 (Some(legacy_binding), Err(_)) => {
969 check_consistency(self, legacy_binding.def());
971 // OK, unambiguous resolution
972 (None, Ok((binding, FromPrelude(from_prelude)))) => {
973 check_consistency(self, binding.def_ignoring_ambiguity());
975 self.record_use(ident, MacroNS, binding);
981 let builtin_attrs = mem::replace(&mut *module.builtin_attrs.borrow_mut(), Vec::new());
982 for (ident, parent_scope) in builtin_attrs {
983 let resolve_legacy = |this: &mut Self| this.resolve_legacy_scope(
984 ident, Some(MacroKind::Attr), &parent_scope, true
986 let resolve_modern = |this: &mut Self| this.resolve_lexical_macro_path_segment(
987 ident, MacroNS, Some(MacroKind::Attr), &parent_scope, true, true, ident.span
988 ).map(|(binding, _)| binding).ok();
990 if let Some(binding) = resolve_legacy(self).or_else(|| resolve_modern(self)) {
991 if binding.def_ignoring_ambiguity() !=
992 Def::NonMacroAttr(NonMacroAttrKind::Builtin) {
993 let builtin_binding = (Def::NonMacroAttr(NonMacroAttrKind::Builtin),
994 ty::Visibility::Public, ident.span, Mark::root())
995 .to_name_binding(self.arenas);
996 self.report_ambiguity_error(ident, binding, builtin_binding);
1002 fn suggest_macro_name(&mut self, name: &str, kind: MacroKind,
1003 err: &mut DiagnosticBuilder<'a>, span: Span) {
1004 // First check if this is a locally-defined bang macro.
1005 let suggestion = if let MacroKind::Bang = kind {
1006 find_best_match_for_name(self.macro_names.iter().map(|ident| &ident.name), name, None)
1009 // Then check global macros.
1011 let names = self.builtin_macros.iter().chain(self.macro_use_prelude.iter())
1012 .filter_map(|(name, binding)| {
1013 if binding.macro_kind() == Some(kind) { Some(name) } else { None }
1015 find_best_match_for_name(names, name, None)
1016 // Then check modules.
1018 let is_macro = |def| {
1019 if let Def::Macro(_, def_kind) = def {
1025 let ident = Ident::new(Symbol::intern(name), span);
1026 self.lookup_typo_candidate(&[ident], MacroNS, is_macro, span)
1029 if let Some(suggestion) = suggestion {
1030 if suggestion != name {
1031 if let MacroKind::Bang = kind {
1032 err.span_suggestion_with_applicability(
1034 "you could try the macro",
1035 suggestion.to_string(),
1036 Applicability::MaybeIncorrect
1039 err.span_suggestion_with_applicability(
1042 suggestion.to_string(),
1043 Applicability::MaybeIncorrect
1047 err.help("have you added the `#[macro_use]` on the module/import?");
1052 fn collect_def_ids(&mut self,
1054 invocation: &'a InvocationData<'a>,
1055 fragment: &AstFragment) {
1056 let Resolver { ref mut invocations, arenas, graph_root, .. } = *self;
1057 let InvocationData { def_index, .. } = *invocation;
1059 let visit_macro_invoc = &mut |invoc: map::MacroInvocationData| {
1060 invocations.entry(invoc.mark).or_insert_with(|| {
1061 arenas.alloc_invocation_data(InvocationData {
1062 def_index: invoc.def_index,
1063 module: Cell::new(graph_root),
1064 parent_legacy_scope: Cell::new(LegacyScope::Uninitialized),
1065 output_legacy_scope: Cell::new(LegacyScope::Uninitialized),
1070 let mut def_collector = DefCollector::new(&mut self.definitions, mark);
1071 def_collector.visit_macro_invoc = Some(visit_macro_invoc);
1072 def_collector.with_parent(def_index, |def_collector| {
1073 fragment.visit_with(def_collector)
1077 pub fn define_macro(&mut self,
1080 current_legacy_scope: &mut LegacyScope<'a>) {
1081 self.local_macro_def_scopes.insert(item.id, self.current_module);
1082 let ident = item.ident;
1083 if ident.name == "macro_rules" {
1084 self.session.span_err(item.span, "user-defined macros may not be named `macro_rules`");
1087 let def_id = self.definitions.local_def_id(item.id);
1088 let ext = Lrc::new(macro_rules::compile(&self.session.parse_sess,
1089 &self.session.features_untracked(),
1090 item, hygiene::default_edition()));
1091 self.macro_map.insert(def_id, ext);
1093 let def = match item.node { ast::ItemKind::MacroDef(ref def) => def, _ => unreachable!() };
1095 let ident = ident.modern();
1096 self.macro_names.insert(ident);
1097 let def = Def::Macro(def_id, MacroKind::Bang);
1098 let vis = ty::Visibility::Invisible; // Doesn't matter for legacy bindings
1099 let binding = (def, vis, item.span, expansion).to_name_binding(self.arenas);
1100 let legacy_binding = self.arenas.alloc_legacy_binding(LegacyBinding {
1101 parent_legacy_scope: *current_legacy_scope, binding, ident
1103 *current_legacy_scope = LegacyScope::Binding(legacy_binding);
1104 self.all_macros.insert(ident.name, def);
1105 if attr::contains_name(&item.attrs, "macro_export") {
1106 let module = self.graph_root;
1107 let vis = ty::Visibility::Public;
1108 self.define(module, ident, MacroNS,
1109 (def, vis, item.span, expansion, IsMacroExport));
1111 if !attr::contains_name(&item.attrs, "rustc_doc_only_macro") {
1112 self.check_reserved_macro_name(ident, MacroNS);
1114 self.unused_macros.insert(def_id);
1117 let module = self.current_module;
1118 let def = Def::Macro(def_id, MacroKind::Bang);
1119 let vis = self.resolve_visibility(&item.vis);
1120 if vis != ty::Visibility::Public {
1121 self.unused_macros.insert(def_id);
1123 self.define(module, ident, MacroNS, (def, vis, item.span, expansion));
1127 fn gate_legacy_custom_derive(&mut self, name: Symbol, span: Span) {
1128 if !self.session.features_untracked().custom_derive {
1129 let sess = &self.session.parse_sess;
1130 let explain = feature_gate::EXPLAIN_CUSTOM_DERIVE;
1131 emit_feature_err(sess, "custom_derive", span, GateIssue::Language, explain);
1132 } else if !self.is_whitelisted_legacy_custom_derive(name) {
1133 self.session.span_warn(span, feature_gate::EXPLAIN_DEPR_CUSTOM_DERIVE);