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