Skip to content

Commit 4ce1b0f

Browse files
Rollup merge of #99358 - compiler-errors:issue-99325, r=oli-obk
Allow `ValTree::try_to_raw_bytes` on `u8` array Fixes #99325 cc `@b-naber` I think who touched this last in 705d818
2 parents ef81fca + 91e91d8 commit 4ce1b0f

File tree

4 files changed

+322
-21
lines changed

4 files changed

+322
-21
lines changed

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

+14-20
Original file line numberDiff line numberDiff line change
@@ -80,31 +80,25 @@ impl<'tcx> ValTree<'tcx> {
8080
}
8181

8282
/// Get the values inside the ValTree as a slice of bytes. This only works for
83-
/// constants with types &str and &[u8].
83+
/// constants with types &str, &[u8], or [u8; _].
8484
pub fn try_to_raw_bytes(self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<&'tcx [u8]> {
8585
match ty.kind() {
8686
ty::Ref(_, inner_ty, _) => match inner_ty.kind() {
87-
ty::Str => {
88-
let leafs = self
89-
.unwrap_branch()
90-
.into_iter()
91-
.map(|v| v.unwrap_leaf().try_to_u8().unwrap());
92-
93-
return Some(tcx.arena.alloc_from_iter(leafs));
94-
}
95-
ty::Slice(slice_ty) if *slice_ty == tcx.types.u8 => {
96-
let leafs = self
97-
.unwrap_branch()
98-
.into_iter()
99-
.map(|v| v.unwrap_leaf().try_to_u8().unwrap());
100-
101-
return Some(tcx.arena.alloc_from_iter(leafs));
102-
}
103-
_ => {}
87+
// `&str` can be interpreted as raw bytes
88+
ty::Str => {}
89+
// `&[u8]` can be interpreted as raw bytes
90+
ty::Slice(slice_ty) if *slice_ty == tcx.types.u8 => {}
91+
// other `&_` can't be interpreted as raw bytes
92+
_ => return None,
10493
},
105-
_ => {}
94+
// `[u8; N]` can be interpreted as raw bytes
95+
ty::Array(array_ty, _) if *array_ty == tcx.types.u8 => {}
96+
// Otherwise, type cannot be interpreted as raw bytes
97+
_ => return None,
10698
}
10799

108-
None
100+
Some(tcx.arena.alloc_from_iter(
101+
self.unwrap_branch().into_iter().map(|v| v.unwrap_leaf().try_to_u8().unwrap()),
102+
))
109103
}
110104
}

compiler/rustc_middle/src/ty/print/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1452,7 +1452,7 @@ pub trait PrettyPrinter<'tcx>:
14521452
}
14531453
},
14541454
(ty::ValTree::Branch(_), ty::Array(t, _)) if *t == u8_type => {
1455-
let bytes = valtree.try_to_raw_bytes(self.tcx(), *t).unwrap_or_else(|| {
1455+
let bytes = valtree.try_to_raw_bytes(self.tcx(), ty).unwrap_or_else(|| {
14561456
bug!("expected to convert valtree to raw bytes for type {:?}", t)
14571457
});
14581458
p!("*");

src/test/mir-opt/issue-99325.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(adt_const_params)]
2+
#![allow(incomplete_features)]
3+
4+
pub fn function_with_bytes<const BYTES: &'static [u8; 4]>() -> &'static [u8] {
5+
BYTES
6+
}
7+
8+
// EMIT_MIR issue_99325.main.mir_map.0.mir
9+
pub fn main() {
10+
assert_eq!(function_with_bytes::<b"AAAA">(), &[0x41, 0x41, 0x41, 0x41]);
11+
assert_eq!(function_with_bytes::<{ &[0x41, 0x41, 0x41, 0x41] }>(), b"AAAA");
12+
}

0 commit comments

Comments
 (0)