Skip to content

Commit

Permalink
Assign correct types to struct-like enum variant constructors
Browse files Browse the repository at this point in the history
Before, the type was just the enum type itself, which caused an
assertion failure in iter_variant in trans::base.

r=brson

Closes #4229
  • Loading branch information
catamorphism committed Dec 24, 2012
1 parent cf768ce commit 499a587
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/librustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,10 @@ fn iter_structural_ty(cx: block, av: ValueRef, t: ty::t,
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;
}
Expand Down
15 changes: 14 additions & 1 deletion src/librustc/middle/typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions src/test/run-pass/enum-variants.rs
Original file line number Diff line number Diff line change
@@ -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" };
}

0 comments on commit 499a587

Please sign in to comment.