Skip to content

Commit a314612

Browse files
committed
Auto merge of #50143 - petrochenkov:mexuniq, r=<try>
Prohibit duplicated `macro_export`s cc #35896 (comment)
2 parents e59f78f + eaebec0 commit a314612

File tree

5 files changed

+40
-23
lines changed

5 files changed

+40
-23
lines changed

src/librustc_resolve/resolve_imports.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -887,14 +887,24 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
887887
// Since import resolution is finished, globs will not define any more names.
888888
*module.globs.borrow_mut() = Vec::new();
889889

890+
let report_already_exported = |this: &mut Self, ident, span, prev_span| {
891+
let msg = format!("a macro named `{}` has already been exported", ident);
892+
this.session.struct_span_err(span, &msg)
893+
.span_label(span, format!("`{}` already exported", ident))
894+
.span_note(prev_span, "previous macro export here")
895+
.emit();
896+
};
897+
890898
let mut reexports = Vec::new();
891899
let mut exported_macro_names = FxHashMap();
892900
if module as *const _ == self.graph_root as *const _ {
893901
let macro_exports = mem::replace(&mut self.macro_exports, Vec::new());
894-
for export in macro_exports.into_iter().rev() {
895-
if exported_macro_names.insert(export.ident.modern(), export.span).is_none() {
896-
reexports.push(export);
902+
for export in macro_exports.into_iter() {
903+
if let Some(prev_span) = exported_macro_names.insert(export.ident.modern(),
904+
export.span) {
905+
report_already_exported(self, export.ident, export.span, prev_span);
897906
}
907+
reexports.push(export);
898908
}
899909
}
900910

@@ -912,13 +922,8 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
912922
self.cstore.export_macros_untracked(def.def_id().krate);
913923
}
914924
if let Def::Macro(..) = def {
915-
if let Some(&span) = exported_macro_names.get(&ident.modern()) {
916-
let msg =
917-
format!("a macro named `{}` has already been exported", ident);
918-
self.session.struct_span_err(span, &msg)
919-
.span_label(span, format!("`{}` already exported", ident))
920-
.span_note(binding.span, "previous macro export here")
921-
.emit();
925+
if let Some(legacy_span) = exported_macro_names.get(&ident.modern()) {
926+
report_already_exported(self, ident, *legacy_span, binding.span);
922927
}
923928
}
924929
reexports.push(Export {
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -8,13 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// aux-build:issue_38715.rs
11+
macro_rules! macro_one { ($($t:tt)*) => ($($t)*) }
1212

13-
// Test that `#[macro_export] macro_rules!` shadow earlier `#[macro_export] macro_rules!`
14-
15-
#[macro_use]
16-
extern crate issue_38715;
17-
18-
fn main() {
19-
foo!();
20-
}
13+
macro_rules! macro_two { ($($t:tt)*) => ($($t)*) }

src/test/run-pass/mod_dir_path.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ pub fn main() {
2020

2121
#[path = "auxiliary"]
2222
mod foo {
23-
mod two_macros;
23+
mod two_macros_2;
2424
}
2525

2626
#[path = "auxiliary"]
2727
mod bar {
28-
macro_rules! m { () => { mod two_macros; } }
28+
macro_rules! m { () => { mod two_macros_2; } }
2929
m!();
3030
}
3131
}

src/test/run-pass/auxiliary/issue_38715.rs renamed to src/test/ui/issue-38715.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
macro_rules! foo { ($i:ident) => {} }
1313

1414
#[macro_export]
15-
macro_rules! foo { () => {} }
15+
macro_rules! foo { () => {} } //~ ERROR a macro named `foo` has already been exported

src/test/ui/issue-38715.stderr

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: a macro named `foo` has already been exported
2+
--> $DIR/issue-38715.rs:15:1
3+
|
4+
LL | macro_rules! foo { () => {} } //~ ERROR a macro named `foo` has already been exported
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `foo` already exported
6+
|
7+
note: previous macro export here
8+
--> $DIR/issue-38715.rs:12:1
9+
|
10+
LL | macro_rules! foo { ($i:ident) => {} }
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error[E0601]: `main` function not found in crate `issue_38715`
14+
|
15+
= note: consider adding a `main` function to `$DIR/issue-38715.rs`
16+
17+
error: aborting due to 2 previous errors
18+
19+
For more information about this error, try `rustc --explain E0601`.

0 commit comments

Comments
 (0)