Skip to content

Commit e189122

Browse files
committed
auto merge of #16452 : epdtry/rust/unreachable-item-ice, r=pcwalton
This code produces an ICE: ```rust #![crate_type = "rlib"] fn main() { if true { return } // remaining code is unreachable match () { () => { static MAGIC: uint = 0; } } } ``` ([playpen](http://is.gd/iwOISB)) The error is "encode_symbol: id not found 18", where 18 is the `NodeId` of the declaration of `MAGIC`. The problem is that `rustc` tries to emit metadata for `MAGIC`, but some of the information is missing because `MAGIC` never gets translated by `trans_item` - the entire body of the `match` gets skipped because the `match` itself is unreachable. This branch simplifies the handling of inner items by always processing them using the `trans_item` visitor, instead of sometimes using the visitor and sometimes waiting until `trans_stmt` encounters the item. This fixes the ICE by making the translation of the item no longer depend on the declaration being reachable code. This branch also reverts #16059 and #16359, since the new change to item translation fixes the same problems as those but is simpler.
2 parents 51c7e20 + 0f847ba commit e189122

11 files changed

+34
-65
lines changed

src/librustc/middle/trans/base.rs

+14-22
Original file line numberDiff line numberDiff line change
@@ -1343,8 +1343,7 @@ pub fn new_fn_ctxt<'a>(ccx: &'a CrateContext,
13431343
output_type: ty::t,
13441344
param_substs: &'a param_substs,
13451345
sp: Option<Span>,
1346-
block_arena: &'a TypedArena<Block<'a>>,
1347-
handle_items: HandleItemsFlag)
1346+
block_arena: &'a TypedArena<Block<'a>>)
13481347
-> FunctionContext<'a> {
13491348
param_substs.validate();
13501349

@@ -1379,8 +1378,7 @@ pub fn new_fn_ctxt<'a>(ccx: &'a CrateContext,
13791378
block_arena: block_arena,
13801379
ccx: ccx,
13811380
debug_context: debug_context,
1382-
scopes: RefCell::new(Vec::new()),
1383-
handle_items: handle_items,
1381+
scopes: RefCell::new(Vec::new())
13841382
};
13851383

13861384
if has_env {
@@ -1708,8 +1706,7 @@ pub fn trans_closure(ccx: &CrateContext,
17081706
abi: Abi,
17091707
has_env: bool,
17101708
is_unboxed_closure: IsUnboxedClosureFlag,
1711-
maybe_load_env: <'a> |&'a Block<'a>| -> &'a Block<'a>,
1712-
handle_items: HandleItemsFlag) {
1709+
maybe_load_env: <'a> |&'a Block<'a>| -> &'a Block<'a>) {
17131710
ccx.stats.n_closures.set(ccx.stats.n_closures.get() + 1);
17141711

17151712
let _icx = push_ctxt("trans_closure");
@@ -1726,8 +1723,7 @@ pub fn trans_closure(ccx: &CrateContext,
17261723
output_type,
17271724
param_substs,
17281725
Some(body.span),
1729-
&arena,
1730-
handle_items);
1726+
&arena);
17311727
let mut bcx = init_function(&fcx, false, output_type);
17321728

17331729
// cleanup scope for the incoming arguments
@@ -1836,8 +1832,7 @@ pub fn trans_fn(ccx: &CrateContext,
18361832
llfndecl: ValueRef,
18371833
param_substs: &param_substs,
18381834
id: ast::NodeId,
1839-
attrs: &[ast::Attribute],
1840-
handle_items: HandleItemsFlag) {
1835+
attrs: &[ast::Attribute]) {
18411836
let _s = StatRecorder::new(ccx, ccx.tcx.map.path_to_string(id).to_string());
18421837
debug!("trans_fn(param_substs={})", param_substs.repr(ccx.tcx()));
18431838
let _icx = push_ctxt("trans_fn");
@@ -1857,8 +1852,7 @@ pub fn trans_fn(ccx: &CrateContext,
18571852
abi,
18581853
false,
18591854
NotUnboxedClosure,
1860-
|bcx| bcx,
1861-
handle_items);
1855+
|bcx| bcx);
18621856
}
18631857

