Skip to content

Commit

Permalink
Prohibit duplicated macro_exports
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Apr 21, 2018
1 parent e59f78f commit eaebec0
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
25 changes: 15 additions & 10 deletions src/librustc_resolve/resolve_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,14 +887,24 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
// Since import resolution is finished, globs will not define any more names.
*module.globs.borrow_mut() = Vec::new();

let report_already_exported = |this: &mut Self, ident, span, prev_span| {
let msg = format!("a macro named `{}` has already been exported", ident);
this.session.struct_span_err(span, &msg)
.span_label(span, format!("`{}` already exported", ident))
.span_note(prev_span, "previous macro export here")
.emit();
};

let mut reexports = Vec::new();
let mut exported_macro_names = FxHashMap();
if module as *const _ == self.graph_root as *const _ {
let macro_exports = mem::replace(&mut self.macro_exports, Vec::new());
for export in macro_exports.into_iter().rev() {
if exported_macro_names.insert(export.ident.modern(), export.span).is_none() {
reexports.push(export);
for export in macro_exports.into_iter() {
if let Some(prev_span) = exported_macro_names.insert(export.ident.modern(),
export.span) {
report_already_exported(self, export.ident, export.span, prev_span);
}
reexports.push(export);
}
}

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

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

// Test that `#[macro_export] macro_rules!` shadow earlier `#[macro_export] macro_rules!`

#[macro_use]
extern crate issue_38715;

fn main() {
foo!();
}
macro_rules! macro_two { ($($t:tt)*) => ($($t)*) }
4 changes: 2 additions & 2 deletions src/test/run-pass/mod_dir_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ pub fn main() {

#[path = "auxiliary"]
mod foo {
mod two_macros;
mod two_macros_2;
}

#[path = "auxiliary"]
mod bar {
macro_rules! m { () => { mod two_macros; } }
macro_rules! m { () => { mod two_macros_2; } }
m!();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
macro_rules! foo { ($i:ident) => {} }

#[macro_export]
macro_rules! foo { () => {} }
macro_rules! foo { () => {} } //~ ERROR a macro named `foo` has already been exported
19 changes: 19 additions & 0 deletions src/test/ui/issue-38715.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error: a macro named `foo` has already been exported
--> $DIR/issue-38715.rs:15:1
|
LL | macro_rules! foo { () => {} } //~ ERROR a macro named `foo` has already been exported
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `foo` already exported
|
note: previous macro export here
--> $DIR/issue-38715.rs:12:1
|
LL | macro_rules! foo { ($i:ident) => {} }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0601]: `main` function not found in crate `issue_38715`
|
= note: consider adding a `main` function to `$DIR/issue-38715.rs`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0601`.

0 comments on commit eaebec0

Please sign in to comment.