Skip to content

Commit b49c7d7

Browse files
committed
Auto merge of #150523 - Kivooeo:add-check-for-u8, r=BoxyUwU
add check for `u8`s in `lit_to_const` Fixes #131052 r? BoxyUwU
2 parents 6e48b44 + c6b03ae commit b49c7d7

File tree

5 files changed

+46
-17
lines changed

5 files changed

+46
-17
lines changed

‎compiler/rustc_mir_build/src/thir/constant.rs‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_abi::Size;
2-
use rustc_ast::{self as ast};
2+
use rustc_ast::{self as ast, UintTy};
33
use rustc_hir::LangItem;
44
use rustc_middle::bug;
55
use rustc_middle::mir::interpret::LitToConstInput;
@@ -44,12 +44,14 @@ pub(crate) fn lit_to_const<'tcx>(
4444
ty::ValTree::from_raw_bytes(tcx, str_bytes)
4545
}
4646
(ast::LitKind::ByteStr(byte_sym, _), ty::Ref(_, inner_ty, _))
47-
if matches!(inner_ty.kind(), ty::Slice(_) | ty::Array(..)) =>
47+
if let ty::Slice(ty) | ty::Array(ty, _) = inner_ty.kind()
48+
&& let ty::Uint(UintTy::U8) = ty.kind() =>
4849
{
4950
ty::ValTree::from_raw_bytes(tcx, byte_sym.as_byte_str())
5051
}
51-
(ast::LitKind::ByteStr(byte_sym, _), ty::Slice(_) | ty::Array(..))
52-
if tcx.features().deref_patterns() =>
52+
(ast::LitKind::ByteStr(byte_sym, _), ty::Slice(inner_ty) | ty::Array(inner_ty, _))
53+
if tcx.features().deref_patterns()
54+
&& let ty::Uint(UintTy::U8) = inner_ty.kind() =>
5355
{
5456
// Byte string literal patterns may have type `[u8]` or `[u8; N]` if `deref_patterns` is
5557
// enabled, in order to allow, e.g., `deref!(b"..."): Vec<u8>`.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Check that a byte string literal to a const parameter with a non-u8
2+
// element type isn't lowered to a ValTree with an incorrect type
3+
4+
#![feature(adt_const_params)]
5+
#![feature(rustc_attrs)]
6+
7+
#[rustc_dump_predicates]
8+
struct ConstBytes<const T: &'static [*mut u8; 3]>
9+
//~^ ERROR rustc_dump_predicates
10+
//~| NOTE Binder { value: ConstArgHasType(T/#0, &'static [*mut u8; 3_usize]), bound_vars: [] }
11+
//~| NOTE Binder { value: TraitPredicate(<ConstBytes<{const error}> as std::marker::Sized>, polarity:Positive), bound_vars: [] }
12+
where
13+
ConstBytes<b"AAA">: Sized;
14+
//~^ ERROR mismatched types
15+
//~| NOTE expected `&[*mut u8; 3]`, found `&[u8; 3]`
16+
//~| NOTE expected reference `&'static [*mut u8; 3]`
17+
18+
fn main() {}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error: rustc_dump_predicates
2+
--> $DIR/byte-string-u8-validation.rs:8:1
3+
|
4+
LL | struct ConstBytes<const T: &'static [*mut u8; 3]>
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: Binder { value: ConstArgHasType(T/#0, &'static [*mut u8; 3_usize]), bound_vars: [] }
8+
= note: Binder { value: TraitPredicate(<ConstBytes<{const error}> as std::marker::Sized>, polarity:Positive), bound_vars: [] }
9+
10+
error[E0308]: mismatched types
11+
--> $DIR/byte-string-u8-validation.rs:13:16
12+
|
13+
LL | ConstBytes<b"AAA">: Sized;
14+
| ^^^^^^ expected `&[*mut u8; 3]`, found `&[u8; 3]`
15+
|
16+
= note: expected reference `&'static [*mut u8; 3]`
17+
found reference `&'static [u8; 3]`
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0308`.

‎tests/ui/const-generics/adt_const_params/mismatch-raw-ptr-in-adt.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ pub fn main() {
99
let _: ConstBytes<b"AAA"> = ConstBytes::<b"BBB">;
1010
//~^ ERROR mismatched types
1111
//~| ERROR mismatched types
12-
//~| ERROR mismatched types
1312
}

‎tests/ui/const-generics/adt_const_params/mismatch-raw-ptr-in-adt.stderr‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,6 @@ LL | struct ConstBytes<const T: &'static [*mut u8; 3]>;
77
= note: `*mut u8` must implement `ConstParamTy_`, but it does not
88
= note: `[*mut u8; 3]` must implement `ConstParamTy_`, but it does not
99

10-
error[E0308]: mismatched types
11-
--> $DIR/mismatch-raw-ptr-in-adt.rs:9:33
12-
|
13-
LL | let _: ConstBytes<b"AAA"> = ConstBytes::<b"BBB">;
14-
| ------------------ ^^^^^^^^^^^^^^^^^^^^ expected `&[65, 65, 65]`, found `&[66, 66, 66]`
15-
| |
16-
| expected due to this
17-
|
18-
= note: expected struct `ConstBytes<&[65, 65, 65]>`
19-
found struct `ConstBytes<&[66, 66, 66]>`
20-
2110
error[E0308]: mismatched types
2211
--> $DIR/mismatch-raw-ptr-in-adt.rs:9:46
2312
|
@@ -36,7 +25,7 @@ LL | let _: ConstBytes<b"AAA"> = ConstBytes::<b"BBB">;
3625
= note: expected reference `&'static [*mut u8; 3]`
3726
found reference `&'static [u8; 3]`
3827

39-
error: aborting due to 4 previous errors
28+
error: aborting due to 3 previous errors
4029

4130
Some errors have detailed explanations: E0308, E0741.
4231
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)