Skip to content

Commit e2cacff

Browse files
authored
Unrolled build for #140871
Rollup merge of #140871 - Amanieu:naked-asm-label, r=compiler-errors Don't lint against named labels in `naked_asm!` Naked functions are allowed to define global labels, just like `global_asm!`.
2 parents 051d0e8 + 1f4561b commit e2cacff

File tree

4 files changed

+47
-31
lines changed

4 files changed

+47
-31
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2849,7 +2849,7 @@ impl InlineAsmOperand {
28492849
}
28502850
}
28512851

2852-
#[derive(Clone, Copy, Encodable, Decodable, Debug, HashStable_Generic, Walkable)]
2852+
#[derive(Clone, Copy, Encodable, Decodable, Debug, HashStable_Generic, Walkable, PartialEq, Eq)]
28532853
pub enum AsmMacro {
28542854
/// The `asm!` macro
28552855
Asm,

compiler/rustc_lint/src/builtin.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2870,14 +2870,23 @@ impl<'tcx> LateLintPass<'tcx> for AsmLabels {
28702870
if let hir::Expr {
28712871
kind:
28722872
hir::ExprKind::InlineAsm(hir::InlineAsm {
2873-
asm_macro: AsmMacro::Asm | AsmMacro::NakedAsm,
2873+
asm_macro: asm_macro @ (AsmMacro::Asm | AsmMacro::NakedAsm),
28742874
template_strs,
28752875
options,
28762876
..
28772877
}),
28782878
..
28792879
} = expr
28802880
{
2881+
// Non-generic naked functions are allowed to define arbitrary
2882+
// labels.
2883+
if *asm_macro == AsmMacro::NakedAsm {
2884+
let def_id = expr.hir_id.owner.def_id;
2885+
if !cx.tcx.generics_of(def_id).requires_monomorphization(cx.tcx) {
2886+
return;
2887+
}
2888+
}
2889+
28812890
// asm with `options(raw)` does not do replacement with `{` and `}`.
28822891
let raw = options.contains(InlineAsmOptions::RAW);
28832892

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

Lines changed: 21 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() {}
@@ -230,3 +239,10 @@ fn closures() {
230239

231240
// Don't trigger on global asm
232241
global_asm!("aaaaaaaa: nop");
242+
243+
trait Foo {
244+
#[unsafe(naked)]
245+
extern "C" fn bbb<'a>(a: &'a u32) {
246+
naked_asm!(".Lbbb: nop; ret;") //~ ERROR avoid using named labels
247+
}
248+
}

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

Lines changed: 15 additions & 24 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,49 +484,49 @@ 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
496+
--> $DIR/named-asm-labels.rs:221:15
506497
|
507-
LL | naked_asm!(".Lbbb: nop; ret;")
508-
| ^^^^^
498+
LL | asm!("closure1: nop");
499+
| ^^^^^^^^
509500
|
510501
= help: only local labels of the form `<number>:` should be used in inline asm
511502
= 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
512503

513504
error: avoid using named labels in inline assembly
514-
--> $DIR/named-asm-labels.rs:212:15
505+
--> $DIR/named-asm-labels.rs:225:15
515506
|
516-
LL | asm!("closure1: nop");
507+
LL | asm!("closure2: nop");
517508
| ^^^^^^^^
518509
|
519510
= help: only local labels of the form `<number>:` should be used in inline asm
520511
= 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
521512

522513
error: avoid using named labels in inline assembly
523-
--> $DIR/named-asm-labels.rs:216:15
514+
--> $DIR/named-asm-labels.rs:235:19
524515
|
525-
LL | asm!("closure2: nop");
526-
| ^^^^^^^^
516+
LL | asm!("closure3: nop");
517+
| ^^^^^^^^
527518
|
528519
= help: only local labels of the form `<number>:` should be used in inline asm
529520
= 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
530521

531522
error: avoid using named labels in inline assembly
532-
--> $DIR/named-asm-labels.rs:226:19
523+
--> $DIR/named-asm-labels.rs:246:21
533524
|
534-
LL | asm!("closure3: nop");
535-
| ^^^^^^^^
525+
LL | naked_asm!(".Lbbb: nop; ret;")
526+
| ^^^^^
536527
|
537528
= help: only local labels of the form `<number>:` should be used in inline asm
538529
= 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
539530

540-
error: aborting due to 56 previous errors; 1 warning emitted
531+
error: aborting due to 55 previous errors; 1 warning emitted
541532

0 commit comments

Comments
 (0)