Skip to content

Commit

Permalink
resolve: Resolve a few very special in macro namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Sep 3, 2018
1 parent 8dad6d8 commit 90641fc
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/librustc_resolve/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
self.define(module, ident, MacroNS,
(def, vis, item.span, expansion, IsMacroExport));
} else {
self.check_reserved_macro_name(ident, MacroNS);
self.unused_macros.insert(def_id);
}
} else {
Expand Down
11 changes: 11 additions & 0 deletions src/librustc_resolve/resolve_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,13 +454,24 @@ impl<'a, 'crateloader> Resolver<'a, 'crateloader> {
})
}

crate fn check_reserved_macro_name(&self, ident: Ident, ns: Namespace) {
// Reserve some names that are not quite covered by the general check
// performed on `Resolver::builtin_attrs`.
if ns == MacroNS &&
(ident.name == "cfg" || ident.name == "cfg_attr" || ident.name == "derive") {
self.session.span_err(ident.span,
&format!("name `{}` is reserved in macro namespace", ident));
}
}

// Define the name or return the existing binding if there is a collision.
pub fn try_define(&mut self,
module: Module<'a>,
ident: Ident,
ns: Namespace,
binding: &'a NameBinding<'a>)
-> Result<(), &'a NameBinding<'a>> {
self.check_reserved_macro_name(ident, ns);
self.update_resolution(module, ident, ns, |this, resolution| {
if let Some(old_binding) = resolution.binding {
if binding.is_glob_import() {
Expand Down
17 changes: 13 additions & 4 deletions src/test/ui/macros/ambiguous-builtin-attrs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#![feature(decl_macro)]
#![feature(decl_macro)] //~ ERROR `feature` is ambiguous

macro feature() {}

macro repr() {}

Expand All @@ -7,21 +9,21 @@ struct S;
#[cfg_attr(all(), repr(C))] //~ ERROR `repr` is ambiguous
struct SCond;

macro cfg() {}
macro cfg() {} //~ ERROR name `cfg` is reserved in macro namespace

#[cfg(all())] //~ ERROR `cfg` is ambiguous
struct A;
#[cfg(any())] // ERROR FIXME
struct A;

macro cfg_attr() {}
macro cfg_attr() {} //~ ERROR name `cfg_attr` is reserved in macro namespace

#[cfg_attr(all(), cold)] // ERROR FIXME
fn g() {}
#[cfg_attr(any(), cold)] // ERROR FIXME
fn h() {}

macro derive() {}
macro derive() {} //~ ERROR name `derive` is reserved in macro namespace

#[derive(Clone)] // ERROR FIXME
struct B;
Expand All @@ -43,4 +45,11 @@ fn f() {}
#[cfg_attr(all(), inline)] //~ ERROR `inline` is ambiguous
fn f_cond() {}

fn non_macro_expanded_location<#[inline] T>() { //~ ERROR `inline` is ambiguous
match 0u8 {
#[repr(C)] //~ ERROR `repr` is ambiguous
_ => {}
}
}

fn main() {}
103 changes: 86 additions & 17 deletions src/test/ui/macros/ambiguous-builtin-attrs.stderr
Original file line number Diff line number Diff line change
@@ -1,88 +1,157 @@
error: name `cfg` is reserved in macro namespace
--> $DIR/ambiguous-builtin-attrs.rs:12:7
|
LL | macro cfg() {} //~ ERROR name `cfg` is reserved in macro namespace
| ^^^

error: name `cfg_attr` is reserved in macro namespace
--> $DIR/ambiguous-builtin-attrs.rs:19:7
|
LL | macro cfg_attr() {} //~ ERROR name `cfg_attr` is reserved in macro namespace
| ^^^^^^^^

error: name `derive` is reserved in macro namespace
--> $DIR/ambiguous-builtin-attrs.rs:26:7
|
LL | macro derive() {} //~ ERROR name `derive` is reserved in macro namespace
| ^^^^^^

error[E0659]: `repr` is ambiguous
--> $DIR/ambiguous-builtin-attrs.rs:5:3
--> $DIR/ambiguous-builtin-attrs.rs:7:3
|
LL | #[repr(C)] //~ ERROR `repr` is ambiguous
| ^^^^
|
note: `repr` could refer to the name defined here
--> $DIR/ambiguous-builtin-attrs.rs:3:1
--> $DIR/ambiguous-builtin-attrs.rs:5:1
|
LL | macro repr() {}
| ^^^^^^^^^^^^^^^
note: `repr` could also refer to the name defined here
--> $DIR/ambiguous-builtin-attrs.rs:5:3
--> $DIR/ambiguous-builtin-attrs.rs:7:3
|
LL | #[repr(C)] //~ ERROR `repr` is ambiguous
| ^^^^

error[E0659]: `repr` is ambiguous
--> $DIR/ambiguous-builtin-attrs.rs:7:19
--> $DIR/ambiguous-builtin-attrs.rs:9:19
|
LL | #[cfg_attr(all(), repr(C))] //~ ERROR `repr` is ambiguous
| ^^^^
|
note: `repr` could refer to the name defined here
--> $DIR/ambiguous-builtin-attrs.rs:3:1
--> $DIR/ambiguous-builtin-attrs.rs:5:1
|
LL | macro repr() {}
| ^^^^^^^^^^^^^^^
note: `repr` could also refer to the name defined here
--> $DIR/ambiguous-builtin-attrs.rs:7:19
--> $DIR/ambiguous-builtin-attrs.rs:9:19
|
LL | #[cfg_attr(all(), repr(C))] //~ ERROR `repr` is ambiguous
| ^^^^

error[E0659]: `cfg` is ambiguous
--> $DIR/ambiguous-builtin-attrs.rs:12:3
--> $DIR/ambiguous-builtin-attrs.rs:14:3
|
LL | #[cfg(all())] //~ ERROR `cfg` is ambiguous
| ^^^
|
note: `cfg` could refer to the name defined here
--> $DIR/ambiguous-builtin-attrs.rs:10:1
--> $DIR/ambiguous-builtin-attrs.rs:12:1
|
LL | macro cfg() {}
LL | macro cfg() {} //~ ERROR name `cfg` is reserved in macro namespace
| ^^^^^^^^^^^^^^
note: `cfg` could also refer to the name defined here
--> $DIR/ambiguous-builtin-attrs.rs:12:3
--> $DIR/ambiguous-builtin-attrs.rs:14:3
|
LL | #[cfg(all())] //~ ERROR `cfg` is ambiguous
| ^^^

error[E0659]: `inline` is ambiguous
--> $DIR/ambiguous-builtin-attrs.rs:41:3
--> $DIR/ambiguous-builtin-attrs.rs:43:3
|
LL | #[inline] //~ ERROR `inline` is ambiguous
| ^^^^^^
|
note: `inline` could refer to the name defined here
--> $DIR/ambiguous-builtin-attrs.rs:39:1
--> $DIR/ambiguous-builtin-attrs.rs:41:1
|
LL | macro_rules! inline { () => () }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: `inline` could also refer to the name defined here
--> $DIR/ambiguous-builtin-attrs.rs:41:3
--> $DIR/ambiguous-builtin-attrs.rs:43:3
|
LL | #[inline] //~ ERROR `inline` is ambiguous
| ^^^^^^

error[E0659]: `inline` is ambiguous
--> $DIR/ambiguous-builtin-attrs.rs:43:19
--> $DIR/ambiguous-builtin-attrs.rs:45:19
|
LL | #[cfg_attr(all(), inline)] //~ ERROR `inline` is ambiguous
| ^^^^^^
|
note: `inline` could refer to the name defined here
--> $DIR/ambiguous-builtin-attrs.rs:39:1
--> $DIR/ambiguous-builtin-attrs.rs:41:1
|
LL | macro_rules! inline { () => () }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: `inline` could also refer to the name defined here
--> $DIR/ambiguous-builtin-attrs.rs:43:19
--> $DIR/ambiguous-builtin-attrs.rs:45:19
|
LL | #[cfg_attr(all(), inline)] //~ ERROR `inline` is ambiguous
| ^^^^^^

error: aborting due to 5 previous errors
error[E0659]: `inline` is ambiguous
--> $DIR/ambiguous-builtin-attrs.rs:48:34
|
LL | fn non_macro_expanded_location<#[inline] T>() { //~ ERROR `inline` is ambiguous
| ^^^^^^
|
note: `inline` could refer to the name defined here
--> $DIR/ambiguous-builtin-attrs.rs:41:1
|
LL | macro_rules! inline { () => () }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: `inline` could also refer to the name defined here
--> $DIR/ambiguous-builtin-attrs.rs:48:34
|
LL | fn non_macro_expanded_location<#[inline] T>() { //~ ERROR `inline` is ambiguous
| ^^^^^^

error[E0659]: `repr` is ambiguous
--> $DIR/ambiguous-builtin-attrs.rs:50:11
|
LL | #[repr(C)] //~ ERROR `repr` is ambiguous
| ^^^^
|
note: `repr` could refer to the name defined here
--> $DIR/ambiguous-builtin-attrs.rs:5:1
|
LL | macro repr() {}
| ^^^^^^^^^^^^^^^
note: `repr` could also refer to the name defined here
--> $DIR/ambiguous-builtin-attrs.rs:50:11
|
LL | #[repr(C)] //~ ERROR `repr` is ambiguous
| ^^^^

error[E0659]: `feature` is ambiguous
--> $DIR/ambiguous-builtin-attrs.rs:1:4
|
LL | #![feature(decl_macro)] //~ ERROR `feature` is ambiguous
| ^^^^^^^
|
note: `feature` could refer to the name defined here
--> $DIR/ambiguous-builtin-attrs.rs:3:1
|
LL | macro feature() {}
| ^^^^^^^^^^^^^^^^^^
note: `feature` could also refer to the name defined here
--> $DIR/ambiguous-builtin-attrs.rs:1:4
|
LL | #![feature(decl_macro)] //~ ERROR `feature` is ambiguous
| ^^^^^^^

error: aborting due to 11 previous errors

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

0 comments on commit 90641fc

Please sign in to comment.