Skip to content

Commit 866a5b1

Browse files
Added support for struct-like enum variants in middle::ty::enum_variants().
1 parent ab34864 commit 866a5b1

File tree

1 file changed

+39
-18
lines changed

1 file changed

+39
-18
lines changed

src/librustc/middle/ty.rs

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3816,41 +3816,62 @@ pub fn enum_variants(cx: ctxt, id: ast::def_id) -> @~[VariantInfo] {
38163816
}, _) => {
38173817
let mut disr_val = -1;
38183818
@enum_definition.variants.iter().transform(|variant| {
3819+
3820+
let ctor_ty = node_id_to_type(cx, variant.node.id);
3821+
38193822
match variant.node.kind {
38203823
ast::tuple_variant_kind(ref args) => {
3821-
let ctor_ty = node_id_to_type(cx, variant.node.id);
3822-
let arg_tys = {
3823-
if args.len() > 0u {
3824-
ty_fn_args(ctor_ty).map(|a| *a)
3825-
} else {
3824+
let arg_tys = if args.len() > 0u {
3825+
ty_fn_args(ctor_ty).map(|a| *a) }
3826+
else {
38263827
~[]
3827-
}
3828-
};
3828+
};
3829+
38293830
match variant.node.disr_expr {
38303831
Some (ex) => {
38313832
disr_val = match const_eval::eval_const_expr(cx,
38323833
ex) {
38333834
const_eval::const_int(val) => val as int,
3834-
_ => cx.sess.bug("tag_variants: bad disr expr")
3835+
_ => cx.sess.bug("enum_variants: bad disr expr")
38353836
}
38363837
}
38373838
_ => disr_val += 1
38383839
}
3839-
@VariantInfo_{args: arg_tys,
3840-
ctor_ty: ctor_ty,
3841-
name: variant.node.name,
3842-
id: ast_util::local_def(variant.node.id),
3843-
disr_val: disr_val,
3844-
vis: variant.node.vis
3840+
@VariantInfo_{
3841+
args: arg_tys,
3842+
ctor_ty: ctor_ty,
3843+
name: variant.node.name,
3844+
id: ast_util::local_def(variant.node.id),
3845+
disr_val: disr_val,
3846+
vis: variant.node.vis
38453847
}
3846-
}
3847-
ast::struct_variant_kind(_) => {
3848-
fail!("struct variant kinds unimpl in enum_variants")
3848+
},
3849+
ast::struct_variant_kind(struct_def) => {
3850+
let arg_tys =
3851+
// Is this check needed for structs too, or are they always guaranteed
3852+
// to have a valid constructor function?
3853+
if struct_def.fields.len() > 0 {
3854+
ty_fn_args(ctor_ty).map(|a| *a)
3855+
} else {
3856+
~[]
3857+
};
3858+
3859+
assert!(variant.node.disr_expr.is_none());
3860+
disr_val += 1;
3861+
3862+
@VariantInfo_{
3863+
args: arg_tys,
3864+
ctor_ty: ctor_ty,
3865+
name: variant.node.name,
3866+
id: ast_util::local_def(variant.node.id),
3867+
disr_val: disr_val,
3868+
vis: variant.node.vis
3869+
}
38493870
}
38503871
}
38513872
}).collect()
38523873
}
3853-
_ => cx.sess.bug("tag_variants: id not bound to an enum")
3874+
_ => cx.sess.bug("enum_variants: id not bound to an enum")
38543875
}
38553876
};
38563877
cx.enum_var_cache.insert(id, result);

0 commit comments

Comments
 (0)