Skip to content

Commit d5d4795

Browse files
Rollup merge of #159513 - Urgau:runtime-symbols-159446, r=fee1-dead
Consider `()` as suspicious only when expecting `!` for runtime symbols Consider `()` as suspicious only when expecting `!` for runtime symbols, as `!` is considered ABI compatible in Rust with `()` (#159446 (comment)). Fixes #159446
2 parents fa34d4c + b1f030a commit d5d4795

3 files changed

Lines changed: 42 additions & 3 deletions

File tree

compiler/rustc_lint/src/runtime_symbols.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,16 @@ fn check<'tcx>(
227227
let expected = Ty::new_fn_ptr(cx.tcx, lang_sig);
228228
let actual = Ty::new_fn_ptr(cx.tcx, user_sig);
229229

230+
// ! is ABI compatible with ()
231+
// https://github.com/rust-lang/rust/issues/159446
232+
let lang_ret_incompatible_with_unit = !lang_sig.output().skip_binder().is_unit()
233+
&& !lang_sig.output().skip_binder().is_never();
234+
let user_sig_ret_is_unit = user_sig.output().skip_binder().is_unit();
235+
230236
if lang_sig.abi() != user_sig.abi()
231237
|| lang_sig.c_variadic() != user_sig.c_variadic()
232238
|| lang_sig.inputs().skip_binder().len() != user_sig.inputs().skip_binder().len()
233-
|| (!lang_sig.output().skip_binder().is_unit()
234-
&& user_sig.output().skip_binder().is_unit())
239+
|| (lang_ret_incompatible_with_unit && user_sig_ret_is_unit)
235240
{
236241
cx.emit_span_lint(
237242
INVALID_RUNTIME_SYMBOL_DEFINITIONS,

tests/ui/lint/runtime-symbols-unix.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ fn suspicious() {
5757
pub static exit2: Option<unsafe extern "C" fn(f32) -> !>;
5858
//~^ WARN suspicious definition of the runtime `exit` symbol
5959
}
60+
61+
extern "C" {
62+
#[link_name = "exit"]
63+
pub fn exit3(code: i32) -> i32;
64+
//~^ WARN suspicious definition of the runtime `exit` symbol
65+
66+
// ! is ABI compatible with ()
67+
// https://github.com/rust-lang/rust/issues/159446
68+
#[link_name = "exit"]
69+
pub fn exit4(code: i32);
70+
//~^ WARN suspicious definition of the runtime `exit` symbol
71+
}
6072
}
6173

6274
fn main() {}

tests/ui/lint/runtime-symbols-unix.stderr

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,27 @@ LL | pub static exit2: Option<unsafe extern "C" fn(f32) -> !>;
124124
= help: either fix the signature or remove any attributes like `#[unsafe(no_mangle)]`, `#[unsafe(export_name = "exit")]`, or `#[link_name = "exit"]`
125125
= help: allow this lint if the signature is compatible
126126

127-
error: aborting due to 8 previous errors; 4 warnings emitted
127+
warning: suspicious definition of the runtime `exit` symbol used by the standard library
128+
--> $DIR/runtime-symbols-unix.rs:64:9
129+
|
130+
LL | pub fn exit3(code: i32) -> i32;
131+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
132+
|
133+
= note: expected `unsafe extern "C" fn(i32) -> !`
134+
found `unsafe extern "C" fn(i32) -> i32`
135+
= help: either fix the signature or remove any attributes like `#[unsafe(no_mangle)]`, `#[unsafe(export_name = "exit")]`, or `#[link_name = "exit"]`
136+
= help: allow this lint if the signature is compatible
137+
138+
warning: suspicious definition of the runtime `exit` symbol used by the standard library
139+
--> $DIR/runtime-symbols-unix.rs:70:9
140+
|
141+
LL | pub fn exit4(code: i32);
142+
| ^^^^^^^^^^^^^^^^^^^^^^^^
143+
|
144+
= note: expected `unsafe extern "C" fn(i32) -> !`
145+
found `unsafe extern "C" fn(i32)`
146+
= help: either fix the signature or remove any attributes like `#[unsafe(no_mangle)]`, `#[unsafe(export_name = "exit")]`, or `#[link_name = "exit"]`
147+
= help: allow this lint if the signature is compatible
148+
149+
error: aborting due to 8 previous errors; 6 warnings emitted
128150

0 commit comments

Comments
 (0)