Skip to content

Commit b8bccfc

Browse files
KenoKristofferC
authored andcommitted
lowering: increment world age after toplevel expressions (#59703)
When a macro expands to `:toplevel` expression, it seems reasonable (as would be if the expression were not wrapped in `:toplevel`) that any contained struct definitions be available thereafter. For example, this applies to `@enum`, which countrly causes world age erros when attempting to define a method on an enum within the same expression. Fix this by having lowering insert and explicit `latestworld` after toplevel. Fixes #59429 Written by Claude (cherry picked from commit 0c8768f)
1 parent 113d097 commit b8bccfc

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/julia-syntax.scm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5032,6 +5032,7 @@ f(x) = yt(x)
50325032
(check-top-level e)
50335033
(let ((val (make-ssavalue)))
50345034
(emit `(= ,val ,e))
5035+
(emit `(latestworld))
50355036
(if tail (emit-return tail val))
50365037
val))
50375038

test/worlds.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,3 +589,21 @@ module C57316; import ..X57316.Y57316 as Z, .Z.Y57316 as W; end
589589
@test !isdefined(B57316, :X57316)
590590
@test !isdefined(C57316, :X57316)
591591
@test !isdefined(C57316, :Y57316)
592+
593+
# issue #59429 - world age semantics with toplevel in macros
594+
module M59429
595+
using Test
596+
macro new_enum(T::Symbol, args...)
597+
esc(quote
598+
@enum $T $(args...)
599+
function Base.hash(x::$T, h::UInt)
600+
rand(UInt)
601+
end
602+
end)
603+
end
604+
605+
@new_enum Foo59429 bar59429 baz59429
606+
607+
# Test that the hash function works without world age issues
608+
@test hash(bar59429, UInt(0)) isa UInt
609+
end

0 commit comments

Comments
 (0)