Auto merge of #150523 - Kivooeo:add-check-for-u8, r=BoxyUwU

add check for `u8`s in `lit_to_const`

Fixes rust-lang/rust#131052

r? BoxyUwU
This commit is contained in:
bors 2026-01-01 07:53:41 +00:00
commit b49c7d784e
5 changed files with 46 additions and 17 deletions

View file

@ -1,5 +1,5 @@
use rustc_abi::Size;
use rustc_ast::{self as ast};
use rustc_ast::{self as ast, UintTy};
use rustc_hir::LangItem;
use rustc_middle::bug;
use rustc_middle::mir::interpret::LitToConstInput;
@ -44,12 +44,14 @@ pub(crate) fn lit_to_const<'tcx>(
ty::ValTree::from_raw_bytes(tcx, str_bytes)
}
(ast::LitKind::ByteStr(byte_sym, _), ty::Ref(_, inner_ty, _))
if matches!(inner_ty.kind(), ty::Slice(_) | ty::Array(..)) =>
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())
}
(ast::LitKind::ByteStr(byte_sym, _), ty::Slice(_) | ty::Array(..))
if tcx.features().deref_patterns() =>
(ast::LitKind::ByteStr(byte_sym, _), ty::Slice(inner_ty) | ty::Array(inner_ty, _))
if tcx.features().deref_patterns()
&& let ty::Uint(UintTy::U8) = inner_ty.kind() =>
{
// 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>`.

View file

@ -0,0 +1,18 @@
// Check that a byte string literal to a const parameter with a non-u8
// element type isn't lowered to a ValTree with an incorrect type
#![feature(adt_const_params)]
#![feature(rustc_attrs)]
#[rustc_dump_predicates]
struct ConstBytes<const T: &'static [*mut u8; 3]>
//~^ ERROR rustc_dump_predicates
//~| NOTE Binder { value: ConstArgHasType(T/#0, &'static [*mut u8; 3_usize]), bound_vars: [] }
//~| NOTE Binder { value: TraitPredicate(<ConstBytes<{const error}> as std::marker::Sized>, polarity:Positive), bound_vars: [] }
where
ConstBytes<b"AAA">: Sized;
//~^ ERROR mismatched types
//~| NOTE expected `&[*mut u8; 3]`, found `&[u8; 3]`
//~| NOTE expected reference `&'static [*mut u8; 3]`
fn main() {}

View file

@ -0,0 +1,21 @@
error: rustc_dump_predicates
--> $DIR/byte-string-u8-validation.rs:8:1
|
LL | struct ConstBytes<const T: &'static [*mut u8; 3]>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: Binder { value: ConstArgHasType(T/#0, &'static [*mut u8; 3_usize]), bound_vars: [] }
= note: Binder { value: TraitPredicate(<ConstBytes<{const error}> as std::marker::Sized>, polarity:Positive), bound_vars: [] }
error[E0308]: mismatched types
--> $DIR/byte-string-u8-validation.rs:13:16
|
LL | ConstBytes<b"AAA">: Sized;
| ^^^^^^ expected `&[*mut u8; 3]`, found `&[u8; 3]`
|
= note: expected reference `&'static [*mut u8; 3]`
found reference `&'static [u8; 3]`
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0308`.

View file

@ -9,5 +9,4 @@ pub fn main() {
let _: ConstBytes<b"AAA"> = ConstBytes::<b"BBB">;
//~^ ERROR mismatched types
//~| ERROR mismatched types
//~| ERROR mismatched types
}

View file

@ -7,17 +7,6 @@ LL | struct ConstBytes<const T: &'static [*mut u8; 3]>;
= note: `*mut u8` must implement `ConstParamTy_`, but it does not
= note: `[*mut u8; 3]` must implement `ConstParamTy_`, but it does not
error[E0308]: mismatched types
--> $DIR/mismatch-raw-ptr-in-adt.rs:9:33
|
LL | let _: ConstBytes<b"AAA"> = ConstBytes::<b"BBB">;
| ------------------ ^^^^^^^^^^^^^^^^^^^^ expected `&[65, 65, 65]`, found `&[66, 66, 66]`
| |
| expected due to this
|
= note: expected struct `ConstBytes<&[65, 65, 65]>`
found struct `ConstBytes<&[66, 66, 66]>`
error[E0308]: mismatched types
--> $DIR/mismatch-raw-ptr-in-adt.rs:9:46
|
@ -36,7 +25,7 @@ LL | let _: ConstBytes<b"AAA"> = ConstBytes::<b"BBB">;
= note: expected reference `&'static [*mut u8; 3]`
found reference `&'static [u8; 3]`
error: aborting due to 4 previous errors
error: aborting due to 3 previous errors
Some errors have detailed explanations: E0308, E0741.
For more information about an error, try `rustc --explain E0308`.