rust-lang / rust-lang/rust

Panic on mem::transmute on negative non zero to Result<(), NonZeroI32>

Open
#159,433 15 comments 2 reactions 1 assignee View on GitHub

@saethlin is already working on this.

Since Jul 17, 2026.

A-MIR C-bug P-medium regression-from-stable-to-stable S-has-bisection T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code

I tried this code:

fn main() {
    let _ok: Result<(), std::num::NonZeroI32> = unsafe { std::mem::transmute(0i32) };
    let _err: Result<(), std::num::NonZeroI32> = unsafe { std::mem::transmute(1i32) };
    let _err: Result<(), std::num::NonZeroI32> = unsafe { std::mem::transmute(-1i32) }; // panics in debug. trying to construct an enum from an invalid value 0xffffffffffffffffffffffffffffffff
}

According to docs it is valid code.

This is results:

  • rustc 1.96 debug: Err(-1)
  • rustc 1.97 debug: aborts with “invalid enum value”
  • rustc 1.97 release: passes
  • current 1.99 nightly: still aborts
Version it worked on

rustc 1.96 debug: Err(-1)

Version with regression

rustc 1.97 debug
rustc --version --verbose:

rustc 1.97.0 (2d8144b78 2026-07-07)
binary: rustc
commit-hash: 2d8144b7880597b6e6d3dfd63a9a9efae3f533d3
commit-date: 2026-07-07
host: aarch64-apple-darwin
release: 1.97.0
LLVM version: 22.1.6
Backtrace
RUST_BACKTRACE=1 cargo run
    Finished [`dev` profile [unoptimized + debuginfo]](https://doc.rust-lang.org/cargo/reference/profiles.html#default-profiles) target(s) in 0.01s
     Running `target/debug/non-zero-i32-result`

thread 'main' (2711674) panicked at src/main.rs:4:59:
trying to construct an enum from an invalid value 0xffffffffffffffffffffffffffffffff
stack backtrace:
   0: __rustc::rust_begin_unwind
             at /rustc/2d8144b7880597b6e6d3dfd63a9a9efae3f533d3/library/std/src/panicking.rs:689:5
   1: core::panicking::panic_nounwind_fmt::runtime
             at /rustc/2d8144b7880597b6e6d3dfd63a9a9efae3f533d3/library/core/src/panicking.rs:122:22
   2: core::panicking::panic_nounwind_fmt
             at /rustc/2d8144b7880597b6e6d3dfd63a9a9efae3f533d3/library/core/src/intrinsics/mod.rs:2450:9
   3: core::panicking::panic_invalid_enum_construction
             at /rustc/2d8144b7880597b6e6d3dfd63a9a9efae3f533d3/library/core/src/panicking.rs:318:5
   4: non_zero_i32_result::main
             at ./src/main.rs:4:59
   5: <fn() as core::ops::function::FnOnce<()>>::call_once
             at /Users/yury/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
thread caused non-unwinding panic. aborting.
zsh: abort      RUST_BACKTRACE=1 cargo run

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.