Skip to content

Commit 8bb7a85

Browse files
committed
Forbid $crate in macro patterns
1 parent 144227d commit 8bb7a85

File tree

3 files changed

+64
-43
lines changed

3 files changed

+64
-43
lines changed

compiler/rustc_expand/src/mbe/quoted.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ fn parse_tree(
221221
let (ident, is_raw) = token.ident().unwrap();
222222
let span = ident.span.with_lo(span.lo());
223223
if ident.name == kw::Crate && !is_raw {
224+
if parsing_patterns {
225+
span_dollar_dollar_or_metavar_in_the_lhs_err(sess, &token);
226+
}
224227
TokenTree::token(token::Ident(kw::DollarCrate, is_raw), span)
225228
} else {
226229
TokenTree::MetaVar(span, ident)
@@ -359,6 +362,6 @@ fn span_dollar_dollar_or_metavar_in_the_lhs_err<'sess>(sess: &'sess ParseSess, t
359362
.span_err(token.span, &format!("unexpected token: {}", pprust::token_to_string(token)));
360363
sess.span_diagnostic.span_note_without_error(
361364
token.span,
362-
"`$$` and meta-variable expressions are not allowed inside macro parameter definitions",
365+
"`$$`, `$crate`, and meta-variable expressions are not allowed inside macro parameter definitions",
363366
);
364367
}

src/test/ui/macros/rfc-3086-metavar-expr/syntax-errors.rs

+6
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ macro_rules! dollar_dollar_in_the_lhs {
5555
};
5656
}
5757

