Skip to content

Commit 6b06ac4

Browse files
committed
Don't lint against named labels in naked_asm!
Naked functions are allowed to define global labels, just like `global_asm!`.
1 parent f191420 commit 6b06ac4

File tree

4 files changed

+37
-32
lines changed

4 files changed

+37
-32
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2784,7 +2784,7 @@ impl InlineAsmOperand {
27842784
}
27852785
}
27862786

2787-
#[derive(Clone, Copy, Encodable, Decodable, Debug, HashStable_Generic)]
2787+
#[derive(Clone, Copy, Encodable, Decodable, Debug, HashStable_Generic, PartialEq, Eq)]
27882788
pub enum AsmMacro {
27892789
/// The `asm!` macro
27902790
Asm,

compiler/rustc_lint/src/builtin.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2874,14 +2874,28 @@ impl<'tcx> LateLintPass<'tcx> for AsmLabels {
28742874
if let hir::Expr {
28752875
kind:
28762876
hir::ExprKind::InlineAsm(hir::InlineAsm {
2877-
asm_macro: AsmMacro::Asm | AsmMacro::NakedAsm,
2877+
asm_macro: asm_macro @ (AsmMacro::Asm | AsmMacro::NakedAsm),
28782878
template_strs,
28792879
options,
28802880
..
28812881
}),
28822882
..
28832883
} = expr
28842884
{
2885+
// Non-generic naked functions are allowed to define arbitrary
2886+
// labels.
2887+
if *asm_macro == AsmMacro::NakedAsm {
2888+
let mono = cx.generics.is_none_or(|generics| {
2889+
generics
2890+
.params
2891+
.iter()
2892+
.all(|param| matches!(param.kind, GenericParamKind::Lifetime { .. }))
2893+
});
2894+
if mono {
2895+
return;
2896+
}
2897+
}
2898+
28852899
// asm with `options(raw)` does not do replacement with `{` and `}`.
28862900
let raw = options.contains(InlineAsmOptions::RAW);
28872901

tests/ui/asm/named-asm-labels.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,10 @@ fn main() {
171171
}
172172
}
173173

174-
// Trigger on naked fns too, even though they can't be inlined, reusing a
175-
// label or LTO can cause labels to break
174+
// Don't trigger on naked functions.
176175
#[unsafe(naked)]
177176
pub extern "C" fn foo() -> i32 {
178177
naked_asm!(".Lfoo: mov rax, {}; ret;", "nop", const 1)
179-
//~^ ERROR avoid using named labels
180178
}
181179

182180
// Make sure that non-naked attributes *do* still let the lint happen
@@ -190,7 +188,18 @@ pub extern "C" fn bar() {
190188
pub extern "C" fn aaa() {
191189
fn _local() {}
192190

193-
naked_asm!(".Laaa: nop; ret;") //~ ERROR avoid using named labels
191+
naked_asm!(".Laaa: nop; ret;")
192+
}
193+
194+
#[unsafe(naked)]
195+
pub extern "C" fn bbb<'a>(a: &'a u32) {
196+
naked_asm!(".Lbbb: nop; ret;")
197+
}
198+
199+
#[unsafe(naked)]
200+
pub extern "C" fn ccc<T>(a: &T) {
201+
naked_asm!(".Lccc: nop; ret;")
202+
//~^ ERROR avoid using named labels
194203
}
195204

196205
pub fn normal() {
@@ -200,7 +209,7 @@ pub fn normal() {
200209
pub extern "C" fn bbb() {
201210
fn _very_local() {}
202211

203-
naked_asm!(".Lbbb: nop; ret;") //~ ERROR avoid using named labels
212+
naked_asm!(".Lbbb: nop; ret;")
204213
}
205214

206215
fn _local2() {}

tests/ui/asm/named-asm-labels.stderr

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -475,16 +475,7 @@ LL | #[warn(named_asm_labels)]
475475
| ^^^^^^^^^^^^^^^^
476476

477477
error: avoid using named labels in inline assembly
478-
--> $DIR/named-asm-labels.rs:178:17
479-
|
480-
LL | naked_asm!(".Lfoo: mov rax, {}; ret;", "nop", const 1)
481-
| ^^^^^
482-
|
483-
= help: only local labels of the form `<number>:` should be used in inline asm
484-
= note: see the asm section of Rust By Example <https://doc.rust-lang.org/nightly/rust-by-example/unsafe/asm.html#labels> for more information
485-
486-
error: avoid using named labels in inline assembly
487-
--> $DIR/named-asm-labels.rs:185:20
478+
--> $DIR/named-asm-labels.rs:183:20
488479
|
489480
LL | unsafe { asm!(".Lbar: mov rax, {}; ret;", "nop", const 1, options(noreturn)) }
490481
| ^^^^^
@@ -493,25 +484,16 @@ LL | unsafe { asm!(".Lbar: mov rax, {}; ret;", "nop", const 1, options(noret
493484
= note: see the asm section of Rust By Example <https://doc.rust-lang.org/nightly/rust-by-example/unsafe/asm.html#labels> for more information
494485

495486
error: avoid using named labels in inline assembly
496-
--> $DIR/named-asm-labels.rs:193:17
487+
--> $DIR/named-asm-labels.rs:201:17
497488
|
498-
LL | naked_asm!(".Laaa: nop; ret;")
489+
LL | naked_asm!(".Lccc: nop; ret;")
499490
| ^^^^^
500491
|
501492
= help: only local labels of the form `<number>:` should be used in inline asm
502493
= note: see the asm section of Rust By Example <https://doc.rust-lang.org/nightly/rust-by-example/unsafe/asm.html#labels> for more information
503494

504495
error: avoid using named labels in inline assembly
505-
--> $DIR/named-asm-labels.rs:203:21
506-
|
507-
LL | naked_asm!(".Lbbb: nop; ret;")
508-
| ^^^^^
509-
|
510-
= help: only local labels of the form `<number>:` should be used in inline asm
511-
= note: see the asm section of Rust By Example <https://doc.rust-lang.org/nightly/rust-by-example/unsafe/asm.html#labels> for more information
512-
513-
error: avoid using named labels in inline assembly
514-
--> $DIR/named-asm-labels.rs:212:15
496+
--> $DIR/named-asm-labels.rs:221:15
515497
|
516498
LL | asm!("closure1: nop");
517499
| ^^^^^^^^
@@ -520,7 +502,7 @@ LL | asm!("closure1: nop");
520502
= note: see the asm section of Rust By Example <https://doc.rust-lang.org/nightly/rust-by-example/unsafe/asm.html#labels> for more information
521503

522504
error: avoid using named labels in inline assembly
523-
--> $DIR/named-asm-labels.rs:216:15
505+
--> $DIR/named-asm-labels.rs:225:15
524506
|
525507
LL | asm!("closure2: nop");
526508
| ^^^^^^^^
@@ -529,13 +511,13 @@ LL | asm!("closure2: nop");
529511
= note: see the asm section of Rust By Example <https://doc.rust-lang.org/nightly/rust-by-example/unsafe/asm.html#labels> for more information
530512

531513
error: avoid using named labels in inline assembly
532-
--> $DIR/named-asm-labels.rs:226:19
514+
--> $DIR/named-asm-labels.rs:235:19
533515
|
534516
LL | asm!("closure3: nop");
535517
| ^^^^^^^^
536518
|
537519
= help: only local labels of the form `<number>:` should be used in inline asm
538520
= note: see the asm section of Rust By Example <https://doc.rust-lang.org/nightly/rust-by-example/unsafe/asm.html#labels> for more information
539521

540-
error: aborting due to 56 previous errors; 1 warning emitted
522+
error: aborting due to 54 previous errors; 1 warning emitted
541523

0 commit comments

Comments
 (0)