Open
Description
If a function pointer is black boxed, the call is treated as opaque:
use core::hint::black_box;
use core::ops::Shl;
#[no_mangle]
pub fn coerce_to_fn() {
let f: fn(u32, u32) -> u32 = u32::shl;
let fbb = black_box(f);
fbb(1, 2);
}
coerce_to_fn:
push rax
lea rax, [rip + <u32 as core::ops::bit::Shl>::shl::hf57c419aa60bbfeb]
mov qword ptr [rsp], rax
mov rax, rsp
mov edi, 1
mov esi, 2
call qword ptr [rsp]
pop rax
ret
However, black boxing a ZST that implements Fn
does nothing:
#[no_mangle]
pub fn with_zst() {
let fbb = black_box(u32::shl);
fbb(1, 2);
}
with_zst:
ret
This difference is subtle and we should lint on it. Suggested by @beetrees.
For reference: https://rust.godbolt.org/z/nvoEcnxKh
Metadata
Metadata
Assignees
Labels
Area: Lints (warnings about flaws in source code) such as unused_mut.Area: Zero-sized types (ZSTs).Category: An issue proposing an enhancement or a PR with one.Call for participation: Medium difficulty. Experience needed to fix: Intermediate.Relevant to the compiler team, which will review and decide on the PR/issue.