Skip to content

Commit 52ed9a2

Browse files
committed
Make = and const toplevel-preserving syntax
Previously, `map-cl-convert` would keep the toplevel flag only when the containing expression was toplevel and each element of the list passed to it satisfied `toplevel-preserving?`. Instead, make it so `toplevel-preserving?` expressions are those where the toplevel flag should be propagated to the children during closure conversion. Fixes #59755.
1 parent 03f031a commit 52ed9a2

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

src/julia-syntax.scm

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4042,14 +4042,12 @@ f(x) = yt(x)
40424042
(and vi (vinfo:nospecialize vi))))
40434043

40444044
(define (toplevel-preserving? e)
4045-
(and (pair? e) (memq (car e) '(if elseif block trycatch tryfinally trycatchelse))))
4045+
(and (pair? e) (memq (car e) '(if elseif block trycatch tryfinally trycatchelse = const))))
40464046

40474047
(define (map-cl-convert exprs fname lam namemap defined toplevel interp opaq toplevel-pure parsed-method-stack (globals (table)) (locals (table)))
40484048
(if toplevel
40494049
(map (lambda (x)
4050-
(let ((tl (lift-toplevel (cl-convert x fname lam namemap defined
4051-
(and toplevel (toplevel-preserving? x))
4052-
interp opaq toplevel-pure parsed-method-stack globals locals))))
4050+
(let ((tl (lift-toplevel (cl-convert x fname lam namemap defined toplevel interp opaq toplevel-pure parsed-method-stack globals locals))))
40534051
(if (null? (cdr tl))
40544052
(car tl)
40554053
`(block ,@(cdr tl) ,(car tl)))))
@@ -4452,7 +4450,7 @@ f(x) = yt(x)
44524450
(cl-convert (cadr e) fname lam namemap defined toplevel interp opaq toplevel-pure parsed-method-stack globals locals))
44534451
(else
44544452
(cons (car e)
4455-
(map-cl-convert (cdr e) fname lam namemap defined toplevel interp opaq toplevel-pure parsed-method-stack globals locals))))))))
4453+
(map-cl-convert (cdr e) fname lam namemap defined (and toplevel (toplevel-preserving? e)) interp opaq toplevel-pure parsed-method-stack globals locals))))))))
44564454

44574455
;; wrapper for `cl-convert-`
44584456
(define (cl-convert e fname lam namemap defined toplevel interp opaq toplevel-pure (parsed-method-stack '()) (globals (table)) (locals (table)))

test/syntax.jl

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4617,3 +4617,47 @@ end
46174617
@test_throws UndefVarError macroexpand(@__MODULE__, :(@undefined_macro(x)))
46184618
@test_throws UndefVarError macroexpand!(@__MODULE__, :(@undefined_macro(x)))
46194619
end
4620+
4621+
# #59755 - Don't hoist global declarations out of toplevel-preserving syntax
4622+
module M59755 end
4623+
@testset "toplevel-preserving syntax" begin
4624+
Core.eval(M59755, :(if true
4625+
global v1::Bool
4626+
else
4627+
const v1 = 1
4628+
end))
4629+
@test !isdefined(M59755, :v1)
4630+
@test Base.binding_kind(M59755, :v1) == Base.PARTITION_KIND_GLOBAL
4631+
@test Core.get_binding_type(M59755, :v1) == Bool
4632+
4633+
Core.eval(M59755, :(if false
4634+
global v2::Bool
4635+
else
4636+
const v2 = 2
4637+
end))
4638+
@test M59755.v2 === 2
4639+
@test Base.binding_kind(M59755, :v2) == Base.PARTITION_KIND_CONST
4640+
4641+
Core.eval(M59755, :(v3 = if true
4642+
global v4::Bool
4643+
4
4644+
else
4645+
const v4 = 5
4646+
6
4647+
end))
4648+
@test M59755.v3 == 4
4649+
@test !isdefined(M59755, :v4)
4650+
@test Base.binding_kind(M59755, :v4) == Base.PARTITION_KIND_GLOBAL
4651+
@test Core.get_binding_type(M59755, :v4) == Bool
4652+
4653+
Core.eval(M59755, :(v5 = if false
4654+
global v6::Bool
4655+
4
4656+
else
4657+
const v6 = 5
4658+
6
4659+
end))
4660+
@test M59755.v5 === 6
4661+
@test M59755.v6 === 5
4662+
@test Base.binding_kind(M59755, :v6) == Base.PARTITION_KIND_CONST
4663+
end

0 commit comments

Comments
 (0)