Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions compiler/rustc_mir_build/src/thir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,18 @@ pub(crate) fn lit_to_const<'tcx>(
let valtree_ty = Ty::new_imm_ref(tcx, tcx.lifetimes.re_static, tcx.types.str_);
(ty::ValTree::from_raw_bytes(tcx, str_bytes), valtree_ty)
}
(ast::LitKind::ByteStr(byte_sym, _), Some(ty::Ref(_, inner_ty, _)))
(ast::LitKind::ByteStr(byte_sym, _), Some(ty::Ref(region, inner_ty, mutbl)))
if let ty::Slice(ty) | ty::Array(ty, _) = inner_ty.kind()
&& let ty::Uint(UintTy::U8) = ty.kind() =>
{
(ty::ValTree::from_raw_bytes(tcx, byte_sym.as_byte_str()), expected_ty.unwrap())
let valtree_ty = if inner_ty.is_slice() {
expected_ty.unwrap()
} else {
let array_ty =
Ty::new_array(tcx, tcx.types.u8, byte_sym.as_byte_str().len() as u64);
Ty::new_ref(tcx, *region, array_ty, *mutbl)
};
(ty::ValTree::from_raw_bytes(tcx, byte_sym.as_byte_str()), valtree_ty)
}
(
ast::LitKind::ByteStr(byte_sym, _),
Expand All @@ -52,7 +59,12 @@ pub(crate) fn lit_to_const<'tcx>(
{
// Byte string literal patterns may have type `[u8]` or `[u8; N]` if `deref_patterns` is
// enabled, in order to allow, e.g., `deref!(b"..."): Vec<u8>`.
(ty::ValTree::from_raw_bytes(tcx, byte_sym.as_byte_str()), expected_ty.unwrap())
let valtree_ty = if expected_ty.unwrap().is_slice() {
expected_ty.unwrap()
} else {
Ty::new_array(tcx, tcx.types.u8, byte_sym.as_byte_str().len() as u64)
};
(ty::ValTree::from_raw_bytes(tcx, byte_sym.as_byte_str()), valtree_ty)
}
(ast::LitKind::ByteStr(byte_sym, _), _) => {
let valtree = ty::ValTree::from_raw_bytes(tcx, byte_sym.as_byte_str());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//! Regression test for #163904 (used to ICE)
#![feature(gca_min_const_items, gca_macroless_items)]

const X4: &[u8; usize::MAX] = b"";

@Suryansh-Dey Suryansh-Dey Oct 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used usize::MAX because the truncated number was differing in 32-bit system(CI) and 64-bit system(My laptop). With usize::MAX it prints usize::MAX instead of a platform dependent truncated number.

View changes since the review

//~^ ERROR the constant `&*b""` is not of type `&'static [u8; usize::MAX]`
static Y4: u8 = X4[0];

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: the constant `&*b""` is not of type `&'static [u8; usize::MAX]`
--> $DIR/byte-str-len-mismatch-ice-163904.rs:4:1
|
LL | const X4: &[u8; usize::MAX] = b"";
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&'static [u8; usize::MAX]`, found `&'static [u8; 0]`

error: aborting due to 1 previous error

Loading