Assign correct types to struct-like enum variant constructors

Before, the type was just the enum type itself, which caused an
assertion failure in iter_variant in trans::base.

r=brson

Closes #4229
This commit is contained in:
Tim Chevalier 2012-12-20 00:02:46 -07:00
parent cf768ce1f4
commit 499a58708f
3 changed files with 29 additions and 2 deletions

View File

@ -545,7 +545,10 @@ fn iter_variant(cx: block, a_tup: ValueRef,
j += 1u;
}
}
_ => cx.tcx().sess.bug(~"iter_variant: not a function type")
_ => cx.tcx().sess.bug(fmt!("iter_variant: not a function type: \
%s (variant name = %s)",
cx.ty_to_str(fn_ty),
cx.sess().str_of(variant.name)))
}
return cx;
}

View File

@ -158,13 +158,26 @@ fn get_enum_variant_types(ccx: @crate_ctxt,
result_ty = Some(enum_ty);
}
ast::struct_variant_kind(struct_def) => {
result_ty = Some(enum_ty);
// XXX: Merge with computation of the the same value below?
let tpt = {bounds: ty_param_bounds(ccx, ty_params),
region_param: rp,
ty: enum_ty};
convert_struct(
ccx, rp, struct_def, ty_params, tpt, variant.node.id);
// Compute the ctor arg types from the struct fields
let struct_fields = do struct_def.fields.map |struct_field| {
{mode: ast::expl(ast::by_val),
ty: ty::node_id_to_type(ccx.tcx, (*struct_field).node.id)
}
};
result_ty = Some(ty::mk_fn(tcx, FnTyBase {
meta: FnMeta {purity: ast::pure_fn,
proto: ast::ProtoBare,
onceness: ast::Many,
bounds: @~[],
region: ty::re_static,
ret_style: ast::return_val},
sig: FnSig {inputs: struct_fields, output: enum_ty }}));
}
ast::enum_variant_kind(ref enum_definition) => {
get_enum_variant_types(ccx,

View File

@ -0,0 +1,11 @@
enum Animal {
Dog (~str, float),
Cat { name: ~str, weight: float }
}
fn main() {
let mut a: Animal = Dog(~"Cocoa", 37.2);
a = Cat{ name: ~"Spotty", weight: 2.7 };
// permuting the fields should work too
let c = Cat { weight: 3.1, name: ~"Spreckles" };
}