Skip to content

Commit bef0aa9

Browse files
committed
---
yaml --- r: 41702 b: refs/heads/master c: abae612 h: refs/heads/master v: v3
1 parent 565e1ff commit bef0aa9

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: f76e28aa1c093c59671749006e1a7ae203d66b7c
2+
refs/heads/master: abae61257c4d866bb321bfb80ad16b7531736f7e
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 2f46b763da2c098913884f101b6d71d69af41b49
55
refs/heads/try: 3d5418789064fdb463e872a4e651af1c628a3650

trunk/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
}

trunk/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)