Skip to content

Commit 6e130cc

Browse files
committed
---
yaml --- r: 31722 b: refs/heads/dist-snap c: 175be53 h: refs/heads/master v: v3
1 parent 46a3ac4 commit 6e130cc

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: d0c6ce338884ee21843f4b40bf6bf18d222ce5df
99
refs/heads/incoming: d9317a174e434d4c99fc1a37fd7dc0d2f5328d37
10-
refs/heads/dist-snap: 4be8239ac2fe2209dd4f19279151aa18792e6c4a
10+
refs/heads/dist-snap: 175be53e3f62664e445a57c2ad9e23f4bcb296b2
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/dist-snap/src/rustc/middle/check_const.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ fn check_expr(sess: session, def_map: resolve3::DefMap,
103103
expr_vec(_, m_imm) |
104104
expr_addr_of(m_imm, _) |
105105
expr_tup(*) |
106+
expr_struct(*) |
106107
expr_rec(*) => { }
107108
expr_addr_of(*) => {
108109
sess.span_err(

branches/dist-snap/src/rustc/middle/const_eval.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ fn classify(e: @expr,
9595
}
9696
}
9797

98+
ast::expr_struct(_, fs, none) |
9899
ast::expr_rec(fs, none) => {
99100
let cs = do vec::map(fs) |f| {
100101
if f.node.mutbl == ast::m_imm {

branches/dist-snap/src/rustc/middle/trans/consts.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,32 @@ fn const_expr(cx: @crate_ctxt, e: @ast::expr) -> ValueRef {
153153
ast::expr_tup(es) => {
154154
C_struct(es.map(|e| const_expr(cx, e)))
155155
}
156+
ast::expr_struct(_, fs, _) => {
157+
let ety = ty::expr_ty(cx.tcx, e);
158+
let llty = type_of::type_of(cx, ety);
159+
let class_fields =
160+
match ty::get(ety).struct {
161+
ty::ty_class(clsid, _) =>
162+
ty::lookup_class_fields(cx.tcx, clsid),
163+
_ =>
164+
cx.tcx.sess.span_bug(e.span,
165+
~"didn't resolve to a struct")
166+
};
167+
let mut cs = ~[];
168+
for class_fields.each |class_field| {
169+
let mut found = false;
170+
for fs.each |field| {
171+
if class_field.ident == field.node.ident {
172+
found = true;
173+
vec::push(cs, const_expr(cx, field.node.expr));
174+
}
175+
}
176+
if !found {
177+
cx.tcx.sess.span_bug(e.span, ~"missing struct field");
178+
}
179+
}
180+
C_named_struct(llty, cs)
181+
}
156182
ast::expr_rec(fs, none) => {
157183
C_struct(fs.map(|f| const_expr(cx, f.node.expr)))
158184
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
struct foo { a: int; b: int; c: int; }
3+
4+
const x : foo = foo { a:1, b:2, c: 3 };
5+
const y : foo = foo { b:2, c:3, a: 1 };
6+
const z : &foo = &foo { a: 10, b: 22, c: 12 };
7+
8+
fn main() {
9+
assert x.b == 2;
10+
assert x == y;
11+
assert z.b == 22;
12+
io::println(fmt!{"0x%x", x.b as uint});
13+
io::println(fmt!{"0x%x", z.c as uint});
14+
}

0 commit comments

Comments
 (0)