Skip to content

Commit 565e1ff

Browse files
committed
---
yaml --- r: 41701 b: refs/heads/master c: f76e28a h: refs/heads/master i: 41699: 6829f26 v: v3
1 parent 8626c3f commit 565e1ff

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
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: 452642422dda045b89cbcb2b7d011c85c5202d5d
2+
refs/heads/master: f76e28aa1c093c59671749006e1a7ae203d66b7c
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 2f46b763da2c098913884f101b6d71d69af41b49
55
refs/heads/try: 3d5418789064fdb463e872a4e651af1c628a3650

trunk/src/librustc/middle/trans/base.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,16 +2308,21 @@ fn get_item_val(ccx: @crate_ctxt, id: ast::node_id) -> ValueRef {
23082308
let my_path = vec::append(/*bad*/copy *pth,
23092309
~[path_name(i.ident)]);
23102310
match i.node {
2311-
ast::item_const(_, _) => {
2311+
ast::item_const(_, expr) => {
23122312
let typ = ty::node_id_to_type(ccx.tcx, i.id);
23132313
let s = mangle_exported_name(ccx, my_path, typ);
2314-
let g = str::as_c_str(s, |buf| {
2315-
unsafe {
2316-
llvm::LLVMAddGlobal(ccx.llmod, type_of(ccx, typ), buf)
2317-
}
2318-
});
2319-
ccx.item_symbols.insert(i.id, s);
2320-
g
2314+
// We need the translated value here, because for enums the
2315+
// LLVM type is not fully determined by the Rust type.
2316+
let v = consts::const_expr(ccx, expr);
2317+
ccx.const_values.insert(id, v);
2318+
unsafe {
2319+
let llty = llvm::LLVMTypeOf(v);
2320+
let g = str::as_c_str(s, |buf| {
2321+
llvm::LLVMAddGlobal(ccx.llmod, llty, buf)
2322+
});
2323+
ccx.item_symbols.insert(i.id, s);
2324+
g
2325+
}
23212326
}
23222327
ast::item_fn(_, purity, _, _) => {
23232328
let llfn = if purity != ast::extern_fn {

trunk/src/librustc/middle/trans/consts.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,12 +464,13 @@ fn const_expr(cx: @crate_ctxt, e: @ast::expr) -> ValueRef {
464464
}
465465
}
466466

467-
fn trans_const(ccx: @crate_ctxt, e: @ast::expr, id: ast::node_id) {
467+
fn trans_const(ccx: @crate_ctxt, _e: @ast::expr, id: ast::node_id) {
468468
unsafe {
469469
let _icx = ccx.insn_ctxt("trans_const");
470470
let g = base::get_item_val(ccx, id);
471-
let v = const_expr(ccx, e);
472-
ccx.const_values.insert(id, v);
471+
// At this point, get_item_val has already translated the
472+
// constant's initializer to determine its LLVM type.
473+
let v = ccx.const_values.get(id);
473474
llvm::LLVMSetInitializer(g, v);
474475
llvm::LLVMSetGlobalConstant(g, True);
475476
}

trunk/src/librustc/middle/trans/expr.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,11 @@ fn trans_def_lvalue(bcx: block,
798798
ast::def_const(did) => {
799799
let const_ty = expr_ty(bcx, ref_expr);
800800
let val = if did.crate == ast::local_crate {
801-
base::get_item_val(ccx, did.node)
801+
// The LLVM global has the type of its initializer,
802+
// which may not be equal to the enum's type for
803+
// non-C-like enums.
804+
PointerCast(bcx, base::get_item_val(ccx, did.node),
805+
T_ptr(type_of(bcx.ccx(), const_ty)))
802806
} else {
803807
base::trans_external_path(ccx, did, const_ty)
804808
};

0 commit comments

Comments
 (0)