Skip to content

Commit 4d6bfde

Browse files
committed
Improve error message when _ is used for in/inout asm operands
1 parent e7f7fe4 commit 4d6bfde

File tree

3 files changed

+61
-25
lines changed

3 files changed

+61
-25
lines changed

compiler/rustc_builtin_macros/src/asm.rs

+12
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ fn parse_args<'a>(
117117
let mut explicit_reg = false;
118118
let op = if !is_global_asm && p.eat_keyword(kw::In) {
119119
let reg = parse_reg(&mut p, &mut explicit_reg)?;
120+
if p.eat_keyword(kw::Underscore) {
121+
let err = ecx.struct_span_err(p.token.span, "_ cannot be used for input operands");
122+
return Err(err);
123+
}
120124
let expr = p.parse_expr()?;
121125
ast::InlineAsmOperand::In { reg, expr }
122126
} else if !is_global_asm && p.eat_keyword(sym::out) {
@@ -129,6 +133,10 @@ fn parse_args<'a>(
129133
ast::InlineAsmOperand::Out { reg, expr, late: true }
130134
} else if !is_global_asm && p.eat_keyword(sym::inout) {
131135
let reg = parse_reg(&mut p, &mut explicit_reg)?;
136+
if p.eat_keyword(kw::Underscore) {
137+
let err = ecx.struct_span_err(p.token.span, "_ cannot be used for input operands");
138+
return Err(err);
139+
}
132140
let expr = p.parse_expr()?;
133141
if p.eat(&token::FatArrow) {
134142
let out_expr =
@@ -139,6 +147,10 @@ fn parse_args<'a>(
139147
}
140148
} else if !is_global_asm && p.eat_keyword(sym::inlateout) {
141149
let reg = parse_reg(&mut p, &mut explicit_reg)?;
150+
if p.eat_keyword(kw::Underscore) {
151+
let err = ecx.struct_span_err(p.token.span, "_ cannot be used for input operands");
152+
return Err(err);
153+
}
142154
let expr = p.parse_expr()?;
143155
if p.eat(&token::FatArrow) {
144156
let out_expr =

src/test/ui/asm/parse-error.rs

+6
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ fn main() {
7676
//~^ ERROR asm template must be a string literal
7777
asm!("{1}", format!("{{{}}}", 0), in(reg) foo, out(reg) bar);
7878
//~^ ERROR asm template must be a string literal
79+
asm!("{}", in(reg) _);
80+
//~^ ERROR _ cannot be used for input operands
81+
asm!("{}", inout(reg) _);
82+
//~^ ERROR _ cannot be used for input operands
83+
asm!("{}", inlateout(reg) _);
84+
//~^ ERROR _ cannot be used for input operands
7985
}
8086
}
8187

src/test/ui/asm/parse-error.stderr

+43-25
Original file line numberDiff line numberDiff line change
@@ -214,162 +214,180 @@ LL | asm!("{1}", format!("{{{}}}", 0), in(reg) foo, out(reg) bar);
214214
|
215215
= note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)
216216

217+
error: _ cannot be used for input operands
218+
--> $DIR/parse-error.rs:79:28
219+
|
220+
LL | asm!("{}", in(reg) _);
221+
| ^
222+
223+
error: _ cannot be used for input operands
224+
--> $DIR/parse-error.rs:81:31
225+
|
226+
LL | asm!("{}", inout(reg) _);
227+
| ^
228+
229+
error: _ cannot be used for input operands
230+
--> $DIR/parse-error.rs:83:35
231+
|
232+
LL | asm!("{}", inlateout(reg) _);
233+
| ^
234+
217235
error: requires at least a template string argument
218-
--> $DIR/parse-error.rs:84:1
236+
--> $DIR/parse-error.rs:90:1
219237
|
220238
LL | global_asm!();
221239
| ^^^^^^^^^^^^^^
222240

