Skip to content

Commit 63efff5

Browse files
committed
Auto merge of #47009 - eddyb:issue-46855, r=arielb1
rustc_trans: support ZST indexing involving uninhabited types. Fixes #46855 in a minimal way. I decided against supporting non-memory `Rvalue::Len` in this PR (see #46855 (comment)), as `PlaceContext::Inspect` is also used for `Rvalue::Discriminant`. r? @arielb1
2 parents 71ed31f + 57bb8ab commit 63efff5

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/librustc_trans/mir/operand.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,22 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
267267

268268
// Moves out of scalar and scalar pair fields are trivial.
269269
if let &mir::Place::Projection(ref proj) = place {
270-
if let mir::ProjectionElem::Field(ref f, _) = proj.elem {
271-
if let Some(o) = self.maybe_trans_consume_direct(bcx, &proj.base) {
272-
return Some(o.extract_field(bcx, f.index()));
270+
if let Some(o) = self.maybe_trans_consume_direct(bcx, &proj.base) {
271+
match proj.elem {
272+
mir::ProjectionElem::Field(ref f, _) => {
273+
return Some(o.extract_field(bcx, f.index()));
274+
}
275+
mir::ProjectionElem::Index(_) |
276+
mir::ProjectionElem::ConstantIndex { .. } => {
277+
// ZSTs don't require any actual memory access.
278+
// FIXME(eddyb) deduplicate this with the identical
279+
// checks in `trans_consume` and `extract_field`.
280+
let elem = o.layout.field(bcx.ccx, 0);
281+
if elem.is_zst() {
282+
return Some(OperandRef::new_zst(bcx.ccx, elem));
283+
}
284+
}
285+
_ => {}
273286
}
274287
}
275288
}

src/test/run-pass/issue-46855.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
// compile-flags: -Zmir-opt-level=1
12+
13+
#![feature(slice_patterns)]
14+
15+
use std::mem;
16+
17+
#[derive(Copy, Clone)]
18+
enum Never {}
19+
20+
union Foo {
21+
a: u64,
22+
b: Never
23+
}
24+
25+
fn foo(xs: [(Never, u32); 1]) -> u32 { xs[0].1 }
26+
27+
fn bar([(_, x)]: [(Never, u32); 1]) -> u32 { x }
28+
29+
fn main() {
30+
println!("{}", mem::size_of::<Foo>());
31+
32+
let f = [Foo { a: 42 }, Foo { a: 10 }];
33+
println!("{:?}", unsafe { f[0].a });
34+
}

0 commit comments

Comments
 (0)