Skip to content

Commit 7bb0334

Browse files
committed
Inline constants across crates.
1 parent 50277ec commit 7bb0334

File tree

7 files changed

+133
-26
lines changed

7 files changed

+133
-26
lines changed

src/librustc/metadata/encoder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ fn encode_info_for_item(ecx: @EncodeContext, ebml_w: writer::Encoder,
616616
let must_write =
617617
match item.node {
618618
item_enum(_, _) | item_impl(*) | item_trait(*) | item_struct(*) |
619-
item_mod(*) | item_foreign_mod(*) => true,
619+
item_mod(*) | item_foreign_mod(*) | item_const(*) => true,
620620
_ => false
621621
};
622622
if !must_write && !reachable(ecx, item.id) { return; }
@@ -639,6 +639,7 @@ fn encode_info_for_item(ecx: @EncodeContext, ebml_w: writer::Encoder,
639639
encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id));
640640
encode_symbol(ecx, ebml_w, item.id);
641641
encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident));
642+
(ecx.encode_inlined_item)(ecx, ebml_w, path, ii_item(item));
642643
ebml_w.end_tag();
643644
}
644645
item_fn(_, purity, ref generics, _) => {

src/librustc/middle/check_const.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,11 @@ pub fn check_expr(sess: Session,
124124
items without type parameters");
125125
}
126126
match def_map.find(&e.id) {
127-
Some(def_variant(_, _)) |
128-
Some(def_struct(_)) => { }
127+
Some(def_const(_)) |
128+
Some(def_fn(_, _)) |
129+
Some(def_variant(_, _)) |
130+
Some(def_struct(_)) => { }
129131

130-
Some(def_const(def_id)) |
131-
Some(def_fn(def_id, _)) => {
132-
if !ast_util::is_local(def_id) {
133-
sess.span_err(
134-
e.span, ~"paths in constants may only refer to \
135-
crate-local constants or functions");
136-
}
137-
}
138132
Some(def) => {
139133
debug!("(checking const) found bad def: %?", def);
140134
sess.span_err(
@@ -246,11 +240,13 @@ pub fn check_item_recursion(sess: Session,
246240
expr_path(*) => {
247241
match env.def_map.find(&e.id) {
248242
Some(def_const(def_id)) => {
249-
match env.ast_map.get(&def_id.node) {
250-
ast_map::node_item(it, _) => {
251-
(v.visit_item)(it, env, v);
243+
if ast_util::is_local(def_id) {
244+
match env.ast_map.get(&def_id.node) {
245+
ast_map::node_item(it, _) => {
246+
(v.visit_item)(it, env, v);
247+
}
248+
_ => fail!(~"const not bound to an item")
252249
}
253-
_ => fail!(~"const not bound to an item")
254250
}
255251
}
256252
_ => ()

src/librustc/middle/trans/consts.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
use core::prelude::*;
1212

1313
use lib::llvm::{llvm, ValueRef, TypeRef, Bool, True, False};
14+
use metadata::csearch;
1415
use middle::const_eval;
1516
use middle::trans::adt;
1617
use middle::trans::base;
1718
use middle::trans::base::get_insn_ctxt;
1819
use middle::trans::common::*;
1920
use middle::trans::consts;
2021
use middle::trans::expr;
22+
use middle::trans::inline;
2123
use middle::trans::machine;
2224
use middle::trans::type_of;
2325
use middle::ty;
@@ -121,10 +123,12 @@ pub fn const_autoderef(cx: @CrateContext, ty: ty::t, v: ValueRef)
121123
}
122124

123125
pub fn get_const_val(cx: @CrateContext, def_id: ast::def_id) -> ValueRef {
124-
if !ast_util::is_local(def_id) {
125-
cx.tcx.sess.bug(~"cross-crate constants");
126-
}
127-
if !cx.const_values.contains_key(&def_id.node) {
126+
let mut def_id = def_id;
127+
if !ast_util::is_local(def_id) ||
128+
!cx.const_values.contains_key(&def_id.node) {
129+
if !ast_util::is_local(def_id) {
130+
def_id = inline::maybe_instantiate_inline(cx, def_id, true);
131+
}
128132
match cx.tcx.items.get(&def_id.node) {
129133
ast_map::node_item(@ast::item {
130134
node: ast::item_const(_, subexpr), _
@@ -419,8 +423,13 @@ fn const_expr_unchecked(cx: @CrateContext, e: @ast::expr) -> ValueRef {
419423
assert pth.types.len() == 0;
420424
match cx.tcx.def_map.find(&e.id) {
421425
Some(ast::def_fn(def_id, _purity)) => {
422-
assert ast_util::is_local(def_id);
423-
let f = base::get_item_val(cx, def_id.node);
426+
let f = if !ast_util::is_local(def_id) {
427+
let ty = csearch::get_type(cx.tcx, def_id).ty;
428+
base::trans_external_path(cx, def_id, ty)
429+
} else {
430+
assert ast_util::is_local(def_id);
431+
base::get_item_val(cx, def_id.node)
432+
};
424433
let ety = ty::expr_ty_adjusted(cx.tcx, e);
425434
match ty::get(ety).sty {
426435
ty::ty_bare_fn(*) | ty::ty_ptr(*) => {

src/librustc/middle/trans/expr.rs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ use middle::trans::consts;
138138
use middle::trans::controlflow;
139139
use middle::trans::datum::*;
140140
use middle::trans::debuginfo;
141+
use middle::trans::inline;
141142
use middle::trans::machine;
142143
use middle::trans::meth;
143144
use middle::trans::tvec;
@@ -984,15 +985,54 @@ fn trans_lvalue_unadjusted(bcx: block, expr: @ast::expr) -> DatumBlock {
984985
match def {
985986
ast::def_const(did) => {
986987
let const_ty = expr_ty(bcx, ref_expr);
987-
let val = if did.crate == ast::local_crate {
988+
989+
#[cfg(stage0)]
990+
fn get_did(_ccx: @CrateContext, did: ast::def_id)
991+
-> ast::def_id {
992+
did
993+
}
994+
995+
#[cfg(stage1)]
996+
#[cfg(stage2)]
997+
#[cfg(stage3)]
998+
fn get_did(ccx: @CrateContext, did: ast::def_id)
999+
-> ast::def_id {
1000+
if did.crate != ast::local_crate {
1001+
inline::maybe_instantiate_inline(ccx, did, true)
1002+
} else {
1003+
did
1004+
}
1005+
}
1006+
1007+
#[cfg(stage0)]
1008+
fn get_val(bcx: block, did: ast::def_id, const_ty: ty::t)
1009+
-> ValueRef {
1010+
let ccx = bcx.ccx();
1011+
if did.crate == ast::local_crate {
1012+
// The LLVM global has the type of its initializer,
1013+
// which may not be equal to the enum's type for
1014+
// non-C-like enums.
1015+
PointerCast(bcx, base::get_item_val(ccx, did.node),
1016+
T_ptr(type_of(bcx.ccx(), const_ty)))
1017+
} else {
1018+
base::trans_external_path(ccx, did, const_ty)
1019+
}
1020+
}
1021+
1022+
#[cfg(stage1)]
1023+
#[cfg(stage2)]
1024+
#[cfg(stage3)]
1025+
fn get_val(bcx: block, did: ast::def_id, const_ty: ty::t)
1026+
-> ValueRef {
9881027
// The LLVM global has the type of its initializer,
9891028
// which may not be equal to the enum's type for
9901029
// non-C-like enums.
991-
PointerCast(bcx, base::get_item_val(ccx, did.node),
1030+
PointerCast(bcx, base::get_item_val(bcx.ccx(), did.node),
9921031
T_ptr(type_of(bcx.ccx(), const_ty)))
993-
} else {
994-
base::trans_external_path(ccx, did, const_ty)
995-
};
1032+
}
1033+
1034+
let did = get_did(ccx, did);
1035+
let val = get_val(bcx, did, const_ty);
9961036
DatumBlock {
9971037
bcx: bcx,
9981038
datum: Datum {val: val,

src/test/auxiliary/cci_const.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 extern fn bar() {
12+
}
13+
14+
pub const foopy: &static/str = "hi there";
15+
pub const uint_val: uint = 12;
16+
pub const uint_expr: uint = (1 << uint_val) - 1;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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:cci_const.rs
13+
14+
extern mod cci_const;
15+
const foo: &static/str = cci_const::foopy;
16+
const a: uint = cci_const::uint_val;
17+
const b: uint = cci_const::uint_expr + 5;
18+
19+
fn main() {
20+
assert a == 12;
21+
let foo2 = a;
22+
assert foo2 == cci_const::uint_val;
23+
assert b == cci_const::uint_expr + 5;
24+
assert foo == cci_const::foopy;
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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:cci_const.rs
13+
14+
extern mod cci_const;
15+
use cci_const::bar;
16+
const foo: *u8 = bar;
17+
18+
fn main() {
19+
assert foo == cci_const::bar;
20+
}

0 commit comments

Comments
 (0)