Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compute the layout of uninhabited structs #64987

Merged
merged 4 commits into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
16 changes: 12 additions & 4 deletions src/librustc/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,10 +825,18 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
});
(present_variants.next(), present_variants.next())
};
if present_first.is_none() {
// Uninhabited because it has no variants, or only absent ones.
return tcx.layout_raw(param_env.and(tcx.types.never));
}
let present_first = if present_first.is_none() {
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
if def.is_enum() {
// Uninhabited because it has no variants, or only absent ones.
return tcx.layout_raw(param_env.and(tcx.types.never));
} else {
// if it's a struct, still compute a layout so that we can still compute the
// field offsets
Some(VariantIdx::new(0))
}
} else {
present_first
};

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.
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add this assert to FieldPlacement::offset? And even add an offset_u64, if you want (I guess offset(i) can just call offset_u64(i as u64)? or maybe we just want an i: impl Into<u64> on offset).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

u64 does not implement From<usize> :(

// Offset is always 0
Size::from_bytes(0)
}
Expand Down
20 changes: 20 additions & 0 deletions src/test/ui/issues/issue-64506.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// check-pass
oli-obk marked this conversation as resolved.
Show resolved Hide resolved

#[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() {}