Skip to content

Commit b893286

Browse files
committed
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. As per rust-lang#4229
1 parent 8554d5e commit b893286

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/librustc/middle/trans/base.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,10 @@ fn iter_structural_ty(cx: block, av: ValueRef, t: ty::t,
545545
j += 1u;
546546
}
547547
}
548-
_ => cx.tcx().sess.bug(~"iter_variant: not a function type")
548+
_ => cx.tcx().sess.bug(fmt!("iter_variant: not a function type: \
549+
%s (variant name = %s)",
550+
cx.ty_to_str(fn_ty),
551+
cx.sess().str_of(variant.name)))
549552
}
550553
return cx;
551554
}

src/librustc/middle/typeck/collect.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,26 @@ fn get_enum_variant_types(ccx: @crate_ctxt,
158158
result_ty = Some(enum_ty);
159159
}
160160
ast::struct_variant_kind(struct_def) => {
161-
result_ty = Some(enum_ty);
162161
// XXX: Merge with computation of the the same value below?
163162
let tpt = {bounds: ty_param_bounds(ccx, ty_params),
164163
region_param: rp,
165164
ty: enum_ty};
166165
convert_struct(
167166
ccx, rp, struct_def, ty_params, tpt, variant.node.id);
167+
// Compute the ctor arg types from the struct fields
168+
let struct_fields = do struct_def.fields.map |struct_field| {
169+
{mode: ast::expl(ast::by_val),
170+
ty: ty::node_id_to_type(ccx.tcx, (*struct_field).node.id)
171+
}
172+
};
173+
result_ty = Some(ty::mk_fn(tcx, FnTyBase {
174+
meta: FnMeta {purity: ast::pure_fn,
175+
proto: ast::ProtoBare,
176+
onceness: ast::Many,
177+
bounds: @~[],
178+
region: ty::re_static,
179+
ret_style: ast::return_val},
180+
sig: FnSig {inputs: struct_fields, output: enum_ty }}));
168181
}
169182
ast::enum_variant_kind(ref enum_definition) => {
170183
get_enum_variant_types(ccx,

src/test/run-pass/enum-variants.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
enum Animal {
2+
Dog (~str, float),
3+
Cat { name: ~str, weight: float }
4+
}
5+
6+
fn main() {
7+
let mut a: Animal = Dog(~"Cocoa", 37.2);
8+
a = Cat{ name: ~"Spotty", weight: 2.7 };
9+
}

0 commit comments

Comments
 (0)