Skip to content

Commit ad27e26

Browse files
committed
librustc: Set enum discriminant only after field translation.
1 parent 27748b0 commit ad27e26

File tree

4 files changed

+20
-25
lines changed

4 files changed

+20
-25
lines changed

src/librustc/middle/trans/adt.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -674,11 +674,10 @@ pub fn trans_case<'a>(bcx: &'a Block<'a>, r: &Repr, discr: Disr)
674674
}
675675

676676
/**
677-
* Begin initializing a new value of the given case of the given
678-
* representation. The fields, if any, should then be initialized via
679-
* `trans_field_ptr`.
677+
* Set the discriminant for a new value of the given case of the given
678+
* representation.
680679
*/
681-
pub fn trans_start_init(bcx: &Block, r: &Repr, val: ValueRef, discr: Disr) {
680+
pub fn trans_set_discr(bcx: &Block, r: &Repr, val: ValueRef, discr: Disr) {
682681
match *r {
683682
CEnum(ity, min, max) => {
684683
assert_discr_in_range(ity, min, max, discr);

src/librustc/middle/trans/base.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1735,14 +1735,12 @@ pub fn trans_named_tuple_constructor<'a>(mut bcx: &'a Block<'a>,
17351735

17361736
if !type_is_zero_size(ccx, result_ty) {
17371737
let repr = adt::represent_type(ccx, result_ty);
1738-
adt::trans_start_init(bcx, &*repr, llresult, disr);
17391738

17401739
match args {
17411740
callee::ArgExprs(exprs) => {
1742-
for (i, expr) in exprs.iter().enumerate() {
1743-
let lldestptr = adt::trans_field_ptr(bcx, &*repr, llresult, disr, i);
1744-
bcx = expr::trans_into(bcx, *expr, expr::SaveIn(lldestptr));
1745-
}
1741+
let fields = exprs.iter().map(|x| *x).enumerate().collect::<Vec<_>>();
1742+
bcx = expr::trans_adt(bcx, &*repr, disr, fields.as_slice(),
1743+
None, expr::SaveIn(llresult));
17461744
}
17471745
_ => ccx.sess().bug("expected expr as arguments for variant/struct tuple constructor")
17481746
}
@@ -1800,7 +1798,6 @@ fn trans_enum_variant_or_tuple_like_struct(ccx: &CrateContext,
18001798

18011799
if !type_is_zero_size(fcx.ccx, result_ty) {
18021800
let repr = adt::represent_type(ccx, result_ty);
1803-
adt::trans_start_init(bcx, &*repr, fcx.llretptr.get().unwrap(), disr);
18041801
for (i, arg_datum) in arg_datums.move_iter().enumerate() {
18051802
let lldestptr = adt::trans_field_ptr(bcx,
18061803
&*repr,
@@ -1809,6 +1806,7 @@ fn trans_enum_variant_or_tuple_like_struct(ccx: &CrateContext,
18091806
i);
18101807
arg_datum.store_to(bcx, lldestptr);
18111808
}
1809+
adt::trans_set_discr(bcx, &*repr, fcx.llretptr.get().unwrap(), disr);
18121810
}
18131811

18141812
finish_fn(&fcx, bcx, result_ty);

src/librustc/middle/trans/closure.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,6 @@ pub fn trans_unboxed_closure<'a>(
502502
let repr = adt::represent_type(bcx.ccx(), node_id_type(bcx, id));
503503

504504
// Create the closure.
505-
adt::trans_start_init(bcx, &*repr, dest_addr, 0);
506505
for freevar in freevars_ptr.iter() {
507506
let datum = expr::trans_local_var(bcx, freevar.def);
508507
let upvar_slot_dest = adt::trans_field_ptr(bcx,
@@ -512,6 +511,7 @@ pub fn trans_unboxed_closure<'a>(
512511
0);
513512
bcx = datum.store_to(bcx, upvar_slot_dest);
514513
}
514+
adt::trans_set_discr(bcx, &*repr, dest_addr, 0);
515515

516516
bcx
517517
}

src/librustc/middle/trans/expr.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -876,8 +876,8 @@ fn trans_def_dps_unadjusted<'a>(
876876
// Nullary variant.
877877
let ty = expr_ty(bcx, ref_expr);
878878
let repr = adt::represent_type(bcx.ccx(), ty);
879-
adt::trans_start_init(bcx, &*repr, lldest,
880-
variant_info.disr_val);
879+
adt::trans_set_discr(bcx, &*repr, lldest,
880+
variant_info.disr_val);
881881
return bcx;
882882
}
883883
}
@@ -886,7 +886,7 @@ fn trans_def_dps_unadjusted<'a>(
886886
match ty::get(ty).sty {
887887
ty::ty_struct(did, _) if ty::has_dtor(bcx.tcx(), did) => {
888888
let repr = adt::represent_type(bcx.ccx(), ty);
889-
adt::trans_start_init(bcx, &*repr, lldest, 0);
889+
adt::trans_set_discr(bcx, &*repr, lldest, 0);
890890
}
891891
_ => {}
892892
}
@@ -1098,7 +1098,7 @@ fn trans_rec_or_struct<'a>(
10981098
* Note that `fields` may be empty; the base expression must always be
10991099
* evaluated for side-effects.
11001100
*/
1101-
struct StructBaseInfo {
1101+
pub struct StructBaseInfo {
11021102
/// The base expression; will be evaluated after all explicit fields.
11031103
expr: Gc<ast::Expr>,
11041104
/// The indices of fields to copy paired with their types.
@@ -1114,14 +1114,12 @@ struct StructBaseInfo {
11141114
* - `optbase` contains information on the base struct (if any) from
11151115
* which remaining fields are copied; see comments on `StructBaseInfo`.
11161116
*/
1117-
fn trans_adt<'a>(
1118-
bcx: &'a Block<'a>,
1119-
repr: &adt::Repr,
1120-
discr: ty::Disr,
1121-
fields: &[(uint, Gc<ast::Expr>)],
1122-
optbase: Option<StructBaseInfo>,
1123-
dest: Dest)
1124-
-> &'a Block<'a> {
1117+
pub fn trans_adt<'a>(bcx: &'a Block<'a>,
1118+
repr: &adt::Repr,
1119+
discr: ty::Disr,
1120+
fields: &[(uint, Gc<ast::Expr>)],
1121+
optbase: Option<StructBaseInfo>,
1122+
dest: Dest) -> &'a Block<'a> {
11251123
let _icx = push_ctxt("trans_adt");
11261124
let fcx = bcx.fcx;
11271125
let mut bcx = bcx;
@@ -1143,8 +1141,6 @@ fn trans_adt<'a>(
11431141
// failure occur before the ADT as a whole is ready.
11441142
let custom_cleanup_scope = fcx.push_custom_cleanup_scope();
11451143

1146-
adt::trans_start_init(bcx, repr, addr, discr);
1147-
11481144
for &(i, ref e) in fields.iter() {
11491145
let dest = adt::trans_field_ptr(bcx, repr, addr, discr, i);
11501146
let e_ty = expr_ty_adjusted(bcx, &**e);
@@ -1166,6 +1162,8 @@ fn trans_adt<'a>(
11661162
}
11671163
}
11681164

1165+
adt::trans_set_discr(bcx, repr, addr, discr);
1166+
11691167
fcx.pop_custom_cleanup_scope(custom_cleanup_scope);
11701168

11711169
return bcx;

0 commit comments

Comments
 (0)