Skip to content

Commit 0c8768f

Browse files
authored
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
1 parent 9c31eec commit 0c8768f

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
@@ -5106,6 +5106,7 @@ f(x) = yt(x)
51065106
(check-top-level e)
51075107
(let ((val (make-ssavalue)))
51085108
(emit `(= ,val ,e))
5109+
(emit `(latestworld))
51095110
(if tail (emit-return tail val))
51105111
val))
51115112

test/worlds.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,3 +598,21 @@ function f()
598598
end
599599
end
600600
@test_throws ErrorException("importing Random into M57965 conflicts with an existing global") M57965.f()
601+
602+
# issue #59429 - world age semantics with toplevel in macros
603+
module M59429
604+
using Test
605+
macro new_enum(T::Symbol, args...)
606+
esc(quote
607+
@enum $T $(args...)
608+
function Base.hash(x::$T, h::UInt)
609+
rand(UInt)
610+
end
611+
end)
612+
end
613+
614+
@new_enum Foo59429 bar59429 baz59429
615+
616+
# Test that the hash function works without world age issues
617+
@test hash(bar59429, UInt(0)) isa UInt
618+
end

0 commit comments

Comments
 (0)