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