Skip to content

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

Description

@yury

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

Metadata

Metadata

Assignees

Labels

A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlC-bugCategory: This is a bug.P-mediumMedium priorityS-has-bisectionStatus: A bisection has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions