]> git.lizzy.rs Git - rust.git/blob - src/librustc_resolve/macros.rs
save-analysis: make sure we save the def for the last segment of a path
[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, Segment, 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, Debug)]
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 #[derive(Debug)]
74 pub struct LegacyBinding<'a> {
75     binding: &'a NameBinding<'a>,
76     /// Legacy scope into which the `macro_rules` item was planted.
77     parent_legacy_scope: LegacyScope<'a>,
78     ident: Ident,
79 }
80
81 /// Scope introduced by a `macro_rules!` macro.
82 /// Starts at the macro's definition and ends at the end of the macro's parent module
83 /// (named or unnamed), or even further if it escapes with `#[macro_use]`.
84 /// Some macro invocations need to introduce legacy scopes too because they
85 /// potentially can expand into macro definitions.
86 #[derive(Copy, Clone, Debug)]
87 pub enum LegacyScope<'a> {
88     /// Created when invocation data is allocated in the arena,
89     /// must be replaced with a proper scope later.
90     Uninitialized,
91     /// Empty "root" scope at the crate start containing no names.
92     Empty,
93     /// Scope introduced by a `macro_rules!` macro definition.
94     Binding(&'a LegacyBinding<'a>),
95     /// Scope introduced by a macro invocation that can potentially
96     /// create a `macro_rules!` macro definition.
97     Invocation(&'a InvocationData<'a>),
98 }
99
100 /// Everything you need to resolve a macro or import path.
101 #[derive(Clone, Debug)]
102 pub struct ParentScope<'a> {
103     crate module: Module<'a>,
104     crate expansion: Mark,
105     crate legacy: LegacyScope<'a>,
106     crate derives: Vec<ast::Path>,
107 }
108
109 // Macro namespace is separated into two sub-namespaces, one for bang macros and
110 // one for attribute-like macros (attributes, derives).
111 // We ignore resolutions from one sub-namespace when searching names in scope for another.
112 fn sub_namespace_mismatch(requirement: Option<MacroKind>, candidate: Option<MacroKind>) -> bool {
113     #[derive(PartialEq)]
114     enum SubNS { Bang, AttrLike }
115     let sub_ns = |kind| match kind {
116         MacroKind::Bang => Some(SubNS::Bang),
117         MacroKind::Attr | MacroKind::Derive => Some(SubNS::AttrLike),
118         MacroKind::ProcMacroStub => None,
119     };
120     let requirement = requirement.and_then(|kind| sub_ns(kind));
121     let candidate = candidate.and_then(|kind| sub_ns(kind));
122     // "No specific sub-namespace" means "matches anything" for both requirements and candidates.
123     candidate.is_some() && requirement.is_some() && candidate != requirement
124 }
125
126 impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
127     fn next_node_id(&mut self) -> ast::NodeId {
128         self.session.next_node_id()
129     }
130
131     fn get_module_scope(&mut self, id: ast::NodeId) -> Mark {
132         let mark = Mark::fresh(Mark::root());
133         let module = self.module_map[&self.definitions.local_def_id(id)];
134         self.invocations.insert(mark, self.arenas.alloc_invocation_data(InvocationData {
135             module: Cell::new(module),
136             def_index: module.def_id().unwrap().index,
137             parent_legacy_scope: Cell::new(LegacyScope::Empty),
138             output_legacy_scope: Cell::new(LegacyScope::Empty),
139         }));
140         mark
141     }
142
143     fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item> {
144         struct EliminateCrateVar<'b, 'a: 'b, 'crateloader: 'a>(
145             &'b mut Resolver<'a, 'crateloader>, Span
146         );
147
148         impl<'a, 'b, 'crateloader> Folder for EliminateCrateVar<'a, 'b, 'crateloader> {
149             fn fold_path(&mut self, path: ast::Path) -> ast::Path {
150                 match self.fold_qpath(None, path) {
151                     (None, path) => path,
152                     _ => unreachable!(),
153                 }
154             }
155
156             fn fold_qpath(&mut self, mut qself: Option<ast::QSelf>, mut path: ast::Path)
157                           -> (Option<ast::QSelf>, ast::Path) {
158                 qself = qself.map(|ast::QSelf { ty, path_span, position }| {
159                     ast::QSelf {
160                         ty: self.fold_ty(ty),
161                         path_span: self.new_span(path_span),
162                         position,
163                     }
164                 });
165
166                 if path.segments[0].ident.name == keywords::DollarCrate.name() {
167                     let module = self.0.resolve_crate_root(path.segments[0].ident);
168                     path.segments[0].ident.name = keywords::CrateRoot.name();
169                     if !module.is_local() {
170                         let span = path.segments[0].ident.span;
171                         path.segments.insert(1, match module.kind {
172                             ModuleKind::Def(_, name) => ast::PathSegment::from_ident(
173                                 ast::Ident::with_empty_ctxt(name).with_span_pos(span)
174                             ),
175                             _ => unreachable!(),
176                         });
177                         if let Some(qself) = &mut qself {
178                             qself.position += 1;
179                         }
180                     }
181                 }
182                 (qself, path)
183             }
184
185             fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
186                 fold::noop_fold_mac(mac, self)
187             }
188         }
189
190         let ret = EliminateCrateVar(self, item.span).fold_item(item);
191         assert!(ret.len() == 1);
192         ret.into_iter().next().unwrap()
193     }
194
195     fn is_whitelisted_legacy_custom_derive(&self, name: Name) -> bool {
196         self.whitelisted_legacy_custom_derives.contains(&name)
197     }
198
199     fn visit_ast_fragment_with_placeholders(&mut self, mark: Mark, fragment: &AstFragment,
200                                             derives: &[Mark]) {
201         let invocation = self.invocations[&mark];
202         self.collect_def_ids(mark, invocation, fragment);
203
204         self.current_module = invocation.module.get();
205         self.current_module.unresolved_invocations.borrow_mut().remove(&mark);
206         self.current_module.unresolved_invocations.borrow_mut().extend(derives);
207         self.invocations.extend(derives.iter().map(|&derive| (derive, invocation)));
208         let mut visitor = BuildReducedGraphVisitor {
209             resolver: self,
210             current_legacy_scope: invocation.parent_legacy_scope.get(),
211             expansion: mark,
212         };
213         fragment.visit_with(&mut visitor);
214         invocation.output_legacy_scope.set(visitor.current_legacy_scope);
215     }
216
217     fn add_builtin(&mut self, ident: ast::Ident, ext: Lrc<SyntaxExtension>) {
218         let def_id = DefId {
219             krate: CrateNum::BuiltinMacros,
220             index: DefIndex::from_array_index(self.macro_map.len(),
221                                               DefIndexAddressSpace::Low),
222         };
223         let kind = ext.kind();
224         self.macro_map.insert(def_id, ext);
225         let binding = self.arenas.alloc_name_binding(NameBinding {
226             kind: NameBindingKind::Def(Def::Macro(def_id, kind), false),
227             span: DUMMY_SP,
228             vis: ty::Visibility::Invisible,
229             expansion: Mark::root(),
230         });
231         if self.builtin_macros.insert(ident.name, binding).is_some() {
232             self.session.span_err(ident.span,
233                                   &format!("built-in macro `{}` was already defined", ident));
234         }
235     }
236
237     fn resolve_imports(&mut self) {
238         ImportResolver { resolver: self }.resolve_imports()
239     }
240
241     // Resolves attribute and derive legacy macros from `#![plugin(..)]`.
242     fn find_legacy_attr_invoc(&mut self, attrs: &mut Vec<ast::Attribute>, allow_derive: bool)
243                               -> Option<ast::Attribute> {
244         if !allow_derive {
245             return None;
246         }
247
248         // Check for legacy derives
249         for i in 0..attrs.len() {
250             let name = attrs[i].name();
251
252             if name == "derive" {
253                 let result = attrs[i].parse_list(&self.session.parse_sess, |parser| {
254                     parser.parse_path_allowing_meta(PathStyle::Mod)
255                 });
256
257                 let mut traits = match result {
258                     Ok(traits) => traits,
259                     Err(mut e) => {
260                         e.cancel();
261                         continue
262                     }
263                 };
264
265                 for j in 0..traits.len() {
266                     if traits[j].segments.len() > 1 {
267                         continue
268                     }
269                     let trait_name = traits[j].segments[0].ident.name;
270                     let legacy_name = Symbol::intern(&format!("derive_{}", trait_name));
271                     if !self.builtin_macros.contains_key(&legacy_name) {
272                         continue
273                     }
274                     let span = traits.remove(j).span;
275                     self.gate_legacy_custom_derive(legacy_name, span);
276                     if traits.is_empty() {
277                         attrs.remove(i);
278                     } else {
279                         let mut tokens = Vec::with_capacity(traits.len() - 1);
280                         for (j, path) in traits.iter().enumerate() {
281                             if j > 0 {
282                                 tokens.push(TokenTree::Token(attrs[i].span, Token::Comma).into());
283                             }
284                             tokens.reserve((path.segments.len() * 2).saturating_sub(1));
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 path_span = path.span;
466         let mut path = Segment::from_path(path);
467
468         // Possibly apply the macro helper hack
469         if kind == MacroKind::Bang && path.len() == 1 &&
470            path[0].ident.span.ctxt().outer().expn_info()
471                .map_or(false, |info| info.local_inner_macros) {
472             let root = Ident::new(keywords::DollarCrate.name(), path[0].ident.span);
473             path.insert(0, Segment::from_ident(root));
474         }
475
476         if path.len() > 1 {
477             let def = match self.resolve_path(None, &path, Some(MacroNS), parent_scope,
478                                               false, path_span, 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(path_span,
485                                                   "fail to resolve non-ident macro path");
486                             Err(Determinacy::Determined)
487                         } else {
488                             Ok(def)
489                         }
490                     }
491                 },
492                 PathResult::Module(..) => unreachable!(),
493                 PathResult::Indeterminate if !force => return Err(Determinacy::Undetermined),
494                 _ => {
495                     self.found_unresolved_macro = true;
496                     Err(Determinacy::Determined)
497                 },
498             };
499
500             parent_scope.module.macro_resolutions.borrow_mut()
501                 .push((path, parent_scope.clone(), path_span));
502
503             def
504         } else {
505             let binding = self.early_resolve_ident_in_lexical_scope(
506                 path[0].ident, MacroNS, Some(kind), parent_scope, false, force, path_span
507             );
508             match binding {
509                 Ok(..) => {}
510                 Err(Determinacy::Determined) => self.found_unresolved_macro = true,
511                 Err(Determinacy::Undetermined) => return Err(Determinacy::Undetermined),
512             }
513
514             parent_scope.module.legacy_macro_resolutions.borrow_mut()
515                 .push((path[0].ident, kind, parent_scope.clone(), binding.ok()));
516
517             binding.map(|binding| binding.def_ignoring_ambiguity())
518         }
519     }
520
521     // Resolve an identifier in lexical scope.
522     // This is a variation of `fn resolve_ident_in_lexical_scope` that can be run during
523     // expansion and import resolution (perhaps they can be merged in the future).
524     // The function is used for resolving initial segments of macro paths (e.g. `foo` in
525     // `foo::bar!(); or `foo!();`) and can be used for "uniform path" imports in the future.
526     crate fn early_resolve_ident_in_lexical_scope(
527         &mut self,
528         mut ident: Ident,
529         ns: Namespace,
530         kind: Option<MacroKind>,
531         parent_scope: &ParentScope<'a>,
532         record_used: bool,
533         force: bool,
534         path_span: Span,
535     ) -> Result<&'a NameBinding<'a>, Determinacy> {
536         // General principles:
537         // 1. Not controlled (user-defined) names should have higher priority than controlled names
538         //    built into the language or standard library. This way we can add new names into the
539         //    language or standard library without breaking user code.
540         // 2. "Closed set" below means new names cannot appear after the current resolution attempt.
541         // Places to search (in order of decreasing priority):
542         // (Type NS)
543         // 1. FIXME: Ribs (type parameters), there's no necessary infrastructure yet
544         //    (open set, not controlled).
545         // 2. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
546         //    (open, not controlled).
547         // 3. Extern prelude (closed, not controlled).
548         // 4. Tool modules (closed, controlled right now, but not in the future).
549         // 5. Standard library prelude (de-facto closed, controlled).
550         // 6. Language prelude (closed, controlled).
551         // (Value NS)
552         // 1. FIXME: Ribs (local variables), there's no necessary infrastructure yet
553         //    (open set, not controlled).
554         // 2. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
555         //    (open, not controlled).
556         // 3. Standard library prelude (de-facto closed, controlled).
557         // (Macro NS)
558         // 1-3. Derive helpers (open, not controlled). All ambiguities with other names
559         //    are currently reported as errors. They should be higher in priority than preludes
560         //    and probably even names in modules according to the "general principles" above. They
561         //    also should be subject to restricted shadowing because are effectively produced by
562         //    derives (you need to resolve the derive first to add helpers into scope), but they
563         //    should be available before the derive is expanded for compatibility.
564         //    It's mess in general, so we are being conservative for now.
565         // 1-3. `macro_rules` (open, not controlled), loop through legacy scopes. Have higher
566         //    priority than prelude macros, but create ambiguities with macros in modules.
567         // 1-3. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
568         //    (open, not controlled). Have higher priority than prelude macros, but create
569         //    ambiguities with `macro_rules`.
570         // 4. `macro_use` prelude (open, the open part is from macro expansions, not controlled).
571         // 4a. User-defined prelude from macro-use
572         //    (open, the open part is from macro expansions, not controlled).
573         // 4b. Standard library prelude is currently implemented as `macro-use` (closed, controlled)
574         // 5. Language prelude: builtin macros (closed, controlled, except for legacy plugins).
575         // 6. Language prelude: builtin attributes (closed, controlled).
576         // 4-6. Legacy plugin helpers (open, not controlled). Similar to derive helpers,
577         //    but introduced by legacy plugins using `register_attribute`. Priority is somewhere
578         //    in prelude, not sure where exactly (creates ambiguities with any other prelude names).
579
580         enum WhereToResolve<'a> {
581             DeriveHelpers,
582             MacroRules(LegacyScope<'a>),
583             Module(Module<'a>),
584             MacroUsePrelude,
585             BuiltinMacros,
586             BuiltinAttrs,
587             LegacyPluginHelpers,
588             ExternPrelude,
589             ToolPrelude,
590             StdLibPrelude,
591             BuiltinTypes,
592         }
593
594         bitflags! {
595             struct Flags: u8 {
596                 const DERIVE_HELPERS = 1 << 0;
597                 const MACRO_RULES    = 1 << 1;
598                 const MODULE         = 1 << 2;
599                 const PRELUDE        = 1 << 3;
600             }
601         }
602
603         assert!(force || !record_used); // `record_used` implies `force`
604         ident = ident.modern();
605
606         // This is *the* result, resolution from the scope closest to the resolved identifier.
607         // However, sometimes this result is "weak" because it comes from a glob import or
608         // a macro expansion, and in this case it cannot shadow names from outer scopes, e.g.
609         // mod m { ... } // solution in outer scope
610         // {
611         //     use prefix::*; // imports another `m` - innermost solution
612         //                    // weak, cannot shadow the outer `m`, need to report ambiguity error
613         //     m::mac!();
614         // }
615         // So we have to save the innermost solution and continue searching in outer scopes
616         // to detect potential ambiguities.
617         let mut innermost_result: Option<(&NameBinding, Flags, /* conflicts with */ Flags)> = None;
618
619         // Go through all the scopes and try to resolve the name.
620         let mut where_to_resolve = WhereToResolve::DeriveHelpers;
621         let mut use_prelude = !parent_scope.module.no_implicit_prelude;
622         loop {
623             let result = match where_to_resolve {
624                 WhereToResolve::DeriveHelpers => {
625                     let mut result = Err(Determinacy::Determined);
626                     for derive in &parent_scope.derives {
627                         let parent_scope = ParentScope { derives: Vec::new(), ..*parent_scope };
628                         if let Ok((_, ext)) = self.resolve_macro_to_def(derive, MacroKind::Derive,
629                                                                         &parent_scope, force) {
630                             if let SyntaxExtension::ProcMacroDerive(_, helper_attrs, _) = &*ext {
631                                 if helper_attrs.contains(&ident.name) {
632                                     let binding =
633                                         (Def::NonMacroAttr(NonMacroAttrKind::DeriveHelper),
634                                         ty::Visibility::Public, derive.span, Mark::root())
635                                         .to_name_binding(self.arenas);
636                                     result = Ok((binding, Flags::DERIVE_HELPERS, Flags::all()));
637                                     break;
638                                 }
639                             }
640                         }
641                     }
642                     result
643                 }
644                 WhereToResolve::MacroRules(legacy_scope) => match legacy_scope {
645                     LegacyScope::Binding(legacy_binding) if ident == legacy_binding.ident =>
646                         Ok((legacy_binding.binding, Flags::MACRO_RULES, Flags::empty())),
647                     _ => Err(Determinacy::Determined),
648                 }
649                 WhereToResolve::Module(module) => {
650                     let orig_current_module = mem::replace(&mut self.current_module, module);
651                     let binding = self.resolve_ident_in_module_unadjusted(
652                         ModuleOrUniformRoot::Module(module),
653                         ident,
654                         ns,
655                         true,
656                         record_used,
657                         path_span,
658                     );
659                     self.current_module = orig_current_module;
660                     binding.map(|binding| (binding, Flags::MODULE, Flags::empty()))
661                 }
662                 WhereToResolve::MacroUsePrelude => {
663                     match self.macro_use_prelude.get(&ident.name).cloned() {
664                         Some(binding) => Ok((binding, Flags::PRELUDE, Flags::empty())),
665                         None => Err(Determinacy::Determined),
666                     }
667                 }
668                 WhereToResolve::BuiltinMacros => {
669                     match self.builtin_macros.get(&ident.name).cloned() {
670                         Some(binding) => Ok((binding, Flags::PRELUDE, Flags::empty())),
671                         None => Err(Determinacy::Determined),
672                     }
673                 }
674                 WhereToResolve::BuiltinAttrs => {
675                     if is_builtin_attr_name(ident.name) {
676                         let binding = (Def::NonMacroAttr(NonMacroAttrKind::Builtin),
677                                        ty::Visibility::Public, ident.span, Mark::root())
678                                        .to_name_binding(self.arenas);
679                         Ok((binding, Flags::PRELUDE, Flags::empty()))
680                     } else {
681                         Err(Determinacy::Determined)
682                     }
683                 }
684                 WhereToResolve::LegacyPluginHelpers => {
685                     if self.session.plugin_attributes.borrow().iter()
686                                                      .any(|(name, _)| ident.name == &**name) {
687                         let binding = (Def::NonMacroAttr(NonMacroAttrKind::LegacyPluginHelper),
688                                        ty::Visibility::Public, ident.span, Mark::root())
689                                        .to_name_binding(self.arenas);
690                         Ok((binding, Flags::PRELUDE, Flags::PRELUDE))
691                     } else {
692                         Err(Determinacy::Determined)
693                     }
694                 }
695                 WhereToResolve::ExternPrelude => {
696                     let mut result = Err(Determinacy::Determined);
697                     if use_prelude {
698                         if let Some(binding) = self.extern_prelude_get(ident, !record_used,
699                                                                        innermost_result.is_some()) {
700                             result = Ok((binding, Flags::PRELUDE, Flags::empty()));
701                         }
702                     }
703                     result
704                 }
705                 WhereToResolve::ToolPrelude => {
706                     if use_prelude && is_known_tool(ident.name) {
707                         let binding = (Def::ToolMod, ty::Visibility::Public,
708                                        ident.span, Mark::root()).to_name_binding(self.arenas);
709                         Ok((binding, Flags::PRELUDE, Flags::empty()))
710                     } else {
711                         Err(Determinacy::Determined)
712                     }
713                 }
714                 WhereToResolve::StdLibPrelude => {
715                     let mut result = Err(Determinacy::Determined);
716                     if use_prelude {
717                         if let Some(prelude) = self.prelude {
718                             if let Ok(binding) = self.resolve_ident_in_module_unadjusted(
719                                 ModuleOrUniformRoot::Module(prelude),
720                                 ident,
721                                 ns,
722                                 false,
723                                 false,
724                                 path_span,
725                             ) {
726                                 result = Ok((binding, Flags::PRELUDE, Flags::empty()));
727                             }
728                         }
729                     }
730                     result
731                 }
732                 WhereToResolve::BuiltinTypes => {
733                     match self.primitive_type_table.primitive_types.get(&ident.name).cloned() {
734                         Some(prim_ty) => {
735                             let binding = (Def::PrimTy(prim_ty), ty::Visibility::Public,
736                                            ident.span, Mark::root()).to_name_binding(self.arenas);
737                             Ok((binding, Flags::PRELUDE, Flags::empty()))
738                         }
739                         None => Err(Determinacy::Determined)
740                     }
741                 }
742             };
743
744             macro_rules! continue_search { () => {
745                 where_to_resolve = match where_to_resolve {
746                     WhereToResolve::DeriveHelpers =>
747                         WhereToResolve::MacroRules(parent_scope.legacy),
748                     WhereToResolve::MacroRules(legacy_scope) => match legacy_scope {
749                         LegacyScope::Binding(binding) =>
750                             WhereToResolve::MacroRules(binding.parent_legacy_scope),
751                         LegacyScope::Invocation(invocation) =>
752                             WhereToResolve::MacroRules(invocation.output_legacy_scope.get()),
753                         LegacyScope::Empty => WhereToResolve::Module(parent_scope.module),
754                         LegacyScope::Uninitialized => unreachable!(),
755                     }
756                     WhereToResolve::Module(module) => {
757                         match self.hygienic_lexical_parent(module, &mut ident.span) {
758                             Some(parent_module) => WhereToResolve::Module(parent_module),
759                             None => {
760                                 use_prelude = !module.no_implicit_prelude;
761                                 match ns {
762                                     TypeNS => WhereToResolve::ExternPrelude,
763                                     ValueNS => WhereToResolve::StdLibPrelude,
764                                     MacroNS => WhereToResolve::MacroUsePrelude,
765                                 }
766                             }
767                         }
768                     }
769                     WhereToResolve::MacroUsePrelude => WhereToResolve::BuiltinMacros,
770                     WhereToResolve::BuiltinMacros => WhereToResolve::BuiltinAttrs,
771                     WhereToResolve::BuiltinAttrs => WhereToResolve::LegacyPluginHelpers,
772                     WhereToResolve::LegacyPluginHelpers => break, // nowhere else to search
773                     WhereToResolve::ExternPrelude => WhereToResolve::ToolPrelude,
774                     WhereToResolve::ToolPrelude => WhereToResolve::StdLibPrelude,
775                     WhereToResolve::StdLibPrelude => match ns {
776                         TypeNS => WhereToResolve::BuiltinTypes,
777                         ValueNS => break, // nowhere else to search
778                         MacroNS => unreachable!(),
779                     }
780                     WhereToResolve::BuiltinTypes => break, // nowhere else to search
781                 };
782
783                 continue;
784             }}
785
786             match result {
787                 Ok((binding, flags, ambig_flags)) => {
788                     if sub_namespace_mismatch(kind, binding.macro_kind()) {
789                         continue_search!();
790                     }
791
792                     if !record_used {
793                         return Ok(binding);
794                     }
795
796                     if let Some((innermost_binding, innermost_flags, innermost_ambig_flags))
797                             = innermost_result {
798                         // Found another solution, if the first one was "weak", report an error.
799                         if binding.def() != innermost_binding.def() &&
800                            (innermost_binding.is_glob_import() ||
801                             innermost_binding.may_appear_after(parent_scope.expansion, binding) ||
802                             innermost_flags.intersects(ambig_flags) ||
803                             flags.intersects(innermost_ambig_flags) ||
804                             (innermost_flags.contains(Flags::MACRO_RULES) &&
805                              flags.contains(Flags::MODULE) &&
806                              !self.disambiguate_legacy_vs_modern(innermost_binding, binding))) {
807                             self.ambiguity_errors.push(AmbiguityError {
808                                 ident,
809                                 b1: innermost_binding,
810                                 b2: binding,
811                             });
812                             return Ok(innermost_binding);
813                         }
814                     } else {
815                         // Found the first solution.
816                         innermost_result = Some((binding, flags, ambig_flags));
817                     }
818
819                     continue_search!();
820                 },
821                 Err(Determinacy::Determined) => {
822                     continue_search!();
823                 }
824                 Err(Determinacy::Undetermined) => return Err(Determinacy::determined(force)),
825             }
826         }
827
828         // The first found solution was the only one, return it.
829         if let Some((binding, ..)) = innermost_result {
830             return Ok(binding);
831         }
832
833         let determinacy = Determinacy::determined(force);
834         if determinacy == Determinacy::Determined && kind == Some(MacroKind::Attr) {
835             // For single-segment attributes interpret determinate "no resolution" as a custom
836             // attribute. (Lexical resolution implies the first segment and attr kind should imply
837             // the last segment, so we are certainly working with a single-segment attribute here.)
838             assert!(ns == MacroNS);
839             let binding = (Def::NonMacroAttr(NonMacroAttrKind::Custom),
840                            ty::Visibility::Public, ident.span, Mark::root())
841                            .to_name_binding(self.arenas);
842             Ok(binding)
843         } else {
844             Err(determinacy)
845         }
846     }
847
848     pub fn finalize_current_module_macro_resolutions(&mut self) {
849         let module = self.current_module;
850
851         let macro_resolutions =
852             mem::replace(&mut *module.macro_resolutions.borrow_mut(), Vec::new());
853         for (mut path, parent_scope, path_span) in macro_resolutions {
854             // FIXME: Path resolution will ICE if segment IDs present.
855             for seg in &mut path { seg.id = None; }
856             match self.resolve_path(None, &path, Some(MacroNS), &parent_scope,
857                                     true, path_span, CrateLint::No) {
858                 PathResult::NonModule(_) => {},
859                 PathResult::Failed(span, msg, _) => {
860                     resolve_error(self, span, ResolutionError::FailedToResolve(&msg));
861                 }
862                 _ => unreachable!(),
863             }
864         }
865
866         let legacy_macro_resolutions =
867             mem::replace(&mut *module.legacy_macro_resolutions.borrow_mut(), Vec::new());
868         for (ident, kind, parent_scope, initial_binding) in legacy_macro_resolutions {
869             let binding = self.early_resolve_ident_in_lexical_scope(
870                 ident, MacroNS, Some(kind), &parent_scope, true, true, ident.span
871             );
872             match binding {
873                 Ok(binding) => {
874                     let def = binding.def_ignoring_ambiguity();
875                     if let Some(initial_binding) = initial_binding {
876                         self.record_use(ident, MacroNS, initial_binding);
877                         let initial_def = initial_binding.def_ignoring_ambiguity();
878                         if self.ambiguity_errors.is_empty() &&
879                            def != initial_def && def != Def::Err {
880                             // Make sure compilation does not succeed if preferred macro resolution
881                             // has changed after the macro had been expanded. In theory all such
882                             // situations should be reported as ambiguity errors, so this is a bug.
883                             span_bug!(ident.span, "inconsistent resolution for a macro");
884                         }
885                     } else {
886                         // It's possible that the macro was unresolved (indeterminate) and silently
887                         // expanded into a dummy fragment for recovery during expansion.
888                         // Now, post-expansion, the resolution may succeed, but we can't change the
889                         // past and need to report an error.
890                         let msg = format!("cannot determine resolution for the {} `{}`",
891                                           kind.descr(), ident);
892                         let msg_note = "import resolution is stuck, try simplifying macro imports";
893                         self.session.struct_span_err(ident.span, &msg).note(msg_note).emit();
894                     }
895                 }
896                 Err(..) => {
897                     assert!(initial_binding.is_none());
898                     let bang = if kind == MacroKind::Bang { "!" } else { "" };
899                     let msg =
900                         format!("cannot find {} `{}{}` in this scope", kind.descr(), ident, bang);
901                     let mut err = self.session.struct_span_err(ident.span, &msg);
902                     self.suggest_macro_name(&ident.as_str(), kind, &mut err, ident.span);
903                     err.emit();
904                 }
905             }
906         }
907
908         let builtin_attrs = mem::replace(&mut *module.builtin_attrs.borrow_mut(), Vec::new());
909         for (ident, parent_scope) in builtin_attrs {
910             let binding = self.early_resolve_ident_in_lexical_scope(
911                 ident, MacroNS, Some(MacroKind::Attr), &parent_scope, true, true, ident.span
912             );
913             if let Ok(binding) = binding {
914                 if binding.def_ignoring_ambiguity() !=
915                         Def::NonMacroAttr(NonMacroAttrKind::Builtin) {
916                     let builtin_binding = (Def::NonMacroAttr(NonMacroAttrKind::Builtin),
917                                            ty::Visibility::Public, ident.span, Mark::root())
918                                            .to_name_binding(self.arenas);
919                     self.report_ambiguity_error(ident, binding, builtin_binding);
920                 }
921             }
922         }
923     }
924
925     fn suggest_macro_name(&mut self, name: &str, kind: MacroKind,
926                           err: &mut DiagnosticBuilder<'a>, span: Span) {
927         // First check if this is a locally-defined bang macro.
928         let suggestion = if let MacroKind::Bang = kind {
929             find_best_match_for_name(self.macro_names.iter().map(|ident| &ident.name), name, None)
930         } else {
931             None
932         // Then check global macros.
933         }.or_else(|| {
934             let names = self.builtin_macros.iter().chain(self.macro_use_prelude.iter())
935                                                   .filter_map(|(name, binding)| {
936                 if binding.macro_kind() == Some(kind) { Some(name) } else { None }
937             });
938             find_best_match_for_name(names, name, None)
939         // Then check modules.
940         }).or_else(|| {
941             let is_macro = |def| {
942                 if let Def::Macro(_, def_kind) = def {
943                     def_kind == kind
944                 } else {
945                     false
946                 }
947             };
948             let ident = Ident::new(Symbol::intern(name), span);
949             self.lookup_typo_candidate(&[Segment::from_ident(ident)], MacroNS, is_macro, span)
950         });
951
952         if let Some(suggestion) = suggestion {
953             if suggestion != name {
954                 if let MacroKind::Bang = kind {
955                     err.span_suggestion_with_applicability(
956                         span,
957                         "you could try the macro",
958                         suggestion.to_string(),
959                         Applicability::MaybeIncorrect
960                     );
961                 } else {
962                     err.span_suggestion_with_applicability(
963                         span,
964                         "try",
965                         suggestion.to_string(),
966                         Applicability::MaybeIncorrect
967                     );
968                 }
969             } else {
970                 err.help("have you added the `#[macro_use]` on the module/import?");
971             }
972         }
973     }
974
975     fn collect_def_ids(&mut self,
976                        mark: Mark,
977                        invocation: &'a InvocationData<'a>,
978                        fragment: &AstFragment) {
979         let Resolver { ref mut invocations, arenas, graph_root, .. } = *self;
980         let InvocationData { def_index, .. } = *invocation;
981
982         let visit_macro_invoc = &mut |invoc: map::MacroInvocationData| {
983             invocations.entry(invoc.mark).or_insert_with(|| {
984                 arenas.alloc_invocation_data(InvocationData {
985                     def_index: invoc.def_index,
986                     module: Cell::new(graph_root),
987                     parent_legacy_scope: Cell::new(LegacyScope::Uninitialized),
988                     output_legacy_scope: Cell::new(LegacyScope::Uninitialized),
989                 })
990             });
991         };
992
993         let mut def_collector = DefCollector::new(&mut self.definitions, mark);
994         def_collector.visit_macro_invoc = Some(visit_macro_invoc);
995         def_collector.with_parent(def_index, |def_collector| {
996             fragment.visit_with(def_collector)
997         });
998     }
999
1000     pub fn define_macro(&mut self,
1001                         item: &ast::Item,
1002                         expansion: Mark,
1003                         current_legacy_scope: &mut LegacyScope<'a>) {
1004         self.local_macro_def_scopes.insert(item.id, self.current_module);
1005         let ident = item.ident;
1006         if ident.name == "macro_rules" {
1007             self.session.span_err(item.span, "user-defined macros may not be named `macro_rules`");
1008         }
1009
1010         let def_id = self.definitions.local_def_id(item.id);
1011         let ext = Lrc::new(macro_rules::compile(&self.session.parse_sess,
1012                                                &self.session.features_untracked(),
1013                                                item, hygiene::default_edition()));
1014         self.macro_map.insert(def_id, ext);
1015
1016         let def = match item.node { ast::ItemKind::MacroDef(ref def) => def, _ => unreachable!() };
1017         if def.legacy {
1018             let ident = ident.modern();
1019             self.macro_names.insert(ident);
1020             let def = Def::Macro(def_id, MacroKind::Bang);
1021             let vis = ty::Visibility::Invisible; // Doesn't matter for legacy bindings
1022             let binding = (def, vis, item.span, expansion).to_name_binding(self.arenas);
1023             self.set_binding_parent_module(binding, self.current_module);
1024             let legacy_binding = self.arenas.alloc_legacy_binding(LegacyBinding {
1025                 parent_legacy_scope: *current_legacy_scope, binding, ident
1026             });
1027             *current_legacy_scope = LegacyScope::Binding(legacy_binding);
1028             self.all_macros.insert(ident.name, def);
1029             if attr::contains_name(&item.attrs, "macro_export") {
1030                 let module = self.graph_root;
1031                 let vis = ty::Visibility::Public;
1032                 self.define(module, ident, MacroNS,
1033                             (def, vis, item.span, expansion, IsMacroExport));
1034             } else {
1035                 if !attr::contains_name(&item.attrs, "rustc_doc_only_macro") {
1036                     self.check_reserved_macro_name(ident, MacroNS);
1037                 }
1038                 self.unused_macros.insert(def_id);
1039             }
1040         } else {
1041             let module = self.current_module;
1042             let def = Def::Macro(def_id, MacroKind::Bang);
1043             let vis = self.resolve_visibility(&item.vis);
1044             if vis != ty::Visibility::Public {
1045                 self.unused_macros.insert(def_id);
1046             }
1047             self.define(module, ident, MacroNS, (def, vis, item.span, expansion));
1048         }
1049     }
1050
1051     fn gate_legacy_custom_derive(&mut self, name: Symbol, span: Span) {
1052         if !self.session.features_untracked().custom_derive {
1053             let sess = &self.session.parse_sess;
1054             let explain = feature_gate::EXPLAIN_CUSTOM_DERIVE;
1055             emit_feature_err(sess, "custom_derive", span, GateIssue::Language, explain);
1056         } else if !self.is_whitelisted_legacy_custom_derive(name) {
1057             self.session.span_warn(span, feature_gate::EXPLAIN_DEPR_CUSTOM_DERIVE);
1058         }
1059     }
1060 }