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