Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lint on black_boxing ZSTs #137658

Open
tgross35 opened this issue Feb 26, 2025 · 0 comments
Open

Lint on black_boxing ZSTs #137658

tgross35 opened this issue Feb 26, 2025 · 0 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-zst Area: Zero-sized types (ZST). C-enhancement Category: An issue proposing an enhancement or a PR with one. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tgross35
Copy link
Contributor

tgross35 commented Feb 26, 2025

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

@tgross35 tgross35 added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-zst Area: Zero-sized types (ZST). C-enhancement Category: An issue proposing an enhancement or a PR with one. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 26, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 26, 2025
@tgross35 tgross35 removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-zst Area: Zero-sized types (ZST). C-enhancement Category: An issue proposing an enhancement or a PR with one. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants