Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix ICE on const eval of union field #47794

Merged
merged 2 commits into from
Jan 28, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/librustc_trans/mir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ impl<'a, 'tcx> Const<'tcx> {
}
}
_ => {
const_get_elt(self.llval, layout.llvm_field_index(i))
match layout.fields {
layout::FieldPlacement::Union(_) => self.llval,
_ => const_get_elt(self.llval, layout.llvm_field_index(i)),
}
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/test/run-pass/union/union-const-eval-field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(const_fn)]

union DummyUnion {
field1: i32,
field2: i32,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if you make them different types, say, i64 for the second field`, and maybe do some arithmetic on it?

}

const fn read_field() -> i32 {
const UNION: DummyUnion = DummyUnion { field1: 5 };
const FIELD: i32 = unsafe { UNION.field2 };
FIELD
}

fn main() {
assert_eq!(read_field(), 5);
}