Skip to content

Commit 0375ee2

Browse files
authored
Always interpret in current world (#478)
* always interpret in current world * add a test * julia 1.0 compat
1 parent bd55bd9 commit 0375ee2

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/utils.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,20 @@ function to_function(@nospecialize(x))
2525
isa(x, GlobalRef) ? getfield(x.mod, x.name) : x
2626
end
2727

28+
@static if isdefined(Base, :get_world_counter)
29+
import Base: get_world_counter
30+
else
31+
get_world_counter() = ccall(:jl_get_world_counter, UInt, ())
32+
end
33+
2834
"""
2935
method = whichtt(tt)
3036
3137
Like `which` except it operates on the complete tuple-type `tt`.
3238
"""
3339
function whichtt(@nospecialize(tt))
3440
# TODO: provide explicit control over world age? In case we ever need to call "old" methods.
35-
m = ccall(:jl_gf_invoke_lookup, Any, (Any, UInt), tt, typemax(UInt))
41+
m = ccall(:jl_gf_invoke_lookup, Any, (Any, UInt), tt, get_world_counter())
3642
m === nothing && return nothing
3743
isa(m, Method) && return m
3844
return m.func::Method

test/interpret.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,3 +758,28 @@ end
758758

759759
@test @interpret(g()) === true
760760
end
761+
762+
const override_world = typemax(Csize_t) - 1
763+
macro unreachable(ex)
764+
quote
765+
world_counter = cglobal(:jl_world_counter, Csize_t)
766+
regular_world = unsafe_load(world_counter)
767+
768+
$(Expr(:tryfinally, # don't introduce scope
769+
quote
770+
unsafe_store!(world_counter, $(override_world-1))
771+
$(esc(ex))
772+
end,
773+
quote
774+
unsafe_store!(world_counter, regular_world)
775+
end
776+
))
777+
end
778+
end
779+
780+
@testset "unreachable worlds" begin
781+
foobar() = 42
782+
@unreachable foobar() = "nope"
783+
784+
@test @interpret(foobar()) == foobar()
785+
end

0 commit comments

Comments
 (0)