Skip to content

Commit e2964f7

Browse files
committed
---
yaml --- r: 228757 b: refs/heads/try c: 9a196aa h: refs/heads/master i: 228755: b92d39d v: v3
1 parent f5a88f7 commit e2964f7

File tree

20 files changed

+295
-137
lines changed

20 files changed

+295
-137
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: aca2057ed5fb7af3f8905b2bc01f72fa001c35c8
33
refs/heads/snap-stage3: 1af31d4974e33027a68126fa5a5a3c2c6491824f
4-
refs/heads/try: 11aa8756c1a6439fa73ef804665d13fc1bb29fef
4+
refs/heads/try: 9a196aa173c7d08dc865c1814647b2c9f4d9f68a
55
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
66
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
77
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try/src/librustc/middle/check_static_recursion.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// This compiler pass detects static items that refer to themselves
11+
// This compiler pass detects constants that refer to themselves
1212
// recursively.
1313

1414
use ast_map;
@@ -18,6 +18,7 @@ use util::nodemap::NodeMap;
1818

1919
use syntax::{ast, ast_util};
2020
use syntax::codemap::Span;
21+
use syntax::feature_gate::emit_feature_err;
2122
use syntax::visit::Visitor;
2223
use syntax::visit;
2324

@@ -125,8 +126,27 @@ impl<'a, 'ast: 'a> CheckItemRecursionVisitor<'a, 'ast> {
125126
}
126127
fn with_item_id_pushed<F>(&mut self, id: ast::NodeId, f: F)
127128
where F: Fn(&mut Self) {
128-
if self.idstack.iter().any(|x| *x == id) {
129-
span_err!(self.sess, *self.root_span, E0265, "recursive constant");
129+
if self.idstack.iter().any(|&x| x == id) {
130+
let any_static = self.idstack.iter().any(|&x| {
131+
if let ast_map::NodeItem(item) = self.ast_map.get(x) {
132+
if let ast::ItemStatic(..) = item.node {
133+
true
134+
} else {
135+
false
136+
}
137+
} else {
138+
false
139+
}
140+
});
141+
if any_static {
142+
if !self.sess.features.borrow().static_recursion {
143+
emit_feature_err(&self.sess.parse_sess.span_diagnostic,
144+
"static_recursion",
145+
*self.root_span, "recursive static");
146+
}
147+
} else {
148+
span_err!(self.sess, *self.root_span, E0265, "recursive constant");
149+
}
130150
return;
131151
}
132152
self.idstack.push(id);

branches/try/src/librustc/middle/infer/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -698,8 +698,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
698698
}
699699
}
700700