18641858
pub fn trans_enum_variant(ccx: &CrateContext,
@@ -1964,7 +1958,7 @@ fn trans_enum_variant_or_tuple_like_struct(ccx: &CrateContext,
19641958

19651959
let arena = TypedArena::new();
19661960
let fcx = new_fn_ctxt(ccx, llfndecl, ctor_id, false, result_ty,
1967-
param_substs, None, &arena, TranslateItems);
1961+
param_substs, None, &arena);
19681962
let bcx = init_function(&fcx, false, result_ty);
19691963

19701964
assert!(!fcx.needs_ret_allocas);
@@ -2066,24 +2060,22 @@ pub fn trans_item(ccx: &CrateContext, item: &ast::Item) {
20662060
llfn,
20672061
&param_substs::empty(),
20682062
item.id,
2069-
None,
2070-
TranslateItems);
2063+
None);
20712064
} else {
20722065
trans_fn(ccx,
20732066
&**decl,
20742067
&**body,
20752068
llfn,
20762069
&param_substs::empty(),
20772070
item.id,
2078-
item.attrs.as_slice(),
2079-
TranslateItems);
2071+
item.attrs.as_slice());
20802072
}
2081-
} else {
2082-
// Be sure to travel more than just one layer deep to catch nested
2083-
// items in blocks and such.
2084-
let mut v = TransItemVisitor{ ccx: ccx };
2085-
v.visit_block(&**body, ());
20862073
}
2074+
2075+
// Be sure to travel more than just one layer deep to catch nested
2076+
// items in blocks and such.
2077+
let mut v = TransItemVisitor{ ccx: ccx };
2078+
v.visit_block(&**body, ());
20872079
}
20882080
ast::ItemImpl(ref generics, _, _, ref ms) => {
20892081
meth::trans_impl(ccx, item.ident, ms.as_slice(), generics, item.id);

src/librustc/middle/trans/callee.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,7 @@ pub fn trans_unboxing_shim(bcx: &Block,
339339
return_type,
340340
&empty_param_substs,
341341
None,
342-
&block_arena,
343-
TranslateItems);
342+
&block_arena);
344343
let mut bcx = init_function(&fcx, false, return_type);
345344

346345
// Create the substituted versions of the self type.

src/librustc/middle/trans/closure.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,7 @@ pub fn trans_expr_fn<'a>(
394394
ty::ty_fn_abi(fty),
395395
true,
396396
NotUnboxedClosure,
397-
|bcx| load_environment(bcx, cdata_ty, &freevars, store),
398-
bcx.fcx.handle_items);
397+
|bcx| load_environment(bcx, cdata_ty, &freevars, store));
399398
fill_fn_pair(bcx, dest_addr, llfn, llbox);
400399
bcx
401400
}
@@ -487,8 +486,7 @@ pub fn trans_unboxed_closure<'a>(
487486
ty::ty_fn_abi(function_type),
488487
true,
489488
IsUnboxedClosure,
490-
|bcx| load_unboxed_closure_environment(bcx, freevars_ptr),
491-
bcx.fcx.handle_items);
489+
|bcx| load_unboxed_closure_environment(bcx, freevars_ptr));
492490

493491
// Don't hoist this to the top of the function. It's perfectly legitimate
494492
// to have a zero-size unboxed closure (in which case dest will be
@@ -575,7 +573,7 @@ pub fn get_wrapper_for_bare_fn(ccx: &CrateContext,
575573
let arena = TypedArena::new();
576574
let empty_param_substs = param_substs::empty();
577575
let fcx = new_fn_ctxt(ccx, llfn, ast::DUMMY_NODE_ID, true, f.sig.output,
578-
&empty_param_substs, None, &arena, TranslateItems);
576+
&empty_param_substs, None, &arena);
579577
let bcx = init_function(&fcx, true, f.sig.output);
580578

