Skip to content

constructing invalid value of type [u8; 2]: at [0], encountered uninitialized memory, but expected an integer #155168

@matthiaskrgr

Description

@matthiaskrgr

I tried this code:

#![feature(min_generic_const_args)]
#![feature(adt_const_params, unsized_const_params, generic_const_parameter_types)]

use std::marker::ConstParamTy_;

fn foo<T: ConstParamTy_, const N: usize, const M: [T; N]>() -> [T; N] {
    M // Undefined Behavior: constructing invalid value of type [u8; 2]: at [0], encountered uninitialized memory, but expected an integer
}

fn main() {
    let a: [u8; 2] = foo::<u8, 2, { [] }>();
}

I expected to see this happen: explanation
no ub, probably we are missing some checks and some diagnostics here

Instead, this happened: explanation
ub

error: Undefined Behavior: constructing invalid value of type [u8; 2]: at [0], encountered uninitialized memory, but expected an integer
 --> src/main.rs:7:5
  |
7 |     M
  |     ^ Undefined Behavior occurred here
  |
  = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
  = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
  = note: stack backtrace:
          0: foo::<u8, 2, *b"">
              at src/main.rs:7:5: 7:6
          1: main
              at src/main.rs:11:22: 11:44
          2: <fn() as std::ops::FnOnce<()>>::call_once - shim(fn())
              at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5: 250:71
          3: std::sys::backtrace::__rust_begin_short_backtrace::<fn(), ()>
              at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/backtrace.rs:166:18: 166:21
          4: std::rt::lang_start::<()>::{closure#0}
              at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:206:18: 206:75
          5: std::ops::function::impls::<impl std::ops::FnOnce<()> for &dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe>::call_once
              at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:287:13: 287:31
          6: std::panicking::catch_unwind::do_call::<&dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe, i32>
              at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:581:40: 581:43
          7: std::panicking::catch_unwind::<i32, &dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe>
              at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:544:19: 544:88
          8: std::panic::catch_unwind::<&dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe, i32>
              at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panic.rs:359:14: 359:40
          9: std::rt::lang_start_internal::{closure#0}
              at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:175:24: 175:49
          10: std::panicking::catch_unwind::do_call::<{closure@std::rt::lang_start_internal::{closure#0}}, isize>
              at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:581:40: 581:43
          11: std::panicking::catch_unwind::<isize, {closure@std::rt::lang_start_internal::{closure#0}}>
              at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:544:19: 544:88
          12: std::panic::catch_unwind::<{closure@std::rt::lang_start_internal::{closure#0}}, isize>
              at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panic.rs:359:14: 359:40
          13: std::rt::lang_start_internal
              at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:171:5: 193:7
          14: std::rt::lang_start::<()>
              at /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/rt.rs:205:5: 210:6

error: aborting due to 1 previous error; 4 warnings emitted

Meta

rustc --version --verbose:

rustc 1.96.0-nightly (02c7f9bec 2026-04-10)
binary: rustc
commit-hash: 02c7f9bec0fd583160f8bcccb830216023b07bee
commit-date: 2026-04-10
host: x86_64-unknown-linux-gnu
release: 1.96.0-nightly
LLVM version: 22.1.2
Backtrace

<backtrace>

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.F-adt_const_params`#![feature(adt_const_params)]`F-generic_const_parameter_types`#![feature(generic_const_parameter_types)]`F-min_generic_const_args`#![feature(min_generic_const_args)]`F-unsized_const_params`#![feature(unsized_const_params)]`needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions