Skip to content

Commit 354bfc8

Browse files
committed
Bare functions can coerce to shared closures
1 parent 3bb020a commit 354bfc8

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/comp/middle/typeck.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ fn do_autoderef(fcx: @fn_ctxt, sp: span, t: ty::t) -> ty::t {
891891
fail;
892892
}
893893

894-
fn do_fn_block_coerce(fcx: @fn_ctxt, sp: span, actual: ty::t, expected: ty::t)
894+
fn do_fn_ty_coerce(fcx: @fn_ctxt, sp: span, actual: ty::t, expected: ty::t)
895895
-> ty::t {
896896

897897
// fns can be silently coerced to blocks when being used as
@@ -910,6 +910,15 @@ fn do_fn_block_coerce(fcx: @fn_ctxt, sp: span, actual: ty::t, expected: ty::t)
910910
_ { actual }
911911
}
912912
}
913+
some(ty::ty_fn(ast::proto_bare., args, ret_ty, cf, constrs)) {
914+
alt structure_of_maybe(fcx, sp, expected) {
915+
some(ty::ty_fn(ast::proto_fn., _, _, _, _)) {
916+
ty::mk_fn(fcx.ccx.tcx, ast::proto_fn, args, ret_ty, cf,
917+
constrs)
918+
}
919+
_ { actual }
920+
}
921+
}
913922
_ { actual }
914923
}
915924
}
@@ -932,7 +941,7 @@ mod demand {
932941
ty::t {
933942
full(fcx, sp, expected, actual, [], false).ty
934943
}
935-
fn block_coerce(fcx: @fn_ctxt, sp: span, expected: ty::t, actual: ty::t)
944+
fn fn_coerce(fcx: @fn_ctxt, sp: span, expected: ty::t, actual: ty::t)
936945
-> ty::t {
937946
full(fcx, sp, expected, actual, [], true).ty
938947
}
@@ -945,10 +954,10 @@ mod demand {
945954
// Requires that the two types unify, and prints an error message if they
946955
// don't. Returns the unified type and the type parameter substitutions.
947956
fn full(fcx: @fn_ctxt, sp: span, expected: ty::t, actual: ty::t,
948-
ty_param_substs_0: [ty::t], do_block_coerce: bool) ->
957+
ty_param_substs_0: [ty::t], do_fn_coerce: bool) ->
949958
ty_param_substs_and_ty {
950-
if do_block_coerce {
951-
actual = do_fn_block_coerce(fcx, sp, actual, expected);
959+
if do_fn_coerce {
960+
actual = do_fn_ty_coerce(fcx, sp, actual, expected);
952961
}
953962

954963
let ty_param_substs: [mutable ty::t] = [mutable];
@@ -1676,7 +1685,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
16761685
if is_block == check_blocks {
16771686
bot |=
16781687
check_expr_with_unifier(fcx, a,
1679-
demand::block_coerce,
1688+
demand::fn_coerce,
16801689
arg_tys[i].ty);
16811690
}
16821691
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn# bare() {}
2+
3+
fn likes_shared(f: fn@()) { f() }
4+
5+
fn main() {
6+
likes_shared(bare);
7+
}

0 commit comments

Comments
 (0)