581579
let args = create_datums_for_fn_args(&fcx,

src/librustc/middle/trans/common.rs

-9
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,6 @@ impl<T:Subst+Clone> SubstP for T {
224224
pub type RvalueDatum = datum::Datum<datum::Rvalue>;
225225
pub type LvalueDatum = datum::Datum<datum::Lvalue>;
226226

227-
#[deriving(Clone, Eq, PartialEq)]
228-
pub enum HandleItemsFlag {
229-
IgnoreItems,
230-
TranslateItems,
231-
}
232-
233227
// Function context. Every LLVM function we create will have one of
234228
// these.
235229
pub struct FunctionContext<'a> {
@@ -303,9 +297,6 @@ pub struct FunctionContext<'a> {
303297

304298
// Cleanup scopes.
305299
pub scopes: RefCell<Vec<cleanup::CleanupScope<'a>> >,
306-
307-
// How to handle items encountered during translation of this function.
308-
pub handle_items: HandleItemsFlag,
309300
}
310301

311302
impl<'a> FunctionContext<'a> {

src/librustc/middle/trans/controlflow.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,8 @@ pub fn trans_stmt<'a>(cx: &'a Block<'a>,
6969
debuginfo::create_local_var_metadata(bcx, &**local);
7070
}
7171
}
72-
ast::DeclItem(ref i) => {
73-
match fcx.handle_items {
74-
TranslateItems => trans_item(cx.fcx.ccx, &**i),
75-
IgnoreItems => {}
76-
}
77-
}
72+
// Inner items are visited by `trans_item`/`trans_meth`.
73+
ast::DeclItem(_) => {},
7874
}
7975
}
8076
ast::StmtMac(..) => cx.tcx().sess.bug("unexpanded macro")

src/librustc/middle/trans/foreign.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -577,8 +577,7 @@ pub fn trans_rust_fn_with_foreign_abi(ccx: &CrateContext,
577577
llwrapfn: ValueRef,
578578
param_substs: &param_substs,
579579
id: ast::NodeId,
580-
hash: Option<&str>,
581-
handle_items: HandleItemsFlag) {
580+
hash: Option<&str>) {
582581
let _icx = push_ctxt("foreign::build_foreign_fn");
583582

584583
let fnty = ty::node_id_to_type(ccx.tcx(), id);
@@ -587,8 +586,7 @@ pub fn trans_rust_fn_with_foreign_abi(ccx: &CrateContext,
587586

588587
unsafe { // unsafe because we call LLVM operations
589588
// Build up the Rust function (`foo0` above).
590-
let llrustfn = build_rust_fn(ccx, decl, body, param_substs, attrs, id,
591-
hash, handle_items);
589+
let llrustfn = build_rust_fn(ccx, decl, body, param_substs, attrs, id, hash);
592590

593591
// Build up the foreign wrapper (`foo` above).
594592
return build_wrap_fn(ccx, llrustfn, llwrapfn, &tys, mty);
@@ -600,8 +598,7 @@ pub fn trans_rust_fn_with_foreign_abi(ccx: &CrateContext,
600598
param_substs: &param_substs,
601599
attrs: &[ast::Attribute],
602600
id: ast::NodeId,
603-
hash: Option<&str>,
604-
handle_items: HandleItemsFlag)
601+
hash: Option<&str>)
605602
-> ValueRef {
606603
let _icx = push_ctxt("foreign::foreign::build_rust_fn");
607604
let tcx = ccx.tcx();
@@ -633,7 +630,7 @@ pub fn trans_rust_fn_with_foreign_abi(ccx: &CrateContext,
633630

634631
let llfn = base::decl_internal_rust_fn(ccx, t, ps.as_slice());
635632
base::set_llvm_fn_attrs(attrs, llfn);
636-
base::trans_fn(ccx, decl, body, llfn, param_substs, id, [], handle_items);
633+
base::trans_fn(ccx, decl, body, llfn, param_substs, id, []);
637634
llfn
638635
}
639636

src/librustc/middle/trans/glue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ fn make_generic_glue(ccx: &CrateContext,
468468
let arena = TypedArena::new();
469469
let empty_param_substs = param_substs::empty();
470470
let fcx = new_fn_ctxt(ccx, llfn, ast::DUMMY_NODE_ID, false, ty::mk_nil(),
471-
&empty_param_substs, None, &arena, TranslateItems);
471+
&empty_param_substs, None, &arena);
472472

473473
let bcx = init_function(&fcx, false, ty::mk_nil());
474474

src/librustc/middle/trans/inline.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub fn maybe_instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId)
133133
if unparameterized {
134134
let llfn = get_item_val(ccx, mth.id);
135135
trans_fn(ccx, &*mth.pe_fn_decl(), &*mth.pe_body(), llfn,
136-
&param_substs::empty(), mth.id, [], TranslateItems);
136+
&param_substs::empty(), mth.id, []);
137137
}
138138
local_def(mth.id)
139139
}

