Skip to content

Commit

Permalink
globals are not affect_free (ref #9535)
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash committed Jan 3, 2015
1 parent 21abad2 commit d071904
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
25 changes: 17 additions & 8 deletions base/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1988,7 +1988,19 @@ end
# and some affect-free calls (allow_volatile=false) -- affect_free means the call
# cannot be affected by previous calls, except assignment nodes
function effect_free(e::ANY, sv, allow_volatile::Bool)
if isa(e,Symbol) || isa(e,SymbolNode) || isa(e,Number) || isa(e,AbstractString) ||
if isa(e,SymbolNode)
allow_volatile && return true
if is_global(sv, (e::SymbolNode).name)
return false
end
end
if isa(e,Symbol)
allow_volatile && return true
if is_global(sv, e::Symbol)
return false
end
end
if isa(e,Number) || isa(e,AbstractString) ||
isa(e,TopNode) || isa(e,QuoteNode) || isa(e,Type) || isa(e,Tuple)
return true
end
Expand Down Expand Up @@ -2389,9 +2401,7 @@ function inlineable(f, e::Expr, atypes, sv, enclosing_ast)
# we need to keep it as a variable name
for vi in vinflist
if vi[1] === a && vi[3] != 0
if !islocal
islocal = true
end
islocal = true
aeitype = tmerge(aeitype, vi[2])
if aeitype === Any
break
Expand Down Expand Up @@ -2431,14 +2441,13 @@ function inlineable(f, e::Expr, atypes, sv, enclosing_ast)
occ = 0
for j = length(body.args):-1:1
b = body.args[j]
if occ < 1
occ += occurs_more(b, x->is(x,a), 5)
if occ < 6
occ += occurs_more(b, x->is(x,a), 6)
end
if occ > 0 && affect_free && !effect_free(b, sv, true) #TODO: we could short-circuit this test better by memoizing effect_free(b) in the for loop over i
affect_free = false
end
if occ > 5
occ = 6
if occ > 5 && !affect_free
break
end
end
Expand Down
7 changes: 7 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2076,3 +2076,10 @@ f9534g(a,b,c...) = c[0]
f9534h(a,b,c...) = c[a]
@test f9534h(4,2,3,4,5,6) == 6
@test try; f9534h(5,2,3,4,5,6) catch ex; (ex::BoundsError).a === (3,4,5,6) && ex.i == 5; end

# issue #9535
counter9535 = 0
f9535() = (global counter9535; counter9535 += 1; counter9535)
g9535() = (f9535(),f9535())
@assert g9535() == (1,2)
@assert g9535() == (3,4)

1 comment on commit d071904

@JeffBezanson
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Please sign in to comment.