Skip to content

Commit 6f45f62

Browse files
committed
Proper characters in labels, ignore comments
1 parent 1f8f863 commit 6f45f62

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

compiler/rustc_builtin_macros/src/asm.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -495,16 +495,21 @@ fn expand_preparsed_asm(
495495
// A semicolon might not actually be specified as a separator for all targets, but it seems like LLVM accepts it always
496496
let statements = template_str.split(|c| matches!(c, '\n' | ';'));
497497
for statement in statements {
498+
// If there's a comment, trim it from the statement
499+
let statement = statement.find("//").map_or(statement, |idx| &statement[..idx]);
498500
let mut start_idx = 0;
499501
for (idx, _) in statement.match_indices(':') {
500502
let possible_label = statement[start_idx..idx].trim();
501503
let mut chars = possible_label.chars();
502504
if let Some(c) = chars.next() {
503-
// A label starts with an alphabetic character and continues with alphanumeric characters
504-
if c.is_alphabetic() {
505-
if chars.all(char::is_alphanumeric) {
506-
found_labels.push(possible_label);
507-
}
505+
// A label starts with an alphabetic character or . or _ and continues with alphanumeric characters, _, or $
506+
if (c.is_alphabetic() || matches!(c, '.' | '_'))
507+
&& chars.all(|c| c.is_alphanumeric() || matches!(c, '_' | '$'))
508+
{
509+
found_labels.push(possible_label);
510+
} else {
511+
// If we encounter a non-label, there cannot be any further labels, so stop checking
512+
break;
508513
}
509514
}
510515

src/test/ui/asm/named_asm_labels.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ fn main() {
6161
// should not trigger lint, but may be invalid asm
6262
asm!("ab cd: nop");
6363

64-
// Only `blah:` should trigger
65-
asm!("1bar: blah: nop"); //~ ERROR do not use named labels
64+
// `blah:` does not trigger because labels need to be at the start
65+
// of the statement, and there was already a non-label
66+
asm!("1bar: blah: nop");
6667

6768
// Only `blah1:` should trigger
6869
asm!("blah1: 2bar: nop"); //~ ERROR do not use named labels
@@ -88,6 +89,21 @@ fn main() {
8889
// Intentionally breaking span finding
8990
// equivalent to "ABC: nop"
9091
asm!("\x41\x42\x43\x3A\x20\x6E\x6F\x70"); //~ ERROR do not use named labels
92+
93+
// Non-label colons - should pass
94+
// (most of these are stolen from other places)
95+
asm!("{:l}", in(reg) 0i64);
96+
asm!("{:e}", in(reg) 0f32);
97+
asm!("mov rax, qword ptr fs:[0]");
98+
99+
// Comments
100+
asm!(
101+
r"
102+
ab: nop // ab: does foo
103+
// cd: nop
104+
"
105+
);
106+
//~^^^^ ERROR do not use named labels
91107
}
92108
}
93109

src/test/ui/asm/named_asm_labels.stderr

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,7 @@ LL | nop ; blah4: nop
207207
= 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
208208

209209
error: do not use named labels in inline assembly
210-
--> $DIR/named_asm_labels.rs:65:21
211-
|
212-
LL | asm!("1bar: blah: nop");
213-
| ^^^^
214-
|
215-
= help: Only GAS local labels of the form `N:` where N is a number may be used in inline asm
216-
= 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
217-
218-
error: do not use named labels in inline assembly
219-
--> $DIR/named_asm_labels.rs:68:15
210+
--> $DIR/named_asm_labels.rs:69:15
220211
|
221212
LL | asm!("blah1: 2bar: nop");
222213
| ^^^^^
@@ -225,7 +216,7 @@ LL | asm!("blah1: 2bar: nop");
225216
= 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
226217

227218
error: do not use named labels in inline assembly
228-
--> $DIR/named_asm_labels.rs:71:15
219+
--> $DIR/named_asm_labels.rs:72:15
229220
|
230221
LL | asm!("def: def: nop");
231222
| ^^^
@@ -234,7 +225,7 @@ LL | asm!("def: def: nop");
234225
= 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
235226

236227
error: do not use named labels in inline assembly
237-
--> $DIR/named_asm_labels.rs:72:15
228+
--> $DIR/named_asm_labels.rs:73:15
238229
|
239230
LL | asm!("def: nop\ndef: nop");
240231
| ^^^
@@ -243,7 +234,7 @@ LL | asm!("def: nop\ndef: nop");
243234
= 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
244235

245236
error: do not use named labels in inline assembly
246-
--> $DIR/named_asm_labels.rs:73:15
237+
--> $DIR/named_asm_labels.rs:74:15
247238
|
248239
LL | asm!("def: nop; def: nop");
249240
| ^^^
@@ -252,7 +243,7 @@ LL | asm!("def: nop; def: nop");
252243
= 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
253244

254245
error: do not use named labels in inline assembly
255-
--> $DIR/named_asm_labels.rs:81:15
246+
--> $DIR/named_asm_labels.rs:82:15
256247
|
257248
LL | asm!("fooo\u{003A} nop");
258249
| ^^^^^^^^^^^^^^^^
@@ -261,7 +252,7 @@ LL | asm!("fooo\u{003A} nop");
261252
= 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
262253

263254
error: do not use named labels in inline assembly
264-
--> $DIR/named_asm_labels.rs:82:15
255+
--> $DIR/named_asm_labels.rs:83:15
265256
|
266257
LL | asm!("foooo\x3A nop");
267258
| ^^^^^^^^^^^^^
@@ -270,7 +261,7 @@ LL | asm!("foooo\x3A nop");
270261
= 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
271262

272263
error: do not use named labels in inline assembly
273-
--> $DIR/named_asm_labels.rs:85:15
264+
--> $DIR/named_asm_labels.rs:86:15
274265
|
275266
LL | asm!("fooooo:\u{000A} nop");
276267
| ^^^^^^
@@ -279,7 +270,7 @@ LL | asm!("fooooo:\u{000A} nop");
279270
= 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
280271

281272
error: do not use named labels in inline assembly
282-
--> $DIR/named_asm_labels.rs:86:15
273+
--> $DIR/named_asm_labels.rs:87:15
283274
|
284275
LL | asm!("foooooo:\x0A nop");
285276
| ^^^^^^^
@@ -288,13 +279,22 @@ LL | asm!("foooooo:\x0A nop");
288279
= 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
289280

290281
error: do not use named labels in inline assembly
291-
--> $DIR/named_asm_labels.rs:90:14
282+
--> $DIR/named_asm_labels.rs:91:14
292283
|
293284
LL | asm!("\x41\x42\x43\x3A\x20\x6E\x6F\x70");
294285
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
295286
|
296287
= help: Only GAS local labels of the form `N:` where N is a number may be used in inline asm
297288
= 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
298289

290+
error: do not use named labels in inline assembly
291+
--> $DIR/named_asm_labels.rs:102:13
292+
|
293+
LL | ab: nop // ab: does foo
294+
| ^^
295+
|
296+
= help: Only GAS local labels of the form `N:` where N is a number may be used in inline asm
297+
= 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
298+
299299
error: aborting due to 33 previous errors
300300

0 commit comments

Comments
 (0)