223241
error: asm template must be a string literal
224-
--> $DIR/parse-error.rs:86:13
242+
--> $DIR/parse-error.rs:92:13
225243
|
226244
LL | global_asm!(FOO);
227245
| ^^^
228246

229247
error: expected token: `,`
230-
--> $DIR/parse-error.rs:88:18
248+
--> $DIR/parse-error.rs:94:18
231249
|
232250
LL | global_asm!("{}" FOO);
233251
| ^^^ expected `,`
234252

235253
error: expected operand, options, or additional template string
236-
--> $DIR/parse-error.rs:90:19
254+
--> $DIR/parse-error.rs:96:19
237255
|
238256
LL | global_asm!("{}", FOO);
239257
| ^^^ expected operand, options, or additional template string
240258

241259
error: expected expression, found end of macro arguments
242-
--> $DIR/parse-error.rs:92:24
260+
--> $DIR/parse-error.rs:98:24
243261
|
244262
LL | global_asm!("{}", const);
245263
| ^ expected expression
246264

247265
error: expected one of `,`, `.`, `?`, or an operator, found `FOO`
248-
--> $DIR/parse-error.rs:94:30
266+
--> $DIR/parse-error.rs:100:30
249267
|
250268
LL | global_asm!("{}", const(reg) FOO);
251269
| ^^^ expected one of `,`, `.`, `?`, or an operator
252270

253271
error: expected one of `)`, `att_syntax`, or `raw`, found `FOO`
254-
--> $DIR/parse-error.rs:96:25
272+
--> $DIR/parse-error.rs:102:25
255273
|
256274
LL | global_asm!("", options(FOO));
257275
| ^^^ expected one of `)`, `att_syntax`, or `raw`
258276

259277
error: expected one of `)`, `att_syntax`, or `raw`, found `nomem`
260-
--> $DIR/parse-error.rs:98:25
278+
--> $DIR/parse-error.rs:104:25
261279
|
262280
LL | global_asm!("", options(nomem FOO));
263281
| ^^^^^ expected one of `)`, `att_syntax`, or `raw`
264282

265283
error: expected one of `)`, `att_syntax`, or `raw`, found `nomem`
266-
--> $DIR/parse-error.rs:100:25
284+
--> $DIR/parse-error.rs:106:25
267285
|
268286
LL | global_asm!("", options(nomem, FOO));
269287
| ^^^^^ expected one of `)`, `att_syntax`, or `raw`
270288

271289
error: arguments are not allowed after options
272-
--> $DIR/parse-error.rs:102:30
290+
--> $DIR/parse-error.rs:108:30
273291
|
274292
LL | global_asm!("{}", options(), const FOO);
275293
| --------- ^^^^^^^^^ argument
276294
| |
277295
| previous options
278296

279297
error: expected string literal
280-
--> $DIR/parse-error.rs:104:29
298+
--> $DIR/parse-error.rs:110:29
281299
|
282300
LL | global_asm!("", clobber_abi(FOO));
283301
| ^^^ not a string literal
284302

285303
error: expected `)`, found `FOO`
286-
--> $DIR/parse-error.rs:106:33
304+
--> $DIR/parse-error.rs:112:33
287305
|
288306
LL | global_asm!("", clobber_abi("C" FOO));
289307
| ^^^ expected `)`
290308

291309
error: expected `)`, found `,`
292-
--> $DIR/parse-error.rs:108:32
310+
--> $DIR/parse-error.rs:114:32
293311
|
294312
LL | global_asm!("", clobber_abi("C", FOO));
295313
| ^ expected `)`
296314

297315
error: arguments are not allowed after clobber_abi
298-
--> $DIR/parse-error.rs:110:37
316+
--> $DIR/parse-error.rs:116:37
299317
|
300318
LL | global_asm!("{}", clobber_abi("C"), const FOO);
301319
| ---------------- ^^^^^^^^^ argument
302320
| |
303321
| clobber_abi
304322

