about summary refs log tree commit diff
diff options
context:
space:
mode:
authorasquared31415 <34665709+asquared31415@users.noreply.github.com>2021-02-18 14:27:11 -0500
committerasquared31415 <34665709+asquared31415@users.noreply.github.com>2021-02-18 14:27:11 -0500
commit12c6a12d62fda3d5d070fe3915f03154ff7c80df (patch)
treebca07f5862f16fb7f90a79c1aa7b4502bd700a8a
parentcb2effd44e667d133e31ef334e30d10195218ce6 (diff)
downloadrust-12c6a12d62fda3d5d070fe3915f03154ff7c80df.tar.gz
rust-12c6a12d62fda3d5d070fe3915f03154ff7c80df.zip
Emit error when trying to use assembler syntax directives in `asm!`
-rw-r--r--compiler/rustc_builtin_macros/src/asm.rs53
-rw-r--r--src/test/ui/asm/inline-syntax.rs14
-rw-r--r--src/test/ui/asm/inline-syntax.stderr36
3 files changed, 102 insertions, 1 deletions
diff --git a/compiler/rustc_builtin_macros/src/asm.rs b/compiler/rustc_builtin_macros/src/asm.rs
index 36cd6c281b4..168dee6678d 100644
--- a/compiler/rustc_builtin_macros/src/asm.rs
+++ b/compiler/rustc_builtin_macros/src/asm.rs
@@ -7,7 +7,10 @@ use rustc_errors::{Applicability, DiagnosticBuilder};
 use rustc_expand::base::{self, *};
 use rustc_parse::parser::Parser;
 use rustc_parse_format as parse;
-use rustc_span::symbol::{kw, sym, Symbol};
+use rustc_span::{
+    symbol::{kw, sym, Symbol},
+    BytePos,
+};
 use rustc_span::{InnerSpan, Span};
 
 struct AsmArgs {
@@ -465,6 +468,54 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, sp: Span, args: AsmArgs) -> P<ast
         for piece in unverified_pieces {
             match piece {
                 parse::Piece::String(s) => {
+                    if let Some(idx) = s.find(".intel_syntax") {
+                        let mut end = idx + ".intel_syntax".len();
+                        if let Some(prefix_idx) = s.split_at(end).1.find("noprefix") {
+                            // Should be a space and it should be immediately after
+                            if prefix_idx == 1 {
+                                end += " noprefix".len();
+                            }
+                        }
+
+                        let syntax_span =
+                            template_span.from_inner(InnerSpan::new(idx + 1, end + 1));
+                        let mut err = ecx.struct_span_err(syntax_span, "intel sytnax is the default syntax, and trying to use this directive may cause issues");
+                        err.span_suggestion(
+                            syntax_span,
+                            "Remove this assembler directive",
+                            s.replace(&s[idx..end], "").to_string(),
+                            Applicability::MachineApplicable,
+                        );
+                        err.emit();
+                    }
+
+                    if let Some(idx) = s.find(".att_syntax") {
+                        let mut end = idx + ".att_syntax".len();
+                        if let Some(prefix_idx) = s.split_at(end).1.find("noprefix") {
+                            // Should be a space and it should be immediately after
+                            if prefix_idx == 1 {
+                                end += " noprefix".len();
+                            }
+                        }
+
+                        let syntax_span =
+                            template_span.from_inner(InnerSpan::new(idx + 1, end + 1));
+                        let mut err = ecx.struct_span_err(syntax_span, "using the .att_syntax directive may cause issues, use the att_syntax option instead");
+                        let asm_end = sp.hi() - BytePos(2);
+                        let suggestions = vec![
+                            (syntax_span, "".to_string()),
+                            (
+                                Span::new(asm_end, asm_end, sp.ctxt()),
+                                ", options(att_syntax)".to_string(),
+                            ),
+                        ];
+                        err.multipart_suggestion(
+                            "Remove the assembler directive and replace it with options(att_syntax)",
+                            suggestions,
+                            Applicability::MachineApplicable,
+                        );
+                        err.emit();
+                    }
                     template.push(ast::InlineAsmTemplatePiece::String(s.to_string()))
                 }
                 parse::Piece::NextArgument(arg) => {
diff --git a/src/test/ui/asm/inline-syntax.rs b/src/test/ui/asm/inline-syntax.rs
new file mode 100644
index 00000000000..ec8ff071885
--- /dev/null
+++ b/src/test/ui/asm/inline-syntax.rs
@@ -0,0 +1,14 @@
+#![feature(asm, llvm_asm)]
+
+fn main() {
+    unsafe {
+        asm!(".intel_syntax noprefix", "nop");
+        //~^ ERROR intel sytnax is the default syntax
+        asm!(".intel_syntax aaa noprefix", "nop");
+        //~^ ERROR intel sytnax is the default syntax
+        asm!(".att_syntax noprefix", "nop");
+        //~^ ERROR using the .att_syntax directive may cause issues
+        asm!(".att_syntax bbb noprefix", "nop");
+        //~^ ERROR using the .att_syntax directive may cause issues
+    }
+}
diff --git a/src/test/ui/asm/inline-syntax.stderr b/src/test/ui/asm/inline-syntax.stderr
new file mode 100644
index 00000000000..bd792660c6a
--- /dev/null
+++ b/src/test/ui/asm/inline-syntax.stderr
@@ -0,0 +1,36 @@
+error: intel sytnax is the default syntax, and trying to use this directive may cause issues
+  --> $DIR/inline-syntax.rs:5:15
+   |
+LL |         asm!(".intel_syntax noprefix", "nop");
+   |               ^^^^^^^^^^^^^^^^^^^^^^ help: Remove this assembler directive
+
+error: intel sytnax is the default syntax, and trying to use this directive may cause issues
+  --> $DIR/inline-syntax.rs:7:15
+   |
+LL |         asm!(".intel_syntax aaa noprefix", "nop");
+   |               ^^^^^^^^^^^^^ help: Remove this assembler directive: `aaa noprefix`
+
+error: using the .att_syntax directive may cause issues, use the att_syntax option instead
+  --> $DIR/inline-syntax.rs:9:15
+   |
+LL |         asm!(".att_syntax noprefix", "nop");
+   |               ^^^^^^^^^^^^^^^^^^^^
+   |
+help: Remove the assembler directive and replace it with options(att_syntax)
+   |
+LL |         asm!("", "nop", options(att_syntax));
+   |              --       ^^^^^^^^^^^^^^^^^^^^^
+
+error: using the .att_syntax directive may cause issues, use the att_syntax option instead
+  --> $DIR/inline-syntax.rs:11:15
+   |
+LL |         asm!(".att_syntax bbb noprefix", "nop");
+   |               ^^^^^^^^^^^
+   |
+help: Remove the assembler directive and replace it with options(att_syntax)
+   |
+LL |         asm!(" bbb noprefix", "nop", options(att_syntax));
+   |              --                    ^^^^^^^^^^^^^^^^^^^^^
+
+error: aborting due to 4 previous errors
+