Skip to content

Commit dd18cb6

Browse files
committed
resolve: Resolve a few very special in macro namespace
1 parent 3ef8754 commit dd18cb6

File tree

4 files changed

+111
-21
lines changed

4 files changed

+111
-21
lines changed

Diff for: src/librustc_resolve/macros.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
10821082
self.define(module, ident, MacroNS,
10831083
(def, vis, item.span, expansion, IsMacroExport));
10841084
} else {
1085+
self.check_reserved_macro_name(ident, MacroNS);
10851086
self.unused_macros.insert(def_id);
10861087
}
10871088
} else {

Diff for: src/librustc_resolve/resolve_imports.rs

+11
Original file line numberDiff line numberDiff line change
@@ -454,13 +454,24 @@ impl<'a, 'crateloader> Resolver<'a, 'crateloader> {
454454
})
455455
}
456456

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

Diff for: src/test/ui/macros/ambiguous-builtin-attrs.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#![feature(decl_macro)]
1+
#![feature(decl_macro)] //~ ERROR `feature` is ambiguous
2+
3+
macro feature() {}
24

35
macro repr() {}
46

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

10-
macro cfg() {}
12+
macro cfg() {} //~ ERROR name `cfg` is reserved in macro namespace
1113

1214
#[cfg(all())] //~ ERROR `cfg` is ambiguous
1315
struct A;
1416
#[cfg(any())] // ERROR FIXME
1517
struct A;
1618

17-
macro cfg_attr() {}
19+
macro cfg_attr() {} //~ ERROR name `cfg_attr` is reserved in macro namespace
1820

1921
#[cfg_attr(all(), cold)] // ERROR FIXME
2022
fn g() {}
2123
#[cfg_attr(any(), cold)] // ERROR FIXME
2224
fn h() {}
2325

24-
macro derive() {}
26+
macro derive() {} //~ ERROR name `derive` is reserved in macro namespace
2527

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

48+
fn non_macro_expanded_location<#[inline] T>() { //~ ERROR `inline` is ambiguous
49+
match 0u8 {
50+
#[repr(C)] //~ ERROR `repr` is ambiguous
51+
_ => {}
52+
}
53+
}
54+
4655
fn main() {}

Diff for: src/test/ui/macros/ambiguous-builtin-attrs.stderr

+86-17
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,157 @@
1+
error: name `cfg` is reserved in macro namespace
2+
--> $DIR/ambiguous-builtin-attrs.rs:12:7
3+
|
4+
LL | macro cfg() {} //~ ERROR name `cfg` is reserved in macro namespace
5+
| ^^^
6+
7+
error: name `cfg_attr` is reserved in macro namespace
8+
--> $DIR/ambiguous-builtin-attrs.rs:19:7
9+
|
10+
LL | macro cfg_attr() {} //~ ERROR name `cfg_attr` is reserved in macro namespace
11+
| ^^^^^^^^
12+
13+
error: name `derive` is reserved in macro namespace
14+
--> $DIR/ambiguous-builtin-attrs.rs:26:7
15+
|
16+
LL | macro derive() {} //~ ERROR name `derive` is reserved in macro namespace
17+
| ^^^^^^
18+
119
error[E0659]: `repr` is ambiguous
2-
--> $DIR/ambiguous-builtin-attrs.rs:5:3
20+
--> $DIR/ambiguous-builtin-attrs.rs:7:3
321
|
422
LL | #[repr(C)] //~ ERROR `repr` is ambiguous
523
| ^^^^
624
|
725
note: `repr` could refer to the name defined here
8-
--> $DIR/ambiguous-builtin-attrs.rs:3:1
26+
--> $DIR/ambiguous-builtin-attrs.rs:5:1
927
|
1028
LL | macro repr() {}
1129
| ^^^^^^^^^^^^^^^
1230
note: `repr` could also refer to the name defined here
13-
--> $DIR/ambiguous-builtin-attrs.rs:5:3
31+
--> $DIR/ambiguous-builtin-attrs.rs:7:3
1432
|
1533
LL | #[repr(C)] //~ ERROR `repr` is ambiguous
1634
| ^^^^
1735

1836
error[E0659]: `repr` is ambiguous
19-
--> $DIR/ambiguous-builtin-attrs.rs:7:19
37+
--> $DIR/ambiguous-builtin-attrs.rs:9:19
2038
|
2139
LL | #[cfg_attr(all(), repr(C))] //~ ERROR `repr` is ambiguous
2240
| ^^^^
2341
|
2442
note: `repr` could refer to the name defined here
25-
--> $DIR/ambiguous-builtin-attrs.rs:3:1
43+
--> $DIR/ambiguous-builtin-attrs.rs:5:1
2644
|
2745
LL | macro repr() {}
2846
| ^^^^^^^^^^^^^^^
2947
note: `repr` could also refer to the name defined here
30-
--> $DIR/ambiguous-builtin-attrs.rs:7:19
48+
--> $DIR/ambiguous-builtin-attrs.rs:9:19
3149
|
3250
LL | #[cfg_attr(all(), repr(C))] //~ ERROR `repr` is ambiguous
3351
| ^^^^
3452

