Skip to content

Commit 88487d8

Browse files
committed
auto merge of #7557 : michaelwoerister/rust/enum_structs, r=pcwalton
After getting an ICE trying to use the `Repr` enum from middle::trans::adt (see issue #7527), I tried to implement the missing case for struct-like enum variants in `middle::ty::enum_variants()`. It seems to work now (and passes make check) but there are still some uncertainties that bother me: + I'm not sure I did everything, right. Especially getting the variant constructor function from the variant node id is just copied from the tuple-variant case. Someone with more experience in the code base should be able to see rather quickly whether this OK so. + It is kind of strange that I could not reproduce the ICE with a smaller test case. The unimplemented code path never seems to be hit in most cases, even when using the exact same `Repr` enum, just with `ty::t` replaced by an opaque pointer. Also, within the `adt` module, `Repr` and matching on it is used multiple times, again without running into problems. Can anyone explain why this is the case? That would be much appreciated. Apart from that, I hope this PR is useful.
2 parents b168252 + 866a5b1 commit 88487d8

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
@@ -3828,41 +3828,62 @@ pub fn enum_variants(cx: ctxt, id: ast::def_id) -> @~[VariantInfo] {
38283828
}, _) => {
38293829
let mut disr_val = -1;
38303830
@enum_definition.variants.iter().transform(|variant| {
3831+
3832+
let ctor_ty = node_id_to_type(cx, variant.node.id);
3833+
38313834
match variant.node.kind {
38323835
ast::tuple_variant_kind(ref args) => {
3833-
let ctor_ty = node_id_to_type(cx, variant.node.id);
3834-
let arg_tys = {
3835-
if args.len() > 0u {
3836-
ty_fn_args(ctor_ty).map(|a| *a)
3837-
} else {
3836+
let arg_tys = if args.len() > 0u {
3837+
ty_fn_args(ctor_ty).map(|a| *a) }
3838+
else {
38383839
~[]
3839-
}
3840-
};
3840+
};
3841+
38413842
match variant.node.disr_expr {
38423843
Some (ex) => {
38433844
disr_val = match const_eval::eval_const_expr(cx,
38443845
ex) {
38453846
const_eval::const_int(val) => val as int,
3846-
_ => cx.sess.bug("tag_variants: bad disr expr")
3847+
_ => cx.sess.bug("enum_variants: bad disr expr")
38473848
}
38483849
}
38493850
_ => disr_val += 1
38503851
}
3851-
@VariantInfo_{args: arg_tys,
3852-
ctor_ty: ctor_ty,
3853-
name: variant.node.name,
3854-
id: ast_util::local_def(variant.node.id),
3855-
disr_val: disr_val,
3856-
vis: variant.node.vis
3852+
@VariantInfo_{
3853+
args: arg_tys,
3854+
ctor_ty: ctor_ty,
3855+
name: variant.node.name,
3856+
id: ast_util::local_def(variant.node.id),
3857+
disr_val: disr_val,
3858+
vis: variant.node.vis
38573859
}
3858-
}
3859-
ast::struct_variant_kind(_) => {
3860-
fail!("struct variant kinds unimpl in enum_variants")
3860+
},
3861+
ast::struct_variant_kind(struct_def) => {
3862+
let arg_tys =
3863+
// Is this check needed for structs too, or are they always guaranteed
3864+
// to have a valid constructor function?
3865+
if struct_def.fields.len() > 0 {
3866+
ty_fn_args(ctor_ty).map(|a| *a)
3867+
} else {
3868+
~[]
3869+
};
3870+
3871+
assert!(variant.node.disr_expr.is_none());
3872+
disr_val += 1;
3873+
3874+
@VariantInfo_{
3875+
args: arg_tys,
3876+
ctor_ty: ctor_ty,
3877+
name: variant.node.name,
3878+
id: ast_util::local_def(variant.node.id),
3879+
disr_val: disr_val,
3880+
vis: variant.node.vis
3881+
}
38613882
}
38623883
}
38633884
}).collect()
38643885
}
3865-
_ => cx.sess.bug("tag_variants: id not bound to an enum")
3886+
_ => cx.sess.bug("enum_variants: id not bound to an enum")
38663887
}
38673888
};
38683889
cx.enum_var_cache.insert(id, result);

0 commit comments

Comments
 (0)