Skip to content

Commit e21e1d6

Browse files
committed
Auto merge of #87740 - npmccallum:naked_args, r=Amanieu
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 `@joshtriplett` `@Amanieu` `@haraldh`
2 parents 2ddb65c + 9c0147c commit e21e1d6

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
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);
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// only-x86_64
2+
#![deny(unused)]
3+
#![feature(asm)]
4+
#![feature(naked_functions)]
5+
#![crate_type = "lib"]
6+
7+
pub trait Trait {
8+
extern "sysv64" fn trait_associated(a: usize, b: usize) -> usize;
9+
extern "sysv64" fn trait_method(&self, a: usize, b: usize) -> usize;
10+
}
11+
12+
pub mod normal {
13+
pub extern "sysv64" fn function(a: usize, b: usize) -> usize {
14+
//~^ ERROR unused variable: `a`
15+
//~| ERROR unused variable: `b`
16+
unsafe { asm!("", options(noreturn)); }
17+
}
18+
19+
pub struct Normal;
20+
21+
impl Normal {
22+
pub extern "sysv64" fn associated(a: usize, b: usize) -> usize {
23+
//~^ ERROR unused variable: `a`
24+
//~| ERROR unused variable: `b`
25+
unsafe { asm!("", options(noreturn)); }
26+
}
27+
28+
pub extern "sysv64" fn method(&self, a: usize, b: usize) -> usize {
29+
//~^ ERROR unused variable: `a`
30+
//~| ERROR unused variable: `b`
31+
unsafe { asm!("", options(noreturn)); }
32+
}
33+
}
34+
35+
impl super::Trait for Normal {
36+
extern "sysv64" fn trait_associated(a: usize, b: usize) -> usize {
37+
//~^ ERROR unused variable: `a`
38+
//~| ERROR unused variable: `b`
39+
unsafe { asm!("", options(noreturn)); }
40+
}
41+
42+
extern "sysv64" fn trait_method(&self, a: usize, b: usize) -> usize {
43+
//~^ ERROR unused variable: `a`
44+
//~| ERROR unused variable: `b`
45+
unsafe { asm!("", options(noreturn)); }
46+
}
47+
}
48+
}
49+
50+
pub mod naked {
51+
#[naked]
52+
pub extern "sysv64" fn function(a: usize, b: usize) -> usize {
53+
unsafe { asm!("", options(noreturn)); }
54+
}
55+
56+
pub struct Naked;
57+
58+
impl Naked {
59+
#[naked]
60+
pub extern "sysv64" fn associated(a: usize, b: usize) -> usize {
61+
unsafe { asm!("", options(noreturn)); }
62+
}
63+
64+
#[naked]
65+
pub extern "sysv64" fn method(&self, a: usize, b: usize) -> usize {
66+
unsafe { asm!("", options(noreturn)); }
67+
}
68+
}
69+
70+
impl super::Trait for Naked {
71+
#[naked]
72+
extern "sysv64" fn trait_associated(a: usize, b: usize) -> usize {
73+
unsafe { asm!("", options(noreturn)); }
74+
}
75+
76+
#[naked]
77+
extern "sysv64" fn trait_method(&self, a: usize, b: usize) -> usize {
78+
unsafe { asm!("", options(noreturn)); }
79+
}
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
error: unused variable: `a`
2+
--> $DIR/naked-functions-unused.rs:13:37
3+
|
4+
LL | pub extern "sysv64" fn function(a: usize, b: usize) -> usize {
5+
| ^ help: if this is intentional, prefix it with an underscore: `_a`
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/naked-functions-unused.rs:2:9
9+
|
10+
LL | #![deny(unused)]
11+
| ^^^^^^
12+
= note: `#[deny(unused_variables)]` implied by `#[deny(unused)]`
13+
14+
error: unused variable: `b`
15+
--> $DIR/naked-functions-unused.rs:13:47
16+
|
17+
LL | pub extern "sysv64" fn function(a: usize, b: usize) -> usize {
18+
| ^ help: if this is intentional, prefix it with an underscore: `_b`
19+
20+
error: unused variable: `a`
21+
--> $DIR/naked-functions-unused.rs:22:43
22+
|
23+
LL | pub extern "sysv64" fn associated(a: usize, b: usize) -> usize {
24+
| ^ help: if this is intentional, prefix it with an underscore: `_a`
25+
26+
error: unused variable: `b`
27+
--> $DIR/naked-functions-unused.rs:22:53
28+
|
29+
LL | pub extern "sysv64" fn associated(a: usize, b: usize) -> usize {
30+
| ^ help: if this is intentional, prefix it with an underscore: `_b`
31+
32+
error: unused variable: `a`
33+
--> $DIR/naked-functions-unused.rs:28:46
34+
|
35+
LL | pub extern "sysv64" fn method(&self, a: usize, b: usize) -> usize {
36+
| ^ help: if this is intentional, prefix it with an underscore: `_a`
37+
38+
error: unused variable: `b`
39+
--> $DIR/naked-functions-unused.rs:28:56
40+
|
41+
LL | pub extern "sysv64" fn method(&self, a: usize, b: usize) -> usize {
42+
| ^ help: if this is intentional, prefix it with an underscore: `_b`
43+
44+
error: unused variable: `a`
45+
--> $DIR/naked-functions-unused.rs:36:45
46+
|
47+
LL | extern "sysv64" fn trait_associated(a: usize, b: usize) -> usize {
48+
| ^ help: if this is intentional, prefix it with an underscore: `_a`
49+
50+
error: unused variable: `b`
51+
--> $DIR/naked-functions-unused.rs:36:55
52+
|
53+
LL | extern "sysv64" fn trait_associated(a: usize, b: usize) -> usize {
54+
| ^ help: if this is intentional, prefix it with an underscore: `_b`
55+
56+
error: unused variable: `a`
57+
--> $DIR/naked-functions-unused.rs:42:48
58+
|
59+
LL | extern "sysv64" fn trait_method(&self, a: usize, b: usize) -> usize {
60+
| ^ help: if this is intentional, prefix it with an underscore: `_a`
61+
62+
error: unused variable: `b`
63+
--> $DIR/naked-functions-unused.rs:42:58
64+
|
65+
LL | extern "sysv64" fn trait_method(&self, a: usize, b: usize) -> usize {
66+
| ^ help: if this is intentional, prefix it with an underscore: `_b`
67+
68+
error: aborting due to 10 previous errors
69+

0 commit comments

Comments
 (0)