Skip to content

Commit

Permalink
resolve: Refactor away MacroBinding
Browse files Browse the repository at this point in the history
`fn resolve_legacy_scope` can now resolve only to `macro_rules!` items,
`fn resolve_lexical_macro_path_segment` is for everything else - modularized macros, preludes
  • Loading branch information
petrochenkov committed Aug 20, 2018
1 parent 23e9a1d commit c2788a8
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 116 deletions.
4 changes: 2 additions & 2 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ use std::mem::replace;
use rustc_data_structures::sync::Lrc;

use resolve_imports::{ImportDirective, ImportDirectiveSubclass, NameResolution, ImportResolver};
use macros::{InvocationData, LegacyBinding, MacroBinding};
use macros::{InvocationData, LegacyBinding};

// NB: This module needs to be declared first so diagnostics are
// registered before they are used.
Expand Down Expand Up @@ -3529,7 +3529,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
} else if opt_ns == Some(MacroNS) {
assert!(ns == TypeNS);
self.resolve_lexical_macro_path_segment(ident, ns, record_used, record_used,
false, path_span).map(MacroBinding::binding)
false, path_span).map(|(b, _)| b)
} else {
let record_used_id =
if record_used { crate_lint.node_id().or(Some(CRATE_NODE_ID)) } else { None };
Expand Down
176 changes: 71 additions & 105 deletions src/librustc_resolve/macros.rs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ LL | MyTrait!(); //~ ERROR can't use a procedural macro from the same crate
| ^^^^^^^

error: can't use a procedural macro from the same crate that defines it
--> $DIR/macro-namespace-reserved-2.rs:44:3
--> $DIR/macro-namespace-reserved-2.rs:43:3
|
LL | #[my_macro] //~ ERROR can't use a procedural macro from the same crate that defines it
| ^^^^^^^^

error: can't use a procedural macro from the same crate that defines it
--> $DIR/macro-namespace-reserved-2.rs:46:3
--> $DIR/macro-namespace-reserved-2.rs:45:3
|
LL | #[my_macro_attr] //~ ERROR can't use a procedural macro from the same crate that defines it
| ^^^^^^^^^^^^^

error: can't use a procedural macro from the same crate that defines it
--> $DIR/macro-namespace-reserved-2.rs:48:3
--> $DIR/macro-namespace-reserved-2.rs:47:3
|
LL | #[MyTrait] //~ ERROR can't use a procedural macro from the same crate that defines it
| ^^^^^^^
Expand Down
21 changes: 21 additions & 0 deletions src/test/ui/imports/issue-53269.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Ambiguity between a `macro_rules` macro and a non-existent import recovered as `Def::Err`

macro_rules! mac { () => () }

mod m {
use nonexistent_module::mac; //~ ERROR unresolved import `nonexistent_module`

mac!(); //~ ERROR `mac` is ambiguous
}

fn main() {}
26 changes: 26 additions & 0 deletions src/test/ui/imports/issue-53269.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error[E0432]: unresolved import `nonexistent_module`
--> $DIR/issue-53269.rs:16:9
|
LL | use nonexistent_module::mac; //~ ERROR unresolved import `nonexistent_module`
| ^^^^^^^^^^^^^^^^^^ Maybe a missing `extern crate nonexistent_module;`?

error: `mac` is ambiguous
--> $DIR/issue-53269.rs:18:5
|
LL | mac!(); //~ ERROR `mac` is ambiguous
| ^^^
|
note: `mac` could refer to the macro defined here
--> $DIR/issue-53269.rs:13:1
|
LL | macro_rules! mac { () => () }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: `mac` could also refer to the macro imported here
--> $DIR/issue-53269.rs:16:9
|
LL | use nonexistent_module::mac; //~ ERROR unresolved import `nonexistent_module`
| ^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0432`.
17 changes: 17 additions & 0 deletions src/test/ui/imports/issue-53512.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Macro from prelude is shadowed by non-existent import recovered as `Def::Err`.

use std::assert; //~ ERROR unresolved import `std::assert`

fn main() {
assert!(true);
}
9 changes: 9 additions & 0 deletions src/test/ui/imports/issue-53512.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0432]: unresolved import `std::assert`
--> $DIR/issue-53512.rs:13:5
|
LL | use std::assert; //~ ERROR unresolved import `std::assert`
| ^^^^^^^^^^^ no `assert` in the root

error: aborting due to previous error

For more information about this error, try `rustc --explain E0432`.
4 changes: 2 additions & 2 deletions src/test/ui/imports/shadow_builtin_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ mod m4 {

mod m5 {
macro_rules! m { () => {
macro_rules! panic { () => {} } //~ ERROR `panic` is already in scope
macro_rules! panic { () => {} }
} }
m!();
panic!();
panic!(); //~ ERROR `panic` is ambiguous
}

#[macro_use(n)]
Expand Down
13 changes: 9 additions & 4 deletions src/test/ui/imports/shadow_builtin_macros.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
error: `panic` is already in scope
error: `panic` is ambiguous
--> $DIR/shadow_builtin_macros.rs:43:5
|
LL | panic!(); //~ ERROR `panic` is ambiguous
| ^^^^^
|
note: `panic` could refer to the macro defined here
--> $DIR/shadow_builtin_macros.rs:40:9
|
LL | macro_rules! panic { () => {} } //~ ERROR `panic` is already in scope
LL | macro_rules! panic { () => {} }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | } }
LL | m!();
| ----- in this macro invocation
|
= note: macro-expanded `macro_rules!`s may not shadow existing macros (see RFC 1560)
note: `panic` could also refer to the macro imported here

error[E0659]: `panic` is ambiguous
--> $DIR/shadow_builtin_macros.rs:25:14
Expand Down

0 comments on commit c2788a8

Please sign in to comment.