Closed
Description
Array created using repeat expression [i128::MIN; 1]
and returned from a function actually returns [0i128; 1]
.
Interestingly the problem does not happen when using the list syntax [i128::MIN]
to create the array.
I tried this code:
fn main() {
fn min_array_ok() -> [i128; 1] {
[i128::MIN]
}
assert_eq!(min_array_ok(), [-170141183460469231731687303715884105728i128]);
fn min_array_nok() -> [i128; 1] {
[i128::MIN; 1]
}
assert_eq!(min_array_nok(), [-170141183460469231731687303715884105728i128]); // panic!
}
I expected to see this happen: the second assert_eq
should pass
Instead, this happened: the second assert_eq
fails screaming that
thread 'main' panicked at 'assertion failed: `(left == right)`
left: `[0]`,
right: `[-170141183460469231731687303715884105728]`', src/main.rs:10:5
Meta
Problem exists on stable, beta and nightly
rustc --version --verbose
:
rustc 1.63.0 (4b91a6ea7 2022-08-08)
binary: rustc
commit-hash: 4b91a6ea7258a947e59c6522cd5898e7c0a6a88f
commit-date: 2022-08-08
host: x86_64-unknown-linux-gnu
release: 1.63.0
LLVM version: 14.0.5
Backtrace
0: rust_begin_unwind
at /rustc/289279de116707f28cf9c18e4bbb8c6ec84ad75b/library/std/src/panicking.rs:584:5
1: core::panicking::panic_fmt
at /rustc/289279de116707f28cf9c18e4bbb8c6ec84ad75b/library/core/src/panicking.rs:142:14
2: core::panicking::assert_failed_inner
3: core::panicking::assert_failed
at /rustc/289279de116707f28cf9c18e4bbb8c6ec84ad75b/library/core/src/panicking.rs:181:5
4: playground::main
at ./[src/main.rs:10](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021#):5
5: core::ops::function::FnOnce::call_once
at /rustc/289279de116707f28cf9c18e4bbb8c6ec84ad75b/library/core/src/ops/function.rs:248:5