Skip to content

Commit cb6d2df

Browse files
committed
Auto merge of #53461 - petrochenkov:pmu, r=alexcrichton
resolve: Do not error on access to proc macros imported with `#[macro_use]` This error is artificial, but previously, when `#[macro_use] extern crate x;` was stable, but non-derive proc macros were not, it worked like kind of a feature gate. Now both features are stable, so the error is no longer necessary. This PR simplifies how `#[macro_use] extern crate x;` works - it takes all items from macro namespace of `x`'s root and puts them into macro prelude from which they all can now be accessed.
2 parents 8a2dec6 + e411bb3 commit cb6d2df

File tree

6 files changed

+2
-83
lines changed

6 files changed

+2
-83
lines changed

src/librustc_resolve/lib.rs

-8
Original file line numberDiff line numberDiff line change
@@ -1429,8 +1429,6 @@ pub struct Resolver<'a, 'b: 'a> {
14291429
ambiguity_errors: Vec<AmbiguityError<'a>>,
14301430
/// `use` injections are delayed for better placement and deduplication
14311431
use_injections: Vec<UseError<'a>>,
1432-
/// `use` injections for proc macros wrongly imported with #[macro_use]
1433-
proc_mac_errors: Vec<macros::ProcMacError>,
14341432
/// crate-local macro expanded `macro_export` referred to by a module-relative path
14351433
macro_expanded_macro_export_errors: BTreeSet<(Span, Span)>,
14361434

@@ -1458,9 +1456,6 @@ pub struct Resolver<'a, 'b: 'a> {
14581456
/// Avoid duplicated errors for "name already defined".
14591457
name_already_seen: FxHashMap<Name, Span>,
14601458

1461-
/// A set of procedural macros imported by `#[macro_use]` that have already been warned about
1462-
warned_proc_macros: FxHashSet<Name>,
1463-
14641459
potentially_unused_imports: Vec<&'a ImportDirective<'a>>,
14651460

14661461
/// This table maps struct IDs into struct constructor IDs,
@@ -1744,7 +1739,6 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
17441739
privacy_errors: Vec::new(),
17451740
ambiguity_errors: Vec::new(),
17461741
use_injections: Vec::new(),
1747-
proc_mac_errors: Vec::new(),
17481742
macro_expanded_macro_export_errors: BTreeSet::new(),
17491743

17501744
arenas,
@@ -1766,7 +1760,6 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
17661760
local_macro_def_scopes: FxHashMap(),
17671761
name_already_seen: FxHashMap(),
17681762
whitelisted_legacy_custom_derives: Vec::new(),
1769-
warned_proc_macros: FxHashSet(),
17701763
potentially_unused_imports: Vec::new(),
17711764
struct_constructors: DefIdMap(),
17721765
found_unresolved_macro: false,
@@ -4602,7 +4595,6 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
46024595

46034596
fn report_errors(&mut self, krate: &Crate) {
46044597
self.report_with_use_injections(krate);
4605-
self.report_proc_macro_import(krate);
46064598
let mut reported_spans = FxHashSet();
46074599

46084600
for &(span_use, span_def) in &self.macro_expanded_macro_export_errors {

src/librustc_resolve/macros.rs

-73
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX, DefIndex,
1919
use rustc::hir::def::{Def, NonMacroAttrKind};
2020
use rustc::hir::map::{self, DefCollector};
2121
use rustc::{ty, lint};
22-
use rustc::middle::cstore::CrateStore;
2322
use syntax::ast::{self, Name, Ident};
2423
use syntax::attr;
2524
use syntax::errors::DiagnosticBuilder;
@@ -110,14 +109,6 @@ pub struct ParentScope<'a> {
110109
crate derives: Vec<ast::Path>,
111110
}
112111

113-
pub struct ProcMacError {
114-
crate_name: Symbol,
115-
name: Symbol,
116-
module: ast::NodeId,
117-
use_span: Span,
118-
warn_msg: &'static str,
119-
}
120-
121112
// Macro namespace is separated into two sub-namespaces, one for bang macros and
122113
// one for attribute-like macros (attributes, derives).
123114
// We ignore resolutions from one sub-namespace when searching names in scope for another.
@@ -980,7 +971,6 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
980971
check_consistency(self, binding.def_ignoring_ambiguity());
981972
if from_prelude {
982973
self.record_use(ident, MacroNS, binding);
983-
self.err_if_macro_use_proc_macro(ident.name, span, binding);
984974
}
985975
}
986976
};
@@ -1132,69 +1122,6 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
11321122
}
11331123
}
11341124

