Skip to content

Assign correct types to struct-like enum variant constructors #4233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 9 additions & 0 deletions src/test/run-pass/enum-variants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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 };
}