1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use {AmbiguityError, Resolver, ResolutionError, resolve_error};
12 use {Module, ModuleKind, NameBinding, NameBindingKind, PathResult};
13 use Namespace::{self, MacroNS};
14 use build_reduced_graph::BuildReducedGraphVisitor;
15 use resolve_imports::ImportResolver;
16 use rustc::hir::def_id::{DefId, BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, DefIndex};
17 use rustc::hir::def::{Def, Export};
18 use rustc::hir::map::{self, DefCollector};
19 use rustc::{ty, lint};
20 use syntax::ast::{self, Name, Ident};
21 use syntax::attr::{self, HasAttrs};
22 use syntax::errors::DiagnosticBuilder;
23 use syntax::ext::base::{self, Annotatable, Determinacy, MultiModifier, MultiDecorator};
24 use syntax::ext::base::{MacroKind, SyntaxExtension, Resolver as SyntaxResolver};
25 use syntax::ext::expand::{Expansion, ExpansionKind, Invocation, InvocationKind, find_attr_invoc};
26 use syntax::ext::hygiene::Mark;
27 use syntax::ext::placeholders::placeholder;
28 use syntax::ext::tt::macro_rules;
29 use syntax::feature_gate::{self, emit_feature_err, GateIssue};
30 use syntax::fold::{self, Folder};
31 use syntax::parse::parser::PathStyle;
32 use syntax::parse::token::{self, Token};
34 use syntax::symbol::{Symbol, keywords};
35 use syntax::tokenstream::{TokenStream, TokenTree, Delimited};
36 use syntax::util::lev_distance::find_best_match_for_name;
37 use syntax_pos::{Span, DUMMY_SP};
44 pub struct InvocationData<'a> {
45 pub module: Cell<Module<'a>>,
46 pub def_index: DefIndex,
47 // True if this expansion is in a `const_expr` position, for example `[u32; m!()]`.
48 // c.f. `DefCollector::visit_const_expr`.
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>>,
57 impl<'a> InvocationData<'a> {
58 pub fn root(graph_root: Module<'a>) -> Self {
60 module: Cell::new(graph_root),
61 def_index: CRATE_DEF_INDEX,
63 legacy_scope: Cell::new(LegacyScope::Empty),
64 expansion: Cell::new(LegacyScope::Empty),
69 #[derive(Copy, Clone)]
70 pub enum LegacyScope<'a> {
72 Invocation(&'a InvocationData<'a>), // The scope of the invocation, not including its expansion
73 Expansion(&'a InvocationData<'a>), // The scope of the invocation, including its expansion
74 Binding(&'a LegacyBinding<'a>),
77 pub struct LegacyBinding<'a> {
78 pub parent: Cell<LegacyScope<'a>>,
84 #[derive(Copy, Clone)]
85 pub enum MacroBinding<'a> {
86 Legacy(&'a LegacyBinding<'a>),
87 Global(&'a NameBinding<'a>),
88 Modern(&'a NameBinding<'a>),
91 impl<'a> MacroBinding<'a> {
92 pub fn span(self) -> Span {
94 MacroBinding::Legacy(binding) => binding.span,
95 MacroBinding::Global(binding) | MacroBinding::Modern(binding) => binding.span,
99 pub fn binding(self) -> &'a NameBinding<'a> {
101 MacroBinding::Global(binding) | MacroBinding::Modern(binding) => binding,
102 MacroBinding::Legacy(_) => panic!("unexpected MacroBinding::Legacy"),
107 impl<'a> base::Resolver for Resolver<'a> {
108 fn next_node_id(&mut self) -> ast::NodeId {
109 self.session.next_node_id()
112 fn get_module_scope(&mut self, id: ast::NodeId) -> Mark {
113 let mark = Mark::fresh();
114 let module = self.module_map[&self.definitions.local_def_id(id)];
115 self.invocations.insert(mark, self.arenas.alloc_invocation_data(InvocationData {
116 module: Cell::new(module),
117 def_index: module.def_id().unwrap().index,
119 legacy_scope: Cell::new(LegacyScope::Empty),
120 expansion: Cell::new(LegacyScope::Empty),
125 fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item> {
126 struct EliminateCrateVar<'b, 'a: 'b>(&'b mut Resolver<'a>, Span);
128 impl<'a, 'b> Folder for EliminateCrateVar<'a, 'b> {
129 fn fold_path(&mut self, mut path: ast::Path) -> ast::Path {
130 let ident = path.segments[0].identifier;
131 if ident.name == "$crate" {
132 path.segments[0].identifier.name = keywords::CrateRoot.name();
133 let module = self.0.resolve_crate_var(ident.ctxt, self.1);
134 if !module.is_local() {
135 let span = path.segments[0].span;
136 path.segments.insert(1, match module.kind {
137 ModuleKind::Def(_, name) => ast::PathSegment::from_ident(
138 ast::Ident::with_empty_ctxt(name), span
147 fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
148 fold::noop_fold_mac(mac, self)
152 EliminateCrateVar(self, item.span).fold_item(item).expect_one("")
155 fn is_whitelisted_legacy_custom_derive(&self, name: Name) -> bool {
156 self.whitelisted_legacy_custom_derives.contains(&name)
159 fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion, derives: &[Mark]) {
160 let invocation = self.invocations[&mark];
161 self.collect_def_ids(invocation, expansion);
163 self.current_module = invocation.module.get();
164 self.current_module.unresolved_invocations.borrow_mut().remove(&mark);
165 self.current_module.unresolved_invocations.borrow_mut().extend(derives);
166 for &derive in derives {
167 self.invocations.insert(derive, invocation);
169 let mut visitor = BuildReducedGraphVisitor {
171 legacy_scope: LegacyScope::Invocation(invocation),
174 expansion.visit_with(&mut visitor);
175 invocation.expansion.set(visitor.legacy_scope);
178 fn add_builtin(&mut self, ident: ast::Ident, ext: Rc<SyntaxExtension>) {
180 krate: BUILTIN_MACROS_CRATE,
181 index: DefIndex::new(self.macro_map.len()),
183 let kind = ext.kind();
184 self.macro_map.insert(def_id, ext);
185 let binding = self.arenas.alloc_name_binding(NameBinding {
186 kind: NameBindingKind::Def(Def::Macro(def_id, kind)),
188 vis: ty::Visibility::Invisible,
189 expansion: Mark::root(),
191 self.global_macros.insert(ident.name, binding);
194 fn resolve_imports(&mut self) {
195 ImportResolver { resolver: self }.resolve_imports()
198 // Resolves attribute and derive legacy macros from `#![plugin(..)]`.
199 fn find_legacy_attr_invoc(&mut self, attrs: &mut Vec<ast::Attribute>)
200 -> Option<ast::Attribute> {
201 for i in 0..attrs.len() {
202 let name = unwrap_or!(attrs[i].name(), continue);
204 if self.session.plugin_attributes.borrow().iter()
205 .any(|&(ref attr_nm, _)| name == &**attr_nm) {
206 attr::mark_known(&attrs[i]);
209 match self.global_macros.get(&name).cloned() {
210 Some(binding) => match *binding.get_macro(self) {
211 MultiModifier(..) | MultiDecorator(..) | SyntaxExtension::AttrProcMacro(..) => {
212 return Some(attrs.remove(i))
220 // Check for legacy derives
221 for i in 0..attrs.len() {
222 let name = unwrap_or!(attrs[i].name(), continue);
224 if name == "derive" {
225 let result = attrs[i].parse_list(&self.session.parse_sess, |parser| {
226 parser.parse_path_allowing_meta(PathStyle::Mod)
229 let mut traits = match result {
230 Ok(traits) => traits,
237 for j in 0..traits.len() {
238 if traits[j].segments.len() > 1 {
241 let trait_name = traits[j].segments[0].identifier.name;
242 let legacy_name = Symbol::intern(&format!("derive_{}", trait_name));
243 if !self.global_macros.contains_key(&legacy_name) {
246 let span = traits.remove(j).span;
247 self.gate_legacy_custom_derive(legacy_name, span);
248 if traits.is_empty() {
251 let mut tokens = Vec::new();
252 for (j, path) in traits.iter().enumerate() {
254 tokens.push(TokenTree::Token(attrs[i].span, Token::Comma).into());
256 for (k, segment) in path.segments.iter().enumerate() {
258 tokens.push(TokenTree::Token(path.span, Token::ModSep).into());
260 let tok = Token::Ident(segment.identifier);
261 tokens.push(TokenTree::Token(path.span, tok).into());
264 attrs[i].tokens = TokenTree::Delimited(attrs[i].span, Delimited {
266 tts: TokenStream::concat(tokens).into(),
269 return Some(ast::Attribute {
270 path: ast::Path::from_ident(span, Ident::with_empty_ctxt(legacy_name)),
271 tokens: TokenStream::empty(),
272 id: attr::mk_attr_id(),
273 style: ast::AttrStyle::Outer,
274 is_sugared_doc: false,
284 fn resolve_invoc(&mut self, invoc: &mut Invocation, scope: Mark, force: bool)
285 -> Result<Option<Rc<SyntaxExtension>>, Determinacy> {
286 let def = match invoc.kind {
287 InvocationKind::Attr { attr: None, .. } => return Ok(None),
288 _ => match self.resolve_invoc_to_def(invoc, scope, force) {
290 Err(determinacy) => return Err(determinacy),
293 self.macro_defs.insert(invoc.expansion_data.mark, def.def_id());
294 self.unused_macros.remove(&def.def_id());
295 Ok(Some(self.get_macro(def)))
298 fn resolve_macro(&mut self, scope: Mark, path: &ast::Path, kind: MacroKind, force: bool)
299 -> Result<Rc<SyntaxExtension>, Determinacy> {
300 self.resolve_macro_to_def(scope, path, kind, force).map(|def| {
301 self.unused_macros.remove(&def.def_id());
306 fn check_unused_macros(&self) {
307 for did in self.unused_macros.iter() {
308 let id_span = match *self.macro_map[did] {
309 SyntaxExtension::NormalTT(_, isp, _) => isp,
312 if let Some((id, span)) = id_span {
313 let lint = lint::builtin::UNUSED_MACROS;
314 let msg = "unused macro definition".to_string();
315 self.session.add_lint(lint, id, span, msg);
317 bug!("attempted to create unused macro error, but span not available");
323 impl<'a> Resolver<'a> {
324 fn resolve_invoc_to_def(&mut self, invoc: &mut Invocation, scope: Mark, force: bool)
325 -> Result<Def, Determinacy> {
326 let (attr, traits, item) = match invoc.kind {
327 InvocationKind::Attr { ref mut attr, ref traits, ref mut item } => (attr, traits, item),
328 InvocationKind::Bang { ref mac, .. } => {
329 return self.resolve_macro_to_def(scope, &mac.node.path, MacroKind::Bang, force);
331 InvocationKind::Derive { ref path, .. } => {
332 return self.resolve_macro_to_def(scope, path, MacroKind::Derive, force);
337 let path = attr.as_ref().unwrap().path.clone();
338 let mut determinacy = Determinacy::Determined;
339 match self.resolve_macro_to_def(scope, &path, MacroKind::Attr, force) {
340 Ok(def) => return Ok(def),
341 Err(Determinacy::Undetermined) => determinacy = Determinacy::Undetermined,
342 Err(Determinacy::Determined) if force => return Err(Determinacy::Determined),
343 Err(Determinacy::Determined) => {}
346 let attr_name = match path.segments.len() {
347 1 => path.segments[0].identifier.name,
348 _ => return Err(determinacy),
351 match self.resolve_macro(scope, path, MacroKind::Derive, force) {
352 Ok(ext) => if let SyntaxExtension::ProcMacroDerive(_, ref inert_attrs) = *ext {
353 if inert_attrs.contains(&attr_name) {
354 // FIXME(jseyfried) Avoid `mem::replace` here.
355 let dummy_item = placeholder(ExpansionKind::Items, ast::DUMMY_NODE_ID)
356 .make_items().pop().unwrap();
357 let dummy_item = Annotatable::Item(dummy_item);
358 *item = mem::replace(item, dummy_item).map_attrs(|mut attrs| {
359 let inert_attr = attr.take().unwrap();
360 attr::mark_known(&inert_attr);
361 if self.proc_macro_enabled {
362 *attr = find_attr_invoc(&mut attrs);
364 attrs.push(inert_attr);
368 return Err(Determinacy::Undetermined);
370 Err(Determinacy::Undetermined) => determinacy = Determinacy::Undetermined,
371 Err(Determinacy::Determined) => {}
378 fn resolve_macro_to_def(&mut self, scope: Mark, path: &ast::Path, kind: MacroKind, force: bool)
379 -> Result<Def, Determinacy> {
380 let ast::Path { ref segments, span } = *path;
381 if segments.iter().any(|segment| segment.parameters.is_some()) {
383 if segments.last().unwrap().parameters.is_some() { "macro" } else { "module" };
384 let msg = format!("type parameters are not allowed on {}s", kind);
385 self.session.span_err(path.span, &msg);
386 return Err(Determinacy::Determined);
389 let path: Vec<_> = segments.iter().map(|seg| seg.identifier).collect();
390 let invocation = self.invocations[&scope];
391 self.current_module = invocation.module.get();
394 if !self.use_extern_macros && self.gated_errors.insert(span) {
395 let msg = "non-ident macro paths are experimental";
396 let feature = "use_extern_macros";
397 emit_feature_err(&self.session.parse_sess, feature, span, GateIssue::Language, msg);
398 self.found_unresolved_macro = true;
399 return Err(Determinacy::Determined);
402 let def = match self.resolve_path(&path, Some(MacroNS), false, span) {
403 PathResult::NonModule(path_res) => match path_res.base_def() {
404 Def::Err => Err(Determinacy::Determined),
407 PathResult::Module(..) => unreachable!(),
408 PathResult::Indeterminate if !force => return Err(Determinacy::Undetermined),
410 self.found_unresolved_macro = true;
411 Err(Determinacy::Determined)
414 self.current_module.nearest_item_scope().macro_resolutions.borrow_mut()
415 .push((path.into_boxed_slice(), span));
419 let name = path[0].name;
420 let legacy_resolution = self.resolve_legacy_scope(&invocation.legacy_scope, name, false);
421 let result = if let Some(MacroBinding::Legacy(binding)) = legacy_resolution {
422 Ok(Def::Macro(binding.def_id, MacroKind::Bang))
424 match self.resolve_lexical_macro_path_segment(path[0], MacroNS, false, span) {
425 Ok(binding) => Ok(binding.binding().def_ignoring_ambiguity()),
426 Err(Determinacy::Undetermined) if !force => return Err(Determinacy::Undetermined),
428 self.found_unresolved_macro = true;
429 Err(Determinacy::Determined)
434 self.current_module.nearest_item_scope().legacy_macro_resolutions.borrow_mut()
435 .push((scope, path[0], span, kind));
440 // Resolve the initial segment of a non-global macro path (e.g. `foo` in `foo::bar!();`)
441 pub fn resolve_lexical_macro_path_segment(&mut self,
446 -> Result<MacroBinding<'a>, Determinacy> {
447 let mut module = Some(self.current_module);
448 let mut potential_illegal_shadower = Err(Determinacy::Determined);
450 if record_used { Determinacy::Determined } else { Determinacy::Undetermined };
452 let result = if let Some(module) = module {
453 // Since expanded macros may not shadow the lexical scope and
454 // globs may not shadow global macros (both enforced below),
455 // we resolve with restricted shadowing (indicated by the penultimate argument).
456 self.resolve_ident_in_module(module, ident, ns, true, record_used, path_span)
457 .map(MacroBinding::Modern)
459 self.global_macros.get(&ident.name).cloned().ok_or(determinacy)
460 .map(MacroBinding::Global)
463 match result.map(MacroBinding::binding) {
468 if let Ok(MacroBinding::Modern(shadower)) = potential_illegal_shadower {
469 if shadower.def() != binding.def() {
470 let name = ident.name;
471 self.ambiguity_errors.push(AmbiguityError {
479 return potential_illegal_shadower;
482 if binding.expansion != Mark::root() ||
483 (binding.is_glob_import() && module.unwrap().def().is_some()) {
484 potential_illegal_shadower = result;
489 Err(Determinacy::Undetermined) => return Err(Determinacy::Undetermined),
490 Err(Determinacy::Determined) => {}
493 module = match module {
494 Some(module) => match module.kind {
495 ModuleKind::Block(..) => module.parent,
496 ModuleKind::Def(..) => None,
498 None => return potential_illegal_shadower,
503 pub fn resolve_legacy_scope(&mut self,
504 mut scope: &'a Cell<LegacyScope<'a>>,
507 -> Option<MacroBinding<'a>> {
508 let mut possible_time_travel = None;
509 let mut relative_depth: u32 = 0;
510 let mut binding = None;
513 LegacyScope::Empty => break,
514 LegacyScope::Expansion(invocation) => {
515 match invocation.expansion.get() {
516 LegacyScope::Invocation(_) => scope.set(invocation.legacy_scope.get()),
517 LegacyScope::Empty => {
518 if possible_time_travel.is_none() {
519 possible_time_travel = Some(scope);
521 scope = &invocation.legacy_scope;
525 scope = &invocation.expansion;
529 LegacyScope::Invocation(invocation) => {
530 relative_depth = relative_depth.saturating_sub(1);
531 scope = &invocation.legacy_scope;
533 LegacyScope::Binding(potential_binding) => {
534 if potential_binding.name == name {
535 if (!self.use_extern_macros || record_used) && relative_depth > 0 {
536 self.disallowed_shadowing.push(potential_binding);
538 binding = Some(potential_binding);
541 scope = &potential_binding.parent;
546 let binding = if let Some(binding) = binding {
547 MacroBinding::Legacy(binding)
548 } else if let Some(binding) = self.global_macros.get(&name).cloned() {
549 if !self.use_extern_macros {
550 self.record_use(Ident::with_empty_ctxt(name), MacroNS, binding, DUMMY_SP);
552 MacroBinding::Global(binding)
557 if !self.use_extern_macros {
558 if let Some(scope) = possible_time_travel {
559 // Check for disallowed shadowing later
560 self.lexical_macro_resolutions.push((name, scope));
567 pub fn finalize_current_module_macro_resolutions(&mut self) {
568 let module = self.current_module;
569 for &(ref path, span) in module.macro_resolutions.borrow().iter() {
570 match self.resolve_path(path, Some(MacroNS), true, span) {
571 PathResult::NonModule(_) => {},
572 PathResult::Failed(msg, _) => {
573 resolve_error(self, span, ResolutionError::FailedToResolve(&msg));
579 for &(mark, ident, span, kind) in module.legacy_macro_resolutions.borrow().iter() {
580 let legacy_scope = &self.invocations[&mark].legacy_scope;
581 let legacy_resolution = self.resolve_legacy_scope(legacy_scope, ident.name, true);
582 let resolution = self.resolve_lexical_macro_path_segment(ident, MacroNS, true, span);
583 match (legacy_resolution, resolution) {
584 (Some(MacroBinding::Legacy(legacy_binding)), Ok(MacroBinding::Modern(binding))) => {
585 let msg1 = format!("`{}` could refer to the macro defined here", ident);
586 let msg2 = format!("`{}` could also refer to the macro imported here", ident);
587 self.session.struct_span_err(span, &format!("`{}` is ambiguous", ident))
588 .span_note(legacy_binding.span, &msg1)
589 .span_note(binding.span, &msg2)
592 (Some(MacroBinding::Global(binding)), Ok(MacroBinding::Global(_))) => {
593 self.record_use(ident, MacroNS, binding, span);
594 self.err_if_macro_use_proc_macro(ident.name, span, binding);
597 let msg = match kind {
599 format!("cannot find macro `{}!` in this scope", ident),
601 format!("cannot find attribute macro `{}` in this scope", ident),
603 format!("cannot find derive macro `{}` in this scope", ident),
605 let mut err = self.session.struct_span_err(span, &msg);
606 self.suggest_macro_name(&ident.name.as_str(), kind, &mut err, span);
614 fn suggest_macro_name(&mut self, name: &str, kind: MacroKind,
615 err: &mut DiagnosticBuilder<'a>, span: Span) {
616 // First check if this is a locally-defined bang macro.
617 let suggestion = if let MacroKind::Bang = kind {
618 find_best_match_for_name(self.macro_names.iter(), name, None)
621 // Then check global macros.
623 // FIXME: get_macro needs an &mut Resolver, can we do it without cloning?
624 let global_macros = self.global_macros.clone();
625 let names = global_macros.iter().filter_map(|(name, binding)| {
626 if binding.get_macro(self).kind() == kind {
632 find_best_match_for_name(names, name, None)
633 // Then check modules.
635 if !self.use_extern_macros {
638 let is_macro = |def| {
639 if let Def::Macro(_, def_kind) = def {
645 let ident = Ident::from_str(name);
646 self.lookup_typo_candidate(&vec![ident], MacroNS, is_macro, span)
649 if let Some(suggestion) = suggestion {
650 if suggestion != name {
651 if let MacroKind::Bang = kind {
652 err.help(&format!("did you mean `{}!`?", suggestion));
654 err.help(&format!("did you mean `{}`?", suggestion));
657 err.help("have you added the `#[macro_use]` on the module/import?");
662 fn collect_def_ids(&mut self, invocation: &'a InvocationData<'a>, expansion: &Expansion) {
663 let Resolver { ref mut invocations, arenas, graph_root, .. } = *self;
664 let InvocationData { def_index, const_expr, .. } = *invocation;
666 let visit_macro_invoc = &mut |invoc: map::MacroInvocationData| {
667 invocations.entry(invoc.mark).or_insert_with(|| {
668 arenas.alloc_invocation_data(InvocationData {
669 def_index: invoc.def_index,
670 const_expr: invoc.const_expr,
671 module: Cell::new(graph_root),
672 expansion: Cell::new(LegacyScope::Empty),
673 legacy_scope: Cell::new(LegacyScope::Empty),
678 let mut def_collector = DefCollector::new(&mut self.definitions);
679 def_collector.visit_macro_invoc = Some(visit_macro_invoc);
680 def_collector.with_parent(def_index, |def_collector| {
682 if let Expansion::Expr(ref expr) = *expansion {
683 def_collector.visit_const_expr(expr);
686 expansion.visit_with(def_collector)
690 pub fn define_macro(&mut self, item: &ast::Item, legacy_scope: &mut LegacyScope<'a>) {
691 self.local_macro_def_scopes.insert(item.id, self.current_module);
692 let ident = item.ident;
693 if ident.name == "macro_rules" {
694 self.session.span_err(item.span, "user-defined macros may not be named `macro_rules`");
697 let def_id = self.definitions.local_def_id(item.id);
698 let ext = Rc::new(macro_rules::compile(&self.session.parse_sess,
699 &self.session.features,
701 self.macro_map.insert(def_id, ext);
702 *legacy_scope = LegacyScope::Binding(self.arenas.alloc_legacy_binding(LegacyBinding {
703 parent: Cell::new(*legacy_scope), name: ident.name, def_id: def_id, span: item.span,
705 self.macro_names.insert(ident.name);
707 if attr::contains_name(&item.attrs, "macro_export") {
708 let def = Def::Macro(def_id, MacroKind::Bang);
709 self.macro_exports.push(Export { name: ident.name, def: def, span: item.span });
711 self.unused_macros.insert(def_id);
715 /// Error if `ext` is a Macros 1.1 procedural macro being imported by `#[macro_use]`
716 fn err_if_macro_use_proc_macro(&mut self, name: Name, use_span: Span,
717 binding: &NameBinding<'a>) {
718 use self::SyntaxExtension::*;
720 let krate = binding.def().def_id().krate;
722 // Plugin-based syntax extensions are exempt from this check
723 if krate == BUILTIN_MACROS_CRATE { return; }
725 let ext = binding.get_macro(self);
728 // If `ext` is a procedural macro, check if we've already warned about it
729 AttrProcMacro(_) | ProcMacro(_) => if !self.warned_proc_macros.insert(name) { return; },
733 let warn_msg = match *ext {
734 AttrProcMacro(_) => "attribute procedural macros cannot be \
735 imported with `#[macro_use]`",
736 ProcMacro(_) => "procedural macros cannot be imported with `#[macro_use]`",
740 let crate_name = self.session.cstore.crate_name(krate);
742 self.session.struct_span_err(use_span, warn_msg)
743 .help(&format!("instead, import the procedural macro like any other item: \
744 `use {}::{};`", crate_name, name))
748 fn gate_legacy_custom_derive(&mut self, name: Symbol, span: Span) {
749 if !self.session.features.borrow().custom_derive {
750 let sess = &self.session.parse_sess;
751 let explain = feature_gate::EXPLAIN_CUSTOM_DERIVE;
752 emit_feature_err(sess, "custom_derive", span, GateIssue::Language, explain);
753 } else if !self.is_whitelisted_legacy_custom_derive(name) {
754 self.session.span_warn(span, feature_gate::EXPLAIN_DEPR_CUSTOM_DERIVE);