Skip to content

Commit ecefeb0

Browse files
committed
auto merge of #8185 : alexcrichton/rust/issue-8179, r=pcwalton
* All globals marked as `pub` won't have the `internal` linkage type set * All global references across crates are forced to use the address of the global in the other crate via an external reference. r? @graydon Closes #8179
2 parents 5890fcf + ea5f829 commit ecefeb0

File tree

4 files changed

+58
-17
lines changed

4 files changed

+58
-17
lines changed

src/librustc/middle/trans/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2458,7 +2458,7 @@ pub fn get_item_val(ccx: @mut CrateContext, id: ast::NodeId) -> ValueRef {
24582458
// LLVM type is not fully determined by the Rust type.
24592459
let v = consts::const_expr(ccx, expr);
24602460
ccx.const_values.insert(id, v);
2461-
exprt = m == ast::m_mutbl;
2461+
exprt = (m == ast::m_mutbl || i.vis == ast::public);
24622462

24632463
unsafe {
24642464
let llty = llvm::LLVMTypeOf(v);

src/librustc/middle/trans/expr.rs

+7-16
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ return type, such as `while` loops or assignments (`a = b`).
115115

116116

117117
use back::abi;
118-
use lib::llvm::{ValueRef, llvm};
118+
use lib::llvm::{ValueRef, llvm, SetLinkage, ExternalLinkage};
119119
use lib;
120120
use metadata::csearch;
121121
use middle::trans::_match;
@@ -132,7 +132,6 @@ use middle::trans::consts;
132132
use middle::trans::controlflow;
133133
use middle::trans::datum::*;
134134
use middle::trans::debuginfo;
135-
use middle::trans::inline;
136135
use middle::trans::machine;
137136
use middle::trans::meth;
138137
use middle::trans::tvec;
@@ -148,7 +147,6 @@ use middle::trans::machine::llsize_of;
148147

149148
use middle::trans::type_::Type;
150149

151-
use std::cast::transmute;
152150
use std::hashmap::HashMap;
153151
use std::vec;
154152
use syntax::print::pprust::{expr_to_str};
@@ -936,20 +934,10 @@ fn trans_lvalue_unadjusted(bcx: @mut Block, expr: @ast::expr) -> DatumBlock {
936934
//! Translates a reference to a path.
937935
938936
let _icx = push_ctxt("trans_def_lvalue");
939-
let ccx = bcx.ccx();
940937
match def {
941938
ast::def_static(did, _) => {
942939
let const_ty = expr_ty(bcx, ref_expr);
943940

944-
fn get_did(ccx: @mut CrateContext, did: ast::def_id)
945-
-> ast::def_id {
946-
if did.crate != ast::LOCAL_CRATE {
947-
inline::maybe_instantiate_inline(ccx, did)
948-
} else {
949-
did
950-
}
951-
}
952-
953941
fn get_val(bcx: @mut Block, did: ast::def_id, const_ty: ty::t)
954942
-> ValueRef {
955943
// For external constants, we don't inline.
@@ -976,16 +964,19 @@ fn trans_lvalue_unadjusted(bcx: @mut Block, expr: @ast::expr) -> DatumBlock {
976964
let symbol = csearch::get_symbol(
977965
bcx.ccx().sess.cstore,
978966
did);
979-
let llval = llvm::LLVMAddGlobal( bcx.ccx().llmod, llty.to_ref(),
980-
transmute::<&u8,*i8>(&symbol[0]));
967+
let llval = do symbol.as_c_str |buf| {
968+
llvm::LLVMAddGlobal(bcx.ccx().llmod,
969+
llty.to_ref(),
970+
buf)
971+
};
972+
SetLinkage(llval, ExternalLinkage);
981973
let extern_const_values = &mut bcx.ccx().extern_const_values;
982974
extern_const_values.insert(did, llval);
983975
llval
984976
}
985977
}
986978
}
987979

988-
let did = get_did(ccx, did);
989980
let val = get_val(bcx, did, const_ty);
990981
DatumBlock {
991982
bcx: bcx,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub static global: int = 3;
12+
13+
pub fn verify_same(a: &'static int) {
14+
let a = a as *int as uint;
15+
let b = &global as *int as uint;
16+
assert_eq!(a, b);
17+
}
18+
19+
condition!{ pub test: int -> (); }
20+
21+
pub fn raise() {
22+
test::cond.raise(3);
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// xfail-fast
12+
// aux-build:xcrate_static_addresses.rs
13+
14+
extern mod xcrate_static_addresses;
15+
16+
use other = xcrate_static_addresses;
17+
18+
pub fn main() {
19+
other::verify_same(&other::global);
20+
21+
// Previously this fail'd because there were two addresses that were being
22+
// used when declaring constants.
23+
do other::test::cond.trap(|_| {
24+
}).inside {
25+
other::raise();
26+
}
27+
}

0 commit comments

Comments
 (0)