Skip to content

Commit

Permalink
Don't ICE in might_permit_raw_init if reference is polymorphic
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Feb 14, 2023
1 parent 0b439b1 commit 087a013
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
9 changes: 7 additions & 2 deletions compiler/rustc_const_eval/src/util/might_permit_raw_init.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustc_middle::ty::layout::{LayoutCx, LayoutOf, TyAndLayout};
use rustc_middle::ty::{ParamEnv, TyCtxt};
use rustc_middle::ty::{ParamEnv, TyCtxt, TypeVisitable};
use rustc_session::Limit;
use rustc_target::abi::{Abi, FieldsShape, InitKind, Scalar, Variants};

Expand Down Expand Up @@ -108,7 +108,12 @@ fn might_permit_raw_init_lax<'tcx>(

// Special magic check for references and boxes (i.e., special pointer types).
if let Some(pointee) = this.ty.builtin_deref(false) {
let pointee = cx.layout_of(pointee.ty).expect("need to be able to compute layouts");
let Ok(pointee) = cx.layout_of(pointee.ty) else {
// Reference is too polymorphic, it has a layout but the pointee does not.
// So we must assume that there may be some substitution that is valid.
assert!(pointee.ty.needs_subst());
return true;
};
// We need to ensure that the LLVM attributes `aligned` and `dereferenceable(size)` are satisfied.
if pointee.align.abi.bytes() > 1 {
// 0x01-filling is not aligned.
Expand Down
24 changes: 24 additions & 0 deletions tests/mir-opt/dont_yeet_assert.generic.InstCombine.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
- // MIR for `generic` before InstCombine
+ // MIR for `generic` after InstCombine

fn generic() -> () {
let mut _0: (); // return place in scope 0 at $DIR/dont_yeet_assert.rs:+0:21: +0:21
let _1: (); // in scope 0 at $DIR/dont_yeet_assert.rs:+1:5: +1:61

bb0: {
StorageLive(_1); // scope 0 at $DIR/dont_yeet_assert.rs:+1:5: +1:61
- _1 = assert_mem_uninitialized_valid::<&T>() -> bb1; // scope 0 at $DIR/dont_yeet_assert.rs:+1:5: +1:61
- // mir::Constant
- // + span: $DIR/dont_yeet_assert.rs:10:5: 10:59
- // + user_ty: UserType(0)
- // + literal: Const { ty: extern "rust-intrinsic" fn() {assert_mem_uninitialized_valid::<&T>}, val: Value(<ZST>) }
+ goto -> bb1; // scope 0 at $DIR/dont_yeet_assert.rs:+1:5: +1:61
}

bb1: {
StorageDead(_1); // scope 0 at $DIR/dont_yeet_assert.rs:+1:61: +1:62
_0 = const (); // scope 0 at $DIR/dont_yeet_assert.rs:+0:21: +2:2
return; // scope 0 at $DIR/dont_yeet_assert.rs:+2:2: +2:2
}
}

11 changes: 11 additions & 0 deletions tests/mir-opt/dont_yeet_assert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// compile-flags: --crate-type=lib
// unit-test: InstCombine

#![feature(core_intrinsics)]

// Want to make sure this assertion isn't compiled away in generic code.

// EMIT_MIR dont_yeet_assert.generic.InstCombine.diff
pub fn generic<T>() {
core::intrinsics::assert_mem_uninitialized_valid::<&T>();
}
8 changes: 8 additions & 0 deletions tests/ui/lint/invalid_value-polymorphic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// compile-flags: --crate-type=lib -Zmir-enable-passes=+InstCombine
// build-pass

#![feature(core_intrinsics)]

pub fn generic<T>() {
core::intrinsics::assert_mem_uninitialized_valid::<&T>();
}

0 comments on commit 087a013

Please sign in to comment.