src/librustc/middle/trans/meth.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,10 @@ pub fn trans_impl(ccx: &CrateContext,
7575
llfn,
7676
&param_substs::empty(),
7777
method.id,
78-
[],
79-
TranslateItems);
80-
} else {
81-
let mut v = TransItemVisitor{ ccx: ccx };
82-
visit::walk_method_helper(&mut v, &**method, ());
78+
[]);
8379
}
80+
let mut v = TransItemVisitor{ ccx: ccx };
81+
visit::walk_method_helper(&mut v, &**method, ());
8482
}
8583
}
8684

src/librustc/middle/trans/monomorphize.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,9 @@ pub fn monomorphic_fn(ccx: &CrateContext,
164164
if abi != abi::Rust {
165165
foreign::trans_rust_fn_with_foreign_abi(
166166
ccx, &**decl, &**body, [], d, &psubsts, fn_id.node,
167-
Some(hash.as_slice()), IgnoreItems);
167+
Some(hash.as_slice()));
168168
} else {
169-
trans_fn(ccx, &**decl, &**body, d, &psubsts, fn_id.node, [],
170-
IgnoreItems);
169+
trans_fn(ccx, &**decl, &**body, d, &psubsts, fn_id.node, []);
171170
}
172171

173172
d
@@ -201,8 +200,7 @@ pub fn monomorphic_fn(ccx: &CrateContext,
201200
ast_map::NodeMethod(mth) => {
202201
let d = mk_lldecl(abi::Rust);
203202
set_llvm_fn_attrs(mth.attrs.as_slice(), d);
204-
trans_fn(ccx, &*mth.pe_fn_decl(), &*mth.pe_body(), d, &psubsts, mth.id, [],
205-
IgnoreItems);
203+
trans_fn(ccx, &*mth.pe_fn_decl(), &*mth.pe_body(), d, &psubsts, mth.id, []);
206204
d
207205
}
208206
ast_map::NodeTraitMethod(method) => {
@@ -211,7 +209,7 @@ pub fn monomorphic_fn(ccx: &CrateContext,
211209
let d = mk_lldecl(abi::Rust);
212210
set_llvm_fn_attrs(mth.attrs.as_slice(), d);
213211
trans_fn(ccx, &*mth.pe_fn_decl(), &*mth.pe_body(), d,
214-
&psubsts, mth.id, [], IgnoreItems);
212+
&psubsts, mth.id, []);
215213
d
216214
}
217215
_ => {

src/librustc/middle/trans/reflect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ impl<'a, 'b> Reflector<'a, 'b> {
312312
let empty_param_substs = param_substs::empty();
313313
let fcx = new_fn_ctxt(ccx, llfdecl, ast::DUMMY_NODE_ID, false,
314314
ty::mk_u64(), &empty_param_substs,
315-
None, &arena, TranslateItems);
315+
None, &arena);
316316
let bcx = init_function(&fcx, false, ty::mk_u64());
317317

318318
// we know the return type of llfdecl is an int here, so

0 commit comments

Comments
 (0)