rust-lang / rust-lang/rust

Warn user when the whole function is optimized to a `ud2`

Open
#141,220 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-codegen A-lints C-feature-request T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
pub fn bad_func1() -> i32 {
    let arr = [1, 2, 3];
    unsafe { *arr.get_unchecked(5) }
}
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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Use the provided Rust reproducer with rustc 1.86.0 as the starting case, then investigate the compiler's final compilation stages where the whole function becomes a single ud2. Done means emitting a warning rather than a hard error for this case while leaving optimization behavior intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.