Skip to content

Commit 6e5e54f

Browse files
committed
Use unions for uninhabitedness checking rather than mem::transmute
1 parent 51e1c64 commit 6e5e54f

File tree

3 files changed

+18
-26
lines changed

3 files changed

+18
-26
lines changed

src/librustc/ty/layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
557557
let size = element.size.checked_mul(count, dl)
558558
.ok_or(LayoutError::SizeOverflow(ty))?;
559559

560-
let abi = if size != Size::ZERO && ty.conservative_is_uninhabited(tcx) {
560+
let abi = if count != 0 && ty.conservative_is_uninhabited(tcx) {
561561
Abi::Uninhabited
562562
} else {
563563
Abi::Aggregate { sized: true }
Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2-
// file at the top-level directory of this distribution and at
3-
// http://rust-lang.org/COPYRIGHT.
4-
//
5-
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6-
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7-
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8-
// option. This file may not be copied, modified, or distributed
9-
// except according to those terms.
10-
111
#![feature(const_transmute)]
122
#![allow(const_err)] // make sure we cannot allow away the errors tested here
133

@@ -16,14 +6,18 @@ use std::mem;
166
#[derive(Copy, Clone)]
177
enum Bar {}
188

19-
const BAD_BAD_BAD: Bar = unsafe { mem::transmute(()) };
20-
//~^ ERROR it is undefined behavior to use this value
9+
union TransmuteUnion<A: Clone + Copy, B: Clone + Copy> {
10+
a: A,
11+
b: B,
12+
}
13+
14+
const BAD_BAD_BAD: Bar = unsafe { (TransmuteUnion::<(), Bar> { a: () }).b };
15+
//~^ ERROR this constant likely exhibits undefined behavior
2116

2217
const BAD_BAD_REF: &Bar = unsafe { mem::transmute(1usize) };
2318
//~^ ERROR it is undefined behavior to use this value
2419

25-
const BAD_BAD_ARRAY: [Bar; 1] = unsafe { mem::transmute(()) };
26-
//~^ ERROR it is undefined behavior to use this value
20+
const BAD_BAD_ARRAY: [Bar; 1] = unsafe { (TransmuteUnion::<(), [Bar; 1]> { a: () }).b };
21+
//~^ ERROR this constant likely exhibits undefined behavior
2722

28-
fn main() {
29-
}
23+
fn main() {}

src/test/ui/consts/const-eval/ub-uninhabit.stderr

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
error[E0080]: it is undefined behavior to use this value
22
--> $DIR/ub-uninhabit.rs:19:1
33
|
4-
LL | const BAD_BAD_BAD: Bar = unsafe { mem::transmute(()) };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------------------^^^
6-
| |
7-
| entered unreachable code
4+
LL | const BAD_BAD_BAD: Bar = unsafe { (TransmuteUnion::<(), Bar> { a: () }).b };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of an uninhabited type
86
|
9-
= note: #[deny(const_err)] on by default
7+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
108

119
error[E0080]: it is undefined behavior to use this value
1210
--> $DIR/ub-uninhabit.rs:22:1
@@ -19,10 +17,10 @@ LL | const BAD_BAD_REF: &Bar = unsafe { mem::transmute(1usize) };
1917
error[E0080]: it is undefined behavior to use this value
2018
--> $DIR/ub-uninhabit.rs:25:1
2119
|
22-
LL | const BAD_BAD_ARRAY: [Bar; 1] = unsafe { mem::transmute(()) };
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------------------^^^
24-
| |
25-
| entered unreachable code
20+
LL | const BAD_BAD_ARRAY: [Bar; 1] = unsafe { (TransmuteUnion::<(), [Bar; 1]> { a: () }).b };
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of an uninhabited type
22+
|
23+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
2624

2725
error: aborting due to 3 previous errors
2826

0 commit comments

Comments
 (0)