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.
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.
11 use {AmbiguityError, CrateLint, Resolver, ResolutionError, is_known_tool, resolve_error};
12 use {Module, ModuleKind, NameBinding, NameBindingKind, PathResult, ToNameBinding};
13 use Namespace::{self, TypeNS, MacroNS};
14 use build_reduced_graph::{BuildReducedGraphVisitor, IsMacroExport};
15 use resolve_imports::ImportResolver;
16 use rustc::hir::def_id::{DefId, BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, DefIndex,
17 DefIndexAddressSpace};
18 use rustc::hir::def::{Def, Export};
19 use rustc::hir::map::{self, DefCollector};
20 use rustc::{ty, lint};
21 use rustc::middle::cstore::CrateStore;
22 use syntax::ast::{self, Name, Ident};
23 use syntax::attr::{self, HasAttrs};
24 use syntax::errors::DiagnosticBuilder;
25 use syntax::ext::base::{self, Annotatable, Determinacy, MultiModifier, MultiDecorator};
26 use syntax::ext::base::{MacroKind, SyntaxExtension, Resolver as SyntaxResolver};
27 use syntax::ext::expand::{self, AstFragment, AstFragmentKind, Invocation, InvocationKind};
28 use syntax::ext::hygiene::{self, Mark};
29 use syntax::ext::placeholders::placeholder;
30 use syntax::ext::tt::macro_rules;
31 use syntax::feature_gate::{self, feature_err, emit_feature_err, is_builtin_attr_name, GateIssue};
32 use syntax::fold::{self, Folder};
33 use syntax::parse::parser::PathStyle;
34 use syntax::parse::token::{self, Token};
36 use syntax::symbol::{Symbol, keywords};
37 use syntax::tokenstream::{TokenStream, TokenTree, Delimited};
38 use syntax::util::lev_distance::find_best_match_for_name;
39 use syntax_pos::{Span, DUMMY_SP};
43 use rustc_data_structures::sync::Lrc;
46 pub struct InvocationData<'a> {
47 pub module: Cell<Module<'a>>,
48 pub def_index: DefIndex,
49 // The scope in which the invocation path is resolved.
50 pub legacy_scope: Cell<LegacyScope<'a>>,
51 // The smallest scope that includes this invocation's expansion,
52 // or `Empty` if this invocation has not been expanded yet.
53 pub expansion: Cell<LegacyScope<'a>>,
56 impl<'a> InvocationData<'a> {
57 pub fn root(graph_root: Module<'a>) -> Self {
59 module: Cell::new(graph_root),
60 def_index: CRATE_DEF_INDEX,
61 legacy_scope: Cell::new(LegacyScope::Empty),
62 expansion: Cell::new(LegacyScope::Empty),
67 #[derive(Copy, Clone)]
68 pub enum LegacyScope<'a> {
70 Invocation(&'a InvocationData<'a>), // The scope of the invocation, not including its expansion
71 Expansion(&'a InvocationData<'a>), // The scope of the invocation, including its expansion
72 Binding(&'a LegacyBinding<'a>),
75 pub struct LegacyBinding<'a> {
76 pub parent: Cell<LegacyScope<'a>>,
82 pub struct ProcMacError {
87 warn_msg: &'static str,
90 #[derive(Copy, Clone)]
91 pub enum MacroBinding<'a> {
92 Legacy(&'a LegacyBinding<'a>),
93 Global(&'a NameBinding<'a>),
94 Modern(&'a NameBinding<'a>),
97 impl<'a> MacroBinding<'a> {
98 pub fn span(self) -> Span {
100 MacroBinding::Legacy(binding) => binding.span,
101 MacroBinding::Global(binding) | MacroBinding::Modern(binding) => binding.span,
105 pub fn binding(self) -> &'a NameBinding<'a> {
107 MacroBinding::Global(binding) | MacroBinding::Modern(binding) => binding,
108 MacroBinding::Legacy(_) => panic!("unexpected MacroBinding::Legacy"),
112 pub fn def_ignoring_ambiguity(self) -> Def {
114 MacroBinding::Legacy(binding) => Def::Macro(binding.def_id, MacroKind::Bang),
115 MacroBinding::Global(binding) | MacroBinding::Modern(binding) =>
116 binding.def_ignoring_ambiguity(),
121 impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
122 fn next_node_id(&mut self) -> ast::NodeId {
123 self.session.next_node_id()
126 fn get_module_scope(&mut self, id: ast::NodeId) -> Mark {
127 let mark = Mark::fresh(Mark::root());
128 let module = self.module_map[&self.definitions.local_def_id(id)];
129 self.invocations.insert(mark, self.arenas.alloc_invocation_data(InvocationData {
130 module: Cell::new(module),
131 def_index: module.def_id().unwrap().index,
132 legacy_scope: Cell::new(LegacyScope::Empty),
133 expansion: Cell::new(LegacyScope::Empty),
138 fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item> {
139 struct EliminateCrateVar<'b, 'a: 'b, 'crateloader: 'a>(
140 &'b mut Resolver<'a, 'crateloader>, Span
143 impl<'a, 'b, 'crateloader> Folder for EliminateCrateVar<'a, 'b, 'crateloader> {
144 fn fold_path(&mut self, path: ast::Path) -> ast::Path {
145 match self.fold_qpath(None, path) {
146 (None, path) => path,
151 fn fold_qpath(&mut self, mut qself: Option<ast::QSelf>, mut path: ast::Path)
152 -> (Option<ast::QSelf>, ast::Path) {
153 qself = qself.map(|ast::QSelf { ty, path_span, position }| {
155 ty: self.fold_ty(ty),
156 path_span: self.new_span(path_span),
161 if path.segments[0].ident.name == keywords::DollarCrate.name() {
162 let module = self.0.resolve_crate_root(path.segments[0].ident);
163 path.segments[0].ident.name = keywords::CrateRoot.name();
164 if !module.is_local() {
165 let span = path.segments[0].ident.span;
166 path.segments.insert(1, match module.kind {
167 ModuleKind::Def(_, name) => ast::PathSegment::from_ident(
168 ast::Ident::with_empty_ctxt(name).with_span_pos(span)
172 if let Some(qself) = &mut qself {
180 fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
181 fold::noop_fold_mac(mac, self)
185 EliminateCrateVar(self, item.span).fold_item(item).expect_one("")
188 fn is_whitelisted_legacy_custom_derive(&self, name: Name) -> bool {
189 self.whitelisted_legacy_custom_derives.contains(&name)
192 fn visit_ast_fragment_with_placeholders(&mut self, mark: Mark, fragment: &AstFragment,
194 let invocation = self.invocations[&mark];
195 self.collect_def_ids(mark, invocation, fragment);
197 self.current_module = invocation.module.get();
198 self.current_module.unresolved_invocations.borrow_mut().remove(&mark);
199 self.unresolved_invocations_macro_export.remove(&mark);
200 self.current_module.unresolved_invocations.borrow_mut().extend(derives);
201 self.unresolved_invocations_macro_export.extend(derives);
202 for &derive in derives {
203 self.invocations.insert(derive, invocation);
205 let mut visitor = BuildReducedGraphVisitor {
207 legacy_scope: LegacyScope::Invocation(invocation),
210 fragment.visit_with(&mut visitor);
211 invocation.expansion.set(visitor.legacy_scope);
214 fn add_builtin(&mut self, ident: ast::Ident, ext: Lrc<SyntaxExtension>) {
216 krate: BUILTIN_MACROS_CRATE,
217 index: DefIndex::from_array_index(self.macro_map.len(),
218 DefIndexAddressSpace::Low),
220 let kind = ext.kind();
221 self.macro_map.insert(def_id, ext);
222 let binding = self.arenas.alloc_name_binding(NameBinding {
223 kind: NameBindingKind::Def(Def::Macro(def_id, kind), false),
225 vis: ty::Visibility::Invisible,
226 expansion: Mark::root(),
228 self.macro_prelude.insert(ident.name, binding);
231 fn resolve_imports(&mut self) {
232 ImportResolver { resolver: self }.resolve_imports()
235 // Resolves attribute and derive legacy macros from `#![plugin(..)]`.
236 fn find_legacy_attr_invoc(&mut self, attrs: &mut Vec<ast::Attribute>, allow_derive: bool)
237 -> Option<ast::Attribute> {
238 for i in 0..attrs.len() {
239 let name = attrs[i].name();
241 if self.session.plugin_attributes.borrow().iter()
242 .any(|&(ref attr_nm, _)| name == &**attr_nm) {
243 attr::mark_known(&attrs[i]);
246 match self.macro_prelude.get(&name).cloned() {
247 Some(binding) => match *binding.get_macro(self) {
248 MultiModifier(..) | MultiDecorator(..) | SyntaxExtension::AttrProcMacro(..) => {
249 return Some(attrs.remove(i))
257 if !allow_derive { return None }
259 // Check for legacy derives
260 for i in 0..attrs.len() {
261 let name = attrs[i].name();
263 if name == "derive" {
264 let result = attrs[i].parse_list(&self.session.parse_sess, |parser| {
265 parser.parse_path_allowing_meta(PathStyle::Mod)
268 let mut traits = match result {
269 Ok(traits) => traits,
276 for j in 0..traits.len() {
277 if traits[j].segments.len() > 1 {
280 let trait_name = traits[j].segments[0].ident.name;
281 let legacy_name = Symbol::intern(&format!("derive_{}", trait_name));
282 if !self.macro_prelude.contains_key(&legacy_name) {
285 let span = traits.remove(j).span;
286 self.gate_legacy_custom_derive(legacy_name, span);
287 if traits.is_empty() {
290 let mut tokens = Vec::new();
291 for (j, path) in traits.iter().enumerate() {
293 tokens.push(TokenTree::Token(attrs[i].span, Token::Comma).into());
295 for (k, segment) in path.segments.iter().enumerate() {
297 tokens.push(TokenTree::Token(path.span, Token::ModSep).into());
299 let tok = Token::from_ast_ident(segment.ident);
300 tokens.push(TokenTree::Token(path.span, tok).into());
303 attrs[i].tokens = TokenTree::Delimited(attrs[i].span, Delimited {
305 tts: TokenStream::concat(tokens).into(),
308 return Some(ast::Attribute {
309 path: ast::Path::from_ident(Ident::new(legacy_name, span)),
310 tokens: TokenStream::empty(),
311 id: attr::mk_attr_id(),
312 style: ast::AttrStyle::Outer,
313 is_sugared_doc: false,
323 fn resolve_invoc(&mut self, invoc: &mut Invocation, scope: Mark, force: bool)
324 -> Result<Option<Lrc<SyntaxExtension>>, Determinacy> {
325 let def = match invoc.kind {
326 InvocationKind::Attr { attr: None, .. } => return Ok(None),
327 _ => self.resolve_invoc_to_def(invoc, scope, force)?,
329 if let Def::Macro(_, MacroKind::ProcMacroStub) = def {
330 self.report_proc_macro_stub(invoc.span());
331 return Err(Determinacy::Determined);
332 } else if let Def::NonMacroAttr = def {
333 if let InvocationKind::Attr { .. } = invoc.kind {
334 if !self.session.features_untracked().tool_attributes {
335 feature_err(&self.session.parse_sess, "tool_attributes",
336 invoc.span(), GateIssue::Language,
337 "tool attributes are unstable").emit();
339 return Ok(Some(Lrc::new(SyntaxExtension::NonMacroAttr)));
341 self.report_non_macro_attr(invoc.path_span());
342 return Err(Determinacy::Determined);
345 let def_id = def.def_id();
347 self.macro_defs.insert(invoc.expansion_data.mark, def_id);
348 let normal_module_def_id =
349 self.macro_def_scope(invoc.expansion_data.mark).normal_ancestor_id;
350 self.definitions.add_parent_module_of_macro_def(invoc.expansion_data.mark,
351 normal_module_def_id);
353 self.unused_macros.remove(&def_id);
354 let ext = self.get_macro(def);
355 invoc.expansion_data.mark.set_default_transparency(ext.default_transparency());
356 invoc.expansion_data.mark.set_is_builtin(def_id.krate == BUILTIN_MACROS_CRATE);
360 fn resolve_macro(&mut self, scope: Mark, path: &ast::Path, kind: MacroKind, force: bool)
361 -> Result<Lrc<SyntaxExtension>, Determinacy> {
362 self.resolve_macro_to_def(scope, path, kind, force).and_then(|def| {
363 if let Def::Macro(_, MacroKind::ProcMacroStub) = def {
364 self.report_proc_macro_stub(path.span);
365 return Err(Determinacy::Determined);
366 } else if let Def::NonMacroAttr = def {
367 self.report_non_macro_attr(path.span);
368 return Err(Determinacy::Determined);
370 self.unused_macros.remove(&def.def_id());
371 Ok(self.get_macro(def))
375 fn check_unused_macros(&self) {
376 for did in self.unused_macros.iter() {
377 let id_span = match *self.macro_map[did] {
378 SyntaxExtension::NormalTT { def_info, .. } |
379 SyntaxExtension::DeclMacro { def_info, .. } => def_info,
382 if let Some((id, span)) = id_span {
383 let lint = lint::builtin::UNUSED_MACROS;
384 let msg = "unused macro definition";
385 self.session.buffer_lint(lint, id, span, msg);
387 bug!("attempted to create unused macro error, but span not available");
393 impl<'a, 'cl> Resolver<'a, 'cl> {
394 fn report_proc_macro_stub(&self, span: Span) {
395 self.session.span_err(span,
396 "can't use a procedural macro from the same crate that defines it");
399 fn report_non_macro_attr(&self, span: Span) {
400 self.session.span_err(span,
401 "expected a macro, found non-macro attribute");
404 fn resolve_invoc_to_def(&mut self, invoc: &mut Invocation, scope: Mark, force: bool)
405 -> Result<Def, Determinacy> {
406 let (attr, traits, item) = match invoc.kind {
407 InvocationKind::Attr { ref mut attr, ref traits, ref mut item } => (attr, traits, item),
408 InvocationKind::Bang { ref mac, .. } => {
409 return self.resolve_macro_to_def(scope, &mac.node.path, MacroKind::Bang, force);
411 InvocationKind::Derive { ref path, .. } => {
412 return self.resolve_macro_to_def(scope, path, MacroKind::Derive, force);
417 let path = attr.as_ref().unwrap().path.clone();
418 let mut determinacy = Determinacy::Determined;
419 match self.resolve_macro_to_def(scope, &path, MacroKind::Attr, force) {
420 Ok(def) => return Ok(def),
421 Err(Determinacy::Undetermined) => determinacy = Determinacy::Undetermined,
422 Err(Determinacy::Determined) if force => return Err(Determinacy::Determined),
423 Err(Determinacy::Determined) => {}
426 // Ok at this point we've determined that the `attr` above doesn't
427 // actually resolve at this time, so we may want to report an error.
428 // It could be the case, though, that `attr` won't ever resolve! If
429 // there's a custom derive that could be used it might declare `attr` as
430 // a custom attribute accepted by the derive. In this case we don't want
431 // to report this particular invocation as unresolved, but rather we'd
432 // want to move on to the next invocation.
434 // This loop here looks through all of the derive annotations in scope
435 // and tries to resolve them. If they themselves successfully resolve
436 // *and* the resolve mentions that this attribute's name is a registered
437 // custom attribute then we flag this attribute as known and update
438 // `invoc` above to point to the next invocation.
440 // By then returning `Undetermined` we should continue resolution to
441 // resolve the next attribute.
442 let attr_name = match path.segments.len() {
443 1 => path.segments[0].ident.name,
444 _ => return Err(determinacy),
447 match self.resolve_macro(scope, path, MacroKind::Derive, force) {
448 Ok(ext) => if let SyntaxExtension::ProcMacroDerive(_, ref inert_attrs, _) = *ext {
449 if inert_attrs.contains(&attr_name) {
450 // FIXME(jseyfried) Avoid `mem::replace` here.
451 let dummy_item = placeholder(AstFragmentKind::Items, ast::DUMMY_NODE_ID)
452 .make_items().pop().unwrap();
453 let dummy_item = Annotatable::Item(dummy_item);
454 *item = mem::replace(item, dummy_item).map_attrs(|mut attrs| {
455 let inert_attr = attr.take().unwrap();
456 attr::mark_known(&inert_attr);
457 if self.use_extern_macros {
458 *attr = expand::find_attr_invoc(&mut attrs);
460 attrs.push(inert_attr);
463 return Err(Determinacy::Undetermined)
466 Err(Determinacy::Undetermined) => determinacy = Determinacy::Undetermined,
467 Err(Determinacy::Determined) => {}
474 fn resolve_macro_to_def(&mut self, scope: Mark, path: &ast::Path, kind: MacroKind, force: bool)
475 -> Result<Def, Determinacy> {
476 let def = self.resolve_macro_to_def_inner(scope, path, kind, force);
477 if def != Err(Determinacy::Undetermined) {
478 // Do not report duplicated errors on every undetermined resolution.
479 path.segments.iter().find(|segment| segment.args.is_some()).map(|segment| {
480 self.session.span_err(segment.args.as_ref().unwrap().span(),
481 "generic arguments in macro path");
484 if kind != MacroKind::Bang && path.segments.len() > 1 && def != Ok(Def::NonMacroAttr) {
485 if !self.session.features_untracked().proc_macro_path_invoc {
487 &self.session.parse_sess,
488 "proc_macro_path_invoc",
491 "paths of length greater than one in macro invocations are \
499 pub fn resolve_macro_to_def_inner(&mut self, scope: Mark, path: &ast::Path,
500 kind: MacroKind, force: bool)
501 -> Result<Def, Determinacy> {
502 let ast::Path { ref segments, span } = *path;
503 let mut path: Vec<_> = segments.iter().map(|seg| seg.ident).collect();
504 let invocation = self.invocations[&scope];
505 let module = invocation.module.get();
506 self.current_module = if module.is_trait() { module.parent.unwrap() } else { module };
508 // Possibly apply the macro helper hack
509 if self.use_extern_macros && kind == MacroKind::Bang && path.len() == 1 &&
510 path[0].span.ctxt().outer().expn_info().map_or(false, |info| info.local_inner_macros) {
511 let root = Ident::new(keywords::DollarCrate.name(), path[0].span);
512 path.insert(0, root);
516 if !self.use_extern_macros && self.gated_errors.insert(span) {
517 let msg = "non-ident macro paths are experimental";
518 let feature = "use_extern_macros";
519 emit_feature_err(&self.session.parse_sess, feature, span, GateIssue::Language, msg);
520 self.found_unresolved_macro = true;
521 return Err(Determinacy::Determined);
524 let def = match self.resolve_path(&path, Some(MacroNS), false, span, CrateLint::No) {
525 PathResult::NonModule(path_res) => match path_res.base_def() {
526 Def::Err => Err(Determinacy::Determined),
528 if path_res.unresolved_segments() > 0 {
529 self.found_unresolved_macro = true;
530 self.session.span_err(span, "fail to resolve non-ident macro path");
531 Err(Determinacy::Determined)
537 PathResult::Module(..) => unreachable!(),
538 PathResult::Indeterminate if !force => return Err(Determinacy::Undetermined),
540 self.found_unresolved_macro = true;
541 Err(Determinacy::Determined)
544 self.current_module.nearest_item_scope().macro_resolutions.borrow_mut()
545 .push((path.into_boxed_slice(), span));
549 let legacy_resolution = self.resolve_legacy_scope(&invocation.legacy_scope, path[0], false);
550 let result = if let Some(MacroBinding::Legacy(binding)) = legacy_resolution {
551 Ok(Def::Macro(binding.def_id, MacroKind::Bang))
553 match self.resolve_lexical_macro_path_segment(path[0], MacroNS, false, span) {
554 Ok(binding) => Ok(binding.binding().def_ignoring_ambiguity()),
555 Err(Determinacy::Undetermined) if !force => return Err(Determinacy::Undetermined),
557 self.found_unresolved_macro = true;
558 Err(Determinacy::Determined)
563 self.current_module.nearest_item_scope().legacy_macro_resolutions.borrow_mut()
564 .push((scope, path[0], kind, result.ok()));
569 // Resolve the initial segment of a non-global macro path
570 // (e.g. `foo` in `foo::bar!(); or `foo!();`).
571 // This is a variation of `fn resolve_ident_in_lexical_scope` that can be run during
572 // expansion and import resolution (perhaps they can be merged in the future).
573 pub fn resolve_lexical_macro_path_segment(&mut self,
578 -> Result<MacroBinding<'a>, Determinacy> {
579 // General principles:
580 // 1. Not controlled (user-defined) names should have higher priority than controlled names
581 // built into the language or standard library. This way we can add new names into the
582 // language or standard library without breaking user code.
583 // 2. "Closed set" below means new names can appear after the current resolution attempt.
584 // Places to search (in order of decreasing priority):
586 // 1. FIXME: Ribs (type parameters), there's no necessary infrastructure yet
587 // (open set, not controlled).
588 // 2. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
589 // (open, not controlled).
590 // 3. Extern prelude (closed, not controlled).
591 // 4. Tool modules (closed, controlled right now, but not in the future).
592 // 5. Standard library prelude (de-facto closed, controlled).
593 // 6. Language prelude (closed, controlled).
595 // 1. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
596 // (open, not controlled).
597 // 2. Macro prelude (language, standard library, user-defined legacy plugins lumped into
598 // one set) (open, the open part is from macro expansions, not controlled).
599 // 2a. User-defined prelude from macro-use
600 // (open, the open part is from macro expansions, not controlled).
601 // 2b. Standard library prelude, currently just a macro-use (closed, controlled)
602 // 2c. Language prelude, perhaps including builtin attributes
603 // (closed, controlled, except for legacy plugins).
604 // 3. Builtin attributes (closed, controlled).
606 assert!(ns == TypeNS || ns == MacroNS);
607 ident = ident.modern();
609 // Names from inner scope that can't shadow names from outer scopes, e.g.
612 // use prefix::*; // if this imports another `m`, then it can't shadow the outer `m`
613 // // and we have and ambiguity error
616 // This includes names from globs and from macro expansions.
617 let mut potentially_ambiguous_result: Option<MacroBinding> = None;
619 enum WhereToResolve<'a> {
629 // Go through all the scopes and try to resolve the name.
630 let mut where_to_resolve = WhereToResolve::Module(self.current_module);
631 let mut use_prelude = !self.current_module.no_implicit_prelude;
633 let result = match where_to_resolve {
634 WhereToResolve::Module(module) => {
635 let orig_current_module = mem::replace(&mut self.current_module, module);
636 let binding = self.resolve_ident_in_module_unadjusted(
637 module, ident, ns, true, record_used, path_span,
639 self.current_module = orig_current_module;
640 binding.map(MacroBinding::Modern)
642 WhereToResolve::MacroPrelude => {
643 match self.macro_prelude.get(&ident.name).cloned() {
644 Some(binding) => Ok(MacroBinding::Global(binding)),
645 None => Err(Determinacy::Determined),
648 WhereToResolve::BuiltinAttrs => {
649 if is_builtin_attr_name(ident.name) {
650 let binding = (Def::NonMacroAttr, ty::Visibility::Public,
651 ident.span, Mark::root()).to_name_binding(self.arenas);
652 Ok(MacroBinding::Global(binding))
654 Err(Determinacy::Determined)
657 WhereToResolve::ExternPrelude => {
658 if use_prelude && self.extern_prelude.contains(&ident.name) {
659 if !self.session.features_untracked().extern_prelude &&
660 !self.ignore_extern_prelude_feature {
661 feature_err(&self.session.parse_sess, "extern_prelude",
662 ident.span, GateIssue::Language,
663 "access to extern crates through prelude is experimental")
668 self.crate_loader.process_path_extern(ident.name, ident.span);
670 self.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX });
671 self.populate_module_if_necessary(crate_root);
673 let binding = (crate_root, ty::Visibility::Public,
674 ident.span, Mark::root()).to_name_binding(self.arenas);
675 Ok(MacroBinding::Global(binding))
677 Err(Determinacy::Determined)
680 WhereToResolve::ToolPrelude => {
681 if use_prelude && is_known_tool(ident.name) {
682 let binding = (Def::ToolMod, ty::Visibility::Public,
683 ident.span, Mark::root()).to_name_binding(self.arenas);
684 Ok(MacroBinding::Global(binding))
686 Err(Determinacy::Determined)
689 WhereToResolve::StdLibPrelude => {
690 let mut result = Err(Determinacy::Determined);
692 if let Some(prelude) = self.prelude {
694 self.resolve_ident_in_module_unadjusted(prelude, ident, ns,
695 false, false, path_span) {
696 result = Ok(MacroBinding::Global(binding));
702 WhereToResolve::PrimitiveTypes => {
703 if let Some(prim_ty) =
704 self.primitive_type_table.primitive_types.get(&ident.name).cloned() {
705 let binding = (Def::PrimTy(prim_ty), ty::Visibility::Public,
706 ident.span, Mark::root()).to_name_binding(self.arenas);
707 Ok(MacroBinding::Global(binding))
709 Err(Determinacy::Determined)
714 macro_rules! continue_search { () => {
715 where_to_resolve = match where_to_resolve {
716 WhereToResolve::Module(module) => {
717 match self.hygienic_lexical_parent(module, &mut ident.span) {
718 Some(parent_module) => WhereToResolve::Module(parent_module),
720 use_prelude = !module.no_implicit_prelude;
722 WhereToResolve::MacroPrelude
724 WhereToResolve::ExternPrelude
729 WhereToResolve::MacroPrelude => WhereToResolve::BuiltinAttrs,
730 WhereToResolve::BuiltinAttrs => break, // nowhere else to search
731 WhereToResolve::ExternPrelude => WhereToResolve::ToolPrelude,
732 WhereToResolve::ToolPrelude => WhereToResolve::StdLibPrelude,
733 WhereToResolve::StdLibPrelude => WhereToResolve::PrimitiveTypes,
734 WhereToResolve::PrimitiveTypes => break, // nowhere else to search
746 let binding = result.binding();
748 // Found a solution that is ambiguous with a previously found solution.
749 // Push an ambiguity error for later reporting and
750 // return something for better recovery.
751 if let Some(previous_result) = potentially_ambiguous_result {
752 if binding.def() != previous_result.binding().def() {
753 self.ambiguity_errors.push(AmbiguityError {
756 b1: previous_result.binding(),
760 return Ok(previous_result);
764 // Found a solution that's not an ambiguity yet, but is "suspicious" and
765 // can participate in ambiguities later on.
766 // Remember it and go search for other solutions in outer scopes.
767 if binding.is_glob_import() || binding.expansion != Mark::root() {
768 potentially_ambiguous_result = Some(result);
773 // Found a solution that can't be ambiguous, great success.
776 Err(Determinacy::Determined) => {
779 Err(Determinacy::Undetermined) => return Err(Determinacy::Undetermined),
783 // Previously found potentially ambiguous result turned out to not be ambiguous after all.
784 if let Some(previous_result) = potentially_ambiguous_result {
785 return Ok(previous_result);
788 if record_used { Err(Determinacy::Determined) } else { Err(Determinacy::Undetermined) }
791 pub fn resolve_legacy_scope(&mut self,
792 mut scope: &'a Cell<LegacyScope<'a>>,
795 -> Option<MacroBinding<'a>> {
796 let ident = ident.modern();
797 let mut possible_time_travel = None;
798 let mut relative_depth: u32 = 0;
799 let mut binding = None;
802 LegacyScope::Empty => break,
803 LegacyScope::Expansion(invocation) => {
804 match invocation.expansion.get() {
805 LegacyScope::Invocation(_) => scope.set(invocation.legacy_scope.get()),
806 LegacyScope::Empty => {
807 if possible_time_travel.is_none() {
808 possible_time_travel = Some(scope);
810 scope = &invocation.legacy_scope;
814 scope = &invocation.expansion;
818 LegacyScope::Invocation(invocation) => {
819 relative_depth = relative_depth.saturating_sub(1);
820 scope = &invocation.legacy_scope;
822 LegacyScope::Binding(potential_binding) => {
823 if potential_binding.ident == ident {
824 if (!self.use_extern_macros || record_used) && relative_depth > 0 {
825 self.disallowed_shadowing.push(potential_binding);
827 binding = Some(potential_binding);
830 scope = &potential_binding.parent;
835 let binding = if let Some(binding) = binding {
836 MacroBinding::Legacy(binding)
837 } else if let Some(binding) = self.macro_prelude.get(&ident.name).cloned() {
838 if !self.use_extern_macros {
839 self.record_use(ident, MacroNS, binding, DUMMY_SP);
841 MacroBinding::Global(binding)
846 if !self.use_extern_macros {
847 if let Some(scope) = possible_time_travel {
848 // Check for disallowed shadowing later
849 self.lexical_macro_resolutions.push((ident, scope));
856 pub fn finalize_current_module_macro_resolutions(&mut self) {
857 let module = self.current_module;
858 for &(ref path, span) in module.macro_resolutions.borrow().iter() {
859 match self.resolve_path(&path, Some(MacroNS), true, span, CrateLint::No) {
860 PathResult::NonModule(_) => {},
861 PathResult::Failed(span, msg, _) => {
862 resolve_error(self, span, ResolutionError::FailedToResolve(&msg));
868 for &(mark, ident, kind, def) in module.legacy_macro_resolutions.borrow().iter() {
869 let span = ident.span;
870 let legacy_scope = &self.invocations[&mark].legacy_scope;
871 let legacy_resolution = self.resolve_legacy_scope(legacy_scope, ident, true);
872 let resolution = self.resolve_lexical_macro_path_segment(ident, MacroNS, true, span);
874 let check_consistency = |this: &Self, binding: MacroBinding| {
875 if let Some(def) = def {
876 if this.ambiguity_errors.is_empty() && this.disallowed_shadowing.is_empty() &&
877 binding.def_ignoring_ambiguity() != def {
878 // Make sure compilation does not succeed if preferred macro resolution
879 // has changed after the macro had been expanded. In theory all such
880 // situations should be reported as ambiguity errors, so this is span-bug.
881 span_bug!(span, "inconsistent resolution for a macro");
884 // It's possible that the macro was unresolved (indeterminate) and silently
885 // expanded into a dummy fragment for recovery during expansion.
886 // Now, post-expansion, the resolution may succeed, but we can't change the
887 // past and need to report an error.
889 format!("cannot determine resolution for the {} `{}`", kind.descr(), ident);
890 let msg_note = "import resolution is stuck, try simplifying macro imports";
891 this.session.struct_span_err(span, &msg).note(msg_note).emit();
895 match (legacy_resolution, resolution) {
896 (Some(MacroBinding::Legacy(legacy_binding)), Ok(MacroBinding::Modern(binding))) => {
897 if legacy_binding.def_id != binding.def_ignoring_ambiguity().def_id() {
898 let msg1 = format!("`{}` could refer to the macro defined here", ident);
900 format!("`{}` could also refer to the macro imported here", ident);
901 self.session.struct_span_err(span, &format!("`{}` is ambiguous", ident))
902 .span_note(legacy_binding.span, &msg1)
903 .span_note(binding.span, &msg2)
908 assert!(def.is_none());
909 let bang = if kind == MacroKind::Bang { "!" } else { "" };
911 format!("cannot find {} `{}{}` in this scope", kind.descr(), ident, bang);
912 let mut err = self.session.struct_span_err(span, &msg);
913 self.suggest_macro_name(&ident.as_str(), kind, &mut err, span);
916 (Some(MacroBinding::Modern(_)), _) | (_, Ok(MacroBinding::Legacy(_))) => {
917 span_bug!(span, "impossible macro resolution result");
919 // OK, unambiguous resolution
920 (Some(binding), Err(_)) | (None, Ok(binding)) |
921 // OK, legacy wins over global even if their definitions are different
922 (Some(binding @ MacroBinding::Legacy(_)), Ok(MacroBinding::Global(_))) |
923 // OK, modern wins over global even if their definitions are different
924 (Some(MacroBinding::Global(_)), Ok(binding @ MacroBinding::Modern(_))) => {
925 check_consistency(self, binding);
927 (Some(MacroBinding::Global(binding1)), Ok(MacroBinding::Global(binding2))) => {
928 if binding1.def() != binding2.def() {
929 span_bug!(span, "mismatch between same global macro resolutions");
931 check_consistency(self, MacroBinding::Global(binding1));
933 self.record_use(ident, MacroNS, binding1, span);
934 self.err_if_macro_use_proc_macro(ident.name, span, binding1);
940 fn suggest_macro_name(&mut self, name: &str, kind: MacroKind,
941 err: &mut DiagnosticBuilder<'a>, span: Span) {
942 // First check if this is a locally-defined bang macro.
943 let suggestion = if let MacroKind::Bang = kind {
944 find_best_match_for_name(self.macro_names.iter().map(|ident| &ident.name), name, None)
947 // Then check global macros.
949 // FIXME: get_macro needs an &mut Resolver, can we do it without cloning?
950 let macro_prelude = self.macro_prelude.clone();
951 let names = macro_prelude.iter().filter_map(|(name, binding)| {
952 if binding.get_macro(self).kind() == kind {
958 find_best_match_for_name(names, name, None)
959 // Then check modules.
961 if !self.use_extern_macros {
964 let is_macro = |def| {
965 if let Def::Macro(_, def_kind) = def {
971 let ident = Ident::new(Symbol::intern(name), span);
972 self.lookup_typo_candidate(&[ident], MacroNS, is_macro, span)
975 if let Some(suggestion) = suggestion {
976 if suggestion != name {
977 if let MacroKind::Bang = kind {
978 err.span_suggestion(span, "you could try the macro", suggestion.to_string());
980 err.span_suggestion(span, "try", suggestion.to_string());
983 err.help("have you added the `#[macro_use]` on the module/import?");
988 fn collect_def_ids(&mut self,
990 invocation: &'a InvocationData<'a>,
991 fragment: &AstFragment) {
992 let Resolver { ref mut invocations, arenas, graph_root, .. } = *self;
993 let InvocationData { def_index, .. } = *invocation;
995 let visit_macro_invoc = &mut |invoc: map::MacroInvocationData| {
996 invocations.entry(invoc.mark).or_insert_with(|| {
997 arenas.alloc_invocation_data(InvocationData {
998 def_index: invoc.def_index,
999 module: Cell::new(graph_root),
1000 expansion: Cell::new(LegacyScope::Empty),
1001 legacy_scope: Cell::new(LegacyScope::Empty),
1006 let mut def_collector = DefCollector::new(&mut self.definitions, mark);
1007 def_collector.visit_macro_invoc = Some(visit_macro_invoc);
1008 def_collector.with_parent(def_index, |def_collector| {
1009 fragment.visit_with(def_collector)
1013 pub fn define_macro(&mut self,
1016 legacy_scope: &mut LegacyScope<'a>) {
1017 self.local_macro_def_scopes.insert(item.id, self.current_module);
1018 let ident = item.ident;
1019 if ident.name == "macro_rules" {
1020 self.session.span_err(item.span, "user-defined macros may not be named `macro_rules`");
1023 let def_id = self.definitions.local_def_id(item.id);
1024 let ext = Lrc::new(macro_rules::compile(&self.session.parse_sess,
1025 &self.session.features_untracked(),
1026 item, hygiene::default_edition()));
1027 self.macro_map.insert(def_id, ext);
1029 let def = match item.node { ast::ItemKind::MacroDef(ref def) => def, _ => unreachable!() };
1031 let ident = ident.modern();
1032 self.macro_names.insert(ident);
1033 *legacy_scope = LegacyScope::Binding(self.arenas.alloc_legacy_binding(LegacyBinding {
1034 parent: Cell::new(*legacy_scope), ident: ident, def_id: def_id, span: item.span,
1036 let def = Def::Macro(def_id, MacroKind::Bang);
1037 self.all_macros.insert(ident.name, def);
1038 if attr::contains_name(&item.attrs, "macro_export") {
1039 if self.use_extern_macros {
1040 let module = self.graph_root;
1041 let vis = ty::Visibility::Public;
1042 self.define(module, ident, MacroNS,
1043 (def, vis, item.span, expansion, IsMacroExport));
1045 self.macro_exports.push(Export {
1046 ident: ident.modern(),
1048 vis: ty::Visibility::Public,
1053 self.unused_macros.insert(def_id);
1056 let module = self.current_module;
1057 let def = Def::Macro(def_id, MacroKind::Bang);
1058 let vis = self.resolve_visibility(&item.vis);
1059 if vis != ty::Visibility::Public {
1060 self.unused_macros.insert(def_id);
1062 self.define(module, ident, MacroNS, (def, vis, item.span, expansion));
1066 /// Error if `ext` is a Macros 1.1 procedural macro being imported by `#[macro_use]`
1067 fn err_if_macro_use_proc_macro(&mut self, name: Name, use_span: Span,
1068 binding: &NameBinding<'a>) {
1069 let krate = binding.def().def_id().krate;
1071 // Plugin-based syntax extensions are exempt from this check
1072 if krate == BUILTIN_MACROS_CRATE { return; }
1074 let ext = binding.get_macro(self);
1077 // If `ext` is a procedural macro, check if we've already warned about it
1078 SyntaxExtension::AttrProcMacro(..) | SyntaxExtension::ProcMacro { .. } =>
1079 if !self.warned_proc_macros.insert(name) { return; },
1083 let warn_msg = match *ext {
1084 SyntaxExtension::AttrProcMacro(..) =>
1085 "attribute procedural macros cannot be imported with `#[macro_use]`",
1086 SyntaxExtension::ProcMacro { .. } =>
1087 "procedural macros cannot be imported with `#[macro_use]`",
1091 let def_id = self.current_module.normal_ancestor_id;
1092 let node_id = self.definitions.as_local_node_id(def_id).unwrap();
1094 self.proc_mac_errors.push(ProcMacError {
1095 crate_name: self.cstore.crate_name_untracked(krate),
1103 pub fn report_proc_macro_import(&mut self, krate: &ast::Crate) {
1104 for err in self.proc_mac_errors.drain(..) {
1105 let (span, found_use) = ::UsePlacementFinder::check(krate, err.module);
1107 if let Some(span) = span {
1108 let found_use = if found_use { "" } else { "\n" };
1109 self.session.struct_span_err(err.use_span, err.warn_msg)
1112 "instead, import the procedural macro like any other item",
1113 format!("use {}::{};{}", err.crate_name, err.name, found_use),
1116 self.session.struct_span_err(err.use_span, err.warn_msg)
1117 .help(&format!("instead, import the procedural macro like any other item: \
1118 `use {}::{};`", err.crate_name, err.name))
1124 fn gate_legacy_custom_derive(&mut self, name: Symbol, span: Span) {
1125 if !self.session.features_untracked().custom_derive {
1126 let sess = &self.session.parse_sess;
1127 let explain = feature_gate::EXPLAIN_CUSTOM_DERIVE;
1128 emit_feature_err(sess, "custom_derive", span, GateIssue::Language, explain);
1129 } else if !self.is_whitelisted_legacy_custom_derive(name) {
1130 self.session.span_warn(span, feature_gate::EXPLAIN_DEPR_CUSTOM_DERIVE);