Skip to content

Lint on black_boxing ZSTs #137658

Open
Open
@tgross35

Description

@tgross35

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

A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-zstArea: Zero-sized types (ZSTs).C-enhancementCategory: An issue proposing an enhancement or a PR with one.E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions