Skip to content

Commit 5bf2f50

Browse files
committed
Allow unused arguments in asm!
1 parent a2fc33e commit 5bf2f50

File tree

7 files changed

+58
-60
lines changed

7 files changed

+58
-60
lines changed

Diff for: src/doc/unstable-book/src/library-features/asm.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ fn mul(a: u64, b: u64) -> u128 {
201201
);
202202
}
203203
204-
(hi as u128) << 64 + lo as u128
204+
((hi as u128) << 64) + lo as u128
205205
}
206206
```
207207

@@ -382,7 +382,9 @@ The macro will initially be supported only on ARM, AArch64, x86, x86-64 and RISC
382382

383383
The assembler template uses the same syntax as [format strings][format-syntax] (i.e. placeholders are specified by curly braces). The corresponding arguments are accessed in order, by index, or by name. However, implicit named arguments (introduced by [RFC #2795][rfc-2795]) are not supported.
384384

385-
As with format strings, named arguments must appear after positional arguments. Explicit register operands must appear at the end of the operand list, after any named arguments if any. Explicit register operands cannot be used by placeholders in the template string. All other operands must appear at least once in the template string, otherwise a compiler error is generated.
385+
As with format strings, named arguments must appear after positional arguments. Explicit register operands must appear at the end of the operand list, after named arguments if any.
386+
387+
Explicit register operands cannot be used by placeholders in the template string. All other named and positional operands must appear at least once in the template string, otherwise a compiler error is generated.
386388

387389
The exact assembly code syntax is target-specific and opaque to the compiler except for the way operands are substituted into the template string to form the code passed to the assembler.
388390

Diff for: src/librustc_builtin_macros/asm.rs

+6-28
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ use rustc_ast::ptr::P;
33
use rustc_ast::token;
44
use rustc_ast::tokenstream::TokenStream;
55
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
6-
use rustc_errors::{Applicability, DiagnosticBuilder};
6+
use rustc_errors::{pluralize, Applicability, DiagnosticBuilder};
77
use rustc_expand::base::{self, *};
88
use rustc_parse::parser::Parser;
99
use rustc_parse_format as parse;
10+
use rustc_session::lint::builtin::UNUSED_ASM_ARGUMENTS;
1011
use rustc_span::symbol::{kw, sym, Symbol};
1112
use rustc_span::{InnerSpan, Span};
1213

@@ -485,34 +486,11 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, sp: Span, args: AsmArgs) -> P<ast
485486
.into_iter()
486487
.enumerate()
487488
.filter(|&(_, used)| !used)
488-
.map(|(idx, _)| {
489-
if named_pos.contains(&idx) {
490-
// named argument
491-
(operands[idx].1, "named argument never used")
492-
} else {
493-
// positional argument
494-
(operands[idx].1, "argument never used")
495-
}
496-
})
489+
.map(|(idx, _)| operands[idx].1)
497490
.collect();
498-
match unused_operands.len() {
499-
0 => {}
500-
1 => {
501-
let (sp, msg) = unused_operands.into_iter().next().unwrap();
502-
let mut err = ecx.struct_span_err(sp, msg);
503-
err.span_label(sp, msg);
504-
err.emit();
505-
}
506-
_ => {
507-
let mut err = ecx.struct_span_err(
508-
unused_operands.iter().map(|&(sp, _)| sp).collect::<Vec<Span>>(),
509-
"multiple unused asm arguments",
510-
);
511-
for (sp, msg) in unused_operands {
512-
err.span_label(sp, msg);
513-
}
514-
err.emit();
515-
}
491+
if !unused_operands.is_empty() {
492+
let msg = format!("asm argument{} not used in template", pluralize!(unused_operands.len()));
493+
ecx.parse_sess.buffer_lint(UNUSED_ASM_ARGUMENTS, unused_operands, ast::CRATE_NODE_ID, &msg);
516494
}
517495

518496
let line_spans = if parser.line_spans.is_empty() {

Diff for: src/librustc_session/lint/builtin.rs

+6
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,12 @@ declare_lint! {
532532
"unsafe operations in unsafe functions without an explicit unsafe block are deprecated",
533533
}
534534

535+
declare_lint! {
536+
pub UNUSED_ASM_ARGUMENTS,
537+
Warn,
538+
"inline asm arguments not used in the template string",
539+
}
540+
535541
declare_lint_pass! {
536542
/// Does nothing as a lint pass, but registers some `Lint`s
537543
/// that are used by other parts of the compiler.

Diff for: src/test/ui/asm/bad-template.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@ fn main() {
99
//~^ ERROR invalid reference to argument at index 0
1010
asm!("{1}", in(reg) foo);
1111
//~^ ERROR invalid reference to argument at index 1
12-
//~^^ ERROR argument never used
12+
//~^^ WARN asm argument not used in template
1313
asm!("{a}");
1414
//~^ ERROR there is no argument named `a`
1515
asm!("{}", a = in(reg) foo);
1616
//~^ ERROR invalid reference to argument at index 0
17-
//~^^ ERROR argument never used
17+
//~^^ WARN asm argument not used in template
1818
asm!("{1}", a = in(reg) foo);
1919
//~^ ERROR invalid reference to argument at index 1
20-
//~^^ ERROR named argument never used
20+
//~^^ WARN asm argument not used in template
2121
asm!("{}", in("eax") foo);
2222
//~^ ERROR invalid reference to argument at index 0
2323
asm!("{:foo}", in(reg) foo);
2424
//~^ ERROR asm template modifier must be a single character
25+
asm!("", in(reg) 0, in(reg) 1);
26+
//~^ WARN asm arguments not used in template
2527
}
2628
}

Diff for: src/test/ui/asm/bad-template.stderr

+27-19
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ LL | asm!("{1}", in(reg) foo);
1414
|
1515
= note: there is 1 argument
1616

17-
error: argument never used
18-
--> $DIR/bad-template.rs:10:21
19-
|
20-
LL | asm!("{1}", in(reg) foo);
21-
| ^^^^^^^^^^^ argument never used
22-
2317
error: there is no argument named `a`
2418
--> $DIR/bad-template.rs:13:15
2519
|
@@ -41,12 +35,6 @@ note: named arguments cannot be referenced by position
4135
LL | asm!("{}", a = in(reg) foo);
4236
| ^^^^^^^^^^^^^^^
4337

44-
error: named argument never used
45-
--> $DIR/bad-template.rs:15:20
46-
|
47-
LL | asm!("{}", a = in(reg) foo);
48-
| ^^^^^^^^^^^^^^^ named argument never used
49-
5038
error: invalid reference to argument at index 1
5139
--> $DIR/bad-template.rs:18:15
5240
|
@@ -55,12 +43,6 @@ LL | asm!("{1}", a = in(reg) foo);
5543
|
5644
= note: no positional arguments were given
5745

58-
error: named argument never used
59-
--> $DIR/bad-template.rs:18:21
60-
|
61-
LL | asm!("{1}", a = in(reg) foo);
62-
| ^^^^^^^^^^^^^^^ named argument never used
63-
6446
error: invalid reference to argument at index 0
6547
--> $DIR/bad-template.rs:21:15
6648
|
@@ -82,5 +64,31 @@ error: asm template modifier must be a single character
8264
LL | asm!("{:foo}", in(reg) foo);
8365
| ^^^
8466

85-
error: aborting due to 10 previous errors
67+
warning: asm argument not used in template
68+
--> $DIR/bad-template.rs:10:21
69+
|
70+
LL | asm!("{1}", in(reg) foo);
71+
| ^^^^^^^^^^^
72+
|
73+
= note: `#[warn(unused_asm_arguments)]` on by default
74+
75+
warning: asm argument not used in template
76+
--> $DIR/bad-template.rs:15:20
77+
|
78+
LL | asm!("{}", a = in(reg) foo);
79+
| ^^^^^^^^^^^^^^^
80+
81+
warning: asm argument not used in template
82+
--> $DIR/bad-template.rs:18:21
83+
|
84+
LL | asm!("{1}", a = in(reg) foo);
85+
| ^^^^^^^^^^^^^^^
86+
87+
warning: asm arguments not used in template
88+
--> $DIR/bad-template.rs:25:18
89+
|
90+
LL | asm!("", in(reg) 0, in(reg) 1);
91+
| ^^^^^^^^^ ^^^^^^^^^
92+
93+
error: aborting due to 7 previous errors; 4 warnings emitted
8694

