Skip to content

Commit de153d6

Browse files
committedSep 10, 2018
resolve: Reserve a few very special names in macro namespace
1 parent 65c8c97 commit de153d6

File tree

7 files changed

+62
-52
lines changed

7 files changed

+62
-52
lines changed
 

‎src/librustc_resolve/macros.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,9 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
10831083
self.define(module, ident, MacroNS,
10841084
(def, vis, item.span, expansion, IsMacroExport));
10851085
} else {
1086+
if !attr::contains_name(&item.attrs, "rustc_doc_only_macro") {
1087+
self.check_reserved_macro_name(ident, MacroNS);
1088+
}
10861089
self.unused_macros.insert(def_id);
10871090
}
10881091
} else {

‎src/librustc_resolve/resolve_imports.rs

+11
Original file line numberDiff line numberDiff line change
@@ -452,13 +452,24 @@ impl<'a, 'crateloader> Resolver<'a, 'crateloader> {
452452
})
453453
}
454454

455+
crate fn check_reserved_macro_name(&self, ident: Ident, ns: Namespace) {
456+
// Reserve some names that are not quite covered by the general check
457+
// performed on `Resolver::builtin_attrs`.
458+
if ns == MacroNS &&
459+
(ident.name == "cfg" || ident.name == "cfg_attr" || ident.name == "derive") {
460+
self.session.span_err(ident.span,
461+
&format!("name `{}` is reserved in macro namespace", ident));
462+
}
463+
}
464+
455465
// Define the name or return the existing binding if there is a collision.
456466
pub fn try_define(&mut self,
457467
module: Module<'a>,
458468
ident: Ident,
459469
ns: Namespace,
460470
binding: &'a NameBinding<'a>)
461471
-> Result<(), &'a NameBinding<'a>> {
472+
self.check_reserved_macro_name(ident, ns);
462473
self.update_resolution(module, ident, ns, |this, resolution| {
463474
if let Some(old_binding) = resolution.binding {
464475
if binding.is_glob_import() {

‎src/test/ui-fulldeps/proc-macro/ambiguous-builtin-attrs.rs

-13
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,6 @@ struct S;
1111
#[cfg_attr(all(), repr(C))] //~ ERROR `repr` is ambiguous
1212
struct SCond;
1313

14-
#[cfg(all())] //~ ERROR `cfg` is ambiguous
15-
struct A;
16-
#[cfg(any())] // ERROR FIXME
17-
struct A;
18-
19-
#[cfg_attr(all(), cold)] // ERROR FIXME
20-
fn g() {}
21-
#[cfg_attr(any(), cold)] // ERROR FIXME
22-
fn h() {}
23-
24-
#[derive(Clone)] // ERROR FIXME
25-
struct B;
26-
2714
#[test] // OK, shadowed
2815
fn test() {}
2916

‎src/test/ui-fulldeps/proc-macro/ambiguous-builtin-attrs.stderr

+6-24
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,8 @@ LL | #[cfg_attr(all(), repr(C))] //~ ERROR `repr` is ambiguous
3434
| ^^^^
3535
= note: consider adding an explicit import of `repr` to disambiguate
3636

37-
error[E0659]: `cfg` is ambiguous
38-
--> $DIR/ambiguous-builtin-attrs.rs:14:3
39-
|
40-
LL | #[cfg(all())] //~ ERROR `cfg` is ambiguous
41-
| ^^^ ambiguous name
42-
|
43-
note: `cfg` could refer to the name imported here
44-
--> $DIR/ambiguous-builtin-attrs.rs:7:5
45-
|
46-
LL | use builtin_attrs::*;
47-
| ^^^^^^^^^^^^^^^^
48-
note: `cfg` could also refer to the name defined here
49-
--> $DIR/ambiguous-builtin-attrs.rs:14:3
50-
|
51-
LL | #[cfg(all())] //~ ERROR `cfg` is ambiguous
52-
| ^^^
53-
= note: consider adding an explicit import of `cfg` to disambiguate
54-
5537
error[E0659]: `repr` is ambiguous
56-
--> $DIR/ambiguous-builtin-attrs.rs:33:34
38+
--> $DIR/ambiguous-builtin-attrs.rs:20:34
5739
|
5840
LL | fn non_macro_expanded_location<#[repr(C)] T>() { //~ ERROR `repr` is ambiguous
5941
| ^^^^ ambiguous name
@@ -64,14 +46,14 @@ note: `repr` could refer to the name imported here
6446
LL | use builtin_attrs::*;
6547
| ^^^^^^^^^^^^^^^^
6648
note: `repr` could also refer to the name defined here
67-
--> $DIR/ambiguous-builtin-attrs.rs:33:34
49+
--> $DIR/ambiguous-builtin-attrs.rs:20:34
6850
|
6951
LL | fn non_macro_expanded_location<#[repr(C)] T>() { //~ ERROR `repr` is ambiguous
7052
| ^^^^
7153
= note: consider adding an explicit import of `repr` to disambiguate
7254

7355
error[E0659]: `repr` is ambiguous
74-
--> $DIR/ambiguous-builtin-attrs.rs:35:11
56+
--> $DIR/ambiguous-builtin-attrs.rs:22:11
7557
|
7658
LL | #[repr(C)] //~ ERROR `repr` is ambiguous
7759
| ^^^^ ambiguous name
@@ -82,7 +64,7 @@ note: `repr` could refer to the name imported here
8264
LL | use builtin_attrs::*;
8365
| ^^^^^^^^^^^^^^^^
8466
note: `repr` could also refer to the name defined here
85-
--> $DIR/ambiguous-builtin-attrs.rs:35:11
67+
--> $DIR/ambiguous-builtin-attrs.rs:22:11
8668
|
8769
LL | #[repr(C)] //~ ERROR `repr` is ambiguous
8870
| ^^^^
@@ -107,12 +89,12 @@ LL | #![feature(decl_macro)] //~ ERROR `feature` is ambiguous
10789
= note: consider adding an explicit import of `feature` to disambiguate
10890

10991
error[E0425]: cannot find value `NonExistent` in this scope
110-
--> $DIR/ambiguous-builtin-attrs.rs:43:5
92+
--> $DIR/ambiguous-builtin-attrs.rs:30:5
11193
|
11294
LL | NonExistent; //~ ERROR cannot find value `NonExistent` in this scope
11395
| ^^^^^^^^^^^ not found in this scope
11496

115-
error: aborting due to 7 previous errors
97+
error: aborting due to 6 previous errors
11698

11799
Some errors occurred: E0425, E0659.
118100
For more information about an error, try `rustc --explain E0425`.

‎src/test/ui-fulldeps/proc-macro/auxiliary/builtin-attrs.rs

-15
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,6 @@ pub fn repr(_: TokenStream, input: TokenStream) -> TokenStream {
2525
input
2626
}
2727

28-
#[proc_macro_attribute]
29-
pub fn cfg(_: TokenStream, input: TokenStream) -> TokenStream {
30-
input
31-
}
32-
33-
#[proc_macro_attribute]
34-
pub fn cfg_attr(_: TokenStream, input: TokenStream) -> TokenStream {
35-
input
36-
}
37-
38-
#[proc_macro_attribute]
39-
pub fn derive(_: TokenStream, input: TokenStream) -> TokenStream {
40-
input
41-
}
42-
4328
#[proc_macro_attribute]
4429
pub fn test(_: TokenStream, input: TokenStream) -> TokenStream {
4530
"struct Test;".parse().unwrap()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#![crate_type = "proc-macro"]
2+
3+
extern crate proc_macro;
4+
use proc_macro::*;
5+
6+
#[proc_macro_attribute]
7+
pub fn cfg(_: TokenStream, input: TokenStream) -> TokenStream {
8+
//~^ ERROR name `cfg` is reserved in macro namespace
9+
input
10+
}
11+
12+
#[proc_macro_attribute]
13+
pub fn cfg_attr(_: TokenStream, input: TokenStream) -> TokenStream {
14+
//~^ ERROR name `cfg_attr` is reserved in macro namespace
15+
input
16+
}
17+
18+
#[proc_macro_attribute]
19+
pub fn derive(_: TokenStream, input: TokenStream) -> TokenStream {
20+
//~^ ERROR name `derive` is reserved in macro namespace
21+
input
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: name `cfg` is reserved in macro namespace
2+
--> $DIR/reserved-macro-names.rs:7:8
3+
|
4+
LL | pub fn cfg(_: TokenStream, input: TokenStream) -> TokenStream {
5+
| ^^^
6+
7+
error: name `cfg_attr` is reserved in macro namespace
8+
--> $DIR/reserved-macro-names.rs:13:8
9+
|
10+
LL | pub fn cfg_attr(_: TokenStream, input: TokenStream) -> TokenStream {
11+
| ^^^^^^^^
12+
13+
error: name `derive` is reserved in macro namespace
14+
--> $DIR/reserved-macro-names.rs:19:8
15+
|
16+
LL | pub fn derive(_: TokenStream, input: TokenStream) -> TokenStream {
17+
| ^^^^^^
18+
19+
error: aborting due to 3 previous errors
20+

0 commit comments

Comments
 (0)
Please sign in to comment.