Skip to content

Commit 1cf94f5

Browse files
committed
Use unions for uninhabitedness checking rather than mem::transmute
1 parent 7e14ffb commit 1cf94f5

File tree

3 files changed

+23
-31
lines changed

3 files changed

+23
-31
lines changed

src/librustc/ty/layout.rs

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

564-
let abi = if size != Size::ZERO && ty.conservative_is_uninhabited(tcx) {
564+
let abi = if count != 0 && ty.conservative_is_uninhabited(tcx) {
565565
Abi::Uninhabited
566566
} else {
567567
Abi::Aggregate { sized: true }
Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
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

133
use std::mem;
144

155
#[derive(Copy, Clone)]
166
enum Bar {}
177

18-
const BAD_BAD_BAD: Bar = unsafe { mem::transmute(()) };
19-
//~^ ERROR this constant cannot be used
8+
union TransmuteUnion<A: Clone + Copy, B: Clone + Copy> {
9+
a: A,
10+
b: B,
11+
}
12+
13+
const BAD_BAD_BAD: Bar = unsafe { (TransmuteUnion::<(), Bar> { a: () }).b };
14+
//~^ ERROR this constant likely exhibits undefined behavior
2015

2116
const BAD_BAD_REF: &Bar = unsafe { mem::transmute(1usize) };
2217
//~^ ERROR this constant likely exhibits undefined behavior
2318

24-
const BAD_BAD_ARRAY: [Bar; 1] = unsafe { mem::transmute(()) };
25-
//~^ ERROR this constant cannot be used
19+
const BAD_BAD_ARRAY: [Bar; 1] = unsafe { (TransmuteUnion::<(), [Bar; 1]> { a: () }).b };
20+
//~^ ERROR this constant likely exhibits undefined behavior
2621

27-
fn main() {
28-
}
22+
fn main() {}

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
1-
error: this constant cannot be used
2-
--> $DIR/ub-uninhabit.rs:18:1
1+
error[E0080]: this constant likely exhibits undefined behavior
2+
--> $DIR/ub-uninhabit.rs:13: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]: this constant likely exhibits undefined behavior
12-
--> $DIR/ub-uninhabit.rs:21:1
10+
--> $DIR/ub-uninhabit.rs:16:1
1311
|
1412
LL | const BAD_BAD_REF: &Bar = unsafe { mem::transmute(1usize) };
1513
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of an uninhabited type at .<deref>
1614
|
1715
= 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
1816

19-
error: this constant cannot be used
20-
--> $DIR/ub-uninhabit.rs:24:1
17+
error[E0080]: this constant likely exhibits undefined behavior
18+
--> $DIR/ub-uninhabit.rs:19: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)