Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Feb 27, 2022
1 parent 351dfa2 commit ed7ca44
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
4 changes: 1 addition & 3 deletions compiler/rustc_const_eval/src/interpret/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,7 @@ pub(crate) fn type_is_scalar<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
// FIXME: can these ever have Scalar ABI?
ty::Closure(..) | ty::Generator(..) => false,
// Types that don't have scalar layout to begin with.
ty::Array(..) | ty::Slice(..) | ty::Str | ty::Dynamic(..) | ty::Foreign(..) => {
false
}
ty::Array(..) | ty::Slice(..) | ty::Str | ty::Dynamic(..) | ty::Foreign(..) => false,
// Types we should not uusally see here, but when called from CTFE op_to_const these can
// actually happen.
ty::Error(_)
Expand Down
33 changes: 33 additions & 0 deletions src/test/ui/consts/issue-69488.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// run-pass

#![feature(const_ptr_write)]
#![feature(const_fn_trait_bound)]
#![feature(const_mut_refs)]

// Or, equivalently: `MaybeUninit`.
pub union BagOfBits<T: Copy> {
uninit: (),
_storage: T,
}

pub const fn make_1u8_bag<T: Copy>() -> BagOfBits<T> {
assert!(core::mem::size_of::<T>() >= 1);
let mut bag = BagOfBits { uninit: () };
unsafe { (&mut bag as *mut _ as *mut u8).write(1); };
bag
}

pub fn check_bag<T: Copy>(bag: &BagOfBits<T>) {
let val = unsafe { (bag as *const _ as *const u8).read() };
assert_eq!(val, 1);
}

fn main() {
check_bag(&make_1u8_bag::<[usize; 1]>()); // Fine
check_bag(&make_1u8_bag::<usize>()); // Fine

const CONST_ARRAY_BAG: BagOfBits<[usize; 1]> = make_1u8_bag();
check_bag(&CONST_ARRAY_BAG); // Fine.
const CONST_USIZE_BAG: BagOfBits<usize> = make_1u8_bag();
check_bag(&CONST_USIZE_BAG); // Panics
}
16 changes: 16 additions & 0 deletions src/test/ui/consts/issue-94371.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// check-pass

#![feature(const_swap)]
#![feature(const_mut_refs)]

#[repr(C)]
struct Demo(u64, bool, u64, u32, u64, u64, u64);

const C: (Demo, Demo) = {
let mut x = Demo(1, true, 3, 4, 5, 6, 7);
let mut y = Demo(10, false, 12, 13, 14, 15, 16);
std::mem::swap(&mut x, &mut y);
(x, y)
};

fn main() {}

0 comments on commit ed7ca44

Please sign in to comment.