Skip to content

Commit 7a3630b

Browse files
committed
Fix lit_to_const for CStr literals
This was a pre-existing bug that was not exposed by the test suite. We need a layer of branches for each level of the data, and since CStrs are a wrapper around a slice, we were missing a branch here.
1 parent 85f512b commit 7a3630b

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

compiler/rustc_mir_build/src/thir/constant.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ pub(crate) fn lit_to_const<'tcx>(
5858
(ast::LitKind::Byte(n), ty::Uint(ty::UintTy::U8)) => {
5959
ty::ValTree::from_scalar_int(tcx, n.into())
6060
}
61-
(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())
61+
(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+
{
63+
// A CStr is a newtype around a byte slice, so we create the inner slice here.
64+
// We need a branch for each "level" of the data structure.
65+
let bytes = ty::ValTree::from_raw_bytes(tcx, byte_sym.as_byte_str());
66+
ty::ValTree::from_branches(tcx, [bytes])
6367
}
6468
(ast::LitKind::Int(n, _), ty::Uint(ui)) if !neg => {
6569
let scalar_int = trunc(n.get(), *ui);

0 commit comments

Comments
 (0)