Skip to content

Commit a14bf48

Browse files
committed
Auto merge of #77595 - petrochenkov:asmident, r=oli-obk
builtin_macros: Fix use of interpolated identifiers in `asm!` Fixes #77584
2 parents 8ae3b50 + 219c66c commit a14bf48

File tree

5 files changed

+95
-20
lines changed

5 files changed

+95
-20
lines changed

compiler/rustc_builtin_macros/src/asm.rs

+18-17
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ fn parse_args<'a>(
8181
} // accept trailing commas
8282

8383
// Parse options
84-
if p.eat(&token::Ident(sym::options, false)) {
84+
if p.eat_keyword(sym::options) {
8585
parse_options(&mut p, &mut args)?;
8686
allow_templates = false;
8787
continue;
@@ -101,19 +101,19 @@ fn parse_args<'a>(
101101
};
102102

103103
let mut explicit_reg = false;
104-
let op = if p.eat(&token::Ident(kw::In, false)) {
104+
let op = if p.eat_keyword(kw::In) {
105105
let reg = parse_reg(&mut p, &mut explicit_reg)?;
106106
let expr = p.parse_expr()?;
107107
ast::InlineAsmOperand::In { reg, expr }
108-
} else if p.eat(&token::Ident(sym::out, false)) {
108+
} else if p.eat_keyword(sym::out) {
109109
let reg = parse_reg(&mut p, &mut explicit_reg)?;
110110
let expr = if p.eat_keyword(kw::Underscore) { None } else { Some(p.parse_expr()?) };
111111
ast::InlineAsmOperand::Out { reg, expr, late: false }
112-
} else if p.eat(&token::Ident(sym::lateout, false)) {
112+
} else if p.eat_keyword(sym::lateout) {
113113
let reg = parse_reg(&mut p, &mut explicit_reg)?;
114114
let expr = if p.eat_keyword(kw::Underscore) { None } else { Some(p.parse_expr()?) };
115115
ast::InlineAsmOperand::Out { reg, expr, late: true }
116-
} else if p.eat(&token::Ident(sym::inout, false)) {
116+
} else if p.eat_keyword(sym::inout) {
117117
let reg = parse_reg(&mut p, &mut explicit_reg)?;
118118
let expr = p.parse_expr()?;
119119
if p.eat(&token::FatArrow) {
@@ -123,7 +123,7 @@ fn parse_args<'a>(
123123
} else {
124124
ast::InlineAsmOperand::InOut { reg, expr, late: false }
125125
}
126-
} else if p.eat(&token::Ident(sym::inlateout, false)) {
126+
} else if p.eat_keyword(sym::inlateout) {
127127
let reg = parse_reg(&mut p, &mut explicit_reg)?;
128128
let expr = p.parse_expr()?;
129129
if p.eat(&token::FatArrow) {
@@ -133,10 +133,10 @@ fn parse_args<'a>(
133133
} else {
134134
ast::InlineAsmOperand::InOut { reg, expr, late: true }
135135
}
136-
} else if p.eat(&token::Ident(kw::Const, false)) {
136+
} else if p.eat_keyword(kw::Const) {
137137
let expr = p.parse_expr()?;
138138
ast::InlineAsmOperand::Const { expr }
139-
} else if p.eat(&token::Ident(sym::sym, false)) {
139+
} else if p.eat_keyword(sym::sym) {
140140
let expr = p.parse_expr()?;
141141
match expr.kind {
142142
ast::ExprKind::Path(..) => {}
@@ -164,7 +164,7 @@ fn parse_args<'a>(
164164
args.templates.push(template);
165165
continue;
166166
} else {
167-
return Err(p.expect_one_of(&[], &[]).unwrap_err());
167+
return p.unexpected();
168168
};
169169

170170
allow_templates = false;
@@ -333,21 +333,22 @@ fn parse_options<'a>(p: &mut Parser<'a>, args: &mut AsmArgs) -> Result<(), Diagn
333333
p.expect(&token::OpenDelim(token::DelimToken::Paren))?;
334334

335335
while !p.eat(&token::CloseDelim(token::DelimToken::Paren)) {
336-
if p.eat(&token::Ident(sym::pure, false)) {
336+
if p.eat_keyword(sym::pure) {
337337
try_set_option(p, args, sym::pure, ast::InlineAsmOptions::PURE);
338-
} else if p.eat(&token::Ident(sym::nomem, false)) {
338+
} else if p.eat_keyword(sym::nomem) {
339339
try_set_option(p, args, sym::nomem, ast::InlineAsmOptions::NOMEM);
340-
} else if p.eat(&token::Ident(sym::readonly, false)) {
340+
} else if p.eat_keyword(sym::readonly) {
341341
try_set_option(p, args, sym::readonly, ast::InlineAsmOptions::READONLY);
342-
} else if p.eat(&token::Ident(sym::preserves_flags, false)) {
342+
} else if p.eat_keyword(sym::preserves_flags) {
343343
try_set_option(p, args, sym::preserves_flags, ast::InlineAsmOptions::PRESERVES_FLAGS);
344-
} else if p.eat(&token::Ident(sym::noreturn, false)) {
344+
} else if p.eat_keyword(sym::noreturn) {
345345
try_set_option(p, args, sym::noreturn, ast::InlineAsmOptions::NORETURN);
346-
} else if p.eat(&token::Ident(sym::nostack, false)) {
346+
} else if p.eat_keyword(sym::nostack) {
347347
try_set_option(p, args, sym::nostack, ast::InlineAsmOptions::NOSTACK);
348-
} else {
349-
p.expect(&token::Ident(sym::att_syntax, false))?;
348+
} else if p.eat_keyword(sym::att_syntax) {
350349
try_set_option(p, args, sym::att_syntax, ast::InlineAsmOptions::ATT_SYNTAX);
350+
} else {
351+
return p.unexpected();
351352
}
352353

353354
// Allow trailing commas

compiler/rustc_builtin_macros/src/assert.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ fn parse_assert<'a>(
120120
};
121121

122122
if parser.token != token::Eof {
123-
parser.expect_one_of(&[], &[])?;
124-
unreachable!();
123+
return parser.unexpected();
125124
}
126125

127126
Ok(Assert { cond_expr, custom_message })

compiler/rustc_parse/src/parser/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ impl<'a> Parser<'a> {
386386
next
387387
}
388388

389-
crate fn unexpected<T>(&mut self) -> PResult<'a, T> {
389+
pub fn unexpected<T>(&mut self) -> PResult<'a, T> {
390390
match self.expect_one_of(&[], &[]) {
391391
Err(e) => Err(e),
392392
// We can get `Ok(true)` from `recover_closing_delimiter`
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// only-x86_64
2+
3+
#![feature(asm)]
4+
5+
macro_rules! m {
6+
($in:ident $out:ident $lateout:ident $inout:ident $inlateout:ident $const:ident $sym:ident
7+
$pure:ident $nomem:ident $readonly:ident $preserves_flags:ident
8+
$noreturn:ident $nostack:ident $att_syntax:ident $options:ident) => {
9+
unsafe {
10+
asm!("", $in(x) x, $out(x) x, $lateout(x) x, $inout(x) x, $inlateout(x) x,
11+
//~^ ERROR asm outputs are not allowed with the `noreturn` option
12+
const x, sym x,
13+
$options($pure, $nomem, $readonly, $preserves_flags, $noreturn, $nostack, $att_syntax));
14+
//~^ ERROR the `nomem` and `readonly` options are mutually exclusive
15+
//~| ERROR the `pure` and `noreturn` options are mutually exclusive
16+
}
17+
};
18+
}
19+
20+
fn main() {
21+
m!(in out lateout inout inlateout const sym
22+
pure nomem readonly preserves_flags
23+
noreturn nostack att_syntax options);
24+
}
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
error: the `nomem` and `readonly` options are mutually exclusive
2+
--> $DIR/interpolated-idents.rs:13:13
3+
|
4+
LL | $options($pure, $nomem, $readonly, $preserves_flags, $noreturn, $nostack, $att_syntax));
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
...
7+
LL | / m!(in out lateout inout inlateout const sym
8+
LL | | pure nomem readonly preserves_flags
9+
LL | | noreturn nostack att_syntax options);
10+
| |____________________________________________- in this macro invocation
11+
|
12+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
13+
14+
error: the `pure` and `noreturn` options are mutually exclusive
15+
--> $DIR/interpolated-idents.rs:13:13
16+
|
17+
LL | $options($pure, $nomem, $readonly, $preserves_flags, $noreturn, $nostack, $att_syntax));
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19+
...
20+
LL | / m!(in out lateout inout inlateout const sym
21+
LL | | pure nomem readonly preserves_flags
22+
LL | | noreturn nostack att_syntax options);
23+
| |____________________________________________- in this macro invocation
24+
|
25+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
26+
27+
error: asm outputs are not allowed with the `noreturn` option
28+
--> $DIR/interpolated-idents.rs:10:32
29+
|
30+
LL | asm!("", $in(x) x, $out(x) x, $lateout(x) x, $inout(x) x, $inlateout(x) x,
31+
| ^^^^^^^^^ ^^^^^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^^^^^
32+
...
33+
LL | m!(in out lateout inout inlateout const sym
34+
| _____-
35+
| |_____|
36+
| |_____|
37+
| |_____|
38+
| |
39+
LL | | pure nomem readonly preserves_flags
40+
LL | | noreturn nostack att_syntax options);
41+
| | -
42+
| |____________________________________________|
43+
| |____________________________________________in this macro invocation
44+
| |____________________________________________in this macro invocation
45+
| |____________________________________________in this macro invocation
46+
| in this macro invocation
47+
|
48+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
49+
50+
error: aborting due to 3 previous errors
51+

0 commit comments

Comments
 (0)