3553
error[E0659]: `cfg` is ambiguous
36-
--> $DIR/ambiguous-builtin-attrs.rs:12:3
54+
--> $DIR/ambiguous-builtin-attrs.rs:14:3
3755
|
3856
LL | #[cfg(all())] //~ ERROR `cfg` is ambiguous
3957
| ^^^
4058
|
4159
note: `cfg` could refer to the name defined here
42-
--> $DIR/ambiguous-builtin-attrs.rs:10:1
60+
--> $DIR/ambiguous-builtin-attrs.rs:12:1
4361
|
44-
LL | macro cfg() {}
62+
LL | macro cfg() {} //~ ERROR name `cfg` is reserved in macro namespace
4563
| ^^^^^^^^^^^^^^
4664
note: `cfg` could also refer to the name defined here
47-
--> $DIR/ambiguous-builtin-attrs.rs:12:3
65+
--> $DIR/ambiguous-builtin-attrs.rs:14:3
4866
|
4967
LL | #[cfg(all())] //~ ERROR `cfg` is ambiguous
5068
| ^^^
5169

5270
error[E0659]: `inline` is ambiguous
53-
--> $DIR/ambiguous-builtin-attrs.rs:41:3
71+
--> $DIR/ambiguous-builtin-attrs.rs:43:3
5472
|
5573
LL | #[inline] //~ ERROR `inline` is ambiguous
5674
| ^^^^^^
5775
|
5876
note: `inline` could refer to the name defined here
59-
--> $DIR/ambiguous-builtin-attrs.rs:39:1
77+
--> $DIR/ambiguous-builtin-attrs.rs:41:1
6078
|
6179
LL | macro_rules! inline { () => () }
6280
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6381
note: `inline` could also refer to the name defined here
64-
--> $DIR/ambiguous-builtin-attrs.rs:41:3
82+
--> $DIR/ambiguous-builtin-attrs.rs:43:3
6583
|
6684
LL | #[inline] //~ ERROR `inline` is ambiguous
6785
| ^^^^^^
6886

6987
error[E0659]: `inline` is ambiguous
70-
--> $DIR/ambiguous-builtin-attrs.rs:43:19
88+
--> $DIR/ambiguous-builtin-attrs.rs:45:19
7189
|
7290
LL | #[cfg_attr(all(), inline)] //~ ERROR `inline` is ambiguous
7391
| ^^^^^^
7492
|
7593
note: `inline` could refer to the name defined here
76-
--> $DIR/ambiguous-builtin-attrs.rs:39:1
94+
--> $DIR/ambiguous-builtin-attrs.rs:41:1
7795
|
7896
LL | macro_rules! inline { () => () }
7997
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8098
note: `inline` could also refer to the name defined here
81-
--> $DIR/ambiguous-builtin-attrs.rs:43:19
99+
--> $DIR/ambiguous-builtin-attrs.rs:45:19
82100
|
83101
LL | #[cfg_attr(all(), inline)] //~ ERROR `inline` is ambiguous
84102
| ^^^^^^
85103

86-
error: aborting due to 5 previous errors
104+
error[E0659]: `inline` is ambiguous
105+
--> $DIR/ambiguous-builtin-attrs.rs:48:34
106+
|
107+
LL | fn non_macro_expanded_location<#[inline] T>() { //~ ERROR `inline` is ambiguous
108+
| ^^^^^^
109+
|
110+
note: `inline` could refer to the name defined here
111+
--> $DIR/ambiguous-builtin-attrs.rs:41:1
112+
|
113+
LL | macro_rules! inline { () => () }
114+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
115+
note: `inline` could also refer to the name defined here
116+
--> $DIR/ambiguous-builtin-attrs.rs:48:34
117+
|
118+
LL | fn non_macro_expanded_location<#[inline] T>() { //~ ERROR `inline` is ambiguous
119+
| ^^^^^^
120+
121+
error[E0659]: `repr` is ambiguous
122+
--> $DIR/ambiguous-builtin-attrs.rs:50:11
123+
|
124+
LL | #[repr(C)] //~ ERROR `repr` is ambiguous
125+
| ^^^^
126+
|
127+
note: `repr` could refer to the name defined here
128+
--> $DIR/ambiguous-builtin-attrs.rs:5:1
129+
|
130+
LL | macro repr() {}
131+
| ^^^^^^^^^^^^^^^
132+
note: `repr` could also refer to the name defined here
133+
--> $DIR/ambiguous-builtin-attrs.rs:50:11
134+
|
135+
LL | #[repr(C)] //~ ERROR `repr` is ambiguous
136+
| ^^^^
137+
138+
error[E0659]: `feature` is ambiguous
139+
--> $DIR/ambiguous-builtin-attrs.rs:1:4
140+
|
141+
LL | #![feature(decl_macro)] //~ ERROR `feature` is ambiguous
142+
| ^^^^^^^
143+
|
144+
note: `feature` could refer to the name defined here
145+
--> $DIR/ambiguous-builtin-attrs.rs:3:1
146+
|
147+
LL | macro feature() {}
148+
| ^^^^^^^^^^^^^^^^^^
149+
note: `feature` could also refer to the name defined here
150+
--> $DIR/ambiguous-builtin-attrs.rs:1:4
151+
|
152+
LL | #![feature(decl_macro)] //~ ERROR `feature` is ambiguous
153+
| ^^^^^^^
154+
155+
error: aborting due to 11 previous errors
87156

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

0 commit comments

Comments
 (0)