Skip to content

Commit 12c6a12

Browse files
committed
Emit error when trying to use assembler syntax directives in asm!
1 parent cb2effd commit 12c6a12

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

Diff for: compiler/rustc_builtin_macros/src/asm.rs

+52-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ use rustc_errors::{Applicability, DiagnosticBuilder};
77
use rustc_expand::base::{self, *};
88
use rustc_parse::parser::Parser;
99
use rustc_parse_format as parse;
10-
use rustc_span::symbol::{kw, sym, Symbol};
10+
use rustc_span::{
11+
symbol::{kw, sym, Symbol},
12+
BytePos,
13+
};
1114
use rustc_span::{InnerSpan, Span};
1215

1316
struct AsmArgs {
@@ -465,6 +468,54 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, sp: Span, args: AsmArgs) -> P<ast
465468
for piece in unverified_pieces {
466469
match piece {
467470
parse::Piece::String(s) => {
471+
if let Some(idx) = s.find(".intel_syntax") {
472+
let mut end = idx + ".intel_syntax".len();
473+
if let Some(prefix_idx) = s.split_at(end).1.find("noprefix") {
474+
// Should be a space and it should be immediately after
475+
if prefix_idx == 1 {
476+
end += " noprefix".len();
477+
}
478+
}
479+
480+
let syntax_span =
481+
template_span.from_inner(InnerSpan::new(idx + 1, end + 1));
482+
let mut err = ecx.struct_span_err(syntax_span, "intel sytnax is the default syntax, and trying to use this directive may cause issues");
483+
err.span_suggestion(
484+
syntax_span,
485+
"Remove this assembler directive",
486+
s.replace(&s[idx..end], "").to_string(),
487+
Applicability::MachineApplicable,
488+
);
489+
err.emit();
490+
}
491+
492+
if let Some(idx) = s.find(".att_syntax") {
493+
let mut end = idx + ".att_syntax".len();
494+
if let Some(prefix_idx) = s.split_at(end).1.find("noprefix") {
495+
// Should be a space and it should be immediately after
496+
if prefix_idx == 1 {
497+
end += " noprefix".len();
498+
}
499+
}
500+
501+
let syntax_span =
502+
template_span.from_inner(InnerSpan::new(idx + 1, end + 1));
503+
let mut err = ecx.struct_span_err(syntax_span, "using the .att_syntax directive may cause issues, use the att_syntax option instead");
504+
let asm_end = sp.hi() - BytePos(2);
505+
let suggestions = vec![
506+
(syntax_span, "".to_string()),
507+
(
508+
Span::new(asm_end, asm_end, sp.ctxt()),
509+
", options(att_syntax)".to_string(),
510+
),
511+
];
512+
err.multipart_suggestion(
513+
"Remove the assembler directive and replace it with options(att_syntax)",
514+
suggestions,
515+
Applicability::MachineApplicable,
516+
);
517+
err.emit();
518+
}
468519
template.push(ast::InlineAsmTemplatePiece::String(s.to_string()))
469520
}
470521
parse::Piece::NextArgument(arg) => {

Diff for: src/test/ui/asm/inline-syntax.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(asm, llvm_asm)]
2+
3+
fn main() {
4+
unsafe {
5+
asm!(".intel_syntax noprefix", "nop");
6+
//~^ ERROR intel sytnax is the default syntax
7+
asm!(".intel_syntax aaa noprefix", "nop");
8+
//~^ ERROR intel sytnax is the default syntax
9+
asm!(".att_syntax noprefix", "nop");
10+
//~^ ERROR using the .att_syntax directive may cause issues
11+
asm!(".att_syntax bbb noprefix", "nop");
12+
//~^ ERROR using the .att_syntax directive may cause issues
13+
}
14+
}

Diff for: src/test/ui/asm/inline-syntax.stderr

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
error: intel sytnax is the default syntax, and trying to use this directive may cause issues
2+
--> $DIR/inline-syntax.rs:5:15
3+
|
4+
LL | asm!(".intel_syntax noprefix", "nop");
5+
| ^^^^^^^^^^^^^^^^^^^^^^ help: Remove this assembler directive
6+
7+
error: intel sytnax is the default syntax, and trying to use this directive may cause issues
8+
--> $DIR/inline-syntax.rs:7:15
9+
|
10+
LL | asm!(".intel_syntax aaa noprefix", "nop");
11+
| ^^^^^^^^^^^^^ help: Remove this assembler directive: `aaa noprefix`
12+
13+
error: using the .att_syntax directive may cause issues, use the att_syntax option instead
14+
--> $DIR/inline-syntax.rs:9:15
15+
|
16+
LL | asm!(".att_syntax noprefix", "nop");
17+
| ^^^^^^^^^^^^^^^^^^^^
18+
|
19+
help: Remove the assembler directive and replace it with options(att_syntax)
20+
|
21+
LL | asm!("", "nop", options(att_syntax));
22+
| -- ^^^^^^^^^^^^^^^^^^^^^
23+
24+
error: using the .att_syntax directive may cause issues, use the att_syntax option instead
25+
--> $DIR/inline-syntax.rs:11:15
26+
|
27+
LL | asm!(".att_syntax bbb noprefix", "nop");
28+
| ^^^^^^^^^^^
29+
|
30+
help: Remove the assembler directive and replace it with options(att_syntax)
31+
|
32+
LL | asm!(" bbb noprefix", "nop", options(att_syntax));
33+
| -- ^^^^^^^^^^^^^^^^^^^^^
34+
35+
error: aborting due to 4 previous errors
36+

0 commit comments

Comments
 (0)