58+
macro_rules! dollar_crate_in_the_lhs {
59+
( $crate $a:ident ) => {
60+
//~^ ERROR unexpected token: crate
61+
};
62+
}
63+
5864
macro_rules! extra_garbage_after_metavar {
5965
( $( $i:ident ),* ) => {
6066
${count() a b c}

src/test/ui/macros/rfc-3086-metavar-expr/syntax-errors.stderr

+54-42
Original file line numberDiff line numberDiff line change
@@ -16,182 +16,194 @@ error: unexpected token: $
1616
LL | ( $$ $a:ident ) => {
1717
| ^
1818

19-
note: `$$` and meta-variable expressions are not allowed inside macro parameter definitions
19+
note: `$$`, `$crate`, and meta-variable expressions are not allowed inside macro parameter definitions
2020
--> $DIR/syntax-errors.rs:53:8
2121
|
2222
LL | ( $$ $a:ident ) => {
2323
| ^
2424

25+
error: unexpected token: crate
26+
--> $DIR/syntax-errors.rs:59:8
27+
|
28+
LL | ( $crate $a:ident ) => {
29+
| ^^^^^
30+
31+
note: `$$`, `$crate`, and meta-variable expressions are not allowed inside macro parameter definitions
32+
--> $DIR/syntax-errors.rs:59:8
33+
|
34+
LL | ( $crate $a:ident ) => {
35+
| ^^^^^
36+
2537
error: unexpected token: a
26-
--> $DIR/syntax-errors.rs:60:19
38+
--> $DIR/syntax-errors.rs:66:19
2739
|
2840
LL | ${count() a b c}
2941
| ^
3042
|
3143
note: meta-variable expression must not have trailing tokens
32-
--> $DIR/syntax-errors.rs:60:19
44+
--> $DIR/syntax-errors.rs:66:19
3345
|
3446
LL | ${count() a b c}
3547
| ^
3648

3749
error: unexpected token: a
38-
--> $DIR/syntax-errors.rs:63:19
50+
--> $DIR/syntax-errors.rs:69:19
3951
|
4052
LL | ${count(i a b c)}
4153
| ^
4254
|
4355
note: meta-variable expression must not have trailing tokens
44-
--> $DIR/syntax-errors.rs:63:19
56+
--> $DIR/syntax-errors.rs:69:19
4557
|
4658
LL | ${count(i a b c)}
4759
| ^
4860

4961
error: unexpected token: a
50-
--> $DIR/syntax-errors.rs:65:22
62+
--> $DIR/syntax-errors.rs:71:22
5163
|
5264
LL | ${count(i, 1 a b c)}
5365
| ^
5466
|
5567
note: meta-variable expression must not have trailing tokens
56-
--> $DIR/syntax-errors.rs:65:22
68+
--> $DIR/syntax-errors.rs:71:22
5769
|
5870
LL | ${count(i, 1 a b c)}
5971
| ^
6072

6173
error: unexpected token: a
62-
--> $DIR/syntax-errors.rs:67:20
74+
--> $DIR/syntax-errors.rs:73:20
6375
|
6476
LL | ${count(i) a b c}
6577
| ^
6678
|
6779
note: meta-variable expression must not have trailing tokens
68-
--> $DIR/syntax-errors.rs:67:20
80+
--> $DIR/syntax-errors.rs:73:20
6981
|
7082
LL | ${count(i) a b c}
7183
| ^
7284

7385
error: unexpected token: a
74-
--> $DIR/syntax-errors.rs:70:21
86+
--> $DIR/syntax-errors.rs:76:21
7587
|
7688
LL | ${ignore(i) a b c}
7789
| ^
7890
|
7991
note: meta-variable expression must not have trailing tokens
80-
--> $DIR/syntax-errors.rs:70:21
92+
--> $DIR/syntax-errors.rs:76:21
8193
|
8294
LL | ${ignore(i) a b c}
8395
| ^
8496

8597
error: unexpected token: a
86-
--> $DIR/syntax-errors.rs:72:20
98+
--> $DIR/syntax-errors.rs:78:20
8799
|
88100
LL | ${ignore(i a b c)}
89101
| ^
90102
|
91103
note: meta-variable expression must not have trailing tokens
92-
--> $DIR/syntax-errors.rs:72:20
104+
--> $DIR/syntax-errors.rs:78:20
93105
|
94106
LL | ${ignore(i a b c)}
95107
| ^
96108

97109
error: unexpected token: a
98-
--> $DIR/syntax-errors.rs:75:19
110+
--> $DIR/syntax-errors.rs:81:19
99111
|
100112
LL | ${index() a b c}
101113
| ^
102114
|
103115
note: meta-variable expression must not have trailing tokens
104-
--> $DIR/syntax-errors.rs:75:19
116+
--> $DIR/syntax-errors.rs:81:19
105117
|
106118
LL | ${index() a b c}
107119
| ^
108120

109121
error: unexpected token: a
110-
--> $DIR/syntax-errors.rs:77:19
122+
--> $DIR/syntax-errors.rs:83:19
111123
|
112124
LL | ${index(1 a b c)}
113125
| ^
114126
|
115127
note: meta-variable expression must not have trailing tokens
116-
--> $DIR/syntax-errors.rs:77:19
128+
--> $DIR/syntax-errors.rs:83:19
117129
|
118130
LL | ${index(1 a b c)}
119131
| ^
120132

121133
error: unexpected token: a
122-
--> $DIR/syntax-errors.rs:80:19
134+
--> $DIR/syntax-errors.rs:86:19
123135
|
124136
LL | ${index() a b c}
125137
| ^
126138
|
127139
note: meta-variable expression must not have trailing tokens
128-
--> $DIR/syntax-errors.rs:80:19
140+
--> $DIR/syntax-errors.rs:86:19
129141
|
130142
LL | ${index() a b c}
131143
| ^
132144

133145
error: unexpected token: a
134-
--> $DIR/syntax-errors.rs:82:19
146+
--> $DIR/syntax-errors.rs:88:19
135147
|
136148
LL | ${index(1 a b c)}
137149
| ^
138150
|
139151
note: meta-variable expression must not have trailing tokens
140-
--> $DIR/syntax-errors.rs:82:19
152+
--> $DIR/syntax-errors.rs:88:19
141153
|
142154
LL | ${index(1 a b c)}
143155
| ^
144156

145157
error: meta-variable expression depth must be a literal
146-
--> $DIR/syntax-errors.rs:89:33
158+
--> $DIR/syntax-errors.rs:95:33
147159
|
148160
LL | ( $( $i:ident ),* ) => { ${ index(IDX) } };
149161
| ^^^^^
150162

151163
error: unexpected token: {
152-
--> $DIR/syntax-errors.rs:95:8
164+
--> $DIR/syntax-errors.rs:101:8
153165
|
154166
LL | ( ${ length() } ) => {
155167
| ^^^^^^^^^^^^
156168

157-
note: `$$` and meta-variable expressions are not allowed inside macro parameter definitions
158-
--> $DIR/syntax-errors.rs:95:8
169+
note: `$$`, `$crate`, and meta-variable expressions are not allowed inside macro parameter definitions
170+
--> $DIR/syntax-errors.rs:101:8
159171
|
160172
LL | ( ${ length() } ) => {
161173
| ^^^^^^^^^^^^
162174

163175
error: expected one of: `*`, `+`, or `?`
164-
--> $DIR/syntax-errors.rs:95:8
176+
--> $DIR/syntax-errors.rs:101:8
165177
|
166178
LL | ( ${ length() } ) => {
167179
| ^^^^^^^^^^^^
168180

169181
error: expected identifier
170-
--> $DIR/syntax-errors.rs:102:33
182+
--> $DIR/syntax-errors.rs:108:33
171183
|
172184
LL | ( $( $i:ident ),* ) => { ${ ignore() } };
173185
| ^^^^^^
174186

175187
error: only unsuffixes integer literals are supported in meta-variable expressions
176-
--> $DIR/syntax-errors.rs:108:33
188+
--> $DIR/syntax-errors.rs:114:33
177189
|
178190
LL | ( $( $i:ident ),* ) => { ${ index(1u32) } };
179191
| ^^^^^
180192

181193
error: meta-variable expression parameter must be wrapped in parentheses
182-
--> $DIR/syntax-errors.rs:114:33
194+
--> $DIR/syntax-errors.rs:120:33
183195
|
184196
LL | ( $( $i:ident ),* ) => { ${ count{i} } };
185197
| ^^^^^
186198

187199
error: expected identifier
188-
--> $DIR/syntax-errors.rs:120:31
200+
--> $DIR/syntax-errors.rs:126:31
189201
|
190202
LL | ( $( $i:ident ),* ) => { ${ {} } };
191203
| ^^^^^^
192204

193205
error: unrecognized meta-variable expression
194-
--> $DIR/syntax-errors.rs:140:33
206+
--> $DIR/syntax-errors.rs:146:33
195207
|
196208
LL | ( $( $i:ident ),* ) => { ${ aaaaaaaaaaaaaa(i) } };
197209
| ^^^^^^^^^^^^^^ help: supported expressions are count, ignore, index and length
@@ -231,7 +243,7 @@ LL | ( $( $i:ident ),* ) => { count($i) };
231243
| ^^
232244

233245
error: expected expression, found `$`
234-
--> $DIR/syntax-errors.rs:60:9
246+
--> $DIR/syntax-errors.rs:66:9
235247
|
236248
LL | ${count() a b c}
237249
| ^ expected expression
@@ -242,7 +254,7 @@ LL | extra_garbage_after_metavar!(a);
242254
= note: this error originates in the macro `extra_garbage_after_metavar` (in Nightly builds, run with -Z macro-backtrace for more info)
243255

244256
error: expected expression, found `$`
245-
--> $DIR/syntax-errors.rs:89:30
257+
--> $DIR/syntax-errors.rs:95:30
246258
|
247259
LL | ( $( $i:ident ),* ) => { ${ index(IDX) } };
248260
| ^ expected expression
@@ -253,7 +265,7 @@ LL | metavar_depth_is_not_literal!(a);
253265
= note: this error originates in the macro `metavar_depth_is_not_literal` (in Nightly builds, run with -Z macro-backtrace for more info)
254266

255267
error: expected expression, found `$`
256-
--> $DIR/syntax-errors.rs:102:30
268+
--> $DIR/syntax-errors.rs:108:30
257269
|
258270
LL | ( $( $i:ident ),* ) => { ${ ignore() } };
259271
| ^ expected expression
@@ -264,7 +276,7 @@ LL | metavar_token_without_ident!(a);
264276
= note: this error originates in the macro `metavar_token_without_ident` (in Nightly builds, run with -Z macro-backtrace for more info)
265277

266278
error: expected expression, found `$`
267-
--> $DIR/syntax-errors.rs:108:30
279+
--> $DIR/syntax-errors.rs:114:30
268280
|
269281
LL | ( $( $i:ident ),* ) => { ${ index(1u32) } };
270282
| ^ expected expression
@@ -275,7 +287,7 @@ LL | metavar_with_literal_suffix!(a);
275287
= note: this error originates in the macro `metavar_with_literal_suffix` (in Nightly builds, run with -Z macro-backtrace for more info)
276288

277289
error: expected expression, found `$`
278-
--> $DIR/syntax-errors.rs:114:30
290+
--> $DIR/syntax-errors.rs:120:30
279291
|
280292
LL | ( $( $i:ident ),* ) => { ${ count{i} } };
281293
| ^ expected expression
@@ -286,7 +298,7 @@ LL | metavar_without_parens!(a);
286298
= note: this error originates in the macro `metavar_without_parens` (in Nightly builds, run with -Z macro-backtrace for more info)
287299

288300
error: expected expression, found `$`
289-
--> $DIR/syntax-errors.rs:120:30
301+
--> $DIR/syntax-errors.rs:126:30
290302
|
291303
LL | ( $( $i:ident ),* ) => { ${ {} } };
292304
| ^ expected expression
@@ -297,19 +309,19 @@ LL | open_brackets_without_tokens!(a);
297309
= note: this error originates in the macro `open_brackets_without_tokens` (in Nightly builds, run with -Z macro-backtrace for more info)
298310

299311
error: variable `foo` is not recognized in meta-variable expression
300-
--> $DIR/syntax-errors.rs:127:17
312+
--> $DIR/syntax-errors.rs:133:17
301313
|
302314
LL | ${count(foo)}
303315
| ^^^
304316

305317
error: variable `bar` is not recognized in meta-variable expression
306-
--> $DIR/syntax-errors.rs:134:18
318+
--> $DIR/syntax-errors.rs:140:18
307319
|
308320
LL | ${ignore(bar)}
309321
| ^^^
310322

311323
error: expected expression, found `$`
312-
--> $DIR/syntax-errors.rs:140:30
324+
--> $DIR/syntax-errors.rs:146:30
313325
|
314326
LL | ( $( $i:ident ),* ) => { ${ aaaaaaaaaaaaaa(i) } };
315327
| ^ expected expression
@@ -375,11 +387,11 @@ LL | no_curly__rhs_dollar__no_round!(a);
375387
= note: this error originates in the macro `no_curly__rhs_dollar__no_round` (in Nightly builds, run with -Z macro-backtrace for more info)
376388

377389
error[E0425]: cannot find value `a` in this scope
378-
--> $DIR/syntax-errors.rs:153:37
390+
--> $DIR/syntax-errors.rs:159:37
379391
|
380392
LL | no_curly__rhs_dollar__no_round!(a);
381393
| ^ not found in this scope
382394

383-
error: aborting due to 40 previous errors
395+
error: aborting due to 41 previous errors
384396

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

0 commit comments

Comments
 (0)