701-
fn rollback_to(&self, snapshot: CombinedSnapshot) {
702-
debug!("rollback!");
701+
fn rollback_to(&self, cause: &str, snapshot: CombinedSnapshot) {
702+
debug!("rollback_to(cause={})", cause);
703703
let CombinedSnapshot { type_snapshot,
704704
int_snapshot,
705705
float_snapshot,
@@ -759,7 +759,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
759759
debug!("commit_if_ok() -- r.is_ok() = {}", r.is_ok());
760760
match r {
761761
Ok(_) => { self.commit_from(snapshot); }
762-
Err(_) => { self.rollback_to(snapshot); }
762+
Err(_) => { self.rollback_to("commit_if_ok -- error", snapshot); }
763763
}
764764
r
765765
}
@@ -778,6 +778,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
778778

779779
let r = self.commit_if_ok(|_| f());
780780

781+
debug!("commit_regions_if_ok: rolling back everything but regions");
782+
781783
// Roll back any non-region bindings - they should be resolved
782784
// inside `f`, with, e.g. `resolve_type_vars_if_possible`.
783785
self.type_variables
@@ -804,7 +806,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
804806
debug!("probe()");
805807
let snapshot = self.start_snapshot();
806808
let r = f(&snapshot);
807-
self.rollback_to(snapshot);
809+
self.rollback_to("probe", snapshot);
808810
r
809811
}
810812

branches/try/src/librustc/middle/traits/select.rs

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,11 +1351,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
13511351
// correct trait, but also the correct type parameters.
13521352
// For example, we may be trying to upcast `Foo` to `Bar<i32>`,
13531353
// but `Foo` is declared as `trait Foo : Bar<u32>`.
1354-
let upcast_trait_refs = util::supertraits(self.tcx(), poly_trait_ref)
1355-
.filter(|upcast_trait_ref| self.infcx.probe(|_| {
1356-
let upcast_trait_ref = upcast_trait_ref.clone();
1357-
self.match_poly_trait_ref(obligation, upcast_trait_ref).is_ok()
1358-
})).count();
1354+
let upcast_trait_refs =
1355+
util::supertraits(self.tcx(), poly_trait_ref)
1356+
.filter(|upcast_trait_ref| {
1357+
self.infcx.probe(|_| {
1358+
let upcast_trait_ref = upcast_trait_ref.clone();
1359+
self.match_poly_trait_ref(obligation, upcast_trait_ref).is_ok()
1360+
})
1361+
})
1362+
.count();
13591363

13601364
if upcast_trait_refs > 1 {
13611365
// can be upcast in many ways; need more type information
@@ -1627,9 +1631,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
16271631
let principal =
16281632
data.principal_trait_ref_with_self_ty(self.tcx(),
16291633
self.tcx().types.err);
1630-
let desired_def_id = obligation.predicate.def_id();
1634+
let copy_def_id = obligation.predicate.def_id();
16311635
for tr in util::supertraits(self.tcx(), principal) {
1632-
if tr.def_id() == desired_def_id {
1636+
if tr.def_id() == copy_def_id {
16331637
return ok_if(Vec::new())
16341638
}
16351639
}
@@ -2282,31 +2286,41 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
22822286
}
22832287
};
22842288

2285-
// Upcast the object type to the obligation type. There must
2286-
// be exactly one applicable trait-reference; if this were not
2287-
// the case, we would have reported an ambiguity error rather
2288-
// than successfully selecting one of the candidates.
2289-
let mut upcast_trait_refs = util::supertraits(self.tcx(), poly_trait_ref)
2290-
.map(|upcast_trait_ref| {
2291-
(upcast_trait_ref.clone(), self.infcx.probe(|_| {
2292-
self.match_poly_trait_ref(obligation, upcast_trait_ref)
2293-
}).is_ok())
2294-
});
22952289
let mut upcast_trait_ref = None;
2296-
let mut vtable_base = 0;
2290+
let vtable_base;
2291+
2292+
{
2293+
// We want to find the first supertrait in the list of
2294+
// supertraits that we can unify with, and do that
2295+
// unification. We know that there is exactly one in the list
2296+
// where we can unify because otherwise select would have
2297+
// reported an ambiguity. (When we do find a match, also
2298+
// record it for later.)
2299+
let nonmatching =
2300+
util::supertraits(self.tcx(), poly_trait_ref)
2301+
.take_while(|&t| {
2302+
match
2303+
self.infcx.commit_if_ok(
2304+
|_| self.match_poly_trait_ref(obligation, t))
2305+
{
2306+
Ok(_) => { upcast_trait_ref = Some(t); false }
2307+
Err(_) => { true }
2308+
}
2309+
});
2310+
2311+
// Additionally, for each of the nonmatching predicates that
2312+
// we pass over, we sum up the set of number of vtable
2313+
// entries, so that we can compute the offset for the selected
2314+
// trait.
2315+
vtable_base =
2316+
nonmatching.map(|t| util::count_own_vtable_entries(self.tcx(), t))
2317+
.sum();
22972318

2298-
while let Some((supertrait, matches)) = upcast_trait_refs.next() {
2299-
if matches {
2300-
upcast_trait_ref = Some(supertrait);
2301-
break;
2302-
}
2303-
vtable_base += util::count_own_vtable_entries(self.tcx(), supertrait);
23042319
}
2305-
assert!(upcast_trait_refs.all(|(_, matches)| !matches));
23062320

23072321
VtableObjectData {
23082322
upcast_trait_ref: upcast_trait_ref.unwrap(),
2309-
vtable_base: vtable_base
2323+
vtable_base: vtable_base,
23102324
}
23112325
}
23122326

branches/try/src/librustc/middle/ty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1985,7 +1985,7 @@ impl<'tcx> PolyTraitRef<'tcx> {
19851985
/// erase, or otherwise "discharge" these bound regions, we change the
19861986
/// type from `Binder<T>` to just `T` (see
19871987
/// e.g. `liberate_late_bound_regions`).
1988-
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
1988+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
19891989
pub struct Binder<T>(pub T);
19901990

19911991
impl<T> Binder<T> {

branches/try/src/librustc_trans/trans/base.rs

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,7 +2090,7 @@ pub fn trans_item(ccx: &CrateContext, item: &ast::Item) {
20902090
let mut v = TransItemVisitor{ ccx: ccx };
20912091
v.visit_expr(&**expr);
20922092

2093-
let g = consts::trans_static(ccx, m, item.id);
2093+
let g = consts::trans_static(ccx, m, expr, item.id, &item.attrs);
20942094
update_linkage(ccx, g, Some(item.id), OriginalTranslation);
20952095
},
20962096
ast::ItemForeignMod(ref foreign_mod) => {
@@ -2334,44 +2334,25 @@ pub fn get_item_val(ccx: &CrateContext, id: ast::NodeId) -> ValueRef {
23342334
let sym = || exported_name(ccx, id, ty, &i.attrs);
23352335

23362336
let v = match i.node {
2337-
ast::ItemStatic(_, _, ref expr) => {
2337+
ast::ItemStatic(..) => {
23382338
// If this static came from an external crate, then
23392339
// we need to get the symbol from csearch instead of
23402340
// using the current crate's name/version
23412341
// information in the hash of the symbol
23422342
let sym = sym();
23432343
debug!("making {}", sym);
23442344

2345-
// We need the translated value here, because for enums the
2346-
// LLVM type is not fully determined by the Rust type.
2347-
let empty_substs = ccx.tcx().mk_substs(Substs::trans_empty());
2348-
let (v, ty) = consts::const_expr(ccx, &**expr, empty_substs, None);
2349-
ccx.static_values().borrow_mut().insert(id, v);
2350-
unsafe {
2351-
// boolean SSA values are i1, but they have to be stored in i8 slots,
2352-
// otherwise some LLVM optimization passes don't work as expected
2353-
let llty = if ty.is_bool() {
2354-
llvm::LLVMInt8TypeInContext(ccx.llcx())
2355-
} else {
2356-
llvm::LLVMTypeOf(v)
2357-
};
2358-
2359-
// FIXME(nagisa): probably should be declare_global, because no definition
2360-
// is happening here, but we depend on it being defined here from
2361-
// const::trans_static. This all logic should be replaced.
2362-
let g = declare::define_global(ccx, &sym[..],
2363-
Type::from_ref(llty)).unwrap_or_else(||{
2364-
ccx.sess().span_fatal(i.span, &format!("symbol `{}` is already defined",
2365-
sym))
2366-
});
2367-
2368-
if attr::contains_name(&i.attrs,
2369-
"thread_local") {
2370-
llvm::set_thread_local(g, true);
2371-
}
2372-
ccx.item_symbols().borrow_mut().insert(i.id, sym);
2373-
g
2374-
}
2345+
// Create the global before evaluating the initializer;
2346+
// this is necessary to allow recursive statics.
2347+
let llty = type_of(ccx, ty);
2348+
let g = declare::define_global(ccx, &sym[..],
2349+
llty).unwrap_or_else(|| {
2350+
ccx.sess().span_fatal(i.span, &format!("symbol `{}` is already defined",
2351+
sym))
2352+
});
2353+
2354+
ccx.item_symbols().borrow_mut().insert(i.id, sym);
2355+
g
23752356
}
23762357

23772358
ast::ItemFn(_, _, _, abi, _, _) => {
@@ -2738,6 +2719,13 @@ pub fn trans_crate(tcx: &ty::ctxt, analysis: ty::CrateAnalysis) -> CrateTranslat
27382719
if ccx.sess().opts.debuginfo != NoDebugInfo {
27392720
debuginfo::finalize(&ccx);
27402721
}
2722+
for &(old_g, new_g) in ccx.statics_to_rauw().borrow().iter() {
2723+
unsafe {
2724+
let bitcast = llvm::LLVMConstPointerCast(new_g, llvm::LLVMTypeOf(old_g));
2725+
llvm::LLVMReplaceAllUsesWith(old_g, bitcast);
2726+
llvm::LLVMDeleteGlobal(old_g);
2727+
}
2728+
}
27412729
}
27422730

27432731
// Translate the metadata.

branches/try/src/librustc_trans/trans/consts.rs

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ use middle::subst::Substs;
3737
use middle::ty::{self, Ty};
3838
use util::nodemap::NodeMap;
3939

40+
use std::ffi::{CStr, CString};
4041
use libc::c_uint;
41-
use syntax::{ast, ast_util};
42+
use syntax::{ast, ast_util, attr};
4243
use syntax::parse::token;
4344
use syntax::ptr::P;
4445

@@ -898,37 +899,70 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
898899
"bad constant expression type in consts::const_expr"),
899900
}
900901
}
901-
902-
pub fn trans_static(ccx: &CrateContext, m: ast::Mutability, id: ast::NodeId) -> ValueRef {
902+
pub fn trans_static(ccx: &CrateContext,
903+
m: ast::Mutability,
904+
expr: &ast::Expr,
905+
id: ast::NodeId,
906+
attrs: &Vec<ast::Attribute>)
907+
-> ValueRef {
903908
unsafe {
904909
let _icx = push_ctxt("trans_static");
905910
let g = base::get_item_val(ccx, id);
906-
// At this point, get_item_val has already translated the
907-
// constant's initializer to determine its LLVM type.
908-
let v = ccx.static_values().borrow().get(&id).unwrap().clone();
911+
912+
let empty_substs = ccx.tcx().mk_substs(Substs::trans_empty());
913+
let (v, _) = const_expr(ccx, expr, empty_substs, None);
914+
909915
// boolean SSA values are i1, but they have to be stored in i8 slots,
910916
// otherwise some LLVM optimization passes don't work as expected
911-
let v = if llvm::LLVMTypeOf(v) == Type::i1(ccx).to_ref() {
912-
llvm::LLVMConstZExt(v, Type::i8(ccx).to_ref())
917+
let mut val_llty = llvm::LLVMTypeOf(v);
918+
let v = if val_llty == Type::i1(ccx).to_ref() {
919+
val_llty = Type::i8(ccx).to_ref();
920+
llvm::LLVMConstZExt(v, val_llty)
913921
} else {
914922
v
915923
};
924+
925+
let ty = ccx.tcx().node_id_to_type(id);
926+
let llty = type_of::type_of(ccx, ty);
927+
let g = if val_llty == llty.to_ref() {
928+
g
929+
} else {
930+
// If we created the global with the wrong type,
931+
// correct the type.
932+
let empty_string = CString::new("").unwrap();
933+
let name_str_ref = CStr::from_ptr(llvm::LLVMGetValueName(g));
934+
let name_string = CString::new(name_str_ref.to_bytes()).unwrap();
935+
llvm::LLVMSetValueName(g, empty_string.as_ptr());
936+
let new_g = llvm::LLVMGetOrInsertGlobal(
937+
ccx.llmod(), name_string.as_ptr(), val_llty);
938+
// To avoid breaking any invariants, we leave around the old
939+
// global for the moment; we'll replace all references to it
940+
// with the new global later. (See base::trans_crate.)
941+
ccx.statics_to_rauw().borrow_mut().push((g, new_g));
942+
new_g
943+
};
916944
llvm::LLVMSetInitializer(g, v);
917945

918946
// As an optimization, all shared statics which do not have interior
919947
// mutability are placed into read-only memory.
920948
if m != ast::MutMutable {
921-
let node_ty = ccx.tcx().node_id_to_type(id);
922-
let tcontents = node_ty.type_contents(ccx.tcx());
949+
let tcontents = ty.type_contents(ccx.tcx());
923950
if !tcontents.interior_unsafe() {
924-
llvm::LLVMSetGlobalConstant(g, True);
951+
llvm::LLVMSetGlobalConstant(g, llvm::True);
925952
}
926953
}
954+
927955
debuginfo::create_global_var_metadata(ccx, id, g);
956+
957+
if attr::contains_name(attrs,
958+
"thread_local") {
959+
llvm::set_thread_local(g, true);
960+
}
928961
g
929962
}
930963
}
931964

965+
932966
fn get_static_val<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, did: ast::DefId,
933967
ty: Ty<'tcx>) -> ValueRef {
934968
if ast_util::is_local(did) { return base::get_item_val(ccx, did.node) }

0 commit comments

Comments
 (0)