1135-
/// Error if `ext` is a Macros 1.1 procedural macro being imported by `#[macro_use]`
1136-
fn err_if_macro_use_proc_macro(&mut self, name: Name, use_span: Span,
1137-
binding: &NameBinding<'a>) {
1138-
let krate = match binding.def() {
1139-
Def::NonMacroAttr(..) | Def::Err => return,
1140-
Def::Macro(def_id, _) => def_id.krate,
1141-
_ => unreachable!(),
1142-
};
1143-
1144-
// Plugin-based syntax extensions are exempt from this check
1145-
if krate == CrateNum::BuiltinMacros { return; }
1146-
1147-
let ext = binding.get_macro(self);
1148-
1149-
match *ext {
1150-
// If `ext` is a procedural macro, check if we've already warned about it
1151-
SyntaxExtension::AttrProcMacro(..) | SyntaxExtension::ProcMacro { .. } =>
1152-
if !self.warned_proc_macros.insert(name) { return; },
1153-
_ => return,
1154-
}
1155-
1156-
let warn_msg = match *ext {
1157-
SyntaxExtension::AttrProcMacro(..) =>
1158-
"attribute procedural macros cannot be imported with `#[macro_use]`",
1159-
SyntaxExtension::ProcMacro { .. } =>
1160-
"procedural macros cannot be imported with `#[macro_use]`",
1161-
_ => return,
1162-
};
1163-
1164-
let def_id = self.current_module.normal_ancestor_id;
1165-
let node_id = self.definitions.as_local_node_id(def_id).unwrap();
1166-
1167-
self.proc_mac_errors.push(ProcMacError {
1168-
crate_name: self.cstore.crate_name_untracked(krate),
1169-
name,
1170-
module: node_id,
1171-
use_span,
1172-
warn_msg,
1173-
});
1174-
}
1175-
1176-
pub fn report_proc_macro_import(&mut self, krate: &ast::Crate) {
1177-
for err in self.proc_mac_errors.drain(..) {
1178-
let (span, found_use) = ::UsePlacementFinder::check(krate, err.module);
1179-
1180-
if let Some(span) = span {
1181-
let found_use = if found_use { "" } else { "\n" };
1182-
self.session.struct_span_err(err.use_span, err.warn_msg)
1183-
.span_suggestion_with_applicability(
1184-
span,
1185-
"instead, import the procedural macro like any other item",
1186-
format!("use {}::{};{}", err.crate_name, err.name, found_use),
1187-
Applicability::MachineApplicable
1188-
).emit();
1189-
} else {
1190-
self.session.struct_span_err(err.use_span, err.warn_msg)
1191-
.help(&format!("instead, import the procedural macro like any other item: \
1192-
`use {}::{};`", err.crate_name, err.name))
1193-
.emit();
1194-
}
1195-
}
1196-
}
1197-
11981125
fn gate_legacy_custom_derive(&mut self, name: Symbol, span: Span) {
11991126
if !self.session.features_untracked().custom_derive {
12001127
let sess = &self.session.parse_sess;

src/test/compile-fail-fulldeps/proc-macro/macro-use-attr.rs renamed to src/test/ui-fulldeps/proc-macro/macro-use-attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// compile-pass
1112
// aux-build:attr_proc_macro.rs
1213

1314
#[macro_use] extern crate attr_proc_macro;
1415

1516
#[attr_proc_macro]
16-
//~^ ERROR: attribute procedural macros cannot be imported with `#[macro_use]`
1717
struct Foo;
1818

1919
fn main() {

src/test/compile-fail-fulldeps/proc-macro/macro-use-bang.rs renamed to src/test/ui-fulldeps/proc-macro/macro-use-bang.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// compile-pass
1112
// aux-build:bang_proc_macro.rs
1213

1314
#![feature(proc_macro_non_items)]
@@ -17,5 +18,4 @@ extern crate bang_proc_macro;
1718

1819
fn main() {
1920
bang_proc_macro!(println!("Hello, world!"));
20-
//~^ ERROR: procedural macros cannot be imported with `#[macro_use]`
2121
}

0 commit comments

Comments
 (0)