Skip to content

Commit 8460ed6

Browse files
authored
Rollup merge of rust-lang#128207 - folkertdev:asm-parser-generalize, r=Amanieu
improve error message when `global_asm!` uses `asm!` options specifically, what was error: expected one of `)`, `att_syntax`, or `raw`, found `preserves_flags` --> $DIR/bad-options.rs:45:25 | LL | global_asm!("", options(preserves_flags)); | ^^^^^^^^^^^^^^^ expected one of `)`, `att_syntax`, or `raw` is now error: the `preserves_flags` option cannot be used with `global_asm!` --> $DIR/bad-options.rs:45:25 | LL | global_asm!("", options(preserves_flags)); | ^^^^^^^^^^^^^^^ the `preserves_flags` option is not meaningful for global-scoped inline assembly mirroring the phrasing of the [reference](https://doc.rust-lang.org/reference/inline-assembly.html#options). This is also a bit of a refactor for a future `naked_asm!` macro (for use in `#[naked]` functions). Currently this sort of error can come up when switching from inline to global asm, or when a user just isn't that experienced with assembly. With `naked_asm!` added to the mix hitting this error is more likely.
2 parents 23facba + 8859da0 commit 8460ed6

File tree

13 files changed

+172
-69
lines changed

13 files changed

+172
-69
lines changed

compiler/rustc_ast/src/ast.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2266,6 +2266,11 @@ bitflags::bitflags! {
22662266
}
22672267

22682268
impl InlineAsmOptions {
2269+
pub const COUNT: usize = Self::all().bits().count_ones() as usize;
2270+
2271+
pub const GLOBAL_OPTIONS: Self = Self::ATT_SYNTAX.union(Self::RAW);
2272+
pub const NAKED_OPTIONS: Self = Self::ATT_SYNTAX.union(Self::RAW).union(Self::NORETURN);
2273+
22692274
pub fn human_readable_names(&self) -> Vec<&'static str> {
22702275
let mut options = vec![];
22712276

compiler/rustc_builtin_macros/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ builtin_macros_format_use_positional = consider using a positional formatting ar
199199
200200
builtin_macros_global_asm_clobber_abi = `clobber_abi` cannot be used with `global_asm!`
201201
202+
builtin_macros_global_asm_unsupported_option = the `{$symbol}` option cannot be used with `global_asm!`
203+
.label = the `{$symbol}` option is not meaningful for global-scoped inline assembly
204+
.suggestion = remove this option
205+
202206
builtin_macros_invalid_crate_attribute = invalid crate attribute
203207
204208
builtin_macros_multiple_default_attrs = multiple `#[default]` attributes

compiler/rustc_builtin_macros/src/asm.rs

+43-22
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,16 @@ fn err_duplicate_option(p: &Parser<'_>, symbol: Symbol, span: Span) {
310310
p.dcx().emit_err(errors::AsmOptAlreadyprovided { span, symbol, full_span });
311311
}
312312

313+
/// Report an invalid option error.
314+
///
315+
/// This function must be called immediately after the option token is parsed.
316+
/// Otherwise, the suggestion will be incorrect.
317+
fn err_unsupported_option(p: &Parser<'_>, symbol: Symbol, span: Span) {
318+
// Tool-only output
319+
let full_span = if p.token.kind == token::Comma { span.to(p.token.span) } else { span };
320+
p.dcx().emit_err(errors::GlobalAsmUnsupportedOption { span, symbol, full_span });
321+
}
322+
313323
/// Try to set the provided option in the provided `AsmArgs`.
314324
/// If it is already set, report a duplicate option error.
315325
///
@@ -318,13 +328,16 @@ fn err_duplicate_option(p: &Parser<'_>, symbol: Symbol, span: Span) {
318328
fn try_set_option<'a>(
319329
p: &Parser<'a>,
320330
args: &mut AsmArgs,
331+
is_global_asm: bool,
321332
symbol: Symbol,
322333
option: ast::InlineAsmOptions,
323334
) {
324-
if !args.options.contains(option) {
325-
args.options |= option;
326-
} else {
335+
if is_global_asm && !ast::InlineAsmOptions::GLOBAL_OPTIONS.contains(option) {
336+
err_unsupported_option(p, symbol, p.prev_token.span);
337+
} else if args.options.contains(option) {
327338
err_duplicate_option(p, symbol, p.prev_token.span);
339+
} else {
340+
args.options |= option;
328341
}
329342
}
330343

@@ -338,25 +351,33 @@ fn parse_options<'a>(
338351
p.expect(&token::OpenDelim(Delimiter::Parenthesis))?;
339352

340353
while !p.eat(&token::CloseDelim(Delimiter::Parenthesis)) {
341-
if !is_global_asm && p.eat_keyword(sym::pure) {
342-
try_set_option(p, args, sym::pure, ast::InlineAsmOptions::PURE);
343-
} else if !is_global_asm && p.eat_keyword(sym::nomem) {
344-
try_set_option(p, args, sym::nomem, ast::InlineAsmOptions::NOMEM);
345-
} else if !is_global_asm && p.eat_keyword(sym::readonly) {
346-
try_set_option(p, args, sym::readonly, ast::InlineAsmOptions::READONLY);
347-
} else if !is_global_asm && p.eat_keyword(sym::preserves_flags) {
348-
try_set_option(p, args, sym::preserves_flags, ast::InlineAsmOptions::PRESERVES_FLAGS);
349-
} else if !is_global_asm && p.eat_keyword(sym::noreturn) {
350-
try_set_option(p, args, sym::noreturn, ast::InlineAsmOptions::NORETURN);
351-
} else if !is_global_asm && p.eat_keyword(sym::nostack) {
352-
try_set_option(p, args, sym::nostack, ast::InlineAsmOptions::NOSTACK);
353-
} else if !is_global_asm && p.eat_keyword(sym::may_unwind) {
354-
try_set_option(p, args, kw::Raw, ast::InlineAsmOptions::MAY_UNWIND);
355-
} else if p.eat_keyword(sym::att_syntax) {
356-
try_set_option(p, args, sym::att_syntax, ast::InlineAsmOptions::ATT_SYNTAX);
357-
} else if p.eat_keyword(kw::Raw) {
358-
try_set_option(p, args, kw::Raw, ast::InlineAsmOptions::RAW);
359-
} else {
354+
const OPTIONS: [(Symbol, ast::InlineAsmOptions); ast::InlineAsmOptions::COUNT] = [
355+
(sym::pure, ast::InlineAsmOptions::PURE),
356+
(sym::nomem, ast::InlineAsmOptions::NOMEM),
357+
(sym::readonly, ast::InlineAsmOptions::READONLY),
358+
(sym::preserves_flags, ast::InlineAsmOptions::PRESERVES_FLAGS),
359+
(sym::noreturn, ast::InlineAsmOptions::NORETURN),
360+
(sym::nostack, ast::InlineAsmOptions::NOSTACK),
361+
(sym::may_unwind, ast::InlineAsmOptions::MAY_UNWIND),
362+
(sym::att_syntax, ast::InlineAsmOptions::ATT_SYNTAX),
363+
(kw::Raw, ast::InlineAsmOptions::RAW),
364+
];
365+
366+
'blk: {
367+
for (symbol, option) in OPTIONS {
368+
let kw_matched =
369+
if !is_global_asm || ast::InlineAsmOptions::GLOBAL_OPTIONS.contains(option) {
370+
p.eat_keyword(symbol)
371+
} else {
372+
p.eat_keyword_noexpect(symbol)
373+
};
374+
375+
if kw_matched {
376+
try_set_option(p, args, is_global_asm, symbol, option);
377+
break 'blk;
378+
}
379+
}
380+
360381
return p.unexpected();
361382
}
362383

compiler/rustc_builtin_macros/src/errors.rs

+11
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,17 @@ pub(crate) struct AsmOptAlreadyprovided {
845845
pub(crate) full_span: Span,
846846
}
847847

848+
#[derive(Diagnostic)]
849+
#[diag(builtin_macros_global_asm_unsupported_option)]
850+
pub(crate) struct GlobalAsmUnsupportedOption {
851+
#[primary_span]
852+
#[label]
853+
pub(crate) span: Span,
854+
pub(crate) symbol: Symbol,
855+
#[suggestion(code = "", applicability = "machine-applicable", style = "tool-only")]
856+
pub(crate) full_span: Span,
857+
}
858+
848859
#[derive(Diagnostic)]
849860
#[diag(builtin_macros_test_runner_invalid)]
850861
pub(crate) struct TestRunnerInvalid {

compiler/rustc_parse/src/parser/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ impl<'a> Parser<'a> {
599599

600600
/// If the next token is the given keyword, eats it and returns `true`.
601601
/// Otherwise, returns `false`. An expectation is also added for diagnostics purposes.
602-
// Public for rustfmt usage.
602+
// Public for rustc_builtin_macros and rustfmt usage.
603603
#[inline]
604604
pub fn eat_keyword(&mut self, kw: Symbol) -> bool {
605605
if self.check_keyword(kw) {
@@ -631,8 +631,11 @@ impl<'a> Parser<'a> {
631631
false
632632
}
633633

634+
/// If the next token is the given keyword, eats it and returns `true`.
635+
/// Otherwise, returns `false`. No expectation is added.
636+
// Public for rustc_builtin_macros usage.
634637
#[inline]
635-
fn eat_keyword_noexpect(&mut self, kw: Symbol) -> bool {
638+
pub fn eat_keyword_noexpect(&mut self, kw: Symbol) -> bool {
636639
if self.token.is_keyword(kw) {
637640
self.bump();
638641
true

compiler/rustc_passes/src/naked_functions.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,7 @@ impl<'tcx> CheckInlineAssembly<'tcx> {
244244
self.tcx.dcx().emit_err(NakedFunctionsOperands { unsupported_operands });
245245
}
246246

247-
let supported_options =
248-
InlineAsmOptions::RAW | InlineAsmOptions::NORETURN | InlineAsmOptions::ATT_SYNTAX;
249-
let unsupported_options = asm.options.difference(supported_options);
250-
247+
let unsupported_options = asm.options.difference(InlineAsmOptions::NAKED_OPTIONS);
251248
if !unsupported_options.is_empty() {
252249
self.tcx.dcx().emit_err(NakedFunctionsAsmOptions {
253250
span,

tests/ui/asm/parse-error.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,15 @@ global_asm!("{}", const);
111111
global_asm!("{}", const(reg) FOO);
112112
//~^ ERROR expected one of
113113
global_asm!("", options(FOO));
114-
//~^ ERROR expected one of
114+
//~^ ERROR expected one of `)`, `att_syntax`, or `raw`, found `FOO`
115+
global_asm!("", options(FOO,));
116+
//~^ ERROR expected one of `)`, `att_syntax`, or `raw`, found `FOO`
115117
global_asm!("", options(nomem FOO));
116-
//~^ ERROR expected one of
118+
//~^ ERROR the `nomem` option cannot be used with `global_asm!`
119+
//~| ERROR expected one of `)` or `,`, found `FOO`
117120
global_asm!("", options(nomem, FOO));
118-
//~^ ERROR expected one of
121+
//~^ ERROR the `nomem` option cannot be used with `global_asm!`
122+
//~| ERROR expected one of `)`, `att_syntax`, or `raw`, found `FOO`
119123
global_asm!("{}", options(), const FOO);
120124
global_asm!("", clobber_abi(FOO));
121125
//~^ ERROR expected string literal

tests/ui/asm/parse-error.stderr

+38-20
Original file line numberDiff line numberDiff line change
@@ -264,106 +264,124 @@ error: expected one of `)`, `att_syntax`, or `raw`, found `FOO`
264264
LL | global_asm!("", options(FOO));
265265
| ^^^ expected one of `)`, `att_syntax`, or `raw`
266266

267-
error: expected one of `)`, `att_syntax`, or `raw`, found `nomem`
267+
error: expected one of `)`, `att_syntax`, or `raw`, found `FOO`
268268
--> $DIR/parse-error.rs:115:25
269269
|
270-
LL | global_asm!("", options(nomem FOO));
271-
| ^^^^^ expected one of `)`, `att_syntax`, or `raw`
270+
LL | global_asm!("", options(FOO,));
271+
| ^^^ expected one of `)`, `att_syntax`, or `raw`
272272

273-
error: expected one of `)`, `att_syntax`, or `raw`, found `nomem`
273+
error: the `nomem` option cannot be used with `global_asm!`
274274
--> $DIR/parse-error.rs:117:25
275275
|
276+
LL | global_asm!("", options(nomem FOO));
277+
| ^^^^^ the `nomem` option is not meaningful for global-scoped inline assembly
278+
279+
error: expected one of `)` or `,`, found `FOO`
280+
--> $DIR/parse-error.rs:117:31
281+
|
282+
LL | global_asm!("", options(nomem FOO));
283+
| ^^^ expected one of `)` or `,`
284+
285+
error: the `nomem` option cannot be used with `global_asm!`
286+
--> $DIR/parse-error.rs:120:25
287+
|
288+
LL | global_asm!("", options(nomem, FOO));
289+
| ^^^^^ the `nomem` option is not meaningful for global-scoped inline assembly
290+
291+
error: expected one of `)`, `att_syntax`, or `raw`, found `FOO`
292+
--> $DIR/parse-error.rs:120:32
293+
|
276294
LL | global_asm!("", options(nomem, FOO));
277-
| ^^^^^ expected one of `)`, `att_syntax`, or `raw`
295+
| ^^^ expected one of `)`, `att_syntax`, or `raw`
278296

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

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

291309
error: expected string literal
292-
--> $DIR/parse-error.rs:124:34
310+
--> $DIR/parse-error.rs:128:34
293311
|
294312
LL | global_asm!("", clobber_abi("C", FOO));
295313
| ^^^ not a string literal
296314

297315
error: `clobber_abi` cannot be used with `global_asm!`
298-
--> $DIR/parse-error.rs:126:19
316+
--> $DIR/parse-error.rs:130:19
299317
|
300318
LL | global_asm!("{}", clobber_abi("C"), const FOO);
301319
| ^^^^^^^^^^^^^^^^
302320

303321
error: `clobber_abi` cannot be used with `global_asm!`
304-
--> $DIR/parse-error.rs:128:28
322+
--> $DIR/parse-error.rs:132:28
305323
|
306324
LL | global_asm!("", options(), clobber_abi("C"));
307325
| ^^^^^^^^^^^^^^^^
308326

309327
error: `clobber_abi` cannot be used with `global_asm!`
310-
--> $DIR/parse-error.rs:130:30
328+
--> $DIR/parse-error.rs:134:30
311329
|
312330
LL | global_asm!("{}", options(), clobber_abi("C"), const FOO);
313331
| ^^^^^^^^^^^^^^^^
314332

315333
error: `clobber_abi` cannot be used with `global_asm!`
316-
--> $DIR/parse-error.rs:132:17
334+
--> $DIR/parse-error.rs:136:17
317335
|
318336
LL | global_asm!("", clobber_abi("C"), clobber_abi("C"));
319337
| ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^
320338

321339
error: duplicate argument named `a`
322-
--> $DIR/parse-error.rs:134:35
340+
--> $DIR/parse-error.rs:138:35
323341
|
324342
LL | global_asm!("{a}", a = const FOO, a = const BAR);
325343
| ------------- ^^^^^^^^^^^^^ duplicate argument
326344
| |
327345
| previously here
328346

329347
error: argument never used
330-
--> $DIR/parse-error.rs:134:35
348+
--> $DIR/parse-error.rs:138:35
331349
|
332350
LL | global_asm!("{a}", a = const FOO, a = const BAR);
333351
| ^^^^^^^^^^^^^ argument never used
334352
|
335353
= help: if this argument is intentionally unused, consider using it in an asm comment: `"/* {1} */"`
336354

337355
error: expected one of `clobber_abi`, `const`, `options`, or `sym`, found `""`
338-
--> $DIR/parse-error.rs:137:28
356+
--> $DIR/parse-error.rs:141:28
339357
|
340358
LL | global_asm!("", options(), "");
341359
| ^^ expected one of `clobber_abi`, `const`, `options`, or `sym`
342360

343361
error: expected one of `clobber_abi`, `const`, `options`, or `sym`, found `"{}"`
344-
--> $DIR/parse-error.rs:139:30
362+
--> $DIR/parse-error.rs:143:30
345363
|
346364
LL | global_asm!("{}", const FOO, "{}", const FOO);
347365
| ^^^^ expected one of `clobber_abi`, `const`, `options`, or `sym`
348366

349367
error: asm template must be a string literal
350-
--> $DIR/parse-error.rs:141:13
368+
--> $DIR/parse-error.rs:145:13
351369
|
352370
LL | global_asm!(format!("{{{}}}", 0), const FOO);
353371
| ^^^^^^^^^^^^^^^^^^^^
354372
|
355373
= note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)
356374

357375
error: asm template must be a string literal
358-
--> $DIR/parse-error.rs:143:20
376+
--> $DIR/parse-error.rs:147:20
359377
|
360378
LL | global_asm!("{1}", format!("{{{}}}", 0), const FOO, const BAR);
361379
| ^^^^^^^^^^^^^^^^^^^^
362380
|
363381
= note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)
364382

365383
error: expected operand, options, or additional template string
366-
--> $DIR/parse-error.rs:145:19
384+
--> $DIR/parse-error.rs:149:19
367385
|
368386
LL | global_asm!("{}", label {});
369387
| ^^^^^^^^ expected operand, options, or additional template string
@@ -423,6 +441,6 @@ help: consider using `const` instead of `let`
423441
LL | const bar: /* Type */ = 0;
424442
| ~~~~~ ++++++++++++
425443

426-
error: aborting due to 64 previous errors
444+
error: aborting due to 67 previous errors
427445

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

tests/ui/asm/unsupported-option.fixed

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ run-rustfix
2+
3+
use std::arch::global_asm;
4+
5+
fn main() {}
6+
7+
global_asm!("", options(att_syntax, raw));
8+
//~^ ERROR the `nomem` option cannot be used with `global_asm!`
9+
//~| ERROR the `readonly` option cannot be used with `global_asm!`
10+
//~| ERROR the `noreturn` option cannot be used with `global_asm!`

tests/ui/asm/unsupported-option.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ run-rustfix
2+
3+
use std::arch::global_asm;
4+
5+
fn main() {}
6+
7+
global_asm!("", options(att_syntax, nomem, readonly, noreturn, raw));
8+
//~^ ERROR the `nomem` option cannot be used with `global_asm!`
9+
//~| ERROR the `readonly` option cannot be used with `global_asm!`
10+
//~| ERROR the `noreturn` option cannot be used with `global_asm!`
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: the `nomem` option cannot be used with `global_asm!`
2+
--> $DIR/unsupported-option.rs:7:37
3+
|
4+
LL | global_asm!("", options(att_syntax, nomem, readonly, noreturn, raw));
5+
| ^^^^^ the `nomem` option is not meaningful for global-scoped inline assembly
6+
7+
error: the `readonly` option cannot be used with `global_asm!`
8+
--> $DIR/unsupported-option.rs:7:44
9+
|
10+
LL | global_asm!("", options(att_syntax, nomem, readonly, noreturn, raw));
11+
| ^^^^^^^^ the `readonly` option is not meaningful for global-scoped inline assembly
12+
13+
error: the `noreturn` option cannot be used with `global_asm!`
14+
--> $DIR/unsupported-option.rs:7:54
15+
|
16+
LL | global_asm!("", options(att_syntax, nomem, readonly, noreturn, raw));
17+
| ^^^^^^^^ the `noreturn` option is not meaningful for global-scoped inline assembly
18+
19+
error: aborting due to 3 previous errors
20+

tests/ui/asm/x86_64/bad-options.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ fn main() {
3333
}
3434

3535
global_asm!("", options(nomem));
36-
//~^ ERROR expected one of
36+
//~^ ERROR the `nomem` option cannot be used with `global_asm!`
3737
global_asm!("", options(readonly));
38-
//~^ ERROR expected one of
38+
//~^ ERROR the `readonly` option cannot be used with `global_asm!`
3939
global_asm!("", options(noreturn));
40-
//~^ ERROR expected one of
40+
//~^ ERROR the `noreturn` option cannot be used with `global_asm!`
4141
global_asm!("", options(pure));
42-
//~^ ERROR expected one of
42+
//~^ ERROR the `pure` option cannot be used with `global_asm!`
4343
global_asm!("", options(nostack));
44-
//~^ ERROR expected one of
44+
//~^ ERROR the `nostack` option cannot be used with `global_asm!`
4545
global_asm!("", options(preserves_flags));
46-
//~^ ERROR expected one of
46+
//~^ ERROR the `preserves_flags` option cannot be used with `global_asm!`

0 commit comments

Comments
 (0)