]> git.lizzy.rs Git - rust.git/blob - src/librustc_resolve/macros.rs
report kind of deprecated item in message
[rust.git] / src / librustc_resolve / macros.rs
1 //! A bunch of methods and structures more or less related to resolving macros and
2 //! interface provided by `Resolver` to macro expander.
3
4 use crate::imports::ImportResolver;
5 use crate::Namespace::*;
6 use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, Determinacy};
7 use crate::{CrateLint, ParentScope, ResolutionError, Resolver, Scope, ScopeSet, Weak};
8 use crate::{ModuleKind, ModuleOrUniformRoot, NameBinding, PathResult, Segment, ToNameBinding};
9 use rustc_ast::ast::{self, NodeId};
10 use rustc_ast_lowering::ResolverAstLowering;
11 use rustc_ast_pretty::pprust;
12 use rustc_attr::{self as attr, StabilityLevel};
13 use rustc_data_structures::fx::FxHashSet;
14 use rustc_expand::base::{Indeterminate, InvocationRes, ResolverExpand, SyntaxExtension};
15 use rustc_expand::compile_declarative_macro;
16 use rustc_expand::expand::{AstFragment, AstFragmentKind, Invocation, InvocationKind};
17 use rustc_feature::is_builtin_attr_name;
18 use rustc_hir::def::{self, DefKind, NonMacroAttrKind};
19 use rustc_hir::def_id;
20 use rustc_middle::middle::stability;
21 use rustc_middle::{span_bug, ty};
22 use rustc_session::lint::builtin::UNUSED_MACROS;
23 use rustc_session::Session;
24 use rustc_span::edition::Edition;
25 use rustc_span::hygiene::{self, ExpnData, ExpnId, ExpnKind};
26 use rustc_span::symbol::{kw, sym, Ident, Symbol};
27 use rustc_span::{Span, DUMMY_SP};
28
29 use rustc_data_structures::sync::Lrc;
30 use rustc_span::hygiene::{AstPass, MacroKind};
31 use std::{mem, ptr};
32
33 type Res = def::Res<NodeId>;
34
35 /// Binding produced by a `macro_rules` item.
36 /// Not modularized, can shadow previous `macro_rules` bindings, etc.
37 #[derive(Debug)]
38 pub struct MacroRulesBinding<'a> {
39     crate binding: &'a NameBinding<'a>,
40     /// `macro_rules` scope into which the `macro_rules` item was planted.
41     crate parent_macro_rules_scope: MacroRulesScope<'a>,
42     crate ident: Ident,
43 }
44
45 /// The scope introduced by a `macro_rules!` macro.
46 /// This starts at the macro's definition and ends at the end of the macro's parent
47 /// module (named or unnamed), or even further if it escapes with `#[macro_use]`.
48 /// Some macro invocations need to introduce `macro_rules` scopes too because they
49 /// can potentially expand into macro definitions.
50 #[derive(Copy, Clone, Debug)]
51 pub enum MacroRulesScope<'a> {
52     /// Empty "root" scope at the crate start containing no names.
53     Empty,
54     /// The scope introduced by a `macro_rules!` macro definition.
55     Binding(&'a MacroRulesBinding<'a>),
56     /// The scope introduced by a macro invocation that can potentially
57     /// create a `macro_rules!` macro definition.
58     Invocation(ExpnId),
59 }
60
61 // Macro namespace is separated into two sub-namespaces, one for bang macros and
62 // one for attribute-like macros (attributes, derives).
63 // We ignore resolutions from one sub-namespace when searching names in scope for another.
64 fn sub_namespace_match(candidate: Option<MacroKind>, requirement: Option<MacroKind>) -> bool {
65     #[derive(PartialEq)]
66     enum SubNS {
67         Bang,
68         AttrLike,
69     }
70     let sub_ns = |kind| match kind {
71         MacroKind::Bang => SubNS::Bang,
72         MacroKind::Attr | MacroKind::Derive => SubNS::AttrLike,
73     };
74     let candidate = candidate.map(sub_ns);
75     let requirement = requirement.map(sub_ns);
76     // "No specific sub-namespace" means "matches anything" for both requirements and candidates.
77     candidate.is_none() || requirement.is_none() || candidate == requirement
78 }
79
80 // We don't want to format a path using pretty-printing,
81 // `format!("{}", path)`, because that tries to insert
82 // line-breaks and is slow.
83 fn fast_print_path(path: &ast::Path) -> Symbol {
84     if path.segments.len() == 1 {
85         path.segments[0].ident.name
86     } else {
87         let mut path_str = String::with_capacity(64);
88         for (i, segment) in path.segments.iter().enumerate() {
89             if i != 0 {
90                 path_str.push_str("::");
91             }
92             if segment.ident.name != kw::PathRoot {
93                 path_str.push_str(&segment.ident.as_str())
94             }
95         }
96         Symbol::intern(&path_str)
97     }
98 }
99
100 /// The code common between processing `#![register_tool]` and `#![register_attr]`.
101 fn registered_idents(
102     sess: &Session,
103     attrs: &[ast::Attribute],
104     attr_name: Symbol,
105     descr: &str,
106 ) -> FxHashSet<Ident> {
107     let mut registered = FxHashSet::default();
108     for attr in attr::filter_by_name(attrs, attr_name) {
109         for nested_meta in attr.meta_item_list().unwrap_or_default() {
110             match nested_meta.ident() {
111                 Some(ident) => {
112                     if let Some(old_ident) = registered.replace(ident) {
113                         let msg = format!("{} `{}` was already registered", descr, ident);
114                         sess.struct_span_err(ident.span, &msg)
115                             .span_label(old_ident.span, "already registered here")
116                             .emit();
117                     }
118                 }
119                 None => {
120                     let msg = format!("`{}` only accepts identifiers", attr_name);
121                     let span = nested_meta.span();
122                     sess.struct_span_err(span, &msg).span_label(span, "not an identifier").emit();
123                 }
124             }
125         }
126     }
127     registered
128 }
129
130 crate fn registered_attrs_and_tools(
131     sess: &Session,
132     attrs: &[ast::Attribute],
133 ) -> (FxHashSet<Ident>, FxHashSet<Ident>) {
134     let registered_attrs = registered_idents(sess, attrs, sym::register_attr, "attribute");
135     let mut registered_tools = registered_idents(sess, attrs, sym::register_tool, "tool");
136     // We implicitly add `rustfmt` and `clippy` to known tools,
137     // but it's not an error to register them explicitly.
138     let predefined_tools = [sym::clippy, sym::rustfmt];
139     registered_tools.extend(predefined_tools.iter().cloned().map(Ident::with_dummy_span));
140     (registered_attrs, registered_tools)
141 }
142
143 impl<'a> ResolverExpand for Resolver<'a> {
144     fn next_node_id(&mut self) -> NodeId {
145         self.next_node_id()
146     }
147
148     fn resolve_dollar_crates(&mut self) {
149         hygiene::update_dollar_crate_names(|ctxt| {
150             let ident = Ident::new(kw::DollarCrate, DUMMY_SP.with_ctxt(ctxt));
151             match self.resolve_crate_root(ident).kind {
152                 ModuleKind::Def(.., name) if name != kw::Invalid => name,
153                 _ => kw::Crate,
154             }
155         });
156     }
157
158     fn visit_ast_fragment_with_placeholders(&mut self, expansion: ExpnId, fragment: &AstFragment) {
159         // Integrate the new AST fragment into all the definition and module structures.
160         // We are inside the `expansion` now, but other parent scope components are still the same.
161         let parent_scope = ParentScope { expansion, ..self.invocation_parent_scopes[&expansion] };
162         let output_macro_rules_scope = self.build_reduced_graph(fragment, parent_scope);
163         self.output_macro_rules_scopes.insert(expansion, output_macro_rules_scope);
164
165         parent_scope.module.unexpanded_invocations.borrow_mut().remove(&expansion);
166     }
167
168     fn register_builtin_macro(&mut self, ident: Ident, ext: SyntaxExtension) {
169         if self.builtin_macros.insert(ident.name, ext).is_some() {
170             self.session
171                 .span_err(ident.span, &format!("built-in macro `{}` was already defined", ident));
172         }
173     }
174
175     // Create a new Expansion with a definition site of the provided module, or
176     // a fake empty `#[no_implicit_prelude]` module if no module is provided.
177     fn expansion_for_ast_pass(
178         &mut self,
179         call_site: Span,
180         pass: AstPass,
181         features: &[Symbol],
182         parent_module_id: Option<NodeId>,
183     ) -> ExpnId {
184         let expn_id = ExpnId::fresh(Some(ExpnData::allow_unstable(
185             ExpnKind::AstPass(pass),
186             call_site,
187             self.session.edition(),
188             features.into(),
189             None,
190         )));
191
192         let parent_scope = if let Some(module_id) = parent_module_id {
193             let parent_def_id = self.local_def_id(module_id);
194             self.definitions.add_parent_module_of_macro_def(expn_id, parent_def_id.to_def_id());
195             self.module_map[&parent_def_id]
196         } else {
197             self.definitions.add_parent_module_of_macro_def(
198                 expn_id,
199                 def_id::DefId::local(def_id::CRATE_DEF_INDEX),
200             );
201             self.empty_module
202         };
203         self.ast_transform_scopes.insert(expn_id, parent_scope);
204         expn_id
205     }
206
207     fn resolve_imports(&mut self) {
208         ImportResolver { r: self }.resolve_imports()
209     }
210
211     fn resolve_macro_invocation(
212         &mut self,
213         invoc: &Invocation,
214         eager_expansion_root: ExpnId,
215         force: bool,
216     ) -> Result<InvocationRes, Indeterminate> {
217         let invoc_id = invoc.expansion_data.id;
218         let parent_scope = match self.invocation_parent_scopes.get(&invoc_id) {
219             Some(parent_scope) => *parent_scope,
220             None => {
221                 // If there's no entry in the table, then we are resolving an eagerly expanded
222                 // macro, which should inherit its parent scope from its eager expansion root -
223                 // the macro that requested this eager expansion.
224                 let parent_scope = *self
225                     .invocation_parent_scopes
226                     .get(&eager_expansion_root)
227                     .expect("non-eager expansion without a parent scope");
228                 self.invocation_parent_scopes.insert(invoc_id, parent_scope);
229                 parent_scope
230             }
231         };
232
233         let (path, kind, derives, after_derive) = match invoc.kind {
234             InvocationKind::Attr { ref attr, ref derives, after_derive, .. } => (
235                 &attr.get_normal_item().path,
236                 MacroKind::Attr,
237                 self.arenas.alloc_ast_paths(derives),
238                 after_derive,
239             ),
240             InvocationKind::Bang { ref mac, .. } => (&mac.path, MacroKind::Bang, &[][..], false),
241             InvocationKind::Derive { ref path, .. } => (path, MacroKind::Derive, &[][..], false),
242             InvocationKind::DeriveContainer { ref derives, .. } => {
243                 // Block expansion of the container until we resolve all derives in it.
244                 // This is required for two reasons:
245                 // - Derive helper attributes are in scope for the item to which the `#[derive]`
246                 //   is applied, so they have to be produced by the container's expansion rather
247                 //   than by individual derives.
248                 // - Derives in the container need to know whether one of them is a built-in `Copy`.
249                 // FIXME: Try to avoid repeated resolutions for derives here and in expansion.
250                 let mut exts = Vec::new();
251                 let mut helper_attrs = Vec::new();
252                 for path in derives {
253                     exts.push(
254                         match self.resolve_macro_path(
255                             path,
256                             Some(MacroKind::Derive),
257                             &parent_scope,
258                             true,
259                             force,
260                         ) {
261                             Ok((Some(ext), _)) => {
262                                 let span = path
263                                     .segments
264                                     .last()
265                                     .unwrap()
266                                     .ident
267                                     .span
268                                     .normalize_to_macros_2_0();
269                                 helper_attrs.extend(
270                                     ext.helper_attrs.iter().map(|name| Ident::new(*name, span)),
271                                 );
272                                 if ext.is_derive_copy {
273                                     self.add_derive_copy(invoc_id);
274                                 }
275                                 ext
276                             }
277                             Ok(_) | Err(Determinacy::Determined) => {
278                                 self.dummy_ext(MacroKind::Derive)
279                             }
280                             Err(Determinacy::Undetermined) => return Err(Indeterminate),
281                         },
282                     )
283                 }
284                 self.helper_attrs.insert(invoc_id, helper_attrs);
285                 return Ok(InvocationRes::DeriveContainer(exts));
286             }
287         };
288
289         // Derives are not included when `invocations` are collected, so we have to add them here.
290         let parent_scope = &ParentScope { derives, ..parent_scope };
291         let node_id = self.lint_node_id(eager_expansion_root);
292         let (ext, res) = self.smart_resolve_macro_path(path, kind, parent_scope, node_id, force)?;
293
294         let span = invoc.span();
295         invoc_id.set_expn_data(ext.expn_data(
296             parent_scope.expansion,
297             span,
298             fast_print_path(path),
299             res.opt_def_id(),
300         ));
301
302         if let Res::Def(_, _) = res {
303             if after_derive {
304                 self.session.span_err(span, "macro attributes must be placed before `#[derive]`");
305             }
306             let normal_module_def_id = self.macro_def_scope(invoc_id).normal_ancestor_id;
307             self.definitions.add_parent_module_of_macro_def(invoc_id, normal_module_def_id);
308         }
309
310         match invoc.fragment_kind {
311             AstFragmentKind::Arms
312             | AstFragmentKind::Fields
313             | AstFragmentKind::FieldPats
314             | AstFragmentKind::GenericParams
315             | AstFragmentKind::Params
316             | AstFragmentKind::StructFields
317             | AstFragmentKind::Variants => {
318                 if let Res::Def(..) = res {
319                     self.session.span_err(
320                         span,
321                         &format!(
322                             "expected an inert attribute, found {} {}",
323                             res.article(),
324                             res.descr()
325                         ),
326                     );
327                     return Ok(InvocationRes::Single(self.dummy_ext(kind)));
328                 }
329             }
330             _ => {}
331         }
332
333         Ok(InvocationRes::Single(ext))
334     }
335
336     fn check_unused_macros(&mut self) {
337         for (_, &(node_id, span)) in self.unused_macros.iter() {
338             self.lint_buffer.buffer_lint(UNUSED_MACROS, node_id, span, "unused macro definition");
339         }
340     }
341
342     fn lint_node_id(&mut self, expn_id: ExpnId) -> NodeId {
343         self.invocation_parents
344             .get(&expn_id)
345             .map_or(ast::CRATE_NODE_ID, |id| self.def_id_to_node_id[*id])
346     }
347
348     fn has_derive_copy(&self, expn_id: ExpnId) -> bool {
349         self.containers_deriving_copy.contains(&expn_id)
350     }
351
352     fn add_derive_copy(&mut self, expn_id: ExpnId) {
353         self.containers_deriving_copy.insert(expn_id);
354     }
355
356     // The function that implements the resolution logic of `#[cfg_accessible(path)]`.
357     // Returns true if the path can certainly be resolved in one of three namespaces,
358     // returns false if the path certainly cannot be resolved in any of the three namespaces.
359     // Returns `Indeterminate` if we cannot give a certain answer yet.
360     fn cfg_accessible(&mut self, expn_id: ExpnId, path: &ast::Path) -> Result<bool, Indeterminate> {
361         let span = path.span;
362         let path = &Segment::from_path(path);
363         let parent_scope = self.invocation_parent_scopes[&expn_id];
364
365         let mut indeterminate = false;
366         for ns in [TypeNS, ValueNS, MacroNS].iter().copied() {
367             match self.resolve_path(path, Some(ns), &parent_scope, false, span, CrateLint::No) {
368                 PathResult::Module(ModuleOrUniformRoot::Module(_)) => return Ok(true),
369                 PathResult::NonModule(partial_res) if partial_res.unresolved_segments() == 0 => {
370                     return Ok(true);
371                 }
372                 PathResult::Indeterminate => indeterminate = true,
373                 // FIXME: `resolve_path` is not ready to report partially resolved paths
374                 // correctly, so we just report an error if the path was reported as unresolved.
375                 // This needs to be fixed for `cfg_accessible` to be useful.
376                 PathResult::NonModule(..) | PathResult::Failed { .. } => {}
377                 PathResult::Module(_) => panic!("unexpected path resolution"),
378             }
379         }
380
381         if indeterminate {
382             return Err(Indeterminate);
383         }
384
385         self.session
386             .struct_span_err(span, "not sure whether the path is accessible or not")
387             .span_note(span, "`cfg_accessible` is not fully implemented")
388             .emit();
389         Ok(false)
390     }
391 }
392
393 impl<'a> Resolver<'a> {
394     /// Resolve macro path with error reporting and recovery.
395     fn smart_resolve_macro_path(
396         &mut self,
397         path: &ast::Path,
398         kind: MacroKind,
399         parent_scope: &ParentScope<'a>,
400         node_id: NodeId,
401         force: bool,
402     ) -> Result<(Lrc<SyntaxExtension>, Res), Indeterminate> {
403         let (ext, res) = match self.resolve_macro_path(path, Some(kind), parent_scope, true, force)
404         {
405             Ok((Some(ext), res)) => (ext, res),
406             // Use dummy syntax extensions for unresolved macros for better recovery.
407             Ok((None, res)) => (self.dummy_ext(kind), res),
408             Err(Determinacy::Determined) => (self.dummy_ext(kind), Res::Err),
409             Err(Determinacy::Undetermined) => return Err(Indeterminate),
410         };
411
412         // Report errors for the resolved macro.
413         for segment in &path.segments {
414             if let Some(args) = &segment.args {
415                 self.session.span_err(args.span(), "generic arguments in macro path");
416             }
417             if kind == MacroKind::Attr && segment.ident.as_str().starts_with("rustc") {
418                 self.session.span_err(
419                     segment.ident.span,
420                     "attributes starting with `rustc` are reserved for use by the `rustc` compiler",
421                 );
422             }
423         }
424
425         match res {
426             Res::Def(DefKind::Macro(_), def_id) => {
427                 if let Some(def_id) = def_id.as_local() {
428                     self.unused_macros.remove(&def_id);
429                     if self.proc_macro_stubs.contains(&def_id) {
430                         self.session.span_err(
431                             path.span,
432                             "can't use a procedural macro from the same crate that defines it",
433                         );
434                     }
435                 }
436             }
437             Res::NonMacroAttr(..) | Res::Err => {}
438             _ => panic!("expected `DefKind::Macro` or `Res::NonMacroAttr`"),
439         };
440
441         self.check_stability_and_deprecation(&ext, path, node_id);
442
443         Ok(if ext.macro_kind() != kind {
444             let expected = kind.descr_expected();
445             let path_str = pprust::path_to_string(path);
446             let msg = format!("expected {}, found {} `{}`", expected, res.descr(), path_str);
447             self.session
448                 .struct_span_err(path.span, &msg)
449                 .span_label(path.span, format!("not {} {}", kind.article(), expected))
450                 .emit();
451             // Use dummy syntax extensions for unexpected macro kinds for better recovery.
452             (self.dummy_ext(kind), Res::Err)
453         } else {
454             (ext, res)
455         })
456     }
457
458     pub fn resolve_macro_path(
459         &mut self,
460         path: &ast::Path,
461         kind: Option<MacroKind>,
462         parent_scope: &ParentScope<'a>,
463         trace: bool,
464         force: bool,
465     ) -> Result<(Option<Lrc<SyntaxExtension>>, Res), Determinacy> {
466         let path_span = path.span;
467         let mut path = Segment::from_path(path);
468
469         // Possibly apply the macro helper hack
470         if kind == Some(MacroKind::Bang)
471             && path.len() == 1
472             && path[0].ident.span.ctxt().outer_expn_data().local_inner_macros
473         {
474             let root = Ident::new(kw::DollarCrate, path[0].ident.span);
475             path.insert(0, Segment::from_ident(root));
476         }
477
478         let res = if path.len() > 1 {
479             let res = match self.resolve_path(
480                 &path,
481                 Some(MacroNS),
482                 parent_scope,
483                 false,
484                 path_span,
485                 CrateLint::No,
486             ) {
487                 PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => {
488                     Ok(path_res.base_res())
489                 }
490                 PathResult::Indeterminate if !force => return Err(Determinacy::Undetermined),
491                 PathResult::NonModule(..)
492                 | PathResult::Indeterminate
493                 | PathResult::Failed { .. } => Err(Determinacy::Determined),
494                 PathResult::Module(..) => unreachable!(),
495             };
496
497             if trace {
498                 let kind = kind.expect("macro kind must be specified if tracing is enabled");
499                 self.multi_segment_macro_resolutions.push((
500                     path,
501                     path_span,
502                     kind,
503                     *parent_scope,
504                     res.ok(),
505                 ));
506             }
507
508             self.prohibit_imported_non_macro_attrs(None, res.ok(), path_span);
509             res
510         } else {
511             let scope_set = kind.map_or(ScopeSet::All(MacroNS, false), ScopeSet::Macro);
512             let binding = self.early_resolve_ident_in_lexical_scope(
513                 path[0].ident,
514                 scope_set,
515                 parent_scope,
516                 false,
517                 force,
518                 path_span,
519             );
520             if let Err(Determinacy::Undetermined) = binding {
521                 return Err(Determinacy::Undetermined);
522             }
523
524             if trace {
525                 let kind = kind.expect("macro kind must be specified if tracing is enabled");
526                 self.single_segment_macro_resolutions.push((
527                     path[0].ident,
528                     kind,
529                     *parent_scope,
530                     binding.ok(),
531                 ));
532             }
533
534             let res = binding.map(|binding| binding.res());
535             self.prohibit_imported_non_macro_attrs(binding.ok(), res.ok(), path_span);
536             res
537         };
538
539         res.map(|res| (self.get_macro(res), res))
540     }
541
542     // Resolve an identifier in lexical scope.
543     // This is a variation of `fn resolve_ident_in_lexical_scope` that can be run during
544     // expansion and import resolution (perhaps they can be merged in the future).
545     // The function is used for resolving initial segments of macro paths (e.g., `foo` in
546     // `foo::bar!(); or `foo!();`) and also for import paths on 2018 edition.
547     crate fn early_resolve_ident_in_lexical_scope(
548         &mut self,
549         orig_ident: Ident,
550         scope_set: ScopeSet,
551         parent_scope: &ParentScope<'a>,
552         record_used: bool,
553         force: bool,
554         path_span: Span,
555     ) -> Result<&'a NameBinding<'a>, Determinacy> {
556         bitflags::bitflags! {
557             struct Flags: u8 {
558                 const MACRO_RULES          = 1 << 0;
559                 const MODULE               = 1 << 1;
560                 const DERIVE_HELPER_COMPAT = 1 << 2;
561                 const MISC_SUGGEST_CRATE   = 1 << 3;
562                 const MISC_SUGGEST_SELF    = 1 << 4;
563                 const MISC_FROM_PRELUDE    = 1 << 5;
564             }
565         }
566
567         assert!(force || !record_used); // `record_used` implies `force`
568
569         // Make sure `self`, `super` etc produce an error when passed to here.
570         if orig_ident.is_path_segment_keyword() {
571             return Err(Determinacy::Determined);
572         }
573
574         let (ns, macro_kind, is_import) = match scope_set {
575             ScopeSet::All(ns, is_import) => (ns, None, is_import),
576             ScopeSet::AbsolutePath(ns) => (ns, None, false),
577             ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind), false),
578         };
579
580         // This is *the* result, resolution from the scope closest to the resolved identifier.
581         // However, sometimes this result is "weak" because it comes from a glob import or
582         // a macro expansion, and in this case it cannot shadow names from outer scopes, e.g.
583         // mod m { ... } // solution in outer scope
584         // {
585         //     use prefix::*; // imports another `m` - innermost solution
586         //                    // weak, cannot shadow the outer `m`, need to report ambiguity error
587         //     m::mac!();
588         // }
589         // So we have to save the innermost solution and continue searching in outer scopes
590         // to detect potential ambiguities.
591         let mut innermost_result: Option<(&NameBinding<'_>, Flags)> = None;
592         let mut determinacy = Determinacy::Determined;
593
594         // Go through all the scopes and try to resolve the name.
595         let break_result = self.visit_scopes(
596             scope_set,
597             parent_scope,
598             orig_ident,
599             |this, scope, use_prelude, ident| {
600                 let ok = |res, span, arenas| {
601                     Ok((
602                         (res, ty::Visibility::Public, span, ExpnId::root()).to_name_binding(arenas),
603                         Flags::empty(),
604                     ))
605                 };
606                 let result = match scope {
607                     Scope::DeriveHelpers(expn_id) => {
608                         if let Some(attr) = this
609                             .helper_attrs
610                             .get(&expn_id)
611                             .and_then(|attrs| attrs.iter().rfind(|i| ident == **i))
612                         {
613                             let binding = (
614                                 Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper),
615                                 ty::Visibility::Public,
616                                 attr.span,
617                                 expn_id,
618                             )
619                                 .to_name_binding(this.arenas);
620                             Ok((binding, Flags::empty()))
621                         } else {
622                             Err(Determinacy::Determined)
623                         }
624                     }
625                     Scope::DeriveHelpersCompat => {
626                         let mut result = Err(Determinacy::Determined);
627                         for derive in parent_scope.derives {
628                             let parent_scope = &ParentScope { derives: &[], ..*parent_scope };
629                             match this.resolve_macro_path(
630                                 derive,
631                                 Some(MacroKind::Derive),
632                                 parent_scope,
633                                 true,
634                                 force,
635                             ) {
636                                 Ok((Some(ext), _)) => {
637                                     if ext.helper_attrs.contains(&ident.name) {
638                                         let binding = (
639                                             Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper),
640                                             ty::Visibility::Public,
641                                             derive.span,
642                                             ExpnId::root(),
643                                         )
644                                             .to_name_binding(this.arenas);
645                                         result = Ok((binding, Flags::DERIVE_HELPER_COMPAT));
646                                         break;
647                                     }
648                                 }
649                                 Ok(_) | Err(Determinacy::Determined) => {}
650                                 Err(Determinacy::Undetermined) => {
651                                     result = Err(Determinacy::Undetermined)
652                                 }
653                             }
654                         }
655                         result
656                     }
657                     Scope::MacroRules(macro_rules_scope) => match macro_rules_scope {
658                         MacroRulesScope::Binding(macro_rules_binding)
659                             if ident == macro_rules_binding.ident =>
660                         {
661                             Ok((macro_rules_binding.binding, Flags::MACRO_RULES))
662                         }
663                         MacroRulesScope::Invocation(invoc_id)
664                             if !this.output_macro_rules_scopes.contains_key(&invoc_id) =>
665                         {
666                             Err(Determinacy::Undetermined)
667                         }
668                         _ => Err(Determinacy::Determined),
669                     },
670                     Scope::CrateRoot => {
671                         let root_ident = Ident::new(kw::PathRoot, ident.span);
672                         let root_module = this.resolve_crate_root(root_ident);
673                         let binding = this.resolve_ident_in_module_ext(
674                             ModuleOrUniformRoot::Module(root_module),
675                             ident,
676                             ns,
677                             parent_scope,
678                             record_used,
679                             path_span,
680                         );
681                         match binding {
682                             Ok(binding) => Ok((binding, Flags::MODULE | Flags::MISC_SUGGEST_CRATE)),
683                             Err((Determinacy::Undetermined, Weak::No)) => {
684                                 return Some(Err(Determinacy::determined(force)));
685                             }
686                             Err((Determinacy::Undetermined, Weak::Yes)) => {
687                                 Err(Determinacy::Undetermined)
688                             }
689                             Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
690                         }
691                     }
692                     Scope::Module(module) => {
693                         let adjusted_parent_scope = &ParentScope { module, ..*parent_scope };
694                         let binding = this.resolve_ident_in_module_unadjusted_ext(
695                             ModuleOrUniformRoot::Module(module),
696                             ident,
697                             ns,
698                             adjusted_parent_scope,
699                             true,
700                             record_used,
701                             path_span,
702                         );
703                         match binding {
704                             Ok(binding) => {
705                                 let misc_flags = if ptr::eq(module, this.graph_root) {
706                                     Flags::MISC_SUGGEST_CRATE
707                                 } else if module.is_normal() {
708                                     Flags::MISC_SUGGEST_SELF
709                                 } else {
710                                     Flags::empty()
711                                 };
712                                 Ok((binding, Flags::MODULE | misc_flags))
713                             }
714                             Err((Determinacy::Undetermined, Weak::No)) => {
715                                 return Some(Err(Determinacy::determined(force)));
716                             }
717                             Err((Determinacy::Undetermined, Weak::Yes)) => {
718                                 Err(Determinacy::Undetermined)
719                             }
720                             Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
721                         }
722                     }
723                     Scope::RegisteredAttrs => match this.registered_attrs.get(&ident).cloned() {
724                         Some(ident) => ok(
725                             Res::NonMacroAttr(NonMacroAttrKind::Registered),
726                             ident.span,
727                             this.arenas,
728                         ),
729                         None => Err(Determinacy::Determined),
730                     },
731                     Scope::MacroUsePrelude => {
732                         match this.macro_use_prelude.get(&ident.name).cloned() {
733                             Some(binding) => Ok((binding, Flags::MISC_FROM_PRELUDE)),
734                             None => Err(Determinacy::determined(
735                                 this.graph_root.unexpanded_invocations.borrow().is_empty(),
736                             )),
737                         }
738                     }
739                     Scope::BuiltinAttrs => {
740                         if is_builtin_attr_name(ident.name) {
741                             ok(Res::NonMacroAttr(NonMacroAttrKind::Builtin), DUMMY_SP, this.arenas)
742                         } else {
743                             Err(Determinacy::Determined)
744                         }
745                     }
746                     Scope::ExternPrelude => match this.extern_prelude_get(ident, !record_used) {
747                         Some(binding) => Ok((binding, Flags::empty())),
748                         None => Err(Determinacy::determined(
749                             this.graph_root.unexpanded_invocations.borrow().is_empty(),
750                         )),
751                     },
752                     Scope::ToolPrelude => match this.registered_tools.get(&ident).cloned() {
753                         Some(ident) => ok(Res::ToolMod, ident.span, this.arenas),
754                         None => Err(Determinacy::Determined),
755                     },
756                     Scope::StdLibPrelude => {
757                         let mut result = Err(Determinacy::Determined);
758                         if let Some(prelude) = this.prelude {
759                             if let Ok(binding) = this.resolve_ident_in_module_unadjusted(
760                                 ModuleOrUniformRoot::Module(prelude),
761                                 ident,
762                                 ns,
763                                 parent_scope,
764                                 false,
765                                 path_span,
766                             ) {
767                                 if use_prelude || this.is_builtin_macro(binding.res()) {
768                                     result = Ok((binding, Flags::MISC_FROM_PRELUDE));
769                                 }
770                             }
771                         }
772                         result
773                     }
774                     Scope::BuiltinTypes => {
775                         match this.primitive_type_table.primitive_types.get(&ident.name).cloned() {
776                             Some(prim_ty) => ok(Res::PrimTy(prim_ty), DUMMY_SP, this.arenas),
777                             None => Err(Determinacy::Determined),
778                         }
779                     }
780                 };
781
782                 match result {
783                     Ok((binding, flags))
784                         if sub_namespace_match(binding.macro_kind(), macro_kind) =>
785                     {
786                         if !record_used {
787                             return Some(Ok(binding));
788                         }
789
790                         if let Some((innermost_binding, innermost_flags)) = innermost_result {
791                             // Found another solution, if the first one was "weak", report an error.
792                             let (res, innermost_res) = (binding.res(), innermost_binding.res());
793                             if res != innermost_res {
794                                 let builtin = Res::NonMacroAttr(NonMacroAttrKind::Builtin);
795                                 let is_derive_helper_compat = |res, flags: Flags| {
796                                     res == Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper)
797                                         && flags.contains(Flags::DERIVE_HELPER_COMPAT)
798                                 };
799
800                                 let ambiguity_error_kind = if is_import {
801                                     Some(AmbiguityKind::Import)
802                                 } else if innermost_res == builtin || res == builtin {
803                                     Some(AmbiguityKind::BuiltinAttr)
804                                 } else if is_derive_helper_compat(innermost_res, innermost_flags)
805                                     || is_derive_helper_compat(res, flags)
806                                 {
807                                     Some(AmbiguityKind::DeriveHelper)
808                                 } else if innermost_flags.contains(Flags::MACRO_RULES)
809                                     && flags.contains(Flags::MODULE)
810                                     && !this.disambiguate_macro_rules_vs_modularized(
811                                         innermost_binding,
812                                         binding,
813                                     )
814                                     || flags.contains(Flags::MACRO_RULES)
815                                         && innermost_flags.contains(Flags::MODULE)
816                                         && !this.disambiguate_macro_rules_vs_modularized(
817                                             binding,
818                                             innermost_binding,
819                                         )
820                                 {
821                                     Some(AmbiguityKind::MacroRulesVsModularized)
822                                 } else if innermost_binding.is_glob_import() {
823                                     Some(AmbiguityKind::GlobVsOuter)
824                                 } else if innermost_binding
825                                     .may_appear_after(parent_scope.expansion, binding)
826                                 {
827                                     Some(AmbiguityKind::MoreExpandedVsOuter)
828                                 } else {
829                                     None
830                                 };
831                                 if let Some(kind) = ambiguity_error_kind {
832                                     let misc = |f: Flags| {
833                                         if f.contains(Flags::MISC_SUGGEST_CRATE) {
834                                             AmbiguityErrorMisc::SuggestCrate
835                                         } else if f.contains(Flags::MISC_SUGGEST_SELF) {
836                                             AmbiguityErrorMisc::SuggestSelf
837                                         } else if f.contains(Flags::MISC_FROM_PRELUDE) {
838                                             AmbiguityErrorMisc::FromPrelude
839                                         } else {
840                                             AmbiguityErrorMisc::None
841                                         }
842                                     };
843                                     this.ambiguity_errors.push(AmbiguityError {
844                                         kind,
845                                         ident: orig_ident,
846                                         b1: innermost_binding,
847                                         b2: binding,
848                                         misc1: misc(innermost_flags),
849                                         misc2: misc(flags),
850                                     });
851                                     return Some(Ok(innermost_binding));
852                                 }
853                             }
854                         } else {
855                             // Found the first solution.
856                             innermost_result = Some((binding, flags));
857                         }
858                     }
859                     Ok(..) | Err(Determinacy::Determined) => {}
860                     Err(Determinacy::Undetermined) => determinacy = Determinacy::Undetermined,
861                 }
862
863                 None
864             },
865         );
866
867         if let Some(break_result) = break_result {
868             return break_result;
869         }
870
871         // The first found solution was the only one, return it.
872         if let Some((binding, _)) = innermost_result {
873             return Ok(binding);
874         }
875
876         Err(Determinacy::determined(determinacy == Determinacy::Determined || force))
877     }
878
879     crate fn finalize_macro_resolutions(&mut self) {
880         let check_consistency = |this: &mut Self,
881                                  path: &[Segment],
882                                  span,
883                                  kind: MacroKind,
884                                  initial_res: Option<Res>,
885                                  res: Res| {
886             if let Some(initial_res) = initial_res {
887                 if res != initial_res && res != Res::Err && this.ambiguity_errors.is_empty() {
888                     // Make sure compilation does not succeed if preferred macro resolution
889                     // has changed after the macro had been expanded. In theory all such
890                     // situations should be reported as ambiguity errors, so this is a bug.
891                     span_bug!(span, "inconsistent resolution for a macro");
892                 }
893             } else {
894                 // It's possible that the macro was unresolved (indeterminate) and silently
895                 // expanded into a dummy fragment for recovery during expansion.
896                 // Now, post-expansion, the resolution may succeed, but we can't change the
897                 // past and need to report an error.
898                 // However, non-speculative `resolve_path` can successfully return private items
899                 // even if speculative `resolve_path` returned nothing previously, so we skip this
900                 // less informative error if the privacy error is reported elsewhere.
901                 if this.privacy_errors.is_empty() {
902                     let msg = format!(
903                         "cannot determine resolution for the {} `{}`",
904                         kind.descr(),
905                         Segment::names_to_string(path)
906                     );
907                     let msg_note = "import resolution is stuck, try simplifying macro imports";
908                     this.session.struct_span_err(span, &msg).note(msg_note).emit();
909                 }
910             }
911         };
912
913         let macro_resolutions = mem::take(&mut self.multi_segment_macro_resolutions);
914         for (mut path, path_span, kind, parent_scope, initial_res) in macro_resolutions {
915             // FIXME: Path resolution will ICE if segment IDs present.
916             for seg in &mut path {
917                 seg.id = None;
918             }
919             match self.resolve_path(
920                 &path,
921                 Some(MacroNS),
922                 &parent_scope,
923                 true,
924                 path_span,
925                 CrateLint::No,
926             ) {
927                 PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => {
928                     let res = path_res.base_res();
929                     check_consistency(self, &path, path_span, kind, initial_res, res);
930                 }
931                 path_res @ PathResult::NonModule(..) | path_res @ PathResult::Failed { .. } => {
932                     let (span, label) = if let PathResult::Failed { span, label, .. } = path_res {
933                         (span, label)
934                     } else {
935                         (
936                             path_span,
937                             format!(
938                                 "partially resolved path in {} {}",
939                                 kind.article(),
940                                 kind.descr()
941                             ),
942                         )
943                     };
944                     self.report_error(
945                         span,
946                         ResolutionError::FailedToResolve { label, suggestion: None },
947                     );
948                 }
949                 PathResult::Module(..) | PathResult::Indeterminate => unreachable!(),
950             }
951         }
952
953         let macro_resolutions = mem::take(&mut self.single_segment_macro_resolutions);
954         for (ident, kind, parent_scope, initial_binding) in macro_resolutions {
955             match self.early_resolve_ident_in_lexical_scope(
956                 ident,
957                 ScopeSet::Macro(kind),
958                 &parent_scope,
959                 true,
960                 true,
961                 ident.span,
962             ) {
963                 Ok(binding) => {
964                     let initial_res = initial_binding.map(|initial_binding| {
965                         self.record_use(ident, MacroNS, initial_binding, false);
966                         initial_binding.res()
967                     });
968                     let res = binding.res();
969                     let seg = Segment::from_ident(ident);
970                     check_consistency(self, &[seg], ident.span, kind, initial_res, res);
971                 }
972                 Err(..) => {
973                     let expected = kind.descr_expected();
974                     let msg = format!("cannot find {} `{}` in this scope", expected, ident);
975                     let mut err = self.session.struct_span_err(ident.span, &msg);
976                     self.unresolved_macro_suggestions(&mut err, kind, &parent_scope, ident);
977                     err.emit();
978                 }
979             }
980         }
981
982         let builtin_attrs = mem::take(&mut self.builtin_attrs);
983         for (ident, parent_scope) in builtin_attrs {
984             let _ = self.early_resolve_ident_in_lexical_scope(
985                 ident,
986                 ScopeSet::Macro(MacroKind::Attr),
987                 &parent_scope,
988                 true,
989                 true,
990                 ident.span,
991             );
992         }
993     }
994
995     fn check_stability_and_deprecation(
996         &mut self,
997         ext: &SyntaxExtension,
998         path: &ast::Path,
999         node_id: NodeId,
1000     ) {
1001         let span = path.span;
1002         if let Some(stability) = &ext.stability {
1003             if let StabilityLevel::Unstable { reason, issue, is_soft } = stability.level {
1004                 let feature = stability.feature;
1005                 if !self.active_features.contains(&feature) && !span.allows_unstable(feature) {
1006                     let lint_buffer = &mut self.lint_buffer;
1007                     let soft_handler =
1008                         |lint, span, msg: &_| lint_buffer.buffer_lint(lint, node_id, span, msg);
1009                     stability::report_unstable(
1010                         self.session,
1011                         feature,
1012                         reason,
1013                         issue,
1014                         is_soft,
1015                         span,
1016                         soft_handler,
1017                     );
1018                 }
1019             }
1020         }
1021         if let Some(depr) = &ext.deprecation {
1022             let path = pprust::path_to_string(&path);
1023             let (message, lint) = stability::deprecation_message(depr, "macro", &path);
1024             stability::early_report_deprecation(
1025                 &mut self.lint_buffer,
1026                 &message,
1027                 depr.suggestion,
1028                 lint,
1029                 span,
1030             );
1031         }
1032     }
1033
1034     fn prohibit_imported_non_macro_attrs(
1035         &self,
1036         binding: Option<&'a NameBinding<'a>>,
1037         res: Option<Res>,
1038         span: Span,
1039     ) {
1040         if let Some(Res::NonMacroAttr(kind)) = res {
1041             if kind != NonMacroAttrKind::Tool && binding.map_or(true, |b| b.is_import()) {
1042                 let msg =
1043                     format!("cannot use {} {} through an import", kind.article(), kind.descr());
1044                 let mut err = self.session.struct_span_err(span, &msg);
1045                 if let Some(binding) = binding {
1046                     err.span_note(binding.span, &format!("the {} imported here", kind.descr()));
1047                 }
1048                 err.emit();
1049             }
1050         }
1051     }
1052
1053     crate fn check_reserved_macro_name(&mut self, ident: Ident, res: Res) {
1054         // Reserve some names that are not quite covered by the general check
1055         // performed on `Resolver::builtin_attrs`.
1056         if ident.name == sym::cfg || ident.name == sym::cfg_attr || ident.name == sym::derive {
1057             let macro_kind = self.get_macro(res).map(|ext| ext.macro_kind());
1058             if macro_kind.is_some() && sub_namespace_match(macro_kind, Some(MacroKind::Attr)) {
1059                 self.session.span_err(
1060                     ident.span,
1061                     &format!("name `{}` is reserved in attribute namespace", ident),
1062                 );
1063             }
1064         }
1065     }
1066
1067     /// Compile the macro into a `SyntaxExtension` and possibly replace
1068     /// its expander to a pre-defined one for built-in macros.
1069     crate fn compile_macro(&mut self, item: &ast::Item, edition: Edition) -> SyntaxExtension {
1070         let mut result = compile_declarative_macro(
1071             &self.session.parse_sess,
1072             self.session.features_untracked(),
1073             item,
1074             edition,
1075         );
1076
1077         if result.is_builtin {
1078             // The macro was marked with `#[rustc_builtin_macro]`.
1079             if let Some(ext) = self.builtin_macros.remove(&item.ident.name) {
1080                 // The macro is a built-in, replace its expander function
1081                 // while still taking everything else from the source code.
1082                 result.kind = ext.kind;
1083             } else {
1084                 let msg = format!("cannot find a built-in macro with name `{}`", item.ident);
1085                 self.session.span_err(item.span, &msg);
1086             }
1087         }
1088
1089         result
1090     }
1091 }