about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--compiler/rustc_builtin_macros/src/asm.rs15
-rw-r--r--src/test/ui/asm/named_asm_labels.rs20
-rw-r--r--src/test/ui/asm/named_asm_labels.stderr36
3 files changed, 46 insertions, 25 deletions
diff --git a/compiler/rustc_builtin_macros/src/asm.rs b/compiler/rustc_builtin_macros/src/asm.rs
index 1dbb3b45b4e..79f3554fdc8 100644
--- a/compiler/rustc_builtin_macros/src/asm.rs
+++ b/compiler/rustc_builtin_macros/src/asm.rs
@@ -495,16 +495,21 @@ fn expand_preparsed_asm(
             // A semicolon might not actually be specified as a separator for all targets, but it seems like LLVM accepts it always
             let statements = template_str.split(|c| matches!(c, '\n' | ';'));
             for statement in statements {
+                // If there's a comment, trim it from the statement
+                let statement = statement.find("//").map_or(statement, |idx| &statement[..idx]);
                 let mut start_idx = 0;
                 for (idx, _) in statement.match_indices(':') {
                     let possible_label = statement[start_idx..idx].trim();
                     let mut chars = possible_label.chars();
                     if let Some(c) = chars.next() {
-                        // A label starts with an alphabetic character and continues with alphanumeric characters
-                        if c.is_alphabetic() {
-                            if chars.all(char::is_alphanumeric) {
-                                found_labels.push(possible_label);
-                            }
+                        // A label starts with an alphabetic character or . or _ and continues with alphanumeric characters, _, or $
+                        if (c.is_alphabetic() || matches!(c, '.' | '_'))
+                            && chars.all(|c| c.is_alphanumeric() || matches!(c, '_' | '$'))
+                        {
+                            found_labels.push(possible_label);
+                        } else {
+                            // If we encounter a non-label, there cannot be any further labels, so stop checking
+                            break;
                         }
                     }
 
diff --git a/src/test/ui/asm/named_asm_labels.rs b/src/test/ui/asm/named_asm_labels.rs
index ba277b7aad6..feabafdefb7 100644
--- a/src/test/ui/asm/named_asm_labels.rs
+++ b/src/test/ui/asm/named_asm_labels.rs
@@ -61,8 +61,9 @@ fn main() {
         // should not trigger lint, but may be invalid asm
         asm!("ab cd: nop");
 
-        // Only `blah:` should trigger
-        asm!("1bar: blah: nop"); //~ ERROR do not use named labels
+        // `blah:` does not trigger because labels need to be at the start
+        // of the statement, and there was already a non-label
+        asm!("1bar: blah: nop");
 
         // Only `blah1:` should trigger
         asm!("blah1: 2bar: nop"); //~ ERROR do not use named labels
@@ -88,6 +89,21 @@ fn main() {
         // Intentionally breaking span finding
         // equivalent to "ABC: nop"
         asm!("\x41\x42\x43\x3A\x20\x6E\x6F\x70"); //~ ERROR do not use named labels
+
+        // Non-label colons - should pass
+        // (most of these are stolen from other places)
+        asm!("{:l}", in(reg) 0i64);
+        asm!("{:e}", in(reg) 0f32);
+        asm!("mov rax, qword ptr fs:[0]");
+
+        // Comments
+        asm!(
+            r"
+            ab: nop // ab: does foo
+            // cd: nop
+            "
+        );
+        //~^^^^ ERROR do not use named labels
     }
 }
 
diff --git a/src/test/ui/asm/named_asm_labels.stderr b/src/test/ui/asm/named_asm_labels.stderr
index 62e4eef1992..b3f9b93dc9d 100644
--- a/src/test/ui/asm/named_asm_labels.stderr
+++ b/src/test/ui/asm/named_asm_labels.stderr
@@ -207,16 +207,7 @@ LL |             nop ; blah4: nop
    = note: See the asm section of the unstable book <https://doc.rust-lang.org/nightly/unstable-book/library-features/asm.html#labels> for more information
 
 error: do not use named labels in inline assembly
-  --> $DIR/named_asm_labels.rs:65:21
-   |
-LL |         asm!("1bar: blah: nop");
-   |                     ^^^^
-   |
-   = help: Only GAS local labels of the form `N:` where N is a number may be used in inline asm
-   = note: See the asm section of the unstable book <https://doc.rust-lang.org/nightly/unstable-book/library-features/asm.html#labels> for more information
-
-error: do not use named labels in inline assembly
-  --> $DIR/named_asm_labels.rs:68:15
+  --> $DIR/named_asm_labels.rs:69:15
    |
 LL |         asm!("blah1: 2bar: nop");
    |               ^^^^^
@@ -225,7 +216,7 @@ LL |         asm!("blah1: 2bar: nop");
    = note: See the asm section of the unstable book <https://doc.rust-lang.org/nightly/unstable-book/library-features/asm.html#labels> for more information
 
 error: do not use named labels in inline assembly
-  --> $DIR/named_asm_labels.rs:71:15
+  --> $DIR/named_asm_labels.rs:72:15
    |
 LL |         asm!("def: def: nop");
    |               ^^^
@@ -234,7 +225,7 @@ LL |         asm!("def: def: nop");
    = note: See the asm section of the unstable book <https://doc.rust-lang.org/nightly/unstable-book/library-features/asm.html#labels> for more information
 
 error: do not use named labels in inline assembly
-  --> $DIR/named_asm_labels.rs:72:15
+  --> $DIR/named_asm_labels.rs:73:15
    |
 LL |         asm!("def: nop\ndef: nop");
    |               ^^^
@@ -243,7 +234,7 @@ LL |         asm!("def: nop\ndef: nop");
    = note: See the asm section of the unstable book <https://doc.rust-lang.org/nightly/unstable-book/library-features/asm.html#labels> for more information
 
 error: do not use named labels in inline assembly
-  --> $DIR/named_asm_labels.rs:73:15
+  --> $DIR/named_asm_labels.rs:74:15
    |
 LL |         asm!("def: nop; def: nop");
    |               ^^^
@@ -252,7 +243,7 @@ LL |         asm!("def: nop; def: nop");
    = note: See the asm section of the unstable book <https://doc.rust-lang.org/nightly/unstable-book/library-features/asm.html#labels> for more information
 
 error: do not use named labels in inline assembly
-  --> $DIR/named_asm_labels.rs:81:15
+  --> $DIR/named_asm_labels.rs:82:15
    |
 LL |         asm!("fooo\u{003A} nop");
    |               ^^^^^^^^^^^^^^^^
@@ -261,7 +252,7 @@ LL |         asm!("fooo\u{003A} nop");
    = note: See the asm section of the unstable book <https://doc.rust-lang.org/nightly/unstable-book/library-features/asm.html#labels> for more information
 
 error: do not use named labels in inline assembly
-  --> $DIR/named_asm_labels.rs:82:15
+  --> $DIR/named_asm_labels.rs:83:15
    |
 LL |         asm!("foooo\x3A nop");
    |               ^^^^^^^^^^^^^
@@ -270,7 +261,7 @@ LL |         asm!("foooo\x3A nop");
    = note: See the asm section of the unstable book <https://doc.rust-lang.org/nightly/unstable-book/library-features/asm.html#labels> for more information
 
 error: do not use named labels in inline assembly
-  --> $DIR/named_asm_labels.rs:85:15
+  --> $DIR/named_asm_labels.rs:86:15
    |
 LL |         asm!("fooooo:\u{000A} nop");
    |               ^^^^^^
@@ -279,7 +270,7 @@ LL |         asm!("fooooo:\u{000A} nop");
    = note: See the asm section of the unstable book <https://doc.rust-lang.org/nightly/unstable-book/library-features/asm.html#labels> for more information
 
 error: do not use named labels in inline assembly
-  --> $DIR/named_asm_labels.rs:86:15
+  --> $DIR/named_asm_labels.rs:87:15
    |
 LL |         asm!("foooooo:\x0A nop");
    |               ^^^^^^^
@@ -288,7 +279,7 @@ LL |         asm!("foooooo:\x0A nop");
    = note: See the asm section of the unstable book <https://doc.rust-lang.org/nightly/unstable-book/library-features/asm.html#labels> for more information
 
 error: do not use named labels in inline assembly
-  --> $DIR/named_asm_labels.rs:90:14
+  --> $DIR/named_asm_labels.rs:91:14
    |
 LL |         asm!("\x41\x42\x43\x3A\x20\x6E\x6F\x70");
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -296,5 +287,14 @@ LL |         asm!("\x41\x42\x43\x3A\x20\x6E\x6F\x70");
    = help: Only GAS local labels of the form `N:` where N is a number may be used in inline asm
    = note: See the asm section of the unstable book <https://doc.rust-lang.org/nightly/unstable-book/library-features/asm.html#labels> for more information
 
+error: do not use named labels in inline assembly
+  --> $DIR/named_asm_labels.rs:102:13
+   |
+LL |             ab: nop // ab: does foo
+   |             ^^
+   |
+   = help: Only GAS local labels of the form `N:` where N is a number may be used in inline asm
+   = note: See the asm section of the unstable book <https://doc.rust-lang.org/nightly/unstable-book/library-features/asm.html#labels> for more information
+
 error: aborting due to 33 previous errors