Skip to content

Commit 00c61a8

Browse files
Move try_to_raw_bytes from ty::Valtree to ty::Value
Signed-off-by: FedericoBruzzone <[email protected]>
1 parent 6e0dfc8 commit 00c61a8

File tree

3 files changed

+36
-38
lines changed

3 files changed

+36
-38
lines changed

Diff for: compiler/rustc_middle/src/ty/consts/valtree.rs

+23-24
Original file line numberDiff line numberDiff line change
@@ -78,30 +78,6 @@ impl<'tcx> ValTree<'tcx> {
7878
Self::Branch(_) => None,
7979
}
8080
}
81-
82-
/// Get the values inside the ValTree as a slice of bytes. This only works for
83-
/// constants with types &str, &[u8], or [u8; _].
84-
pub fn try_to_raw_bytes(self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<&'tcx [u8]> {
85-
match ty.kind() {
86-
ty::Ref(_, inner_ty, _) => match inner_ty.kind() {
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,
93-
},
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,
98-
}
99-
100-
Some(
101-
tcx.arena
102-
.alloc_from_iter(self.unwrap_branch().into_iter().map(|v| v.unwrap_leaf().to_u8())),
103-
)
104-
}
10581
}
10682

10783
/// A type-level constant value.
@@ -143,6 +119,29 @@ impl<'tcx> Value<'tcx> {
143119
}
144120
self.valtree.try_to_scalar_int().map(|s| s.to_target_usize(tcx))
145121
}
122+
123+
/// Get the values inside the ValTree as a slice of bytes. This only works for
124+
/// constants with types &str, &[u8], or [u8; _].
125+
pub fn try_to_raw_bytes(self, tcx: TyCtxt<'tcx>) -> Option<&'tcx [u8]> {
126+
match self.ty.kind() {
127+
ty::Ref(_, inner_ty, _) => match inner_ty.kind() {
128+
// `&str` can be interpreted as raw bytes
129+
ty::Str => {}
130+
// `&[u8]` can be interpreted as raw bytes
131+
ty::Slice(slice_ty) if *slice_ty == tcx.types.u8 => {}
132+
// other `&_` can't be interpreted as raw bytes
133+
_ => return None,
134+
},
135+
// `[u8; N]` can be interpreted as raw bytes
136+
ty::Array(array_ty, _) if *array_ty == tcx.types.u8 => {}
137+
// Otherwise, type cannot be interpreted as raw bytes
138+
_ => return None,
139+
}
140+
141+
Some(tcx.arena.alloc_from_iter(
142+
self.valtree.unwrap_branch().into_iter().map(|v| v.unwrap_leaf().to_u8()),
143+
))
144+
}
146145
}
147146

148147
impl<'tcx> rustc_type_ir::inherent::ValueConst<TyCtxt<'tcx>> for Value<'tcx> {

Diff for: compiler/rustc_middle/src/ty/print/pretty.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -1801,21 +1801,19 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
18011801
match (cv.valtree, cv.ty.kind()) {
18021802
(ty::ValTree::Branch(_), ty::Ref(_, inner_ty, _)) => match inner_ty.kind() {
18031803
ty::Slice(t) if *t == u8_type => {
1804-
let bytes =
1805-
cv.valtree.try_to_raw_bytes(self.tcx(), cv.ty).unwrap_or_else(|| {
1806-
bug!(
1807-
"expected to convert valtree {:?} to raw bytes for type {:?}",
1808-
cv.valtree,
1809-
t
1810-
)
1811-
});
1804+
let bytes = cv.try_to_raw_bytes(self.tcx()).unwrap_or_else(|| {
1805+
bug!(
1806+
"expected to convert valtree {:?} to raw bytes for type {:?}",
1807+
cv.valtree,
1808+
t
1809+
)
1810+
});
18121811
return self.pretty_print_byte_str(bytes);
18131812
}
18141813
ty::Str => {
1815-
let bytes =
1816-
cv.valtree.try_to_raw_bytes(self.tcx(), cv.ty).unwrap_or_else(|| {
1817-
bug!("expected to convert valtree to raw bytes for type {:?}", cv.ty)
1818-
});
1814+
let bytes = cv.try_to_raw_bytes(self.tcx()).unwrap_or_else(|| {
1815+
bug!("expected to convert valtree to raw bytes for type {:?}", cv.ty)
1816+
});
18191817
p!(write("{:?}", String::from_utf8_lossy(bytes)));
18201818
return Ok(());
18211819
}
@@ -1827,7 +1825,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
18271825
}
18281826
},
18291827
(ty::ValTree::Branch(_), ty::Array(t, _)) if *t == u8_type => {
1830-
let bytes = cv.valtree.try_to_raw_bytes(self.tcx(), cv.ty).unwrap_or_else(|| {
1828+
let bytes = cv.try_to_raw_bytes(self.tcx()).unwrap_or_else(|| {
18311829
bug!("expected to convert valtree to raw bytes for type {:?}", t)
18321830
});
18331831
p!("*");

Diff for: compiler/rustc_symbol_mangling/src/v0.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,8 @@ impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
649649
// HACK(jaic1): hide the `str` type behind a reference
650650
// for the following transformation from valtree to raw bytes
651651
let ref_ty = Ty::new_imm_ref(tcx, tcx.lifetimes.re_static, ct_ty);
652-
let slice = valtree.try_to_raw_bytes(tcx, ref_ty).unwrap_or_else(|| {
652+
let cv = ty::Value { ty: ref_ty, valtree };
653+
let slice = cv.try_to_raw_bytes(tcx).unwrap_or_else(|| {
653654
bug!("expected to get raw bytes from valtree {:?} for type {:}", valtree, ct_ty)
654655
});
655656
let s = std::str::from_utf8(slice).expect("non utf8 str from MIR interpreter");

0 commit comments

Comments
 (0)