305323
error: `clobber_abi` cannot be used with `global_asm!`
306-
--> $DIR/parse-error.rs:110:19
324+
--> $DIR/parse-error.rs:116:19
307325
|
308326
LL | global_asm!("{}", clobber_abi("C"), const FOO);
309327
| ^^^^^^^^^^^^^^^^
310328

311329
error: clobber_abi is not allowed after options
312-
--> $DIR/parse-error.rs:113:28
330+
--> $DIR/parse-error.rs:119:28
313331
|
314332
LL | global_asm!("", options(), clobber_abi("C"));
315333
| --------- ^^^^^^^^^^^^^^^^
316334
| |
317335
| options
318336

319337
error: clobber_abi is not allowed after options
320-
--> $DIR/parse-error.rs:115:30
338+
--> $DIR/parse-error.rs:121:30
321339
|
322340
LL | global_asm!("{}", options(), clobber_abi("C"), const FOO);
323341
| --------- ^^^^^^^^^^^^^^^^
324342
| |
325343
| options
326344

327345
error: clobber_abi specified multiple times
328-
--> $DIR/parse-error.rs:117:35
346+
--> $DIR/parse-error.rs:123:35
329347
|
330348
LL | global_asm!("", clobber_abi("C"), clobber_abi("C"));
331349
| ---------------- ^^^^^^^^^^^^^^^^
332350
| |
333351
| clobber_abi previously specified here
334352

335353
error: duplicate argument named `a`
336-
--> $DIR/parse-error.rs:119:35
354+
--> $DIR/parse-error.rs:125:35
337355
|
338356
LL | global_asm!("{a}", a = const FOO, a = const BAR);
339357
| ------------- ^^^^^^^^^^^^^ duplicate argument
340358
| |
341359
| previously here
342360

343361
error: argument never used
344-
--> $DIR/parse-error.rs:119:35
362+
--> $DIR/parse-error.rs:125:35
345363
|
346364
LL | global_asm!("{a}", a = const FOO, a = const BAR);
347365
| ^^^^^^^^^^^^^ argument never used
348366
|
349367
= help: if this argument is intentionally unused, consider using it in an asm comment: `"/* {1} */"`
350368

351369
error: expected one of `clobber_abi`, `const`, or `options`, found `""`
352-
--> $DIR/parse-error.rs:122:28
370+
--> $DIR/parse-error.rs:128:28
353371
|
354372
LL | global_asm!("", options(), "");
355373
| ^^ expected one of `clobber_abi`, `const`, or `options`
356374

357375
error: expected one of `clobber_abi`, `const`, or `options`, found `"{}"`
358-
--> $DIR/parse-error.rs:124:30
376+
--> $DIR/parse-error.rs:130:30
359377
|
360378
LL | global_asm!("{}", const FOO, "{}", const FOO);
361379
| ^^^^ expected one of `clobber_abi`, `const`, or `options`
362380

363381
error: asm template must be a string literal
364-
--> $DIR/parse-error.rs:126:13
382+
--> $DIR/parse-error.rs:132:13
365383
|
366384
LL | global_asm!(format!("{{{}}}", 0), const FOO);
367385
| ^^^^^^^^^^^^^^^^^^^^
368386
|
369387
= note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)
370388

371389
error: asm template must be a string literal
372-
--> $DIR/parse-error.rs:128:20
390+
--> $DIR/parse-error.rs:134:20
373391
|
374392
LL | global_asm!("{1}", format!("{{{}}}", 0), const FOO, const BAR);
375393
| ^^^^^^^^^^^^^^^^^^^^
@@ -439,6 +457,6 @@ LL | let mut bar = 0;
439457
LL | asm!("{1}", in("eax") foo, const bar);
440458
| ^^^ non-constant value
441459

442-
error: aborting due to 63 previous errors
460+
error: aborting due to 66 previous errors
443461

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

0 commit comments

Comments
 (0)