Skip to content

Commit 8fe6767

Browse files
authored
Unrolled build for #152794
Rollup merge of #152794 - reddevilmidzy:mgca-print, r=BoxyUwU Fix ICE in `try_to_raw_bytes` when array elements have mismatched close: #152683 After #152001, suffixed integer literals preserve their own type during const lowering, so `try_to_raw_bytes` could call `.to_u8()` on a scalar with size > 1, causing an ICE. Fix by using `try_to_bits(Size::from_bytes(1)).ok()` instead. r? BoxyUwU
2 parents 38c0de8 + a5789d0 commit 8fe6767

5 files changed

Lines changed: 64 additions & 5 deletions

File tree

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,24 @@ impl<'tcx> Value<'tcx> {
136136
ty::Ref(_, inner_ty, _) => match inner_ty.kind() {
137137
// `&str` can be interpreted as raw bytes
138138
ty::Str => {}
139-
// `&[u8]` can be interpreted as raw bytes
140-
ty::Slice(slice_ty) if *slice_ty == tcx.types.u8 => {}
139+
// `&[T]` can be interpreted as raw bytes if elements are `u8`
140+
ty::Slice(_) => {}
141141
// other `&_` can't be interpreted as raw bytes
142142
_ => return None,
143143
},
144-
// `[u8; N]` can be interpreted as raw bytes
145-
ty::Array(array_ty, _) if *array_ty == tcx.types.u8 => {}
144+
// `[T; N]` can be interpreted as raw bytes if elements are `u8`
145+
ty::Array(_, _) => {}
146146
// Otherwise, type cannot be interpreted as raw bytes
147147
_ => return None,
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.to_branch().into_iter().map(|ct| {
152+
(*ct)
153+
.try_to_value()
154+
.and_then(|value| (value.ty == tcx.types.u8).then_some(value))
155+
.and_then(|value| value.try_to_leaf().map(|leaf| leaf.to_u8()))
156+
});
152157
// If there is `None` in the iterator, then the array is not a valid array of u8s and we return `None`
153158
let bytes: Vec<u8> = iterator.collect::<Option<Vec<u8>>>()?;
154159

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)