Skip to content

Commit

Permalink
Rollup merge of rust-lang#64987 - oli-obk:code_reuse_prevents_bugs, r…
Browse files Browse the repository at this point in the history
…=eddyb

Compute the layout of uninhabited structs

fixes rust-lang#64506

r? @eddyb
  • Loading branch information
Centril committed Oct 14, 2019
2 parents 446e5e5 + 373c362 commit 9611993
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 18 deletions.
6 changes: 0 additions & 6 deletions src/librustc/mir/interpret/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,6 @@ pub enum UnsupportedOpInfo<'tcx> {
/// Free-form case. Only for errors that are never caught!
Unsupported(String),

/// FIXME(#64506) Error used to work around accessing projections of
/// uninhabited types.
UninhabitedValue,

// -- Everything below is not categorized yet --
FunctionAbiMismatch(Abi, Abi),
FunctionArgMismatch(Ty<'tcx>, Ty<'tcx>),
Expand Down Expand Up @@ -556,8 +552,6 @@ impl fmt::Debug for UnsupportedOpInfo<'tcx> {
not a power of two"),
Unsupported(ref msg) =>
write!(f, "{}", msg),
UninhabitedValue =>
write!(f, "tried to use an uninhabited value"),
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/librustc/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,10 +824,14 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
});
(present_variants.next(), present_variants.next())
};
if present_first.is_none() {
let present_first = match present_first {
present_first @ Some(_) => present_first,
// Uninhabited because it has no variants, or only absent ones.
return tcx.layout_raw(param_env.and(tcx.types.never));
}
None if def.is_enum() => return tcx.layout_raw(param_env.and(tcx.types.never)),
// if it's a struct, still compute a layout so that we can still compute the
// field offsets
None => Some(VariantIdx::new(0)),
};

let is_struct = !def.is_enum() ||
// Only one variant is present.
Expand Down
13 changes: 5 additions & 8 deletions src/librustc_mir/interpret/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustc::mir;
use rustc::mir::interpret::truncate;
use rustc::ty::{self, Ty};
use rustc::ty::layout::{
self, Size, Abi, Align, LayoutOf, TyLayout, HasDataLayout, VariantIdx, PrimitiveExt
self, Size, Align, LayoutOf, TyLayout, HasDataLayout, VariantIdx, PrimitiveExt
};
use rustc::ty::TypeFoldable;

Expand Down Expand Up @@ -377,20 +377,17 @@ where
layout::FieldPlacement::Array { stride, .. } => {
let len = base.len(self)?;
if field >= len {
// This can be violated because this runs during promotion on code where the
// type system has not yet ensured that such things don't happen.
// This can be violated because the index (field) can be a runtime value
// provided by the user.
debug!("tried to access element {} of array/slice with length {}", field, len);
throw_panic!(BoundsCheck { len, index: field });
}
stride * field
}
layout::FieldPlacement::Union(count) => {
// FIXME(#64506) `UninhabitedValue` can be removed when this issue is resolved
if base.layout.abi == Abi::Uninhabited {
throw_unsup!(UninhabitedValue);
}
assert!(field < count as u64,
"Tried to access field {} of union with {} fields", field, count);
"Tried to access field {} of union {:#?} with {} fields",
field, base.layout, count);
// Offset is always 0
Size::from_bytes(0)
}
Expand Down
6 changes: 5 additions & 1 deletion src/librustc_target/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,11 @@ impl FieldPlacement {

pub fn offset(&self, i: usize) -> Size {
match *self {
FieldPlacement::Union(_) => Size::ZERO,
FieldPlacement::Union(count) => {
assert!(i < count,
"Tried to access field {} of union with {} fields", i, count);
Size::ZERO
},
FieldPlacement::Array { stride, count } => {
let i = i as u64;
assert!(i < count);
Expand Down
20 changes: 20 additions & 0 deletions src/test/ui/consts/issue-64506.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// check-pass

#[derive(Copy, Clone)]
pub struct ChildStdin {
inner: AnonPipe,
}

#[derive(Copy, Clone)]
enum AnonPipe {}

const FOO: () = {
union Foo {
a: ChildStdin,
b: (),
}
let x = unsafe { Foo { b: () }.a };
let x = &x.inner;
};

fn main() {}

0 comments on commit 9611993

Please sign in to comment.