Skip to content

Commit 2c70889

Browse files
committed
Fix ICE in try_to_raw_bytes when array elements have mismatched scalar
sizes
1 parent 859951e commit 2c70889

5 files changed

Lines changed: 59 additions & 2 deletions

File tree

compiler/rustc_middle/src/ty/consts/valtree.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt;
22
use std::ops::Deref;
33

4-
use rustc_abi::{FIRST_VARIANT, VariantIdx};
4+
use rustc_abi::{FIRST_VARIANT, Size, VariantIdx};
55
use rustc_data_structures::intern::Interned;
66
use rustc_hir::def::Namespace;
77
use rustc_macros::{
@@ -148,7 +148,10 @@ impl<'tcx> Value<'tcx> {
148148
}
149149

150150
// We create an iterator that yields `Option<u8>`
151-
let iterator = self.to_branch().into_iter().map(|ct| Some(ct.try_to_leaf()?.to_u8()));
151+
let iterator = self
152+
.to_branch()
153+
.into_iter()
154+
.map(|ct| ct.try_to_leaf()?.try_to_bits(Size::from_bytes(1)).ok().map(|b| b as u8));
152155
// If there is `None` in the iterator, then the array is not a valid array of u8s and we return `None`
153156
let bytes: Vec<u8> = iterator.collect::<Option<Vec<u8>>>()?;
154157

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//! Regression test for <https://github.com/rust-lang/rust/issues/152683>
2+
#![expect(incomplete_features)]
3+
#![feature(adt_const_params, generic_const_parameter_types, min_generic_const_args)]
4+
fn foo<const N: usize, const A: [u8; N]>() {}
5+
6+
fn main() {
7+
foo::<_, { [0, 1u8, 2u32, 8u64] }>();
8+
//~^ ERROR the constant `2` is not of type `u8`
9+
//~| ERROR the constant `8` is not of type `u8`
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: the constant `2` is not of type `u8`
2+
--> $DIR/array_elem_type_mismatch.rs:7:16
3+
|
4+
LL | foo::<_, { [0, 1u8, 2u32, 8u64] }>();
5+
| ^^^^^^^^^^^^^^^^^^^^ expected `u8`, found `u32`
6+
7+
error: the constant `8` is not of type `u8`
8+
--> $DIR/array_elem_type_mismatch.rs:7:16
9+
|
10+
LL | foo::<_, { [0, 1u8, 2u32, 8u64] }>();
11+
| ^^^^^^^^^^^^^^^^^^^^ expected `u8`, found `u64`
12+
13+
error: aborting due to 2 previous errors
14+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![expect(incomplete_features)]
2+
#![feature(adt_const_params, min_generic_const_args)]
3+
4+
struct ArrWrap<const N: [u8; 1]>;
5+
6+
fn main() {
7+
let _: ArrWrap<{ [1_u8] }> = ArrWrap::<{ [1_u16] }>;
8+
//~^ ERROR: mismatched types
9+
//~| ERROR the constant `1` is not of type `u8`
10+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/wrapped_array_elem_type_mismatch.rs:7:34
3+
|
4+
LL | let _: ArrWrap<{ [1_u8] }> = ArrWrap::<{ [1_u16] }>;
5+
| ------------------- ^^^^^^^^^^^^^^^^^^^^^^ expected `*b"\x01"`, found `[1]`
6+
| |
7+
| expected due to this
8+
|
9+
= note: expected struct `ArrWrap<*b"\x01">`
10+
found struct `ArrWrap<[1]>`
11+
12+
error: the constant `1` is not of type `u8`
13+
--> $DIR/wrapped_array_elem_type_mismatch.rs:7:46
14+
|
15+
LL | let _: ArrWrap<{ [1_u8] }> = ArrWrap::<{ [1_u16] }>;
16+
| ^^^^^^^ expected `u8`, found `u16`
17+
18+
error: aborting due to 2 previous errors
19+
20+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)