Rollup merge of #88209 - Amanieu:asm_in_underscore, r=nagisa

Improve error message when _ is used for in/inout asm operands

As suggested by ```@Commeownist``` in https://github.com/rust-lang/rust/issues/72016#issuecomment-903102415.
This commit is contained in:
Jubilee 2021-09-11 08:23:40 -07:00 committed by GitHub
commit 3af42a897f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 25 deletions

View file

@ -117,6 +117,10 @@ fn parse_args<'a>(
let mut explicit_reg = false; let mut explicit_reg = false;
let op = if !is_global_asm && p.eat_keyword(kw::In) { let op = if !is_global_asm && p.eat_keyword(kw::In) {
let reg = parse_reg(&mut p, &mut explicit_reg)?; let reg = parse_reg(&mut p, &mut explicit_reg)?;
if p.eat_keyword(kw::Underscore) {
let err = ecx.struct_span_err(p.token.span, "_ cannot be used for input operands");
return Err(err);
}
let expr = p.parse_expr()?; let expr = p.parse_expr()?;
ast::InlineAsmOperand::In { reg, expr } ast::InlineAsmOperand::In { reg, expr }
} else if !is_global_asm && p.eat_keyword(sym::out) { } else if !is_global_asm && p.eat_keyword(sym::out) {
@ -129,6 +133,10 @@ fn parse_args<'a>(
ast::InlineAsmOperand::Out { reg, expr, late: true } ast::InlineAsmOperand::Out { reg, expr, late: true }
} else if !is_global_asm && p.eat_keyword(sym::inout) { } else if !is_global_asm && p.eat_keyword(sym::inout) {
let reg = parse_reg(&mut p, &mut explicit_reg)?; let reg = parse_reg(&mut p, &mut explicit_reg)?;
if p.eat_keyword(kw::Underscore) {
let err = ecx.struct_span_err(p.token.span, "_ cannot be used for input operands");
return Err(err);
}
let expr = p.parse_expr()?; let expr = p.parse_expr()?;
if p.eat(&token::FatArrow) { if p.eat(&token::FatArrow) {
let out_expr = let out_expr =
@ -139,6 +147,10 @@ fn parse_args<'a>(
} }
} else if !is_global_asm && p.eat_keyword(sym::inlateout) { } else if !is_global_asm && p.eat_keyword(sym::inlateout) {
let reg = parse_reg(&mut p, &mut explicit_reg)?; let reg = parse_reg(&mut p, &mut explicit_reg)?;
if p.eat_keyword(kw::Underscore) {
let err = ecx.struct_span_err(p.token.span, "_ cannot be used for input operands");
return Err(err);
}
let expr = p.parse_expr()?; let expr = p.parse_expr()?;
if p.eat(&token::FatArrow) { if p.eat(&token::FatArrow) {
let out_expr = let out_expr =

View file

@ -76,6 +76,12 @@ fn main() {
//~^ ERROR asm template must be a string literal //~^ ERROR asm template must be a string literal
asm!("{1}", format!("{{{}}}", 0), in(reg) foo, out(reg) bar); asm!("{1}", format!("{{{}}}", 0), in(reg) foo, out(reg) bar);
//~^ ERROR asm template must be a string literal //~^ ERROR asm template must be a string literal
asm!("{}", in(reg) _);
//~^ ERROR _ cannot be used for input operands
asm!("{}", inout(reg) _);
//~^ ERROR _ cannot be used for input operands
asm!("{}", inlateout(reg) _);
//~^ ERROR _ cannot be used for input operands
} }
} }

View file

@ -214,62 +214,80 @@ LL | asm!("{1}", format!("{{{}}}", 0), in(reg) foo, out(reg) bar);
| |
= note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)
error: _ cannot be used for input operands
--> $DIR/parse-error.rs:79:28
|
LL | asm!("{}", in(reg) _);
| ^
error: _ cannot be used for input operands
--> $DIR/parse-error.rs:81:31
|
LL | asm!("{}", inout(reg) _);
| ^
error: _ cannot be used for input operands
--> $DIR/parse-error.rs:83:35
|
LL | asm!("{}", inlateout(reg) _);
| ^
error: requires at least a template string argument error: requires at least a template string argument
--> $DIR/parse-error.rs:84:1 --> $DIR/parse-error.rs:90:1
| |
LL | global_asm!(); LL | global_asm!();
| ^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^
error: asm template must be a string literal error: asm template must be a string literal
--> $DIR/parse-error.rs:86:13 --> $DIR/parse-error.rs:92:13
| |
LL | global_asm!(FOO); LL | global_asm!(FOO);
| ^^^ | ^^^
error: expected token: `,` error: expected token: `,`
--> $DIR/parse-error.rs:88:18 --> $DIR/parse-error.rs:94:18
| |
LL | global_asm!("{}" FOO); LL | global_asm!("{}" FOO);
| ^^^ expected `,` | ^^^ expected `,`
error: expected operand, options, or additional template string error: expected operand, options, or additional template string
--> $DIR/parse-error.rs:90:19 --> $DIR/parse-error.rs:96:19
| |
LL | global_asm!("{}", FOO); LL | global_asm!("{}", FOO);
| ^^^ expected operand, options, or additional template string | ^^^ expected operand, options, or additional template string
error: expected expression, found end of macro arguments error: expected expression, found end of macro arguments
--> $DIR/parse-error.rs:92:24 --> $DIR/parse-error.rs:98:24
| |
LL | global_asm!("{}", const); LL | global_asm!("{}", const);
| ^ expected expression | ^ expected expression
error: expected one of `,`, `.`, `?`, or an operator, found `FOO` error: expected one of `,`, `.`, `?`, or an operator, found `FOO`
--> $DIR/parse-error.rs:94:30 --> $DIR/parse-error.rs:100:30
| |
LL | global_asm!("{}", const(reg) FOO); LL | global_asm!("{}", const(reg) FOO);
| ^^^ expected one of `,`, `.`, `?`, or an operator | ^^^ expected one of `,`, `.`, `?`, or an operator
error: expected one of `)`, `att_syntax`, or `raw`, found `FOO` error: expected one of `)`, `att_syntax`, or `raw`, found `FOO`
--> $DIR/parse-error.rs:96:25 --> $DIR/parse-error.rs:102:25
| |
LL | global_asm!("", options(FOO)); LL | global_asm!("", options(FOO));
| ^^^ expected one of `)`, `att_syntax`, or `raw` | ^^^ expected one of `)`, `att_syntax`, or `raw`
error: expected one of `)`, `att_syntax`, or `raw`, found `nomem` error: expected one of `)`, `att_syntax`, or `raw`, found `nomem`
--> $DIR/parse-error.rs:98:25 --> $DIR/parse-error.rs:104:25
| |
LL | global_asm!("", options(nomem FOO)); LL | global_asm!("", options(nomem FOO));
| ^^^^^ expected one of `)`, `att_syntax`, or `raw` | ^^^^^ expected one of `)`, `att_syntax`, or `raw`
error: expected one of `)`, `att_syntax`, or `raw`, found `nomem` error: expected one of `)`, `att_syntax`, or `raw`, found `nomem`
--> $DIR/parse-error.rs:100:25 --> $DIR/parse-error.rs:106:25
| |
LL | global_asm!("", options(nomem, FOO)); LL | global_asm!("", options(nomem, FOO));
| ^^^^^ expected one of `)`, `att_syntax`, or `raw` | ^^^^^ expected one of `)`, `att_syntax`, or `raw`
error: arguments are not allowed after options error: arguments are not allowed after options
--> $DIR/parse-error.rs:102:30 --> $DIR/parse-error.rs:108:30
| |
LL | global_asm!("{}", options(), const FOO); LL | global_asm!("{}", options(), const FOO);
| --------- ^^^^^^^^^ argument | --------- ^^^^^^^^^ argument
@ -277,25 +295,25 @@ LL | global_asm!("{}", options(), const FOO);
| previous options | previous options
error: expected string literal error: expected string literal
--> $DIR/parse-error.rs:104:29 --> $DIR/parse-error.rs:110:29
| |
LL | global_asm!("", clobber_abi(FOO)); LL | global_asm!("", clobber_abi(FOO));
| ^^^ not a string literal | ^^^ not a string literal
error: expected `)`, found `FOO` error: expected `)`, found `FOO`
--> $DIR/parse-error.rs:106:33 --> $DIR/parse-error.rs:112:33
| |
LL | global_asm!("", clobber_abi("C" FOO)); LL | global_asm!("", clobber_abi("C" FOO));
| ^^^ expected `)` | ^^^ expected `)`
error: expected `)`, found `,` error: expected `)`, found `,`
--> $DIR/parse-error.rs:108:32 --> $DIR/parse-error.rs:114:32
| |
LL | global_asm!("", clobber_abi("C", FOO)); LL | global_asm!("", clobber_abi("C", FOO));
| ^ expected `)` | ^ expected `)`
error: arguments are not allowed after clobber_abi error: arguments are not allowed after clobber_abi
--> $DIR/parse-error.rs:110:37 --> $DIR/parse-error.rs:116:37
| |
LL | global_asm!("{}", clobber_abi("C"), const FOO); LL | global_asm!("{}", clobber_abi("C"), const FOO);
| ---------------- ^^^^^^^^^ argument | ---------------- ^^^^^^^^^ argument
@ -303,13 +321,13 @@ LL | global_asm!("{}", clobber_abi("C"), const FOO);
| clobber_abi | clobber_abi
error: `clobber_abi` cannot be used with `global_asm!` error: `clobber_abi` cannot be used with `global_asm!`
--> $DIR/parse-error.rs:110:19 --> $DIR/parse-error.rs:116:19
| |
LL | global_asm!("{}", clobber_abi("C"), const FOO); LL | global_asm!("{}", clobber_abi("C"), const FOO);
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
error: clobber_abi is not allowed after options error: clobber_abi is not allowed after options
--> $DIR/parse-error.rs:113:28 --> $DIR/parse-error.rs:119:28
| |
LL | global_asm!("", options(), clobber_abi("C")); LL | global_asm!("", options(), clobber_abi("C"));
| --------- ^^^^^^^^^^^^^^^^ | --------- ^^^^^^^^^^^^^^^^
@ -317,7 +335,7 @@ LL | global_asm!("", options(), clobber_abi("C"));
| options | options
error: clobber_abi is not allowed after options error: clobber_abi is not allowed after options
--> $DIR/parse-error.rs:115:30 --> $DIR/parse-error.rs:121:30
| |
LL | global_asm!("{}", options(), clobber_abi("C"), const FOO); LL | global_asm!("{}", options(), clobber_abi("C"), const FOO);
| --------- ^^^^^^^^^^^^^^^^ | --------- ^^^^^^^^^^^^^^^^
@ -325,7 +343,7 @@ LL | global_asm!("{}", options(), clobber_abi("C"), const FOO);
| options | options
error: clobber_abi specified multiple times error: clobber_abi specified multiple times
--> $DIR/parse-error.rs:117:35 --> $DIR/parse-error.rs:123:35
| |
LL | global_asm!("", clobber_abi("C"), clobber_abi("C")); LL | global_asm!("", clobber_abi("C"), clobber_abi("C"));
| ---------------- ^^^^^^^^^^^^^^^^ | ---------------- ^^^^^^^^^^^^^^^^
@ -333,7 +351,7 @@ LL | global_asm!("", clobber_abi("C"), clobber_abi("C"));
| clobber_abi previously specified here | clobber_abi previously specified here
error: duplicate argument named `a` error: duplicate argument named `a`
--> $DIR/parse-error.rs:119:35 --> $DIR/parse-error.rs:125:35
| |
LL | global_asm!("{a}", a = const FOO, a = const BAR); LL | global_asm!("{a}", a = const FOO, a = const BAR);
| ------------- ^^^^^^^^^^^^^ duplicate argument | ------------- ^^^^^^^^^^^^^ duplicate argument
@ -341,7 +359,7 @@ LL | global_asm!("{a}", a = const FOO, a = const BAR);
| previously here | previously here
error: argument never used error: argument never used
--> $DIR/parse-error.rs:119:35 --> $DIR/parse-error.rs:125:35
| |
LL | global_asm!("{a}", a = const FOO, a = const BAR); LL | global_asm!("{a}", a = const FOO, a = const BAR);
| ^^^^^^^^^^^^^ argument never used | ^^^^^^^^^^^^^ argument never used
@ -349,19 +367,19 @@ LL | global_asm!("{a}", a = const FOO, a = const BAR);
= help: if this argument is intentionally unused, consider using it in an asm comment: `"/* {1} */"` = help: if this argument is intentionally unused, consider using it in an asm comment: `"/* {1} */"`
error: expected one of `clobber_abi`, `const`, or `options`, found `""` error: expected one of `clobber_abi`, `const`, or `options`, found `""`
--> $DIR/parse-error.rs:122:28 --> $DIR/parse-error.rs:128:28
| |
LL | global_asm!("", options(), ""); LL | global_asm!("", options(), "");
| ^^ expected one of `clobber_abi`, `const`, or `options` | ^^ expected one of `clobber_abi`, `const`, or `options`
error: expected one of `clobber_abi`, `const`, or `options`, found `"{}"` error: expected one of `clobber_abi`, `const`, or `options`, found `"{}"`
--> $DIR/parse-error.rs:124:30 --> $DIR/parse-error.rs:130:30
| |
LL | global_asm!("{}", const FOO, "{}", const FOO); LL | global_asm!("{}", const FOO, "{}", const FOO);
| ^^^^ expected one of `clobber_abi`, `const`, or `options` | ^^^^ expected one of `clobber_abi`, `const`, or `options`
error: asm template must be a string literal error: asm template must be a string literal
--> $DIR/parse-error.rs:126:13 --> $DIR/parse-error.rs:132:13
| |
LL | global_asm!(format!("{{{}}}", 0), const FOO); LL | global_asm!(format!("{{{}}}", 0), const FOO);
| ^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^
@ -369,7 +387,7 @@ LL | global_asm!(format!("{{{}}}", 0), const FOO);
= note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)
error: asm template must be a string literal error: asm template must be a string literal
--> $DIR/parse-error.rs:128:20 --> $DIR/parse-error.rs:134:20
| |
LL | global_asm!("{1}", format!("{{{}}}", 0), const FOO, const BAR); LL | global_asm!("{1}", format!("{{{}}}", 0), const FOO, const BAR);
| ^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^
@ -439,6 +457,6 @@ LL | let mut bar = 0;
LL | asm!("{1}", in("eax") foo, const bar); LL | asm!("{1}", in("eax") foo, const bar);
| ^^^ non-constant value | ^^^ non-constant value
error: aborting due to 63 previous errors error: aborting due to 66 previous errors
For more information about this error, try `rustc --explain E0435`. For more information about this error, try `rustc --explain E0435`.