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