]> git.lizzy.rs Git - rust.git/blob - src/librustc_resolve/macros.rs
44d0c888c5dd248bc63b80dd3a397e7731db9b75
[rust.git] / src / librustc_resolve / macros.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use {AmbiguityError, CrateLint, Resolver, ResolutionError, is_known_tool, resolve_error};
12 use {Module, ModuleKind, NameBinding, NameBindingKind, PathResult, ToNameBinding};
13 use ModuleOrUniformRoot;
14 use Namespace::{self, TypeNS, MacroNS};
15 use build_reduced_graph::{BuildReducedGraphVisitor, IsMacroExport};
16 use resolve_imports::ImportResolver;
17 use rustc::hir::def_id::{DefId, BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, DefIndex,
18                          DefIndexAddressSpace};
19 use rustc::hir::def::{Def, Export, NonMacroAttrKind};
20 use rustc::hir::map::{self, DefCollector};
21 use rustc::{ty, lint};
22 use rustc::middle::cstore::CrateStore;
23 use syntax::ast::{self, Name, Ident};
24 use syntax::attr;
25 use syntax::errors::DiagnosticBuilder;
26 use syntax::ext::base::{self, Determinacy, MultiModifier, MultiDecorator};
27 use syntax::ext::base::{MacroKind, SyntaxExtension, Resolver as SyntaxResolver};
28 use syntax::ext::expand::{AstFragment, Invocation, InvocationKind};
29 use syntax::ext::hygiene::{self, Mark};
30 use syntax::ext::tt::macro_rules;
31 use syntax::feature_gate::{self, feature_err, emit_feature_err, is_builtin_attr_name, GateIssue};
32 use syntax::feature_gate::EXPLAIN_DERIVE_UNDERSCORE;
33 use syntax::fold::{self, Folder};
34 use syntax::parse::parser::PathStyle;
35 use syntax::parse::token::{self, Token};
36 use syntax::ptr::P;
37 use syntax::symbol::{Symbol, keywords};
38 use syntax::tokenstream::{TokenStream, TokenTree, Delimited};
39 use syntax::util::lev_distance::find_best_match_for_name;
40 use syntax_pos::{Span, DUMMY_SP};
41
42 use std::cell::Cell;
43 use std::mem;
44 use rustc_data_structures::sync::Lrc;
45
46 #[derive(Clone)]
47 pub struct InvocationData<'a> {
48     pub module: Cell<Module<'a>>,
49     pub def_index: DefIndex,
50     // The scope in which the invocation path is resolved.
51     pub legacy_scope: Cell<LegacyScope<'a>>,
52     // The smallest scope that includes this invocation's expansion,
53     // or `Empty` if this invocation has not been expanded yet.
54     pub expansion: Cell<LegacyScope<'a>>,
55 }
56
57 impl<'a> InvocationData<'a> {
58     pub fn root(graph_root: Module<'a>) -> Self {
59         InvocationData {
60             module: Cell::new(graph_root),
61             def_index: CRATE_DEF_INDEX,
62             legacy_scope: Cell::new(LegacyScope::Empty),
63             expansion: Cell::new(LegacyScope::Empty),
64         }
65     }
66 }
67
68 #[derive(Copy, Clone)]
69 pub enum LegacyScope<'a> {
70     Empty,
71     Invocation(&'a InvocationData<'a>), // The scope of the invocation, not including its expansion
72     Expansion(&'a InvocationData<'a>), // The scope of the invocation, including its expansion
73     Binding(&'a LegacyBinding<'a>),
74 }
75
76 pub struct LegacyBinding<'a> {
77     pub parent: Cell<LegacyScope<'a>>,
78     pub ident: Ident,
79     def_id: DefId,
80     pub span: Span,
81 }
82
83 pub struct ProcMacError {
84     crate_name: Symbol,
85     name: Symbol,
86     module: ast::NodeId,
87     use_span: Span,
88     warn_msg: &'static str,
89 }
90
91 #[derive(Copy, Clone)]
92 pub enum MacroBinding<'a> {
93     Legacy(&'a LegacyBinding<'a>),
94     Global(&'a NameBinding<'a>),
95     Modern(&'a NameBinding<'a>),
96 }
97
98 impl<'a> MacroBinding<'a> {
99     pub fn span(self) -> Span {
100         match self {
101             MacroBinding::Legacy(binding) => binding.span,
102             MacroBinding::Global(binding) | MacroBinding::Modern(binding) => binding.span,
103         }
104     }
105
106     pub fn binding(self) -> &'a NameBinding<'a> {
107         match self {
108             MacroBinding::Global(binding) | MacroBinding::Modern(binding) => binding,
109             MacroBinding::Legacy(_) => panic!("unexpected MacroBinding::Legacy"),
110         }
111     }
112
113     pub fn def_ignoring_ambiguity(self) -> Def {
114         match self {
115             MacroBinding::Legacy(binding) => Def::Macro(binding.def_id, MacroKind::Bang),
116             MacroBinding::Global(binding) | MacroBinding::Modern(binding) =>
117                 binding.def_ignoring_ambiguity(),
118         }
119     }
120 }
121
122 impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
123     fn next_node_id(&mut self) -> ast::NodeId {
124         self.session.next_node_id()
125     }
126
127     fn get_module_scope(&mut self, id: ast::NodeId) -> Mark {
128         let mark = Mark::fresh(Mark::root());
129         let module = self.module_map[&self.definitions.local_def_id(id)];
130         self.invocations.insert(mark, self.arenas.alloc_invocation_data(InvocationData {
131             module: Cell::new(module),
132             def_index: module.def_id().unwrap().index,
133             legacy_scope: Cell::new(LegacyScope::Empty),
134             expansion: Cell::new(LegacyScope::Empty),
135         }));
136         mark
137     }
138
139     fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item> {
140         struct EliminateCrateVar<'b, 'a: 'b, 'crateloader: 'a>(
141             &'b mut Resolver<'a, 'crateloader>, Span
142         );
143
144         impl<'a, 'b, 'crateloader> Folder for EliminateCrateVar<'a, 'b, 'crateloader> {
145             fn fold_path(&mut self, path: ast::Path) -> ast::Path {
146                 match self.fold_qpath(None, path) {
147                     (None, path) => path,
148                     _ => unreachable!(),
149                 }
150             }
151
152             fn fold_qpath(&mut self, mut qself: Option<ast::QSelf>, mut path: ast::Path)
153                           -> (Option<ast::QSelf>, ast::Path) {
154                 qself = qself.map(|ast::QSelf { ty, path_span, position }| {
155                     ast::QSelf {
156                         ty: self.fold_ty(ty),
157                         path_span: self.new_span(path_span),
158                         position,
159                     }
160                 });
161
162                 if path.segments[0].ident.name == keywords::DollarCrate.name() {
163                     let module = self.0.resolve_crate_root(path.segments[0].ident);
164                     path.segments[0].ident.name = keywords::CrateRoot.name();
165                     if !module.is_local() {
166                         let span = path.segments[0].ident.span;
167                         path.segments.insert(1, match module.kind {
168                             ModuleKind::Def(_, name) => ast::PathSegment::from_ident(
169                                 ast::Ident::with_empty_ctxt(name).with_span_pos(span)
170                             ),
171                             _ => unreachable!(),
172                         });
173                         if let Some(qself) = &mut qself {
174                             qself.position += 1;
175                         }
176                     }
177                 }
178                 (qself, path)
179             }
180
181             fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
182                 fold::noop_fold_mac(mac, self)
183             }
184         }
185
186         EliminateCrateVar(self, item.span).fold_item(item).expect_one("")
187     }
188
189     fn is_whitelisted_legacy_custom_derive(&self, name: Name) -> bool {
190         self.whitelisted_legacy_custom_derives.contains(&name)
191     }
192
193     fn visit_ast_fragment_with_placeholders(&mut self, mark: Mark, fragment: &AstFragment,
194                                             derives: &[Mark]) {
195         let invocation = self.invocations[&mark];
196         self.collect_def_ids(mark, invocation, fragment);
197
198         self.current_module = invocation.module.get();
199         self.current_module.unresolved_invocations.borrow_mut().remove(&mark);
200         self.current_module.unresolved_invocations.borrow_mut().extend(derives);
201         for &derive in derives {
202             self.invocations.insert(derive, invocation);
203         }
204         let mut visitor = BuildReducedGraphVisitor {
205             resolver: self,
206             legacy_scope: LegacyScope::Invocation(invocation),
207             expansion: mark,
208         };
209         fragment.visit_with(&mut visitor);
210         invocation.expansion.set(visitor.legacy_scope);
211     }
212
213     fn add_builtin(&mut self, ident: ast::Ident, ext: Lrc<SyntaxExtension>) {
214         let def_id = DefId {
215             krate: BUILTIN_MACROS_CRATE,
216             index: DefIndex::from_array_index(self.macro_map.len(),
217                                               DefIndexAddressSpace::Low),
218         };
219         let kind = ext.kind();
220         self.macro_map.insert(def_id, ext);
221         let binding = self.arenas.alloc_name_binding(NameBinding {
222             kind: NameBindingKind::Def(Def::Macro(def_id, kind), false),
223             span: DUMMY_SP,
224             vis: ty::Visibility::Invisible,
225             expansion: Mark::root(),
226         });
227         self.macro_prelude.insert(ident.name, binding);
228     }
229
230     fn resolve_imports(&mut self) {
231         ImportResolver { resolver: self }.resolve_imports()
232     }
233
234     // Resolves attribute and derive legacy macros from `#![plugin(..)]`.
235     fn find_legacy_attr_invoc(&mut self, attrs: &mut Vec<ast::Attribute>, allow_derive: bool)
236                               -> Option<ast::Attribute> {
237         for i in 0..attrs.len() {
238             let name = attrs[i].name();
239
240             if self.session.plugin_attributes.borrow().iter()
241                     .any(|&(ref attr_nm, _)| name == &**attr_nm) {
242                 attr::mark_known(&attrs[i]);
243             }
244
245             match self.macro_prelude.get(&name).cloned() {
246                 Some(binding) => match *binding.get_macro(self) {
247                     MultiModifier(..) | MultiDecorator(..) | SyntaxExtension::AttrProcMacro(..) => {
248                         return Some(attrs.remove(i))
249                     }
250                     _ => {}
251                 },
252                 None => {}
253             }
254         }
255
256         if !allow_derive { return None }
257
258         // Check for legacy derives
259         for i in 0..attrs.len() {
260             let name = attrs[i].name();
261
262             if name == "derive" {
263                 let result = attrs[i].parse_list(&self.session.parse_sess, |parser| {
264                     parser.parse_path_allowing_meta(PathStyle::Mod)
265                 });
266
267                 let mut traits = match result {
268                     Ok(traits) => traits,
269                     Err(mut e) => {
270                         e.cancel();
271                         continue
272                     }
273                 };
274
275                 for j in 0..traits.len() {
276                     if traits[j].segments.len() > 1 {
277                         continue
278                     }
279                     let trait_name = traits[j].segments[0].ident.name;
280                     let legacy_name = Symbol::intern(&format!("derive_{}", trait_name));
281                     if !self.macro_prelude.contains_key(&legacy_name) {
282                         continue
283                     }
284                     let span = traits.remove(j).span;
285                     self.gate_legacy_custom_derive(legacy_name, span);
286                     if traits.is_empty() {
287                         attrs.remove(i);
288                     } else {
289                         let mut tokens = Vec::new();
290                         for (j, path) in traits.iter().enumerate() {
291                             if j > 0 {
292                                 tokens.push(TokenTree::Token(attrs[i].span, Token::Comma).into());
293                             }
294                             for (k, segment) in path.segments.iter().enumerate() {
295                                 if k > 0 {
296                                     tokens.push(TokenTree::Token(path.span, Token::ModSep).into());
297                                 }
298                                 let tok = Token::from_ast_ident(segment.ident);
299                                 tokens.push(TokenTree::Token(path.span, tok).into());
300                             }
301                         }
302                         attrs[i].tokens = TokenTree::Delimited(attrs[i].span, Delimited {
303                             delim: token::Paren,
304                             tts: TokenStream::concat(tokens).into(),
305                         }).into();
306                     }
307                     return Some(ast::Attribute {
308                         path: ast::Path::from_ident(Ident::new(legacy_name, span)),
309                         tokens: TokenStream::empty(),
310                         id: attr::mk_attr_id(),
311                         style: ast::AttrStyle::Outer,
312                         is_sugared_doc: false,
313                         span,
314                     });
315                 }
316             }
317         }
318
319         None
320     }
321
322     fn resolve_invoc(&mut self, invoc: &Invocation, scope: Mark, force: bool)
323                      -> Result<Option<Lrc<SyntaxExtension>>, Determinacy> {
324         let def = match invoc.kind {
325             InvocationKind::Attr { attr: None, .. } => return Ok(None),
326             _ => self.resolve_invoc_to_def(invoc, scope, force)?,
327         };
328         if let Def::Macro(_, MacroKind::ProcMacroStub) = def {
329             self.report_proc_macro_stub(invoc.span());
330             return Err(Determinacy::Determined);
331         } else if let Def::NonMacroAttr(attr_kind) = def {
332             // Note that not only attributes, but anything in macro namespace can result in a
333             // `Def::NonMacroAttr` definition (e.g. `inline!()`), so we must report the error
334             // below for these cases.
335             let is_attr_invoc =
336                 if let InvocationKind::Attr { .. } = invoc.kind { true } else { false };
337             let path = invoc.path().expect("no path for non-macro attr");
338             match attr_kind {
339                 NonMacroAttrKind::Tool | NonMacroAttrKind::DeriveHelper |
340                 NonMacroAttrKind::Custom if is_attr_invoc => {
341                     let features = self.session.features_untracked();
342                     if attr_kind == NonMacroAttrKind::Tool &&
343                        !features.tool_attributes {
344                         feature_err(&self.session.parse_sess, "tool_attributes",
345                                     invoc.span(), GateIssue::Language,
346                                     "tool attributes are unstable").emit();
347                     }
348                     if attr_kind == NonMacroAttrKind::Custom {
349                         assert!(path.segments.len() == 1);
350                         let name = path.segments[0].ident.name.as_str();
351                         if name.starts_with("rustc_") {
352                             if !features.rustc_attrs {
353                                 let msg = "unless otherwise specified, attributes with the prefix \
354                                         `rustc_` are reserved for internal compiler diagnostics";
355                                 feature_err(&self.session.parse_sess, "rustc_attrs", invoc.span(),
356                                             GateIssue::Language, &msg).emit();
357                             }
358                         } else if name.starts_with("derive_") {
359                             if !features.custom_derive {
360                                 feature_err(&self.session.parse_sess, "custom_derive", invoc.span(),
361                                             GateIssue::Language, EXPLAIN_DERIVE_UNDERSCORE).emit();
362                             }
363                         } else if !features.custom_attribute {
364                             let msg = format!("The attribute `{}` is currently unknown to the \
365                                                compiler and may have meaning added to it in the \
366                                                future", path);
367                             feature_err(&self.session.parse_sess, "custom_attribute", invoc.span(),
368                                         GateIssue::Language, &msg).emit();
369                         }
370                     }
371
372                     return Ok(Some(Lrc::new(SyntaxExtension::NonMacroAttr {
373                         mark_used: attr_kind == NonMacroAttrKind::Tool,
374                     })));
375                 }
376                 _ => {
377                     self.report_non_macro_attr(path.span, def);
378                     return Err(Determinacy::Determined);
379                 }
380             }
381         }
382         let def_id = def.def_id();
383
384         self.macro_defs.insert(invoc.expansion_data.mark, def_id);
385         let normal_module_def_id =
386             self.macro_def_scope(invoc.expansion_data.mark).normal_ancestor_id;
387         self.definitions.add_parent_module_of_macro_def(invoc.expansion_data.mark,
388                                                         normal_module_def_id);
389
390         self.unused_macros.remove(&def_id);
391         let ext = self.get_macro(def);
392         invoc.expansion_data.mark.set_default_transparency(ext.default_transparency());
393         invoc.expansion_data.mark.set_is_builtin(def_id.krate == BUILTIN_MACROS_CRATE);
394         Ok(Some(ext))
395     }
396
397     fn resolve_macro(&mut self, scope: Mark, path: &ast::Path, kind: MacroKind, force: bool)
398                      -> Result<Lrc<SyntaxExtension>, Determinacy> {
399         self.resolve_macro_to_def(scope, path, kind, force).and_then(|def| {
400             if let Def::Macro(_, MacroKind::ProcMacroStub) = def {
401                 self.report_proc_macro_stub(path.span);
402                 return Err(Determinacy::Determined);
403             } else if let Def::NonMacroAttr(..) = def {
404                 self.report_non_macro_attr(path.span, def);
405                 return Err(Determinacy::Determined);
406             }
407             self.unused_macros.remove(&def.def_id());
408             Ok(self.get_macro(def))
409         })
410     }
411
412     fn check_unused_macros(&self) {
413         for did in self.unused_macros.iter() {
414             let id_span = match *self.macro_map[did] {
415                 SyntaxExtension::NormalTT { def_info, .. } |
416                 SyntaxExtension::DeclMacro { def_info, .. } => def_info,
417                 _ => None,
418             };
419             if let Some((id, span)) = id_span {
420                 let lint = lint::builtin::UNUSED_MACROS;
421                 let msg = "unused macro definition";
422                 self.session.buffer_lint(lint, id, span, msg);
423             } else {
424                 bug!("attempted to create unused macro error, but span not available");
425             }
426         }
427     }
428 }
429
430 impl<'a, 'cl> Resolver<'a, 'cl> {
431     fn report_proc_macro_stub(&self, span: Span) {
432         self.session.span_err(span,
433                               "can't use a procedural macro from the same crate that defines it");
434     }
435
436     fn report_non_macro_attr(&self, span: Span, def: Def) {
437         self.session.span_err(span, &format!("expected a macro, found {}", def.kind_name()));
438     }
439
440     fn resolve_invoc_to_def(&mut self, invoc: &Invocation, scope: Mark, force: bool)
441                             -> Result<Def, Determinacy> {
442         let (attr, traits) = match invoc.kind {
443             InvocationKind::Attr { ref attr, ref traits, .. } => (attr, traits),
444             InvocationKind::Bang { ref mac, .. } => {
445                 return self.resolve_macro_to_def(scope, &mac.node.path, MacroKind::Bang, force);
446             }
447             InvocationKind::Derive { ref path, .. } => {
448                 return self.resolve_macro_to_def(scope, path, MacroKind::Derive, force);
449             }
450         };
451
452         let path = attr.as_ref().unwrap().path.clone();
453         let def = self.resolve_macro_to_def(scope, &path, MacroKind::Attr, force);
454         if let Ok(Def::NonMacroAttr(NonMacroAttrKind::Custom)) = def {} else {
455             return def;
456         }
457
458         // At this point we've found that the `attr` is determinately unresolved and thus can be
459         // interpreted as a custom attribute. Normally custom attributes are feature gated, but
460         // it may be a custom attribute whitelisted by a derive macro and they do not require
461         // a feature gate.
462         //
463         // So here we look through all of the derive annotations in scope and try to resolve them.
464         // If they themselves successfully resolve *and* one of the resolved derive macros
465         // whitelists this attribute's name, then this is a registered attribute and we can convert
466         // it from a "generic custom attrite" into a "known derive helper attribute".
467         enum ConvertToDeriveHelper { Yes, No, DontKnow }
468         let mut convert_to_derive_helper = ConvertToDeriveHelper::No;
469         let attr_name = path.segments[0].ident.name;
470         for path in traits {
471             match self.resolve_macro(scope, path, MacroKind::Derive, force) {
472                 Ok(ext) => if let SyntaxExtension::ProcMacroDerive(_, ref inert_attrs, _) = *ext {
473                     if inert_attrs.contains(&attr_name) {
474                         convert_to_derive_helper = ConvertToDeriveHelper::Yes;
475                         break
476                     }
477                 },
478                 Err(Determinacy::Undetermined) =>
479                     convert_to_derive_helper = ConvertToDeriveHelper::DontKnow,
480                 Err(Determinacy::Determined) => {}
481             }
482         }
483
484         match convert_to_derive_helper {
485             ConvertToDeriveHelper::Yes => Ok(Def::NonMacroAttr(NonMacroAttrKind::DeriveHelper)),
486             ConvertToDeriveHelper::No => def,
487             ConvertToDeriveHelper::DontKnow => Err(Determinacy::determined(force)),
488         }
489     }
490
491     fn resolve_macro_to_def(&mut self, scope: Mark, path: &ast::Path, kind: MacroKind, force: bool)
492                             -> Result<Def, Determinacy> {
493         let def = self.resolve_macro_to_def_inner(scope, path, kind, force);
494         if def != Err(Determinacy::Undetermined) {
495             // Do not report duplicated errors on every undetermined resolution.
496             path.segments.iter().find(|segment| segment.args.is_some()).map(|segment| {
497                 self.session.span_err(segment.args.as_ref().unwrap().span(),
498                                       "generic arguments in macro path");
499             });
500         }
501         if kind != MacroKind::Bang && path.segments.len() > 1 &&
502            def != Ok(Def::NonMacroAttr(NonMacroAttrKind::Tool)) {
503             if !self.session.features_untracked().proc_macro_path_invoc {
504                 emit_feature_err(
505                     &self.session.parse_sess,
506                     "proc_macro_path_invoc",
507                     path.span,
508                     GateIssue::Language,
509                     "paths of length greater than one in macro invocations are \
510                      currently unstable",
511                 );
512             }
513         }
514         def
515     }
516
517     pub fn resolve_macro_to_def_inner(&mut self, scope: Mark, path: &ast::Path,
518                                   kind: MacroKind, force: bool)
519                                   -> Result<Def, Determinacy> {
520         let ast::Path { ref segments, span } = *path;
521         let mut path: Vec<_> = segments.iter().map(|seg| seg.ident).collect();
522         let invocation = self.invocations[&scope];
523         let module = invocation.module.get();
524         self.current_module = if module.is_trait() { module.parent.unwrap() } else { module };
525
526         // Possibly apply the macro helper hack
527         if self.use_extern_macros && kind == MacroKind::Bang && path.len() == 1 &&
528            path[0].span.ctxt().outer().expn_info().map_or(false, |info| info.local_inner_macros) {
529             let root = Ident::new(keywords::DollarCrate.name(), path[0].span);
530             path.insert(0, root);
531         }
532
533         if path.len() > 1 {
534             if !self.use_extern_macros && self.gated_errors.insert(span) {
535                 let msg = "non-ident macro paths are experimental";
536                 let feature = "use_extern_macros";
537                 emit_feature_err(&self.session.parse_sess, feature, span, GateIssue::Language, msg);
538                 self.found_unresolved_macro = true;
539                 return Err(Determinacy::Determined);
540             }
541
542             let res = self.resolve_path(None, &path, Some(MacroNS), false, span, CrateLint::No);
543             let def = match res {
544                 PathResult::NonModule(path_res) => match path_res.base_def() {
545                     Def::Err => Err(Determinacy::Determined),
546                     def @ _ => {
547                         if path_res.unresolved_segments() > 0 {
548                             self.found_unresolved_macro = true;
549                             self.session.span_err(span, "fail to resolve non-ident macro path");
550                             Err(Determinacy::Determined)
551                         } else {
552                             Ok(def)
553                         }
554                     }
555                 },
556                 PathResult::Module(..) => unreachable!(),
557                 PathResult::Indeterminate if !force => return Err(Determinacy::Undetermined),
558                 _ => {
559                     self.found_unresolved_macro = true;
560                     Err(Determinacy::Determined)
561                 },
562             };
563             self.current_module.nearest_item_scope().macro_resolutions.borrow_mut()
564                 .push((path.into_boxed_slice(), span));
565             return def;
566         }
567
568         let legacy_resolution = self.resolve_legacy_scope(&invocation.legacy_scope, path[0], false);
569         let result = if let Some(MacroBinding::Legacy(binding)) = legacy_resolution {
570             Ok(Def::Macro(binding.def_id, MacroKind::Bang))
571         } else {
572             match self.resolve_lexical_macro_path_segment(path[0], MacroNS, false, force,
573                                                           kind == MacroKind::Attr, span) {
574                 Ok(binding) => Ok(binding.binding().def_ignoring_ambiguity()),
575                 Err(Determinacy::Undetermined) => return Err(Determinacy::Undetermined),
576                 Err(Determinacy::Determined) => {
577                     self.found_unresolved_macro = true;
578                     Err(Determinacy::Determined)
579                 }
580             }
581         };
582
583         self.current_module.nearest_item_scope().legacy_macro_resolutions.borrow_mut()
584             .push((scope, path[0], kind, result.ok()));
585
586         result
587     }
588
589     // Resolve the initial segment of a non-global macro path
590     // (e.g. `foo` in `foo::bar!(); or `foo!();`).
591     // This is a variation of `fn resolve_ident_in_lexical_scope` that can be run during
592     // expansion and import resolution (perhaps they can be merged in the future).
593     pub fn resolve_lexical_macro_path_segment(&mut self,
594                                               mut ident: Ident,
595                                               ns: Namespace,
596                                               record_used: bool,
597                                               force: bool,
598                                               is_attr: bool,
599                                               path_span: Span)
600                                               -> Result<MacroBinding<'a>, Determinacy> {
601         // General principles:
602         // 1. Not controlled (user-defined) names should have higher priority than controlled names
603         //    built into the language or standard library. This way we can add new names into the
604         //    language or standard library without breaking user code.
605         // 2. "Closed set" below means new names can appear after the current resolution attempt.
606         // Places to search (in order of decreasing priority):
607         // (Type NS)
608         // 1. FIXME: Ribs (type parameters), there's no necessary infrastructure yet
609         //    (open set, not controlled).
610         // 2. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
611         //    (open, not controlled).
612         // 3. Extern prelude (closed, not controlled).
613         // 4. Tool modules (closed, controlled right now, but not in the future).
614         // 5. Standard library prelude (de-facto closed, controlled).
615         // 6. Language prelude (closed, controlled).
616         // (Macro NS)
617         // 1. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
618         //    (open, not controlled).
619         // 2. Macro prelude (language, standard library, user-defined legacy plugins lumped into
620         //    one set) (open, the open part is from macro expansions, not controlled).
621         // 2a. User-defined prelude from macro-use
622         //    (open, the open part is from macro expansions, not controlled).
623         // 2b. Standard library prelude, currently just a macro-use (closed, controlled)
624         // 2c. Language prelude, perhaps including builtin attributes
625         //    (closed, controlled, except for legacy plugins).
626         // 3. Builtin attributes (closed, controlled).
627
628         assert!(ns == TypeNS  || ns == MacroNS);
629         assert!(force || !record_used); // `record_used` implies `force`
630         ident = ident.modern();
631
632         // Names from inner scope that can't shadow names from outer scopes, e.g.
633         // mod m { ... }
634         // {
635         //     use prefix::*; // if this imports another `m`, then it can't shadow the outer `m`
636         //                    // and we have and ambiguity error
637         //     m::mac!();
638         // }
639         // This includes names from globs and from macro expansions.
640         let mut potentially_ambiguous_result: Option<MacroBinding> = None;
641
642         enum WhereToResolve<'a> {
643             Module(Module<'a>),
644             MacroPrelude,
645             BuiltinAttrs,
646             ExternPrelude,
647             ToolPrelude,
648             StdLibPrelude,
649             PrimitiveTypes,
650         }
651
652         // Go through all the scopes and try to resolve the name.
653         let mut where_to_resolve = WhereToResolve::Module(self.current_module);
654         let mut use_prelude = !self.current_module.no_implicit_prelude;
655         loop {
656             let result = match where_to_resolve {
657                 WhereToResolve::Module(module) => {
658                     let orig_current_module = mem::replace(&mut self.current_module, module);
659                     let binding = self.resolve_ident_in_module_unadjusted(
660                         ModuleOrUniformRoot::Module(module),
661                         ident,
662                         ns,
663                         true,
664                         record_used,
665                         path_span,
666                     );
667                     self.current_module = orig_current_module;
668                     binding.map(MacroBinding::Modern)
669                 }
670                 WhereToResolve::MacroPrelude => {
671                     match self.macro_prelude.get(&ident.name).cloned() {
672                         Some(binding) => Ok(MacroBinding::Global(binding)),
673                         None => Err(Determinacy::Determined),
674                     }
675                 }
676                 WhereToResolve::BuiltinAttrs => {
677                     // FIXME: Only built-in attributes are not considered as candidates for
678                     // non-attributes to fight off regressions on stable channel (#53205).
679                     // We need to come up with some more principled approach instead.
680                     if is_attr && is_builtin_attr_name(ident.name) {
681                         let binding = (Def::NonMacroAttr(NonMacroAttrKind::Builtin),
682                                        ty::Visibility::Public, ident.span, Mark::root())
683                                        .to_name_binding(self.arenas);
684                         Ok(MacroBinding::Global(binding))
685                     } else {
686                         Err(Determinacy::Determined)
687                     }
688                 }
689                 WhereToResolve::ExternPrelude => {
690                     if use_prelude && self.extern_prelude.contains(&ident.name) {
691                         if !self.session.features_untracked().extern_prelude &&
692                            !self.ignore_extern_prelude_feature {
693                             feature_err(&self.session.parse_sess, "extern_prelude",
694                                         ident.span, GateIssue::Language,
695                                         "access to extern crates through prelude is experimental")
696                                         .emit();
697                         }
698
699                         let crate_id =
700                             self.crate_loader.process_path_extern(ident.name, ident.span);
701                         let crate_root =
702                             self.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX });
703                         self.populate_module_if_necessary(crate_root);
704
705                         let binding = (crate_root, ty::Visibility::Public,
706                                        ident.span, Mark::root()).to_name_binding(self.arenas);
707                         Ok(MacroBinding::Global(binding))
708                     } else {
709                         Err(Determinacy::Determined)
710                     }
711                 }
712                 WhereToResolve::ToolPrelude => {
713                     if use_prelude && is_known_tool(ident.name) {
714                         let binding = (Def::ToolMod, ty::Visibility::Public,
715                                        ident.span, Mark::root()).to_name_binding(self.arenas);
716                         Ok(MacroBinding::Global(binding))
717                     } else {
718                         Err(Determinacy::Determined)
719                     }
720                 }
721                 WhereToResolve::StdLibPrelude => {
722                     let mut result = Err(Determinacy::Determined);
723                     if use_prelude {
724                         if let Some(prelude) = self.prelude {
725                             if let Ok(binding) = self.resolve_ident_in_module_unadjusted(
726                                 ModuleOrUniformRoot::Module(prelude),
727                                 ident,
728                                 ns,
729                                 false,
730                                 false,
731                                 path_span,
732                             ) {
733                                 result = Ok(MacroBinding::Global(binding));
734                             }
735                         }
736                     }
737                     result
738                 }
739                 WhereToResolve::PrimitiveTypes => {
740                     if let Some(prim_ty) =
741                             self.primitive_type_table.primitive_types.get(&ident.name).cloned() {
742                         let binding = (Def::PrimTy(prim_ty), ty::Visibility::Public,
743                                        ident.span, Mark::root()).to_name_binding(self.arenas);
744                         Ok(MacroBinding::Global(binding))
745                     } else {
746                         Err(Determinacy::Determined)
747                     }
748                 }
749             };
750
751             macro_rules! continue_search { () => {
752                 where_to_resolve = match where_to_resolve {
753                     WhereToResolve::Module(module) => {
754                         match self.hygienic_lexical_parent(module, &mut ident.span) {
755                             Some(parent_module) => WhereToResolve::Module(parent_module),
756                             None => {
757                                 use_prelude = !module.no_implicit_prelude;
758                                 if ns == MacroNS {
759                                     WhereToResolve::MacroPrelude
760                                 } else {
761                                     WhereToResolve::ExternPrelude
762                                 }
763                             }
764                         }
765                     }
766                     WhereToResolve::MacroPrelude => WhereToResolve::BuiltinAttrs,
767                     WhereToResolve::BuiltinAttrs => break, // nowhere else to search
768                     WhereToResolve::ExternPrelude => WhereToResolve::ToolPrelude,
769                     WhereToResolve::ToolPrelude => WhereToResolve::StdLibPrelude,
770                     WhereToResolve::StdLibPrelude => WhereToResolve::PrimitiveTypes,
771                     WhereToResolve::PrimitiveTypes => break, // nowhere else to search
772                 };
773
774                 continue;
775             }}
776
777             match result {
778                 Ok(result) => {
779                     if !record_used {
780                         return Ok(result);
781                     }
782
783                     let binding = result.binding();
784
785                     // Found a solution that is ambiguous with a previously found solution.
786                     // Push an ambiguity error for later reporting and
787                     // return something for better recovery.
788                     if let Some(previous_result) = potentially_ambiguous_result {
789                         if binding.def() != previous_result.binding().def() {
790                             self.ambiguity_errors.push(AmbiguityError {
791                                 span: path_span,
792                                 name: ident.name,
793                                 b1: previous_result.binding(),
794                                 b2: binding,
795                                 lexical: true,
796                             });
797                             return Ok(previous_result);
798                         }
799                     }
800
801                     // Found a solution that's not an ambiguity yet, but is "suspicious" and
802                     // can participate in ambiguities later on.
803                     // Remember it and go search for other solutions in outer scopes.
804                     if binding.is_glob_import() || binding.expansion != Mark::root() {
805                         potentially_ambiguous_result = Some(result);
806
807                         continue_search!();
808                     }
809
810                     // Found a solution that can't be ambiguous, great success.
811                     return Ok(result);
812                 },
813                 Err(Determinacy::Determined) => {
814                     continue_search!();
815                 }
816                 Err(Determinacy::Undetermined) => return Err(Determinacy::determined(force)),
817             }
818         }
819
820         // Previously found potentially ambiguous result turned out to not be ambiguous after all.
821         if let Some(previous_result) = potentially_ambiguous_result {
822             return Ok(previous_result);
823         }
824
825         let determinacy = Determinacy::determined(force);
826         if determinacy == Determinacy::Determined && is_attr {
827             // For single-segment attributes interpret determinate "no resolution" as a custom
828             // attribute. (Lexical resolution implies the first segment and is_attr should imply
829             // the last segment, so we are certainly working with a single-segment attribute here.)
830             assert!(ns == MacroNS);
831             let binding = (Def::NonMacroAttr(NonMacroAttrKind::Custom),
832                            ty::Visibility::Public, ident.span, Mark::root())
833                            .to_name_binding(self.arenas);
834             Ok(MacroBinding::Global(binding))
835         } else {
836             Err(determinacy)
837         }
838     }
839
840     pub fn resolve_legacy_scope(&mut self,
841                                 mut scope: &'a Cell<LegacyScope<'a>>,
842                                 ident: Ident,
843                                 record_used: bool)
844                                 -> Option<MacroBinding<'a>> {
845         let ident = ident.modern();
846         let mut possible_time_travel = None;
847         let mut relative_depth: u32 = 0;
848         let mut binding = None;
849         loop {
850             match scope.get() {
851                 LegacyScope::Empty => break,
852                 LegacyScope::Expansion(invocation) => {
853                     match invocation.expansion.get() {
854                         LegacyScope::Invocation(_) => scope.set(invocation.legacy_scope.get()),
855                         LegacyScope::Empty => {
856                             if possible_time_travel.is_none() {
857                                 possible_time_travel = Some(scope);
858                             }
859                             scope = &invocation.legacy_scope;
860                         }
861                         _ => {
862                             relative_depth += 1;
863                             scope = &invocation.expansion;
864                         }
865                     }
866                 }
867                 LegacyScope::Invocation(invocation) => {
868                     relative_depth = relative_depth.saturating_sub(1);
869                     scope = &invocation.legacy_scope;
870                 }
871                 LegacyScope::Binding(potential_binding) => {
872                     if potential_binding.ident == ident {
873                         if (!self.use_extern_macros || record_used) && relative_depth > 0 {
874                             self.disallowed_shadowing.push(potential_binding);
875                         }
876                         binding = Some(potential_binding);
877                         break
878                     }
879                     scope = &potential_binding.parent;
880                 }
881             };
882         }
883
884         let binding = if let Some(binding) = binding {
885             MacroBinding::Legacy(binding)
886         } else if let Some(binding) = self.macro_prelude.get(&ident.name).cloned() {
887             if !self.use_extern_macros {
888                 self.record_use(ident, MacroNS, binding, DUMMY_SP);
889             }
890             MacroBinding::Global(binding)
891         } else {
892             return None;
893         };
894
895         if !self.use_extern_macros {
896             if let Some(scope) = possible_time_travel {
897                 // Check for disallowed shadowing later
898                 self.lexical_macro_resolutions.push((ident, scope));
899             }
900         }
901
902         Some(binding)
903     }
904
905     pub fn finalize_current_module_macro_resolutions(&mut self) {
906         let module = self.current_module;
907         for &(ref path, span) in module.macro_resolutions.borrow().iter() {
908             match self.resolve_path(None, &path, Some(MacroNS), true, span, CrateLint::No) {
909                 PathResult::NonModule(_) => {},
910                 PathResult::Failed(span, msg, _) => {
911                     resolve_error(self, span, ResolutionError::FailedToResolve(&msg));
912                 }
913                 _ => unreachable!(),
914             }
915         }
916
917         for &(mark, ident, kind, def) in module.legacy_macro_resolutions.borrow().iter() {
918             let span = ident.span;
919             let legacy_scope = &self.invocations[&mark].legacy_scope;
920             let legacy_resolution = self.resolve_legacy_scope(legacy_scope, ident, true);
921             let resolution = self.resolve_lexical_macro_path_segment(ident, MacroNS, true, true,
922                                                                      kind == MacroKind::Attr, span);
923
924             let check_consistency = |this: &Self, binding: MacroBinding| {
925                 if let Some(def) = def {
926                     if this.ambiguity_errors.is_empty() && this.disallowed_shadowing.is_empty() &&
927                        binding.def_ignoring_ambiguity() != def {
928                         // Make sure compilation does not succeed if preferred macro resolution
929                         // has changed after the macro had been expanded. In theory all such
930                         // situations should be reported as ambiguity errors, so this is span-bug.
931                         span_bug!(span, "inconsistent resolution for a macro");
932                     }
933                 } else {
934                     // It's possible that the macro was unresolved (indeterminate) and silently
935                     // expanded into a dummy fragment for recovery during expansion.
936                     // Now, post-expansion, the resolution may succeed, but we can't change the
937                     // past and need to report an error.
938                     let msg =
939                         format!("cannot determine resolution for the {} `{}`", kind.descr(), ident);
940                     let msg_note = "import resolution is stuck, try simplifying macro imports";
941                     this.session.struct_span_err(span, &msg).note(msg_note).emit();
942                 }
943             };
944
945             match (legacy_resolution, resolution) {
946                 (Some(MacroBinding::Legacy(legacy_binding)), Ok(MacroBinding::Modern(binding))) => {
947                     if legacy_binding.def_id != binding.def_ignoring_ambiguity().def_id() {
948                         let msg1 = format!("`{}` could refer to the macro defined here", ident);
949                         let msg2 =
950                             format!("`{}` could also refer to the macro imported here", ident);
951                         self.session.struct_span_err(span, &format!("`{}` is ambiguous", ident))
952                             .span_note(legacy_binding.span, &msg1)
953                             .span_note(binding.span, &msg2)
954                             .emit();
955                     }
956                 },
957                 (None, Err(_)) => {
958                     assert!(def.is_none());
959                     let bang = if kind == MacroKind::Bang { "!" } else { "" };
960                     let msg =
961                         format!("cannot find {} `{}{}` in this scope", kind.descr(), ident, bang);
962                     let mut err = self.session.struct_span_err(span, &msg);
963                     self.suggest_macro_name(&ident.as_str(), kind, &mut err, span);
964                     err.emit();
965                 },
966                 (Some(MacroBinding::Modern(_)), _) | (_, Ok(MacroBinding::Legacy(_))) => {
967                     span_bug!(span, "impossible macro resolution result");
968                 }
969                 // OK, unambiguous resolution
970                 (Some(binding), Err(_)) | (None, Ok(binding)) |
971                 // OK, legacy wins over global even if their definitions are different
972                 (Some(binding @ MacroBinding::Legacy(_)), Ok(MacroBinding::Global(_))) |
973                 // OK, modern wins over global even if their definitions are different
974                 (Some(MacroBinding::Global(_)), Ok(binding @ MacroBinding::Modern(_))) => {
975                     check_consistency(self, binding);
976                 }
977                 (Some(MacroBinding::Global(binding1)), Ok(MacroBinding::Global(binding2))) => {
978                     if binding1.def() != binding2.def() {
979                         span_bug!(span, "mismatch between same global macro resolutions");
980                     }
981                     check_consistency(self, MacroBinding::Global(binding1));
982
983                     self.record_use(ident, MacroNS, binding1, span);
984                     self.err_if_macro_use_proc_macro(ident.name, span, binding1);
985                 },
986             };
987         }
988     }
989
990     fn suggest_macro_name(&mut self, name: &str, kind: MacroKind,
991                           err: &mut DiagnosticBuilder<'a>, span: Span) {
992         // First check if this is a locally-defined bang macro.
993         let suggestion = if let MacroKind::Bang = kind {
994             find_best_match_for_name(self.macro_names.iter().map(|ident| &ident.name), name, None)
995         } else {
996             None
997         // Then check global macros.
998         }.or_else(|| {
999             // FIXME: get_macro needs an &mut Resolver, can we do it without cloning?
1000             let macro_prelude = self.macro_prelude.clone();
1001             let names = macro_prelude.iter().filter_map(|(name, binding)| {
1002                 if binding.get_macro(self).kind() == kind {
1003                     Some(name)
1004                 } else {
1005                     None
1006                 }
1007             });
1008             find_best_match_for_name(names, name, None)
1009         // Then check modules.
1010         }).or_else(|| {
1011             if !self.use_extern_macros {
1012                 return None;
1013             }
1014             let is_macro = |def| {
1015                 if let Def::Macro(_, def_kind) = def {
1016                     def_kind == kind
1017                 } else {
1018                     false
1019                 }
1020             };
1021             let ident = Ident::new(Symbol::intern(name), span);
1022             self.lookup_typo_candidate(&[ident], MacroNS, is_macro, span)
1023         });
1024
1025         if let Some(suggestion) = suggestion {
1026             if suggestion != name {
1027                 if let MacroKind::Bang = kind {
1028                     err.span_suggestion(span, "you could try the macro", suggestion.to_string());
1029                 } else {
1030                     err.span_suggestion(span, "try", suggestion.to_string());
1031                 }
1032             } else {
1033                 err.help("have you added the `#[macro_use]` on the module/import?");
1034             }
1035         }
1036     }
1037
1038     fn collect_def_ids(&mut self,
1039                        mark: Mark,
1040                        invocation: &'a InvocationData<'a>,
1041                        fragment: &AstFragment) {
1042         let Resolver { ref mut invocations, arenas, graph_root, .. } = *self;
1043         let InvocationData { def_index, .. } = *invocation;
1044
1045         let visit_macro_invoc = &mut |invoc: map::MacroInvocationData| {
1046             invocations.entry(invoc.mark).or_insert_with(|| {
1047                 arenas.alloc_invocation_data(InvocationData {
1048                     def_index: invoc.def_index,
1049                     module: Cell::new(graph_root),
1050                     expansion: Cell::new(LegacyScope::Empty),
1051                     legacy_scope: Cell::new(LegacyScope::Empty),
1052                 })
1053             });
1054         };
1055
1056         let mut def_collector = DefCollector::new(&mut self.definitions, mark);
1057         def_collector.visit_macro_invoc = Some(visit_macro_invoc);
1058         def_collector.with_parent(def_index, |def_collector| {
1059             fragment.visit_with(def_collector)
1060         });
1061     }
1062
1063     pub fn define_macro(&mut self,
1064                         item: &ast::Item,
1065                         expansion: Mark,
1066                         legacy_scope: &mut LegacyScope<'a>) {
1067         self.local_macro_def_scopes.insert(item.id, self.current_module);
1068         let ident = item.ident;
1069         if ident.name == "macro_rules" {
1070             self.session.span_err(item.span, "user-defined macros may not be named `macro_rules`");
1071         }
1072
1073         let def_id = self.definitions.local_def_id(item.id);
1074         let ext = Lrc::new(macro_rules::compile(&self.session.parse_sess,
1075                                                &self.session.features_untracked(),
1076                                                item, hygiene::default_edition()));
1077         self.macro_map.insert(def_id, ext);
1078
1079         let def = match item.node { ast::ItemKind::MacroDef(ref def) => def, _ => unreachable!() };
1080         if def.legacy {
1081             let ident = ident.modern();
1082             self.macro_names.insert(ident);
1083             *legacy_scope = LegacyScope::Binding(self.arenas.alloc_legacy_binding(LegacyBinding {
1084                 parent: Cell::new(*legacy_scope), ident: ident, def_id: def_id, span: item.span,
1085             }));
1086             let def = Def::Macro(def_id, MacroKind::Bang);
1087             self.all_macros.insert(ident.name, def);
1088             if attr::contains_name(&item.attrs, "macro_export") {
1089                 if self.use_extern_macros {
1090                     let module = self.graph_root;
1091                     let vis = ty::Visibility::Public;
1092                     self.define(module, ident, MacroNS,
1093                                 (def, vis, item.span, expansion, IsMacroExport));
1094                 } else {
1095                     self.macro_exports.push(Export {
1096                         ident: ident.modern(),
1097                         def: def,
1098                         vis: ty::Visibility::Public,
1099                         span: item.span,
1100                     });
1101                 }
1102             } else {
1103                 self.unused_macros.insert(def_id);
1104             }
1105         } else {
1106             let module = self.current_module;
1107             let def = Def::Macro(def_id, MacroKind::Bang);
1108             let vis = self.resolve_visibility(&item.vis);
1109             if vis != ty::Visibility::Public {
1110                 self.unused_macros.insert(def_id);
1111             }
1112             self.define(module, ident, MacroNS, (def, vis, item.span, expansion));
1113         }
1114     }
1115
1116     /// Error if `ext` is a Macros 1.1 procedural macro being imported by `#[macro_use]`
1117     fn err_if_macro_use_proc_macro(&mut self, name: Name, use_span: Span,
1118                                    binding: &NameBinding<'a>) {
1119         let krate = binding.def().def_id().krate;
1120
1121         // Plugin-based syntax extensions are exempt from this check
1122         if krate == BUILTIN_MACROS_CRATE { return; }
1123
1124         let ext = binding.get_macro(self);
1125
1126         match *ext {
1127             // If `ext` is a procedural macro, check if we've already warned about it
1128             SyntaxExtension::AttrProcMacro(..) | SyntaxExtension::ProcMacro { .. } =>
1129                 if !self.warned_proc_macros.insert(name) { return; },
1130             _ => return,
1131         }
1132
1133         let warn_msg = match *ext {
1134             SyntaxExtension::AttrProcMacro(..) =>
1135                 "attribute procedural macros cannot be imported with `#[macro_use]`",
1136             SyntaxExtension::ProcMacro { .. } =>
1137                 "procedural macros cannot be imported with `#[macro_use]`",
1138             _ => return,
1139         };
1140
1141         let def_id = self.current_module.normal_ancestor_id;
1142         let node_id = self.definitions.as_local_node_id(def_id).unwrap();
1143
1144         self.proc_mac_errors.push(ProcMacError {
1145             crate_name: self.cstore.crate_name_untracked(krate),
1146             name,
1147             module: node_id,
1148             use_span,
1149             warn_msg,
1150         });
1151     }
1152
1153     pub fn report_proc_macro_import(&mut self, krate: &ast::Crate) {
1154         for err in self.proc_mac_errors.drain(..) {
1155             let (span, found_use) = ::UsePlacementFinder::check(krate, err.module);
1156
1157             if let Some(span) = span {
1158                 let found_use = if found_use { "" } else { "\n" };
1159                 self.session.struct_span_err(err.use_span, err.warn_msg)
1160                     .span_suggestion(
1161                         span,
1162                         "instead, import the procedural macro like any other item",
1163                         format!("use {}::{};{}", err.crate_name, err.name, found_use),
1164                     ).emit();
1165             } else {
1166                 self.session.struct_span_err(err.use_span, err.warn_msg)
1167                     .help(&format!("instead, import the procedural macro like any other item: \
1168                                     `use {}::{};`", err.crate_name, err.name))
1169                     .emit();
1170             }
1171         }
1172     }
1173
1174     fn gate_legacy_custom_derive(&mut self, name: Symbol, span: Span) {
1175         if !self.session.features_untracked().custom_derive {
1176             let sess = &self.session.parse_sess;
1177             let explain = feature_gate::EXPLAIN_CUSTOM_DERIVE;
1178             emit_feature_err(sess, "custom_derive", span, GateIssue::Language, explain);
1179         } else if !self.is_whitelisted_legacy_custom_derive(name) {
1180             self.session.span_warn(span, feature_gate::EXPLAIN_DEPR_CUSTOM_DERIVE);
1181         }
1182     }
1183 }