Skip to content

Commit be46efe

Browse files
committed
steal tests from #59735
1 parent 2774d62 commit be46efe

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

test/worlds.jl

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,3 +611,89 @@ end
611611
# Test that the hash function works without world age issues
612612
@test hash(bar59429, UInt(0)) isa UInt
613613
end
614+
615+
# Backdating tests
616+
module BackdateConstTest
617+
using Test
618+
const world_before = Base.get_world_counter()
619+
f() = BACKDATED_CONST # defined in old world
620+
const world_after_f = Base.get_world_counter()
621+
const BACKDATED_CONST = 42 # defined in new world
622+
@test BACKDATED_CONST == 42
623+
if Base.JLOptions().depwarn <= 1
624+
# depwarn=no or yes: backdating works, emits warning
625+
# Call f from the old world (before BACKDATED_CONST was defined)
626+
@test_warn "Detected access to binding `BackdateConstTest.BACKDATED_CONST`" Base.invoke_in_world(world_after_f, f)
627+
@test Base.invoke_in_world(world_before, isdefinedglobal, @__MODULE__, :BACKDATED_CONST)
628+
else
629+
# depwarn=error: backdating disabled
630+
@test !Base.invoke_in_world(world_before, isdefinedglobal, @__MODULE__, :BACKDATED_CONST)
631+
@test_throws UndefVarError Base.invoke_in_world(world_after_f, f)
632+
end
633+
end
634+
635+
module BackdateImportTest
636+
using Test
637+
module Source
638+
exported_value = 42 # Use a value, not a function
639+
end
640+
const world_before = Base.get_world_counter()
641+
g() = exported_value # defined in old world
642+
const world_after_g = Base.get_world_counter()
643+
import .Source: exported_value # import in new world
644+
@test exported_value == 42
645+
if Base.JLOptions().depwarn <= 1
646+
@test_warn "Detected access to binding `BackdateImportTest.exported_value`" Base.invoke_in_world(world_after_g, g)
647+
@test Base.invoke_in_world(world_before, isdefinedglobal, @__MODULE__, :exported_value)
648+
else
649+
@test !Base.invoke_in_world(world_before, isdefinedglobal, @__MODULE__, :exported_value)
650+
@test_throws UndefVarError Base.invoke_in_world(world_after_g, g)
651+
end
652+
end
653+
654+
module BackdateGlobalTest
655+
using Test
656+
const world_before = Base.get_world_counter()
657+
h() = backdated_global # defined in old world
658+
const world_after_h = Base.get_world_counter()
659+
global backdated_global::Int = 123 # defined in new world
660+
@test backdated_global == 123
661+
if Base.JLOptions().depwarn <= 1
662+
@test_warn "Detected access to binding `BackdateGlobalTest.backdated_global`" Base.invoke_in_world(world_after_h, h)
663+
@test Base.invoke_in_world(world_before, isdefinedglobal, @__MODULE__, :backdated_global)
664+
else
665+
@test !Base.invoke_in_world(world_before, isdefinedglobal, @__MODULE__, :backdated_global)
666+
@test_throws UndefVarError Base.invoke_in_world(world_after_h, h)
667+
end
668+
end
669+
670+
# Test case from issue #58511
671+
module BackdateIssue58511
672+
using Test
673+
function foo()
674+
eval(:(a = 42))
675+
return a
676+
end
677+
if Base.JLOptions().depwarn <= 1
678+
@test_warn "Detected access to binding `BackdateIssue58511.a`" foo()
679+
else
680+
@test_throws UndefVarError foo()
681+
end
682+
end
683+
684+
# Test that implicit imports are NOT backdated
685+
module BackdateNoImplicitTest
686+
using Test
687+
module Source1
688+
export shared_name
689+
shared_name() = 1
690+
end
691+
module Source2
692+
export shared_name
693+
shared_name() = 2
694+
end
695+
const world_before = Base.get_world_counter()
696+
using .Source1, .Source2
697+
@test_throws UndefVarError shared_name()
698+
@test !Base.invoke_in_world(world_before, isdefinedglobal, @__MODULE__, :shared_name)
699+
end

0 commit comments

Comments
 (0)