Skip to content

Commit 9a22ff8

Browse files
committed
Fix lowering of CStr constants to valtrees
They need an extra branch for the newtype.
1 parent 124bfc9 commit 9a22ff8

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

compiler/rustc_mir_build/src/thir/constant.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ pub(crate) fn lit_to_const<'tcx>(
5959
ty::ValTree::from_scalar_int(tcx, n.into())
6060
}
6161
(ast::LitKind::CStr(byte_sym, _), ty::Ref(_, inner_ty, _)) if matches!(inner_ty.kind(), ty::Adt(def, _) if tcx.is_lang_item(def.did(), LangItem::CStr)) => {
62-
ty::ValTree::from_raw_bytes(tcx, byte_sym.as_byte_str())
62+
// A CStr is a newtype around a byte slice, so we create the inner slice here.
63+
// We need a branch for each "level" of the data structure.
64+
let bytes = ty::ValTree::from_raw_bytes(tcx, byte_sym.as_byte_str());
65+
ty::ValTree::from_branches(tcx, [bytes])
6366
}
6467
(ast::LitKind::Int(n, _), ty::Uint(ui)) if !neg => {
6568
let scalar_int = trunc(n.get(), *ui);

0 commit comments

Comments
 (0)