Skip to content

Commit 9c0147c

Browse files
committed
Disable unused variable lint for naked functions
In most calling conventions, accessing function parameters may require stack access. However, naked functions have no assembly prelude to set up stack access. This is why naked functions may only contain a single `asm!()` block. All parameter access is done inside the `asm!()` block, so we cannot validate the liveness of the input parameters. Therefore, we should disable the lint for naked functions. rust-lang/rfcs#2774 rust-lang/rfcs#2972
1 parent 7ac0cb0 commit 9c0147c

File tree

3 files changed

+6
-71
lines changed

3 files changed

+6
-71
lines changed

compiler/rustc_passes/src/liveness.rs

+5
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,11 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> {
332332
}
333333
}
334334

335+
// Don't run unused pass for #[naked]
336+
if self.tcx.has_attr(def_id, sym::naked) {
337+
return;
338+
}
339+
335340
if let Some(captures) = maps.tcx.typeck(local_def_id).closure_min_captures.get(&def_id) {
336341
for &var_hir_id in captures.keys() {
337342
let var_name = maps.tcx.hir().name(var_hir_id);

src/test/ui/asm/naked-functions-unused.rs

-10
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ pub mod normal {
5050
pub mod naked {
5151
#[naked]
5252
pub extern "sysv64" fn function(a: usize, b: usize) -> usize {
53-
//~^ ERROR unused variable: `a`
54-
//~| ERROR unused variable: `b`
5553
unsafe { asm!("", options(noreturn)); }
5654
}
5755

@@ -60,31 +58,23 @@ pub mod naked {
6058
impl Naked {
6159
#[naked]
6260
pub extern "sysv64" fn associated(a: usize, b: usize) -> usize {
63-
//~^ ERROR unused variable: `a`
64-
//~| ERROR unused variable: `b`
6561
unsafe { asm!("", options(noreturn)); }
6662
}
6763

6864
#[naked]
6965
pub extern "sysv64" fn method(&self, a: usize, b: usize) -> usize {
70-
//~^ ERROR unused variable: `a`
71-
//~| ERROR unused variable: `b`
7266
unsafe { asm!("", options(noreturn)); }
7367
}
7468
}
7569

7670
impl super::Trait for Naked {
7771
#[naked]
7872
extern "sysv64" fn trait_associated(a: usize, b: usize) -> usize {
79-
//~^ ERROR unused variable: `a`
80-
//~| ERROR unused variable: `b`
8173
unsafe { asm!("", options(noreturn)); }
8274
}
8375

8476
#[naked]
8577
extern "sysv64" fn trait_method(&self, a: usize, b: usize) -> usize {
86-
//~^ ERROR unused variable: `a`
87-
//~| ERROR unused variable: `b`
8878
unsafe { asm!("", options(noreturn)); }
8979
}
9080
}

src/test/ui/asm/naked-functions-unused.stderr

+1-61
Original file line numberDiff line numberDiff line change
@@ -65,65 +65,5 @@ error: unused variable: `b`
6565
LL | extern "sysv64" fn trait_method(&self, a: usize, b: usize) -> usize {
6666
| ^ help: if this is intentional, prefix it with an underscore: `_b`
6767

68-
error: unused variable: `a`
69-
--> $DIR/naked-functions-unused.rs:52:37
70-
|
71-
LL | pub extern "sysv64" fn function(a: usize, b: usize) -> usize {
72-
| ^ help: if this is intentional, prefix it with an underscore: `_a`
73-
74-
error: unused variable: `b`
75-
--> $DIR/naked-functions-unused.rs:52:47
76-
|
77-
LL | pub extern "sysv64" fn function(a: usize, b: usize) -> usize {
78-
| ^ help: if this is intentional, prefix it with an underscore: `_b`
79-
80-
error: unused variable: `a`
81-
--> $DIR/naked-functions-unused.rs:62:43
82-
|
83-
LL | pub extern "sysv64" fn associated(a: usize, b: usize) -> usize {
84-
| ^ help: if this is intentional, prefix it with an underscore: `_a`
85-
86-
error: unused variable: `b`
87-
--> $DIR/naked-functions-unused.rs:62:53
88-
|
89-
LL | pub extern "sysv64" fn associated(a: usize, b: usize) -> usize {
90-
| ^ help: if this is intentional, prefix it with an underscore: `_b`
91-
92-
error: unused variable: `a`
93-
--> $DIR/naked-functions-unused.rs:69:46
94-
|
95-
LL | pub extern "sysv64" fn method(&self, a: usize, b: usize) -> usize {
96-
| ^ help: if this is intentional, prefix it with an underscore: `_a`
97-
98-
error: unused variable: `b`
99-
--> $DIR/naked-functions-unused.rs:69:56
100-
|
101-
LL | pub extern "sysv64" fn method(&self, a: usize, b: usize) -> usize {
102-
| ^ help: if this is intentional, prefix it with an underscore: `_b`
103-
104-
error: unused variable: `a`
105-
--> $DIR/naked-functions-unused.rs:78:45
106-
|
107-
LL | extern "sysv64" fn trait_associated(a: usize, b: usize) -> usize {
108-
| ^ help: if this is intentional, prefix it with an underscore: `_a`
109-
110-
error: unused variable: `b`
111-
--> $DIR/naked-functions-unused.rs:78:55
112-
|
113-
LL | extern "sysv64" fn trait_associated(a: usize, b: usize) -> usize {
114-
| ^ help: if this is intentional, prefix it with an underscore: `_b`
115-
116-
error: unused variable: `a`
117-
--> $DIR/naked-functions-unused.rs:85:48
118-
|
119-
LL | extern "sysv64" fn trait_method(&self, a: usize, b: usize) -> usize {
120-
| ^ help: if this is intentional, prefix it with an underscore: `_a`
121-
122-
error: unused variable: `b`
123-
--> $DIR/naked-functions-unused.rs:85:58
124-
|
125-
LL | extern "sysv64" fn trait_method(&self, a: usize, b: usize) -> usize {
126-
| ^ help: if this is intentional, prefix it with an underscore: `_b`
127-
128-
error: aborting due to 20 previous errors
68+
error: aborting due to 10 previous errors
12969

0 commit comments

Comments
 (0)