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