Skip to content

Make transmuting inhabited to uninhabited types an error #36489

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 5 additions & 2 deletions src/librustc/middle/intrinsicck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ impl<'a, 'gcx, 'tcx> ExprVisitor<'a, 'gcx, 'tcx> {
let sk_from = SizeSkeleton::compute(from, self.infcx);
let sk_to = SizeSkeleton::compute(to, self.infcx);

// Check for same size using the skeletons.
// Check for transmutable sizes using the skeletons.
if let (Ok(sk_from), Ok(sk_to)) = (sk_from, sk_to) {
if sk_from.same_size(sk_to) {
if sk_from.transmutable(sk_to) {
return;
}

Expand All @@ -86,6 +86,9 @@ impl<'a, 'gcx, 'tcx> ExprVisitor<'a, 'gcx, 'tcx> {
// Try to display a sensible error with as much information as possible.
let skeleton_string = |ty: Ty<'gcx>, sk| {
match sk {
Ok(SizeSkeleton::Uninhabited) => {
format!("uninhabited")
},
Ok(SizeSkeleton::Known(size)) => {
format!("{} bits", size.bits())
}
Expand Down
16 changes: 14 additions & 2 deletions src/librustc/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,9 @@ impl<'a, 'gcx, 'tcx> Layout {
/// enough to statically check common usecases of transmute.
#[derive(Copy, Clone, Debug)]
pub enum SizeSkeleton<'tcx> {
/// The type is uninhabited.
Uninhabited,

/// Any statically computable Layout.
Known(Size),

Expand All @@ -1276,7 +1279,12 @@ impl<'a, 'gcx, 'tcx> SizeSkeleton<'gcx> {
let tcx = infcx.tcx.global_tcx();
assert!(!ty.has_infer_types());

// First try computing a static layout.
// First check if the type is inhabited.
if ty.is_uninhabited(tcx) {
return Ok(SizeSkeleton::Uninhabited);
}

// Try computing a static layout.
let err = match ty.layout(infcx) {
Ok(layout) => {
return Ok(SizeSkeleton::Known(layout.size(&tcx.data_layout)));
Expand Down Expand Up @@ -1321,6 +1329,7 @@ impl<'a, 'gcx, 'tcx> SizeSkeleton<'gcx> {
for field in fields {
let field = field?;
match field {
SizeSkeleton::Uninhabited => {}
SizeSkeleton::Known(size) => {
if size.bytes() > 0 {
return Err(err);
Expand Down Expand Up @@ -1378,8 +1387,11 @@ impl<'a, 'gcx, 'tcx> SizeSkeleton<'gcx> {
}
}

pub fn same_size(self, other: SizeSkeleton) -> bool {
/// Check whether it's possible to transmute from self to other.
pub fn transmutable(self, other: SizeSkeleton) -> bool {
match (self, other) {
(SizeSkeleton::Uninhabited, _) => true,
(_, SizeSkeleton::Uninhabited) => false,
(SizeSkeleton::Known(a), SizeSkeleton::Known(b)) => a == b,
(SizeSkeleton::Pointer { tail: a, .. },
SizeSkeleton::Pointer { tail: b, .. }) => a == b,
Expand Down