Skip to content

Warn user when the whole function is optimized to a ud2 #141220

Open
@HKalbasi

Description

@HKalbasi

Code

pub fn bad_func1(num: i64) -> i32 {
    let v2 = unsafe { std::mem::transmute::<i64, &i32>(num) };
    *v2
}

Current output

No warning, but the assembly only contains a single `ud2` instruction, which will terminate the program with `SIGILL` if executed.

Desired output

Warn user that this function has undefined behavior and will not work as intended.

Rationale and extra context

This warning should be emitted at very last stages of compiling, and it might be very hard to implement in the current compiler architecture. But it can save time of the developer, specially when there is no test coverage for that function.

It should be a warning and not a hard error, since optimizations can work differently from version to version.

Other cases

Rust Version

rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generationA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-feature-requestCategory: A feature request, i.e: not implemented / a PR.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