]> git.lizzy.rs Git - rust.git/blob - src/librustc_resolve/macros.rs
Rollup merge of #53545 - FelixMcFelix:fix-50865-beta, r=petrochenkov
[rust.git] / src / librustc_resolve / macros.rs
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.
4 //
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.
10
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, BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, DefIndex,
18                          DefIndexAddressSpace};
19 use rustc::hir::def::{Def, NonMacroAttrKind};
20 use rustc::hir::map::{self, DefCollector};
21 use rustc::{ty, lint};
22 use rustc::middle::cstore::CrateStore;
23 use syntax::ast::{self, Name, Ident};
24 use syntax::attr;
25 use syntax::errors::DiagnosticBuilder;
26 use syntax::ext::base::{self, Determinacy, MultiModifier, MultiDecorator};
27 use syntax::ext::base::{MacroKind, SyntaxExtension, Resolver as SyntaxResolver};
28 use syntax::ext::expand::{AstFragment, Invocation, InvocationKind};
29 use syntax::ext::hygiene::{self, Mark};
30 use syntax::ext::tt::macro_rules;
31 use syntax::feature_gate::{self, feature_err, emit_feature_err, is_builtin_attr_name, GateIssue};
32 use syntax::feature_gate::EXPLAIN_DERIVE_UNDERSCORE;
33 use syntax::fold::{self, Folder};
34 use syntax::parse::parser::PathStyle;
35 use syntax::parse::token::{self, Token};
36 use syntax::ptr::P;
37 use syntax::symbol::{Symbol, keywords};
38 use syntax::tokenstream::{TokenStream, TokenTree, Delimited};
39 use syntax::util::lev_distance::find_best_match_for_name;
40 use syntax_pos::{Span, DUMMY_SP};
41 use errors::Applicability;
42
43 use std::cell::Cell;
44 use std::mem;
45 use rustc_data_structures::sync::Lrc;
46 use rustc_data_structures::small_vec::ExpectOne;
47
48 crate struct FromPrelude(bool);
49 crate struct FromExpansion(bool);
50
51 #[derive(Clone)]
52 pub struct InvocationData<'a> {
53     pub module: Cell<Module<'a>>,
54     pub def_index: DefIndex,
55     // The scope in which the invocation path is resolved.
56     pub legacy_scope: Cell<LegacyScope<'a>>,
57     // The smallest scope that includes this invocation's expansion,
58     // or `Empty` if this invocation has not been expanded yet.
59     pub expansion: Cell<LegacyScope<'a>>,
60 }
61
62 impl<'a> InvocationData<'a> {
63     pub fn root(graph_root: Module<'a>) -> Self {
64         InvocationData {
65             module: Cell::new(graph_root),
66             def_index: CRATE_DEF_INDEX,
67             legacy_scope: Cell::new(LegacyScope::Empty),
68             expansion: Cell::new(LegacyScope::Empty),
69         }
70     }
71 }
72
73 #[derive(Copy, Clone)]
74 pub enum LegacyScope<'a> {
75     Empty,
76     Invocation(&'a InvocationData<'a>), // The scope of the invocation, not including its expansion
77     Expansion(&'a InvocationData<'a>), // The scope of the invocation, including its expansion
78     Binding(&'a LegacyBinding<'a>),
79 }
80
81 pub struct LegacyBinding<'a> {
82     pub parent: Cell<LegacyScope<'a>>,
83     pub ident: Ident,
84     def_id: DefId,
85     pub span: Span,
86 }
87
88 impl<'a> LegacyBinding<'a> {
89     fn def(&self) -> Def {
90         Def::Macro(self.def_id, MacroKind::Bang)
91     }
92 }
93
94 pub struct ProcMacError {
95     crate_name: Symbol,
96     name: Symbol,
97     module: ast::NodeId,
98     use_span: Span,
99     warn_msg: &'static str,
100 }
101
102 impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
103     fn next_node_id(&mut self) -> ast::NodeId {
104         self.session.next_node_id()
105     }
106
107     fn get_module_scope(&mut self, id: ast::NodeId) -> Mark {
108         let mark = Mark::fresh(Mark::root());
109         let module = self.module_map[&self.definitions.local_def_id(id)];
110         self.invocations.insert(mark, self.arenas.alloc_invocation_data(InvocationData {
111             module: Cell::new(module),
112             def_index: module.def_id().unwrap().index,
113             legacy_scope: Cell::new(LegacyScope::Empty),
114             expansion: Cell::new(LegacyScope::Empty),
115         }));
116         mark
117     }
118
119     fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item> {
120         struct EliminateCrateVar<'b, 'a: 'b, 'crateloader: 'a>(
121             &'b mut Resolver<'a, 'crateloader>, Span
122         );
123
124         impl<'a, 'b, 'crateloader> Folder for EliminateCrateVar<'a, 'b, 'crateloader> {
125             fn fold_path(&mut self, path: ast::Path) -> ast::Path {
126                 match self.fold_qpath(None, path) {
127                     (None, path) => path,
128                     _ => unreachable!(),
129                 }
130             }
131
132             fn fold_qpath(&mut self, mut qself: Option<ast::QSelf>, mut path: ast::Path)
133                           -> (Option<ast::QSelf>, ast::Path) {
134                 qself = qself.map(|ast::QSelf { ty, path_span, position }| {
135                     ast::QSelf {
136                         ty: self.fold_ty(ty),
137                         path_span: self.new_span(path_span),
138                         position,
139                     }
140                 });
141
142                 if path.segments[0].ident.name == keywords::DollarCrate.name() {
143                     let module = self.0.resolve_crate_root(path.segments[0].ident);
144                     path.segments[0].ident.name = keywords::CrateRoot.name();
145                     if !module.is_local() {
146                         let span = path.segments[0].ident.span;
147                         path.segments.insert(1, match module.kind {
148                             ModuleKind::Def(_, name) => ast::PathSegment::from_ident(
149                                 ast::Ident::with_empty_ctxt(name).with_span_pos(span)
150                             ),
151                             _ => unreachable!(),
152                         });
153                         if let Some(qself) = &mut qself {
154                             qself.position += 1;
155                         }
156                     }
157                 }
158                 (qself, path)
159             }
160
161             fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
162                 fold::noop_fold_mac(mac, self)
163             }
164         }
165
166         EliminateCrateVar(self, item.span).fold_item(item).expect_one("")
167     }
168
169     fn is_whitelisted_legacy_custom_derive(&self, name: Name) -> bool {
170         self.whitelisted_legacy_custom_derives.contains(&name)
171     }
172
173     fn visit_ast_fragment_with_placeholders(&mut self, mark: Mark, fragment: &AstFragment,
174                                             derives: &[Mark]) {
175         let invocation = self.invocations[&mark];
176         self.collect_def_ids(mark, invocation, fragment);
177
178         self.current_module = invocation.module.get();
179         self.current_module.unresolved_invocations.borrow_mut().remove(&mark);
180         self.current_module.unresolved_invocations.borrow_mut().extend(derives);
181         for &derive in derives {
182             self.invocations.insert(derive, invocation);
183         }
184         let mut visitor = BuildReducedGraphVisitor {
185             resolver: self,
186             legacy_scope: LegacyScope::Invocation(invocation),
187             expansion: mark,
188         };
189         fragment.visit_with(&mut visitor);
190         invocation.expansion.set(visitor.legacy_scope);
191     }
192
193     fn add_builtin(&mut self, ident: ast::Ident, ext: Lrc<SyntaxExtension>) {
194         let def_id = DefId {
195             krate: BUILTIN_MACROS_CRATE,
196             index: DefIndex::from_array_index(self.macro_map.len(),
197                                               DefIndexAddressSpace::Low),
198         };
199         let kind = ext.kind();
200         self.macro_map.insert(def_id, ext);
201         let binding = self.arenas.alloc_name_binding(NameBinding {
202             kind: NameBindingKind::Def(Def::Macro(def_id, kind), false),
203             span: DUMMY_SP,
204             vis: ty::Visibility::Invisible,
205             expansion: Mark::root(),
206         });
207         self.macro_prelude.insert(ident.name, binding);
208     }
209
210     fn resolve_imports(&mut self) {
211         ImportResolver { resolver: self }.resolve_imports()
212     }
213
214     // Resolves attribute and derive legacy macros from `#![plugin(..)]`.
215     fn find_legacy_attr_invoc(&mut self, attrs: &mut Vec<ast::Attribute>, allow_derive: bool)
216                               -> Option<ast::Attribute> {
217         for i in 0..attrs.len() {
218             let name = attrs[i].name();
219
220             if self.session.plugin_attributes.borrow().iter()
221                     .any(|&(ref attr_nm, _)| name == &**attr_nm) {
222                 attr::mark_known(&attrs[i]);
223             }
224
225             match self.macro_prelude.get(&name).cloned() {
226                 Some(binding) => match *binding.get_macro(self) {
227                     MultiModifier(..) | MultiDecorator(..) | SyntaxExtension::AttrProcMacro(..) => {
228                         return Some(attrs.remove(i))
229                     }
230                     _ => {}
231                 },
232                 None => {}
233             }
234         }
235
236         if !allow_derive { return None }
237
238         // Check for legacy derives
239         for i in 0..attrs.len() {
240             let name = attrs[i].name();
241
242             if name == "derive" {
243                 let result = attrs[i].parse_list(&self.session.parse_sess, |parser| {
244                     parser.parse_path_allowing_meta(PathStyle::Mod)
245                 });
246
247                 let mut traits = match result {
248                     Ok(traits) => traits,
249                     Err(mut e) => {
250                         e.cancel();
251                         continue
252                     }
253                 };
254
255                 for j in 0..traits.len() {
256                     if traits[j].segments.len() > 1 {
257                         continue
258                     }
259                     let trait_name = traits[j].segments[0].ident.name;
260                     let legacy_name = Symbol::intern(&format!("derive_{}", trait_name));
261                     if !self.macro_prelude.contains_key(&legacy_name) {
262                         continue
263                     }
264                     let span = traits.remove(j).span;
265                     self.gate_legacy_custom_derive(legacy_name, span);
266                     if traits.is_empty() {
267                         attrs.remove(i);
268                     } else {
269                         let mut tokens = Vec::new();
270                         for (j, path) in traits.iter().enumerate() {
271                             if j > 0 {
272                                 tokens.push(TokenTree::Token(attrs[i].span, Token::Comma).into());
273                             }
274                             for (k, segment) in path.segments.iter().enumerate() {
275                                 if k > 0 {
276                                     tokens.push(TokenTree::Token(path.span, Token::ModSep).into());
277                                 }
278                                 let tok = Token::from_ast_ident(segment.ident);
279                                 tokens.push(TokenTree::Token(path.span, tok).into());
280                             }
281                         }
282                         attrs[i].tokens = TokenTree::Delimited(attrs[i].span, Delimited {
283                             delim: token::Paren,
284                             tts: TokenStream::concat(tokens).into(),
285                         }).into();
286                     }
287                     return Some(ast::Attribute {
288                         path: ast::Path::from_ident(Ident::new(legacy_name, span)),
289                         tokens: TokenStream::empty(),
290                         id: attr::mk_attr_id(),
291                         style: ast::AttrStyle::Outer,
292                         is_sugared_doc: false,
293                         span,
294                     });
295                 }
296             }
297         }
298
299         None
300     }
301
302     fn resolve_macro_invocation(&mut self, invoc: &Invocation, scope: Mark, force: bool)
303                                 -> Result<Option<Lrc<SyntaxExtension>>, Determinacy> {
304         let (path, kind, derives_in_scope) = match invoc.kind {
305             InvocationKind::Attr { attr: None, .. } =>
306                 return Ok(None),
307             InvocationKind::Attr { attr: Some(ref attr), ref traits, .. } =>
308                 (&attr.path, MacroKind::Attr, &traits[..]),
309             InvocationKind::Bang { ref mac, .. } =>
310                 (&mac.node.path, MacroKind::Bang, &[][..]),
311             InvocationKind::Derive { ref path, .. } =>
312                 (path, MacroKind::Derive, &[][..]),
313         };
314
315         let (def, ext) = self.resolve_macro_to_def(path, kind, scope, derives_in_scope, force)?;
316
317         if let Def::Macro(def_id, _) = def {
318             self.macro_defs.insert(invoc.expansion_data.mark, def_id);
319             let normal_module_def_id =
320                 self.macro_def_scope(invoc.expansion_data.mark).normal_ancestor_id;
321             self.definitions.add_parent_module_of_macro_def(invoc.expansion_data.mark,
322                                                             normal_module_def_id);
323             invoc.expansion_data.mark.set_default_transparency(ext.default_transparency());
324             invoc.expansion_data.mark.set_is_builtin(def_id.krate == BUILTIN_MACROS_CRATE);
325         }
326
327         Ok(Some(ext))
328     }
329
330     fn resolve_macro_path(&mut self, path: &ast::Path, kind: MacroKind, scope: Mark,
331                           derives_in_scope: &[ast::Path], force: bool)
332                           -> Result<Lrc<SyntaxExtension>, Determinacy> {
333         Ok(self.resolve_macro_to_def(path, kind, scope, derives_in_scope, force)?.1)
334     }
335
336     fn check_unused_macros(&self) {
337         for did in self.unused_macros.iter() {
338             let id_span = match *self.macro_map[did] {
339                 SyntaxExtension::NormalTT { def_info, .. } |
340                 SyntaxExtension::DeclMacro { def_info, .. } => def_info,
341                 _ => None,
342             };
343             if let Some((id, span)) = id_span {
344                 let lint = lint::builtin::UNUSED_MACROS;
345                 let msg = "unused macro definition";
346                 self.session.buffer_lint(lint, id, span, msg);
347             } else {
348                 bug!("attempted to create unused macro error, but span not available");
349             }
350         }
351     }
352 }
353
354 impl<'a, 'cl> Resolver<'a, 'cl> {
355     fn resolve_macro_to_def(&mut self, path: &ast::Path, kind: MacroKind, scope: Mark,
356                             derives_in_scope: &[ast::Path], force: bool)
357                             -> Result<(Def, Lrc<SyntaxExtension>), Determinacy> {
358         let def = self.resolve_macro_to_def_inner(path, kind, scope, derives_in_scope, force);
359
360         // Report errors and enforce feature gates for the resolved macro.
361         if def != Err(Determinacy::Undetermined) {
362             // Do not report duplicated errors on every undetermined resolution.
363             for segment in &path.segments {
364                 if let Some(args) = &segment.args {
365                     self.session.span_err(args.span(), "generic arguments in macro path");
366                 }
367             }
368         }
369
370         let def = def?;
371
372         match def {
373             Def::Macro(def_id, macro_kind) => {
374                 self.unused_macros.remove(&def_id);
375                 if macro_kind == MacroKind::ProcMacroStub {
376                     let msg = "can't use a procedural macro from the same crate that defines it";
377                     self.session.span_err(path.span, msg);
378                     return Err(Determinacy::Determined);
379                 }
380             }
381             Def::NonMacroAttr(attr_kind) => {
382                 if kind == MacroKind::Attr {
383                     let features = self.session.features_untracked();
384                     if attr_kind == NonMacroAttrKind::Custom {
385                         assert!(path.segments.len() == 1);
386                         let name = path.segments[0].ident.name.as_str();
387                         if name.starts_with("rustc_") {
388                             if !features.rustc_attrs {
389                                 let msg = "unless otherwise specified, attributes with the prefix \
390                                            `rustc_` are reserved for internal compiler diagnostics";
391                                 feature_err(&self.session.parse_sess, "rustc_attrs", path.span,
392                                             GateIssue::Language, &msg).emit();
393                             }
394                         } else if name.starts_with("derive_") {
395                             if !features.custom_derive {
396                                 feature_err(&self.session.parse_sess, "custom_derive", path.span,
397                                             GateIssue::Language, EXPLAIN_DERIVE_UNDERSCORE).emit();
398                             }
399                         } else if !features.custom_attribute {
400                             let msg = format!("The attribute `{}` is currently unknown to the \
401                                                compiler and may have meaning added to it in the \
402                                                future", path);
403                             feature_err(&self.session.parse_sess, "custom_attribute", path.span,
404                                         GateIssue::Language, &msg).emit();
405                         }
406                     }
407                 } else {
408                     // Not only attributes, but anything in macro namespace can result in
409                     // `Def::NonMacroAttr` definition (e.g. `inline!()`), so we must report
410                     // an error for those cases.
411                     let msg = format!("expected a macro, found {}", def.kind_name());
412                     self.session.span_err(path.span, &msg);
413                     return Err(Determinacy::Determined);
414                 }
415             }
416             _ => panic!("expected `Def::Macro` or `Def::NonMacroAttr`"),
417         }
418
419         Ok((def, self.get_macro(def)))
420     }
421
422     pub fn resolve_macro_to_def_inner(&mut self, path: &ast::Path, kind: MacroKind, scope: Mark,
423                                       derives_in_scope: &[ast::Path], force: bool)
424                                       -> Result<Def, Determinacy> {
425         let ast::Path { ref segments, span } = *path;
426         let mut path: Vec<_> = segments.iter().map(|seg| seg.ident).collect();
427         let invocation = self.invocations[&scope];
428         let module = invocation.module.get();
429         self.current_module = if module.is_trait() { module.parent.unwrap() } else { module };
430
431         // Possibly apply the macro helper hack
432         if kind == MacroKind::Bang && path.len() == 1 &&
433            path[0].span.ctxt().outer().expn_info().map_or(false, |info| info.local_inner_macros) {
434             let root = Ident::new(keywords::DollarCrate.name(), path[0].span);
435             path.insert(0, root);
436         }
437
438         if path.len() > 1 {
439             let res = self.resolve_path(None, &path, Some(MacroNS), false, span, CrateLint::No);
440             let def = match res {
441                 PathResult::NonModule(path_res) => match path_res.base_def() {
442                     Def::Err => Err(Determinacy::Determined),
443                     def @ _ => {
444                         if path_res.unresolved_segments() > 0 {
445                             self.found_unresolved_macro = true;
446                             self.session.span_err(span, "fail to resolve non-ident macro path");
447                             Err(Determinacy::Determined)
448                         } else {
449                             Ok(def)
450                         }
451                     }
452                 },
453                 PathResult::Module(..) => unreachable!(),
454                 PathResult::Indeterminate if !force => return Err(Determinacy::Undetermined),
455                 _ => {
456                     self.found_unresolved_macro = true;
457                     Err(Determinacy::Determined)
458                 },
459             };
460             self.current_module.nearest_item_scope().macro_resolutions.borrow_mut()
461                 .push((path.into_boxed_slice(), span));
462             return def;
463         }
464
465         let legacy_resolution = self.resolve_legacy_scope(&invocation.legacy_scope, path[0], false);
466         let result = if let Some((legacy_binding, _)) = legacy_resolution {
467             Ok(legacy_binding.def())
468         } else {
469             match self.resolve_lexical_macro_path_segment(path[0], MacroNS, false, force,
470                                                           kind == MacroKind::Attr, span) {
471                 Ok((binding, _)) => Ok(binding.def_ignoring_ambiguity()),
472                 Err(Determinacy::Undetermined) => return Err(Determinacy::Undetermined),
473                 Err(Determinacy::Determined) => {
474                     self.found_unresolved_macro = true;
475                     Err(Determinacy::Determined)
476                 }
477             }
478         };
479
480         self.current_module.nearest_item_scope().legacy_macro_resolutions.borrow_mut()
481             .push((scope, path[0], kind, result.ok()));
482
483         if let Ok(Def::NonMacroAttr(NonMacroAttrKind::Custom)) = result {} else {
484             return result;
485         }
486
487         // At this point we've found that the `attr` is determinately unresolved and thus can be
488         // interpreted as a custom attribute. Normally custom attributes are feature gated, but
489         // it may be a custom attribute whitelisted by a derive macro and they do not require
490         // a feature gate.
491         //
492         // So here we look through all of the derive annotations in scope and try to resolve them.
493         // If they themselves successfully resolve *and* one of the resolved derive macros
494         // whitelists this attribute's name, then this is a registered attribute and we can convert
495         // it from a "generic custom attrite" into a "known derive helper attribute".
496         assert!(kind == MacroKind::Attr);
497         enum ConvertToDeriveHelper { Yes, No, DontKnow }
498         let mut convert_to_derive_helper = ConvertToDeriveHelper::No;
499         for derive in derives_in_scope {
500             match self.resolve_macro_path(derive, MacroKind::Derive, scope, &[], force) {
501                 Ok(ext) => if let SyntaxExtension::ProcMacroDerive(_, ref inert_attrs, _) = *ext {
502                     if inert_attrs.contains(&path[0].name) {
503                         convert_to_derive_helper = ConvertToDeriveHelper::Yes;
504                         break
505                     }
506                 },
507                 Err(Determinacy::Undetermined) =>
508                     convert_to_derive_helper = ConvertToDeriveHelper::DontKnow,
509                 Err(Determinacy::Determined) => {}
510             }
511         }
512
513         match convert_to_derive_helper {
514             ConvertToDeriveHelper::Yes => Ok(Def::NonMacroAttr(NonMacroAttrKind::DeriveHelper)),
515             ConvertToDeriveHelper::No => result,
516             ConvertToDeriveHelper::DontKnow => Err(Determinacy::determined(force)),
517         }
518     }
519
520     // Resolve the initial segment of a non-global macro path
521     // (e.g. `foo` in `foo::bar!(); or `foo!();`).
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     crate fn resolve_lexical_macro_path_segment(
525         &mut self,
526         mut ident: Ident,
527         ns: Namespace,
528         record_used: bool,
529         force: bool,
530         is_attr: bool,
531         path_span: Span
532     ) -> Result<(&'a NameBinding<'a>, FromPrelude), Determinacy> {
533         // General principles:
534         // 1. Not controlled (user-defined) names should have higher priority than controlled names
535         //    built into the language or standard library. This way we can add new names into the
536         //    language or standard library without breaking user code.
537         // 2. "Closed set" below means new names can appear after the current resolution attempt.
538         // Places to search (in order of decreasing priority):
539         // (Type NS)
540         // 1. FIXME: Ribs (type parameters), there's no necessary infrastructure yet
541         //    (open set, not controlled).
542         // 2. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
543         //    (open, not controlled).
544         // 3. Extern prelude (closed, not controlled).
545         // 4. Tool modules (closed, controlled right now, but not in the future).
546         // 5. Standard library prelude (de-facto closed, controlled).
547         // 6. Language prelude (closed, controlled).
548         // (Macro NS)
549         // 1. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
550         //    (open, not controlled).
551         // 2. Macro prelude (language, standard library, user-defined legacy plugins lumped into
552         //    one set) (open, the open part is from macro expansions, not controlled).
553         // 2a. User-defined prelude from macro-use
554         //    (open, the open part is from macro expansions, not controlled).
555         // 2b. Standard library prelude, currently just a macro-use (closed, controlled)
556         // 2c. Language prelude, perhaps including builtin attributes
557         //    (closed, controlled, except for legacy plugins).
558         // 3. Builtin attributes (closed, controlled).
559
560         assert!(ns == TypeNS  || ns == MacroNS);
561         assert!(force || !record_used); // `record_used` implies `force`
562         ident = ident.modern();
563
564         // Names from inner scope that can't shadow names from outer scopes, e.g.
565         // mod m { ... }
566         // {
567         //     use prefix::*; // if this imports another `m`, then it can't shadow the outer `m`
568         //                    // and we have and ambiguity error
569         //     m::mac!();
570         // }
571         // This includes names from globs and from macro expansions.
572         let mut potentially_ambiguous_result: Option<(&NameBinding, FromPrelude)> = None;
573
574         enum WhereToResolve<'a> {
575             Module(Module<'a>),
576             MacroPrelude,
577             BuiltinAttrs,
578             ExternPrelude,
579             ToolPrelude,
580             StdLibPrelude,
581             PrimitiveTypes,
582         }
583
584         // Go through all the scopes and try to resolve the name.
585         let mut where_to_resolve = WhereToResolve::Module(self.current_module);
586         let mut use_prelude = !self.current_module.no_implicit_prelude;
587         loop {
588             let result = match where_to_resolve {
589                 WhereToResolve::Module(module) => {
590                     let orig_current_module = mem::replace(&mut self.current_module, module);
591                     let binding = self.resolve_ident_in_module_unadjusted(
592                         ModuleOrUniformRoot::Module(module),
593                         ident,
594                         ns,
595                         true,
596                         record_used,
597                         path_span,
598                     );
599                     self.current_module = orig_current_module;
600                     binding.map(|binding| (binding, FromPrelude(false)))
601                 }
602                 WhereToResolve::MacroPrelude => {
603                     match self.macro_prelude.get(&ident.name).cloned() {
604                         Some(binding) => Ok((binding, FromPrelude(true))),
605                         None => Err(Determinacy::Determined),
606                     }
607                 }
608                 WhereToResolve::BuiltinAttrs => {
609                     // FIXME: Only built-in attributes are not considered as candidates for
610                     // non-attributes to fight off regressions on stable channel (#53205).
611                     // We need to come up with some more principled approach instead.
612                     if is_attr && is_builtin_attr_name(ident.name) {
613                         let binding = (Def::NonMacroAttr(NonMacroAttrKind::Builtin),
614                                        ty::Visibility::Public, ident.span, Mark::root())
615                                        .to_name_binding(self.arenas);
616                         Ok((binding, FromPrelude(true)))
617                     } else {
618                         Err(Determinacy::Determined)
619                     }
620                 }
621                 WhereToResolve::ExternPrelude => {
622                     if use_prelude && self.extern_prelude.contains(&ident.name) {
623                         if !self.session.features_untracked().extern_prelude &&
624                            !self.ignore_extern_prelude_feature {
625                             feature_err(&self.session.parse_sess, "extern_prelude",
626                                         ident.span, GateIssue::Language,
627                                         "access to extern crates through prelude is experimental")
628                                         .emit();
629                         }
630
631                         let crate_id =
632                             self.crate_loader.process_path_extern(ident.name, ident.span);
633                         let crate_root =
634                             self.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX });
635                         self.populate_module_if_necessary(crate_root);
636
637                         let binding = (crate_root, ty::Visibility::Public,
638                                        ident.span, Mark::root()).to_name_binding(self.arenas);
639                         Ok((binding, FromPrelude(true)))
640                     } else {
641                         Err(Determinacy::Determined)
642                     }
643                 }
644                 WhereToResolve::ToolPrelude => {
645                     if use_prelude && is_known_tool(ident.name) {
646                         let binding = (Def::ToolMod, ty::Visibility::Public,
647                                        ident.span, Mark::root()).to_name_binding(self.arenas);
648                         Ok((binding, FromPrelude(true)))
649                     } else {
650                         Err(Determinacy::Determined)
651                     }
652                 }
653                 WhereToResolve::StdLibPrelude => {
654                     let mut result = Err(Determinacy::Determined);
655                     if use_prelude {
656                         if let Some(prelude) = self.prelude {
657                             if let Ok(binding) = self.resolve_ident_in_module_unadjusted(
658                                 ModuleOrUniformRoot::Module(prelude),
659                                 ident,
660                                 ns,
661                                 false,
662                                 false,
663                                 path_span,
664                             ) {
665                                 result = Ok((binding, FromPrelude(true)));
666                             }
667                         }
668                     }
669                     result
670                 }
671                 WhereToResolve::PrimitiveTypes => {
672                     if let Some(prim_ty) =
673                             self.primitive_type_table.primitive_types.get(&ident.name).cloned() {
674                         let binding = (Def::PrimTy(prim_ty), ty::Visibility::Public,
675                                        ident.span, Mark::root()).to_name_binding(self.arenas);
676                         Ok((binding, FromPrelude(true)))
677                     } else {
678                         Err(Determinacy::Determined)
679                     }
680                 }
681             };
682
683             macro_rules! continue_search { () => {
684                 where_to_resolve = match where_to_resolve {
685                     WhereToResolve::Module(module) => {
686                         match self.hygienic_lexical_parent(module, &mut ident.span) {
687                             Some(parent_module) => WhereToResolve::Module(parent_module),
688                             None => {
689                                 use_prelude = !module.no_implicit_prelude;
690                                 if ns == MacroNS {
691                                     WhereToResolve::MacroPrelude
692                                 } else {
693                                     WhereToResolve::ExternPrelude
694                                 }
695                             }
696                         }
697                     }
698                     WhereToResolve::MacroPrelude => WhereToResolve::BuiltinAttrs,
699                     WhereToResolve::BuiltinAttrs => break, // nowhere else to search
700                     WhereToResolve::ExternPrelude => WhereToResolve::ToolPrelude,
701                     WhereToResolve::ToolPrelude => WhereToResolve::StdLibPrelude,
702                     WhereToResolve::StdLibPrelude => WhereToResolve::PrimitiveTypes,
703                     WhereToResolve::PrimitiveTypes => break, // nowhere else to search
704                 };
705
706                 continue;
707             }}
708
709             match result {
710                 Ok(result) => {
711                     if !record_used {
712                         return Ok(result);
713                     }
714
715                     // Found a solution that is ambiguous with a previously found solution.
716                     // Push an ambiguity error for later reporting and
717                     // return something for better recovery.
718                     if let Some(previous_result) = potentially_ambiguous_result {
719                         if result.0.def() != previous_result.0.def() {
720                             self.ambiguity_errors.push(AmbiguityError {
721                                 span: path_span,
722                                 name: ident.name,
723                                 b1: previous_result.0,
724                                 b2: result.0,
725                                 lexical: true,
726                             });
727                             return Ok(previous_result);
728                         }
729                     }
730
731                     // Found a solution that's not an ambiguity yet, but is "suspicious" and
732                     // can participate in ambiguities later on.
733                     // Remember it and go search for other solutions in outer scopes.
734                     if result.0.is_glob_import() || result.0.expansion != Mark::root() {
735                         potentially_ambiguous_result = Some(result);
736
737                         continue_search!();
738                     }
739
740                     // Found a solution that can't be ambiguous, great success.
741                     return Ok(result);
742                 },
743                 Err(Determinacy::Determined) => {
744                     continue_search!();
745                 }
746                 Err(Determinacy::Undetermined) => return Err(Determinacy::determined(force)),
747             }
748         }
749
750         // Previously found potentially ambiguous result turned out to not be ambiguous after all.
751         if let Some(previous_result) = potentially_ambiguous_result {
752             return Ok(previous_result);
753         }
754
755         let determinacy = Determinacy::determined(force);
756         if determinacy == Determinacy::Determined && is_attr {
757             // For single-segment attributes interpret determinate "no resolution" as a custom
758             // attribute. (Lexical resolution implies the first segment and is_attr should imply
759             // the last segment, so we are certainly working with a single-segment attribute here.)
760             assert!(ns == MacroNS);
761             let binding = (Def::NonMacroAttr(NonMacroAttrKind::Custom),
762                            ty::Visibility::Public, ident.span, Mark::root())
763                            .to_name_binding(self.arenas);
764             Ok((binding, FromPrelude(true)))
765         } else {
766             Err(determinacy)
767         }
768     }
769
770     crate fn resolve_legacy_scope(&mut self,
771                                   mut scope: &'a Cell<LegacyScope<'a>>,
772                                   ident: Ident,
773                                   record_used: bool)
774                                   -> Option<(&'a LegacyBinding<'a>, FromExpansion)> {
775         let ident = ident.modern();
776         let mut relative_depth: u32 = 0;
777         loop {
778             match scope.get() {
779                 LegacyScope::Empty => break,
780                 LegacyScope::Expansion(invocation) => {
781                     match invocation.expansion.get() {
782                         LegacyScope::Invocation(_) => scope.set(invocation.legacy_scope.get()),
783                         LegacyScope::Empty => {
784                             scope = &invocation.legacy_scope;
785                         }
786                         _ => {
787                             relative_depth += 1;
788                             scope = &invocation.expansion;
789                         }
790                     }
791                 }
792                 LegacyScope::Invocation(invocation) => {
793                     relative_depth = relative_depth.saturating_sub(1);
794                     scope = &invocation.legacy_scope;
795                 }
796                 LegacyScope::Binding(potential_binding) => {
797                     if potential_binding.ident == ident {
798                         if record_used && relative_depth > 0 {
799                             self.disallowed_shadowing.push(potential_binding);
800                         }
801                         return Some((potential_binding, FromExpansion(relative_depth > 0)));
802                     }
803                     scope = &potential_binding.parent;
804                 }
805             };
806         }
807
808         None
809     }
810
811     pub fn finalize_current_module_macro_resolutions(&mut self) {
812         let module = self.current_module;
813         for &(ref path, span) in module.macro_resolutions.borrow().iter() {
814             match self.resolve_path(None, &path, Some(MacroNS), true, span, CrateLint::No) {
815                 PathResult::NonModule(_) => {},
816                 PathResult::Failed(span, msg, _) => {
817                     resolve_error(self, span, ResolutionError::FailedToResolve(&msg));
818                 }
819                 _ => unreachable!(),
820             }
821         }
822
823         for &(mark, ident, kind, def) in module.legacy_macro_resolutions.borrow().iter() {
824             let span = ident.span;
825             let legacy_scope = &self.invocations[&mark].legacy_scope;
826             let legacy_resolution = self.resolve_legacy_scope(legacy_scope, ident, true);
827             let resolution = self.resolve_lexical_macro_path_segment(ident, MacroNS, true, true,
828                                                                      kind == MacroKind::Attr, span);
829
830             let check_consistency = |this: &Self, new_def: Def| {
831                 if let Some(def) = def {
832                     if this.ambiguity_errors.is_empty() && this.disallowed_shadowing.is_empty() &&
833                        new_def != def && new_def != Def::Err {
834                         // Make sure compilation does not succeed if preferred macro resolution
835                         // has changed after the macro had been expanded. In theory all such
836                         // situations should be reported as ambiguity errors, so this is span-bug.
837                         span_bug!(span, "inconsistent resolution for a macro");
838                     }
839                 } else {
840                     // It's possible that the macro was unresolved (indeterminate) and silently
841                     // expanded into a dummy fragment for recovery during expansion.
842                     // Now, post-expansion, the resolution may succeed, but we can't change the
843                     // past and need to report an error.
844                     let msg =
845                         format!("cannot determine resolution for the {} `{}`", kind.descr(), ident);
846                     let msg_note = "import resolution is stuck, try simplifying macro imports";
847                     this.session.struct_span_err(span, &msg).note(msg_note).emit();
848                 }
849             };
850
851             match (legacy_resolution, resolution) {
852                 (None, Err(_)) => {
853                     assert!(def.is_none());
854                     let bang = if kind == MacroKind::Bang { "!" } else { "" };
855                     let msg =
856                         format!("cannot find {} `{}{}` in this scope", kind.descr(), ident, bang);
857                     let mut err = self.session.struct_span_err(span, &msg);
858                     self.suggest_macro_name(&ident.as_str(), kind, &mut err, span);
859                     err.emit();
860                 },
861                 (Some((legacy_binding, FromExpansion(from_expansion))),
862                  Ok((binding, FromPrelude(false)))) |
863                 (Some((legacy_binding, FromExpansion(from_expansion @ true))),
864                  Ok((binding, FromPrelude(true)))) => {
865                     if legacy_binding.def() != binding.def_ignoring_ambiguity() {
866                         self.report_ambiguity_error(
867                             ident.name, span, true,
868                             legacy_binding.def(), false, false,
869                             from_expansion, legacy_binding.span,
870                             binding.def(), binding.is_import(), binding.is_glob_import(),
871                             binding.expansion != Mark::root(), binding.span,
872                         );
873                     }
874                 },
875                 // OK, non-macro-expanded legacy wins over macro prelude even if defs are different
876                 (Some((legacy_binding, FromExpansion(false))), Ok((_, FromPrelude(true)))) |
877                 // OK, unambiguous resolution
878                 (Some((legacy_binding, _)), Err(_)) => {
879                     check_consistency(self, legacy_binding.def());
880                 }
881                 // OK, unambiguous resolution
882                 (None, Ok((binding, FromPrelude(from_prelude)))) => {
883                     check_consistency(self, binding.def_ignoring_ambiguity());
884                     if from_prelude {
885                         self.record_use(ident, MacroNS, binding, span);
886                         self.err_if_macro_use_proc_macro(ident.name, span, binding);
887                     }
888                 }
889             };
890         }
891     }
892
893     fn suggest_macro_name(&mut self, name: &str, kind: MacroKind,
894                           err: &mut DiagnosticBuilder<'a>, span: Span) {
895         // First check if this is a locally-defined bang macro.
896         let suggestion = if let MacroKind::Bang = kind {
897             find_best_match_for_name(self.macro_names.iter().map(|ident| &ident.name), name, None)
898         } else {
899             None
900         // Then check global macros.
901         }.or_else(|| {
902             // FIXME: get_macro needs an &mut Resolver, can we do it without cloning?
903             let macro_prelude = self.macro_prelude.clone();
904             let names = macro_prelude.iter().filter_map(|(name, binding)| {
905                 if binding.get_macro(self).kind() == kind {
906                     Some(name)
907                 } else {
908                     None
909                 }
910             });
911             find_best_match_for_name(names, name, None)
912         // Then check modules.
913         }).or_else(|| {
914             let is_macro = |def| {
915                 if let Def::Macro(_, def_kind) = def {
916                     def_kind == kind
917                 } else {
918                     false
919                 }
920             };
921             let ident = Ident::new(Symbol::intern(name), span);
922             self.lookup_typo_candidate(&[ident], MacroNS, is_macro, span)
923         });
924
925         if let Some(suggestion) = suggestion {
926             if suggestion != name {
927                 if let MacroKind::Bang = kind {
928                     err.span_suggestion_with_applicability(
929                         span,
930                         "you could try the macro",
931                         suggestion.to_string(),
932                         Applicability::MaybeIncorrect
933                     );
934                 } else {
935                     err.span_suggestion_with_applicability(
936                         span,
937                         "try",
938                         suggestion.to_string(),
939                         Applicability::MaybeIncorrect
940                     );
941                 }
942             } else {
943                 err.help("have you added the `#[macro_use]` on the module/import?");
944             }
945         }
946     }
947
948     fn collect_def_ids(&mut self,
949                        mark: Mark,
950                        invocation: &'a InvocationData<'a>,
951                        fragment: &AstFragment) {
952         let Resolver { ref mut invocations, arenas, graph_root, .. } = *self;
953         let InvocationData { def_index, .. } = *invocation;
954
955         let visit_macro_invoc = &mut |invoc: map::MacroInvocationData| {
956             invocations.entry(invoc.mark).or_insert_with(|| {
957                 arenas.alloc_invocation_data(InvocationData {
958                     def_index: invoc.def_index,
959                     module: Cell::new(graph_root),
960                     expansion: Cell::new(LegacyScope::Empty),
961                     legacy_scope: Cell::new(LegacyScope::Empty),
962                 })
963             });
964         };
965
966         let mut def_collector = DefCollector::new(&mut self.definitions, mark);
967         def_collector.visit_macro_invoc = Some(visit_macro_invoc);
968         def_collector.with_parent(def_index, |def_collector| {
969             fragment.visit_with(def_collector)
970         });
971     }
972
973     pub fn define_macro(&mut self,
974                         item: &ast::Item,
975                         expansion: Mark,
976                         legacy_scope: &mut LegacyScope<'a>) {
977         self.local_macro_def_scopes.insert(item.id, self.current_module);
978         let ident = item.ident;
979         if ident.name == "macro_rules" {
980             self.session.span_err(item.span, "user-defined macros may not be named `macro_rules`");
981         }
982
983         let def_id = self.definitions.local_def_id(item.id);
984         let ext = Lrc::new(macro_rules::compile(&self.session.parse_sess,
985                                                &self.session.features_untracked(),
986                                                item, hygiene::default_edition()));
987         self.macro_map.insert(def_id, ext);
988
989         let def = match item.node { ast::ItemKind::MacroDef(ref def) => def, _ => unreachable!() };
990         if def.legacy {
991             let ident = ident.modern();
992             self.macro_names.insert(ident);
993             *legacy_scope = LegacyScope::Binding(self.arenas.alloc_legacy_binding(LegacyBinding {
994                 parent: Cell::new(*legacy_scope), ident: ident, def_id: def_id, span: item.span,
995             }));
996             let def = Def::Macro(def_id, MacroKind::Bang);
997             self.all_macros.insert(ident.name, def);
998             if attr::contains_name(&item.attrs, "macro_export") {
999                 let module = self.graph_root;
1000                 let vis = ty::Visibility::Public;
1001                 self.define(module, ident, MacroNS,
1002                             (def, vis, item.span, expansion, IsMacroExport));
1003             } else {
1004                 self.unused_macros.insert(def_id);
1005             }
1006         } else {
1007             let module = self.current_module;
1008             let def = Def::Macro(def_id, MacroKind::Bang);
1009             let vis = self.resolve_visibility(&item.vis);
1010             if vis != ty::Visibility::Public {
1011                 self.unused_macros.insert(def_id);
1012             }
1013             self.define(module, ident, MacroNS, (def, vis, item.span, expansion));
1014         }
1015     }
1016
1017     /// Error if `ext` is a Macros 1.1 procedural macro being imported by `#[macro_use]`
1018     fn err_if_macro_use_proc_macro(&mut self, name: Name, use_span: Span,
1019                                    binding: &NameBinding<'a>) {
1020         let krate = match binding.def() {
1021             Def::NonMacroAttr(..) | Def::Err => return,
1022             Def::Macro(def_id, _) => def_id.krate,
1023             _ => unreachable!(),
1024         };
1025
1026         // Plugin-based syntax extensions are exempt from this check
1027         if krate == BUILTIN_MACROS_CRATE { return; }
1028
1029         let ext = binding.get_macro(self);
1030
1031         match *ext {
1032             // If `ext` is a procedural macro, check if we've already warned about it
1033             SyntaxExtension::AttrProcMacro(..) | SyntaxExtension::ProcMacro { .. } =>
1034                 if !self.warned_proc_macros.insert(name) { return; },
1035             _ => return,
1036         }
1037
1038         let warn_msg = match *ext {
1039             SyntaxExtension::AttrProcMacro(..) =>
1040                 "attribute procedural macros cannot be imported with `#[macro_use]`",
1041             SyntaxExtension::ProcMacro { .. } =>
1042                 "procedural macros cannot be imported with `#[macro_use]`",
1043             _ => return,
1044         };
1045
1046         let def_id = self.current_module.normal_ancestor_id;
1047         let node_id = self.definitions.as_local_node_id(def_id).unwrap();
1048
1049         self.proc_mac_errors.push(ProcMacError {
1050             crate_name: self.cstore.crate_name_untracked(krate),
1051             name,
1052             module: node_id,
1053             use_span,
1054             warn_msg,
1055         });
1056     }
1057
1058     pub fn report_proc_macro_import(&mut self, krate: &ast::Crate) {
1059         for err in self.proc_mac_errors.drain(..) {
1060             let (span, found_use) = ::UsePlacementFinder::check(krate, err.module);
1061
1062             if let Some(span) = span {
1063                 let found_use = if found_use { "" } else { "\n" };
1064                 self.session.struct_span_err(err.use_span, err.warn_msg)
1065                     .span_suggestion_with_applicability(
1066                         span,
1067                         "instead, import the procedural macro like any other item",
1068                         format!("use {}::{};{}", err.crate_name, err.name, found_use),
1069                         Applicability::MachineApplicable
1070                     ).emit();
1071             } else {
1072                 self.session.struct_span_err(err.use_span, err.warn_msg)
1073                     .help(&format!("instead, import the procedural macro like any other item: \
1074                                     `use {}::{};`", err.crate_name, err.name))
1075                     .emit();
1076             }
1077         }
1078     }
1079
1080     fn gate_legacy_custom_derive(&mut self, name: Symbol, span: Span) {
1081         if !self.session.features_untracked().custom_derive {
1082             let sess = &self.session.parse_sess;
1083             let explain = feature_gate::EXPLAIN_CUSTOM_DERIVE;
1084             emit_feature_err(sess, "custom_derive", span, GateIssue::Language, explain);
1085         } else if !self.is_whitelisted_legacy_custom_derive(name) {
1086             self.session.span_warn(span, feature_gate::EXPLAIN_DEPR_CUSTOM_DERIVE);
1087         }
1088     }
1089 }