Skip to content

Commit 31d1b7a

Browse files
committed
---
yaml --- r: 39190 b: refs/heads/incoming c: 8fa306a h: refs/heads/master v: v3
1 parent a790c0d commit 31d1b7a

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ refs/heads/try: 3d5418789064fdb463e872a4e651af1c628a3650
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: a810c03263670238bccd64cabb12a23a46e3a278
9-
refs/heads/incoming: 5b5a0df7ee46f4c465eb479a4ceb4a61d621bf5d
9+
refs/heads/incoming: 8fa306a0adb9ef3677bfad9bbd21bb620d4dc5b5
1010
refs/heads/dist-snap: 22efa39382d41b084fde1719df7ae8ce5697d8c9
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/incoming/src/librustc/middle/check_const.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,36 @@ fn check_expr(sess: Session, def_map: resolve::DefMap,
9595
match def_map.find(e.id) {
9696
Some(def_const(def_id)) |
9797
Some(def_fn(def_id, _)) |
98-
Some(def_variant(_, def_id)) => {
98+
Some(def_variant(_, def_id)) |
99+
Some(def_class(def_id)) => {
99100
if !ast_util::is_local(def_id) {
100101
sess.span_err(
101102
e.span, ~"paths in constants may only refer to \
102-
crate-local constants or functions");
103+
crate-local constants, functions, or \
104+
structs");
103105
}
104106
}
105-
_ => {
107+
Some(def) => {
108+
debug!("(checking const) found bad def: %?", def);
106109
sess.span_err(
107110
e.span,
108-
~"paths in constants may only refer to \
109-
constants or functions");
111+
fmt!("paths in constants may only refer to \
112+
constants or functions"));
110113
}
114+
None => {
115+
sess.span_bug(e.span, ~"unbound path in const?!");
116+
}
117+
}
118+
}
119+
expr_call(callee, _, false) => {
120+
match def_map.find(callee.id) {
121+
Some(def_class(*)) => {} // OK.
122+
_ => {
123+
sess.span_err(
124+
e.span,
125+
~"function calls in constants are limited to \
126+
structure constructors");
127+
}
111128
}
112129
}
113130
expr_paren(e) => { check_expr(sess, def_map, method_map,

branches/incoming/src/librustc/middle/trans/consts.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,22 @@ fn const_expr(cx: @crate_ctxt, e: @ast::expr) -> ValueRef {
416416
}
417417
}
418418
}
419-
ast::expr_paren(e) => { return const_expr(cx, e); }
419+
ast::expr_call(callee, args, _) => {
420+
match cx.tcx.def_map.find(callee.id) {
421+
Some(ast::def_class(def_id)) => {
422+
let ety = ty::expr_ty(cx.tcx, e);
423+
let llty = type_of::type_of(cx, ety);
424+
let llstructbody = C_struct(args.map(|a| const_expr(cx, *a)));
425+
if ty::ty_dtor(cx.tcx, def_id).is_present() {
426+
C_named_struct(llty, ~[ llstructbody, C_u8(0) ])
427+
} else {
428+
C_named_struct(llty, ~[ llstructbody ])
429+
}
430+
}
431+
_ => cx.sess.span_bug(e.span, ~"expected a struct def")
432+
}
433+
}
434+
ast::expr_paren(e) => { return const_expr(cx, e); }
420435
_ => cx.sess.span_bug(e.span,
421436
~"bad constant expression type in consts::const_expr")
422437
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
struct Bar(int, int);
2+
3+
const X: Bar = Bar(1, 2);
4+
5+
fn main() {
6+
match X {
7+
Bar(x, y) => {
8+
assert x == 1;
9+
assert y == 2;
10+
}
11+
}
12+
}
13+

0 commit comments

Comments
 (0)