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