Skip to content

Commit a39f69f

Browse files
committed
auto merge of #17036 : pczarn/rust/issue-15913-ICE-with-call-trans, r=alexcrichton
A match in callee.rs was recognizing some foreign fns as named tuple constructors. A reproducible test case for this is nearly impossible since it depends on the way NodeIds happen to be assigned in different crates. Fixes #15913
2 parents dd626b4 + 808e039 commit a39f69f

File tree

4 files changed

+33
-45
lines changed

4 files changed

+33
-45
lines changed

src/librustc/middle/trans/base.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -539,11 +539,7 @@ pub fn get_res_dtor(ccx: &CrateContext,
539539
substs: &subst::Substs)
540540
-> ValueRef {
541541
let _icx = push_ctxt("trans_res_dtor");
542-
let did = if did.krate != ast::LOCAL_CRATE {
543-
inline::maybe_instantiate_inline(ccx, did)
544-
} else {
545-
did
546-
};
542+
let did = inline::maybe_instantiate_inline(ccx, did);
547543

548544
if !substs.types.is_empty() {
549545
assert_eq!(did.krate, ast::LOCAL_CRATE);

src/librustc/middle/trans/callee.rs

+6-18
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,10 @@ fn trans<'a>(bcx: &'a Block<'a>, expr: &ast::Expr) -> Callee<'a> {
141141
let expr_ty = node_id_type(bcx, ref_expr.id);
142142
match def {
143143
def::DefFn(did, _) if {
144-
let def_id = if did.krate != ast::LOCAL_CRATE {
145-
inline::maybe_instantiate_inline(bcx.ccx(), did)
146-
} else {
147-
did
148-
};
149-
match bcx.tcx().map.find(def_id.node) {
144+
let maybe_def_id = inline::get_local_instance(bcx.ccx(), did);
145+
let maybe_ast_node = maybe_def_id.and_then(|def_id| bcx.tcx().map
146+
.find(def_id.node));
147+
match maybe_ast_node {
150148
Some(ast_map::NodeStructCtor(_)) => true,
151149
_ => false
152150
}
@@ -162,11 +160,7 @@ fn trans<'a>(bcx: &'a Block<'a>, expr: &ast::Expr) -> Callee<'a> {
162160
_ => false
163161
} => {
164162
let substs = node_id_substs(bcx, ExprId(ref_expr.id));
165-
let def_id = if did.krate != ast::LOCAL_CRATE {
166-
inline::maybe_instantiate_inline(bcx.ccx(), did)
167-
} else {
168-
did
169-
};
163+
let def_id = inline::maybe_instantiate_inline(bcx.ccx(), did);
170164
Callee { bcx: bcx, data: Intrinsic(def_id.node, substs) }
171165
}
172166
def::DefFn(did, _) |
@@ -524,13 +518,7 @@ pub fn trans_fn_ref_with_vtables(
524518

525519
// Check whether this fn has an inlined copy and, if so, redirect
526520
// def_id to the local id of the inlined copy.
527-
let def_id = {
528-
if def_id.krate != ast::LOCAL_CRATE {
529-
inline::maybe_instantiate_inline(ccx, def_id)
530-
} else {
531-
def_id
532-
}
533-
};
521+
let def_id = inline::maybe_instantiate_inline(ccx, def_id);
534522

535523
// We must monomorphise if the fn has type parameters, is a default method,
536524
// or is a named tuple constructor.

src/librustc/middle/trans/expr.rs

+3-15
Original file line numberDiff line numberDiff line change
@@ -830,19 +830,6 @@ fn trans_def<'a>(bcx: &'a Block<'a>,
830830
// an external global, and return a pointer to that.
831831
let const_ty = expr_ty(bcx, ref_expr);
832832

833-
fn get_did(ccx: &CrateContext, did: ast::DefId)
834-
-> ast::DefId {
835-
if did.krate != ast::LOCAL_CRATE {
836-
// Case 2 or 3. Which one we're in is determined by
837-
// whether the DefId produced by `maybe_instantiate_inline`
838-
// is in the LOCAL_CRATE or not.
839-
inline::maybe_instantiate_inline(ccx, did)
840-
} else {
841-
// Case 1.
842-
did
843-
}
844-
}
845-
846833
fn get_val<'a>(bcx: &'a Block<'a>, did: ast::DefId, const_ty: ty::t)
847834
-> ValueRef {
848835
// For external constants, we don't inline.
@@ -881,8 +868,9 @@ fn trans_def<'a>(bcx: &'a Block<'a>,
881868
}
882869
}
883870
}
884-
885-
let did = get_did(bcx.ccx(), did);
871+
// The DefId produced by `maybe_instantiate_inline`
872+
// may be in the LOCAL_CRATE or not.
873+
let did = inline::maybe_instantiate_inline(bcx.ccx(), did);
886874
let val = get_val(bcx, did, const_ty);
887875
DatumBlock::new(bcx, Datum::new(val, const_ty, LvalueExpr))
888876
}

src/librustc/middle/trans/inline.rs

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -19,18 +19,18 @@ use syntax::ast;
1919
use syntax::ast_util::{local_def, PostExpansionMethod};
2020
use syntax::ast_util;
2121

22-
pub fn maybe_instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId)
23-
-> ast::DefId {
22+
fn instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId)
23+
-> Option<ast::DefId> {
2424
let _icx = push_ctxt("maybe_instantiate_inline");
2525
match ccx.external().borrow().find(&fn_id) {
2626
Some(&Some(node_id)) => {
2727
// Already inline
2828
debug!("maybe_instantiate_inline({}): already inline as node id {}",
2929
ty::item_path_str(ccx.tcx(), fn_id), node_id);
30-
return local_def(node_id);
30+
return Some(local_def(node_id));
3131
}
3232
Some(&None) => {
33-
return fn_id; // Not inlinable
33+
return None; // Not inlinable
3434
}
3535
None => {
3636
// Not seen yet
@@ -41,10 +41,11 @@ pub fn maybe_instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId)
4141
csearch::maybe_get_item_ast(
4242
ccx.tcx(), fn_id,
4343
|a,b,c,d| astencode::decode_inlined_item(a, b, c, d));
44-
return match csearch_result {
44+
45+
let inline_def = match csearch_result {
4546
csearch::not_found => {
4647
ccx.external().borrow_mut().insert(fn_id, None);
47-
fn_id
48+
return None;
4849
}
4950
csearch::found(ast::IIItem(item)) => {
5051
ccx.external().borrow_mut().insert(fn_id, Some(item.id));
@@ -182,4 +183,19 @@ pub fn maybe_instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId)
182183
}
183184
}
184185
};
186+
187+
return Some(inline_def);
188+
}
189+
190+
pub fn get_local_instance(ccx: &CrateContext, fn_id: ast::DefId)
191+
-> Option<ast::DefId> {
192+
if fn_id.krate == ast::LOCAL_CRATE {
193+
Some(fn_id)
194+
} else {
195+
instantiate_inline(ccx, fn_id)
196+
}
197+
}
198+
199+
pub fn maybe_instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId) -> ast::DefId {
200+
get_local_instance(ccx, fn_id).unwrap_or(fn_id)
185201
}

0 commit comments

Comments
 (0)