Skip to content

Commit abae612

Browse files
committed
Allow consts to be non-nullary enum constructors
1 parent f76e28a commit abae612

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/librustc/middle/check_const.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,12 @@ fn check_expr(sess: Session, def_map: resolve::DefMap,
138138
expr_call(callee, _, false) => {
139139
match def_map.find(callee.id) {
140140
Some(def_struct(*)) => {} // OK.
141+
Some(def_variant(*)) => {} // OK.
141142
_ => {
142143
sess.span_err(
143144
e.span,
144145
~"function calls in constants are limited to \
145-
structure constructors");
146+
struct and enum constructors");
146147
}
147148
}
148149
}

src/librustc/middle/trans/consts.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,24 @@ fn const_expr(cx: @crate_ctxt, e: @ast::expr) -> ValueRef {
454454
C_named_struct(llty, ~[ llstructbody ])
455455
}
456456
}
457+
Some(ast::def_variant(tid, vid)) => {
458+
let ety = ty::expr_ty(cx.tcx, e);
459+
let degen = ty::enum_is_univariant(cx.tcx, tid);
460+
let size = shape::static_size_of_enum(cx, ety);
461+
462+
let discrim = base::get_discrim_val(cx, e.span, tid, vid);
463+
let c_args = C_struct(args.map(|a| const_expr(cx, *a)));
464+
465+
let fields = if !degen {
466+
~[discrim, c_args]
467+
} else if size == 0 {
468+
~[discrim]
469+
} else {
470+
~[c_args]
471+
};
472+
473+
C_struct(fields)
474+
}
457475
_ => cx.sess.span_bug(e.span, ~"expected a struct def")
458476
}
459477
}

0 commit comments

Comments
 (0)