Skip to content

Commit 5f0a123

Browse files
committed
Construct const fns based on the type, not the definition.
Otherwise we can add a null environment when we shouldn't. Fixes rust-lang#5210.
1 parent a7de81a commit 5f0a123

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/librustc/middle/trans/consts.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -394,13 +394,22 @@ pub fn const_expr(cx: @CrateContext, e: @ast::expr) -> ValueRef {
394394
ast::expr_path(pth) => {
395395
assert pth.types.len() == 0;
396396
match cx.tcx.def_map.find(&e.id) {
397-
Some(ast::def_fn(def_id, purity)) => {
397+
Some(ast::def_fn(def_id, _purity)) => {
398398
assert ast_util::is_local(def_id);
399399
let f = base::get_item_val(cx, def_id.node);
400-
match purity {
401-
ast::extern_fn =>
402-
llvm::LLVMConstPointerCast(f, T_ptr(T_i8())),
403-
_ => C_struct(~[f, C_null(T_opaque_box_ptr(cx))])
400+
let ety = ty::expr_ty_adjusted(cx.tcx, e);
401+
match ty::get(ety).sty {
402+
ty::ty_bare_fn(*) | ty::ty_ptr(*) => {
403+
llvm::LLVMConstPointerCast(f, T_ptr(T_i8()))
404+
}
405+
ty::ty_closure(*) => {
406+
C_struct(~[f, C_null(T_opaque_box_ptr(cx))])
407+
}
408+
_ => {
409+
cx.sess.span_bug(e.span, fmt!(
410+
"unexpected const fn type: %s",
411+
ty_to_str(cx.tcx, ety)))
412+
}
404413
}
405414
}
406415
Some(ast::def_const(def_id)) => {

0 commit comments

Comments
 (0)