Skip to content

Commit ed7ca44

Browse files
committed
add tests
1 parent 351dfa2 commit ed7ca44

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

compiler/rustc_const_eval/src/interpret/operand.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,7 @@ pub(crate) fn type_is_scalar<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
281281
// FIXME: can these ever have Scalar ABI?
282282
ty::Closure(..) | ty::Generator(..) => false,
283283
// Types that don't have scalar layout to begin with.
284-
ty::Array(..) | ty::Slice(..) | ty::Str | ty::Dynamic(..) | ty::Foreign(..) => {
285-
false
286-
}
284+
ty::Array(..) | ty::Slice(..) | ty::Str | ty::Dynamic(..) | ty::Foreign(..) => false,
287285
// Types we should not uusally see here, but when called from CTFE op_to_const these can
288286
// actually happen.
289287
ty::Error(_)

src/test/ui/consts/issue-69488.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// run-pass
2+
3+
#![feature(const_ptr_write)]
4+
#![feature(const_fn_trait_bound)]
5+
#![feature(const_mut_refs)]
6+
7+
// Or, equivalently: `MaybeUninit`.
8+
pub union BagOfBits<T: Copy> {
9+
uninit: (),
10+
_storage: T,
11+
}
12+
13+
pub const fn make_1u8_bag<T: Copy>() -> BagOfBits<T> {
14+
assert!(core::mem::size_of::<T>() >= 1);
15+
let mut bag = BagOfBits { uninit: () };
16+
unsafe { (&mut bag as *mut _ as *mut u8).write(1); };
17+
bag
18+
}
19+
20+
pub fn check_bag<T: Copy>(bag: &BagOfBits<T>) {
21+
let val = unsafe { (bag as *const _ as *const u8).read() };
22+
assert_eq!(val, 1);
23+
}
24+
25+
fn main() {
26+
check_bag(&make_1u8_bag::<[usize; 1]>()); // Fine
27+
check_bag(&make_1u8_bag::<usize>()); // Fine
28+
29+
const CONST_ARRAY_BAG: BagOfBits<[usize; 1]> = make_1u8_bag();
30+
check_bag(&CONST_ARRAY_BAG); // Fine.
31+
const CONST_USIZE_BAG: BagOfBits<usize> = make_1u8_bag();
32+
check_bag(&CONST_USIZE_BAG); // Panics
33+
}

src/test/ui/consts/issue-94371.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// check-pass
2+
3+
#![feature(const_swap)]
4+
#![feature(const_mut_refs)]
5+
6+
#[repr(C)]
7+
struct Demo(u64, bool, u64, u32, u64, u64, u64);
8+
9+
const C: (Demo, Demo) = {
10+
let mut x = Demo(1, true, 3, 4, 5, 6, 7);
11+
let mut y = Demo(10, false, 12, 13, 14, 15, 16);
12+
std::mem::swap(&mut x, &mut y);
13+
(x, y)
14+
};
15+
16+
fn main() {}

0 commit comments

Comments
 (0)