Skip to content

Commit 768cbbc

Browse files
committed
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.
1 parent 3bd4af8 commit 768cbbc

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-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+
Some(n) if n != 0 => ty.uninhabited_from(visited, tcx),
267+
// If the array is definitely non-empty, it's uninhabited if
268+
// the type of its elements is uninhabited.
269+
_ => DefIdForest::empty()
269270
}
270271
}
271272
TyRef(_, ref tm) => {
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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, []); //~ ERROR refutable pattern in local binding: `T(_, _)` not covered
21+
u
22+
}
23+
24+
fn main() {
25+
println!("{:?}", transmute::<&str, (*const u8, u64)>("type safety"));
26+
}

0 commit comments

Comments
 (0)