Diff for: src/test/ui/asm/parse-error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn main() {
4343
//~^ ERROR arguments are not allowed after options
4444
asm!("{a}", a = const foo, a = const bar);
4545
//~^ ERROR duplicate argument named `a`
46-
//~^^ ERROR argument never used
46+
//~^^ WARN asm argument not used in template
4747
asm!("", a = in("eax") foo);
4848
//~^ ERROR explicit register arguments cannot have names
4949
asm!("{a}", in("eax") foo, a = const bar);

Diff for: src/test/ui/asm/parse-error.stderr

+9-7
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,6 @@ LL | asm!("{a}", a = const foo, a = const bar);
122122
| |
123123
| previously here
124124

125-
error: argument never used
126-
--> $DIR/parse-error.rs:44:36
127-
|
128-
LL | asm!("{a}", a = const foo, a = const bar);
129-
| ^^^^^^^^^^^^^ argument never used
130-
131125
error: explicit register arguments cannot have names
132126
--> $DIR/parse-error.rs:47:18
133127
|
@@ -158,5 +152,13 @@ LL | asm!("{1}", in("eax") foo, const bar);
158152
| |
159153
| explicit register argument
160154

161-
error: aborting due to 24 previous errors
155+
warning: asm argument not used in template
156+
--> $DIR/parse-error.rs:44:36
157+
|
158+
LL | asm!("{a}", a = const foo, a = const bar);
159+
| ^^^^^^^^^^^^^
160+
|
161+
= note: `#[warn(unused_asm_arguments)]` on by default
162+
163+
error: aborting due to 23 previous errors; 1 warning emitted
162164

0 commit comments

Comments
 (0)