Skip to content

Commit d720865

Browse files
committed
fix #4688, inlining too early inside method definitions
1 parent 1dbd8b5 commit d720865

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

base/inference.jl

+8-3
Original file line numberDiff line numberDiff line change
@@ -1528,9 +1528,14 @@ function type_annotate(ast::Expr, states::Array{Any,1}, sv::ANY, rettype::ANY,
15281528
vi[2] = get(decls, vi[1], vi[2])
15291529
end
15301530
end
1531-
na = length(a.args[1])
1532-
li.ast, _ = typeinf(li, ntuple(na+1, i->(i>na ? (Tuple)[1] : Any)),
1533-
li.sparams, li, false)
1531+
# NOTE: this is disabled, as it leads to inlining too early.
1532+
# See issue #4688. We should wait until inner functions are called
1533+
# to optimize them; this will be done by the method cache or
1534+
# builtins.c:jl_trampoline. However if jl_trampoline is changed then
1535+
# this code will need to be restored.
1536+
#na = length(a.args[1])
1537+
#li.ast, _ = typeinf(li, ntuple(na+1, i->(i>na ? (Tuple)[1] : Any)),
1538+
# li.sparams, li, false)
15341539
end
15351540
end
15361541

src/builtins.c

+2
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,8 @@ JL_CALLABLE(jl_trampoline)
801801
jl_function_t *f = (jl_function_t*)F;
802802
assert(f->linfo != NULL);
803803
// to run inference on all thunks. slows down loading files.
804+
// NOTE: if this call to inference is removed, type_annotate in inference.jl
805+
// needs to be updated to infer inner functions.
804806
if (f->linfo->inferred == 0) {
805807
if (!jl_in_inference) {
806808
if (!jl_is_expr(f->linfo->ast)) {

test/core.jl

+11
Original file line numberDiff line numberDiff line change
@@ -1099,3 +1099,14 @@ f4479(::Real,c) = 1
10991099
f4479(::Int, ::Int, ::Bool) = 2
11001100
f4479(::Int, x, a...) = 0
11011101
@test f4479(1,1,true) == 2
1102+
1103+
# issue #4688
1104+
a4688(y) = "should be unreachable by calling b"
1105+
b4688(y) = "not an Int"
1106+
begin
1107+
a4688(y::Int) = "an Int"
1108+
let x = true
1109+
b4688(y::Int) = x == true ? a4688(y) : a4688(y)
1110+
end
1111+
end
1112+
@test b4688(1) == "an Int"

0 commit comments

Comments
 (0)