Skip to content

Commit 9852b23

Browse files
authored
Rollup merge of rust-lang#47600 - varkor:empty-never-array, r=eddyb
Fix type inhabitedness check for arrays Arrays of uninhabited types were considered to also be uninhabited if their length had not been evaluated, causing unsoundness. Fixes rust-lang#47563. r? @eddyb
2 parents 31f1aa5 + c4d0bb3 commit 9852b23

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/librustc/ty/inhabitedness/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,11 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
262262
}))
263263
},
264264
TyArray(ty, len) => {
265-
if len.val.to_const_int().and_then(|i| i.to_u64()) == Some(0) {
266-
DefIdForest::empty()
267-
} else {
268-
ty.uninhabited_from(visited, tcx)
265+
match len.val.to_const_int().and_then(|i| i.to_u64()) {
266+
// If the array is definitely non-empty, it's uninhabited if
267+
// the type of its elements is uninhabited.
268+
Some(n) if n != 0 => ty.uninhabited_from(visited, tcx),
269+
_ => DefIdForest::empty()
269270
}
270271
}
271272
TyRef(_, ref tm) => {
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2017 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+
11+
#![feature(never_type)]
12+
13+
enum Helper<T, U> {
14+
T(T, [!; 0]),
15+
#[allow(dead_code)]
16+
U(U),
17+
}
18+
19+
fn transmute<T, U>(t: T) -> U {
20+
let Helper::U(u) = Helper::T(t, []);
21+
//~^ ERROR refutable pattern in local binding: `T(_, _)` not covered
22+
u
23+
}
24+
25+
fn main() {
26+
println!("{:?}", transmute::<&str, (*const u8, u64)>("type safety"));
27+
}

0 commit comments

Comments
 (0)