Skip to content

Commit f3930aa

Browse files
authored
Auto merge of #38023 - arielb1:constant-evil-x2, r=eddyb
don't double-apply variant padding to const enums `build_const_struct` already returns the struct with padding - don't double-apply it in the `General` case. This should hopefully be the last time we have this sort of bug. Fixes #38002. Beta-nominating because regression. r? @eddyb
2 parents f8614c3 + fcebf3b commit f3930aa

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

Diff for: src/librustc_trans/adt.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -688,9 +688,8 @@ pub fn trans_const<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>, discr: D
688688
let lldiscr = C_integral(Type::from_integer(ccx, d), discr.0 as u64, true);
689689
let mut vals_with_discr = vec![lldiscr];
690690
vals_with_discr.extend_from_slice(vals);
691-
let mut contents = build_const_struct(ccx, &variant,
692-
&vals_with_discr[..]);
693-
let needed_padding = l.size(dl).bytes() - variant.min_size.bytes();
691+
let mut contents = build_const_struct(ccx, &variant, &vals_with_discr[..]);
692+
let needed_padding = l.size(dl).bytes() - variant.stride().bytes();
694693
if needed_padding > 0 {
695694
contents.push(padding(ccx, needed_padding));
696695
}
@@ -703,8 +702,7 @@ pub fn trans_const<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>, discr: D
703702
}
704703
layout::Univariant { ref variant, .. } => {
705704
assert_eq!(discr, Disr(0));
706-
let contents = build_const_struct(ccx,
707-
&variant, vals);
705+
let contents = build_const_struct(ccx, &variant, vals);
708706
C_struct(ccx, &contents[..], variant.packed)
709707
}
710708
layout::Vector { .. } => {
@@ -721,17 +719,15 @@ pub fn trans_const<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>, discr: D
721719
}
722720
layout::StructWrappedNullablePointer { ref nonnull, nndiscr, .. } => {
723721
if discr.0 == nndiscr {
724-
C_struct(ccx, &build_const_struct(ccx, &nonnull, vals),
725-
false)
722+
C_struct(ccx, &build_const_struct(ccx, &nonnull, vals), false)
726723
} else {
727724
let fields = compute_fields(ccx, t, nndiscr as usize, false);
728725
let vals = fields.iter().map(|&ty| {
729726
// Always use null even if it's not the `discrfield`th
730727
// field; see #8506.
731728
C_null(type_of::sizing_type_of(ccx, ty))
732729
}).collect::<Vec<ValueRef>>();
733-
C_struct(ccx, &build_const_struct(ccx, &nonnull, &vals[..]),
734-
false)
730+
C_struct(ccx, &build_const_struct(ccx, &nonnull, &vals[..]), false)
735731
}
736732
}
737733
_ => bug!("trans_const: cannot handle type {} repreented as {:#?}", t, l)

Diff for: src/test/run-pass/issue-38002.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2016 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+
// Check that constant ADTs are translated OK, part k of N.
12+
13+
#![feature(slice_patterns)]
14+
15+
enum Bar {
16+
C
17+
}
18+
19+
enum Foo {
20+
A {},
21+
B {
22+
y: usize,
23+
z: Bar
24+
},
25+
}
26+
27+
const LIST: [(usize, Foo); 2] = [
28+
(51, Foo::B { y: 42, z: Bar::C }),
29+
(52, Foo::B { y: 45, z: Bar::C }),
30+
];
31+
32+
pub fn main() {
33+
match LIST {
34+
[
35+
(51, Foo::B { y: 42, z: Bar::C }),
36+
(52, Foo::B { y: 45, z: Bar::C })
37+
] => {}
38+
_ => {
39+
// I would want to print the enum here, but if
40+
// the discriminant is garbage this causes an
41+
// `unreachable` and silent process exit.
42+
panic!("trivial match failed")
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)