Skip to content

Commit 9174682

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 c171ddb commit 9174682

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
@@ -4063,14 +4063,12 @@ f(x) = yt(x)
40634063
(and vi (vinfo:nospecialize vi))))
40644064

40654065
(define (toplevel-preserving? e)
4066-
(and (pair? e) (memq (car e) '(if elseif block trycatch tryfinally trycatchelse))))
4066+
(and (pair? e) (memq (car e) '(if elseif block trycatch tryfinally trycatchelse = const))))
40674067

40684068
(define (map-cl-convert exprs fname lam namemap defined toplevel interp opaq toplevel-pure parsed-method-stack (globals (table)) (locals (table)))
40694069
(if toplevel
40704070
(map (lambda (x)
4071-
(let ((tl (lift-toplevel (cl-convert x fname lam namemap defined
4072-
(and toplevel (toplevel-preserving? x))
4073-
interp opaq toplevel-pure parsed-method-stack globals locals))))
4071+
(let ((tl (lift-toplevel (cl-convert x fname lam namemap defined toplevel interp opaq toplevel-pure parsed-method-stack globals locals))))
40744072
(if (null? (cdr tl))
40754073
(car tl)
40764074
`(block ,@(cdr tl) ,(car tl)))))
@@ -4473,7 +4471,7 @@ f(x) = yt(x)
44734471
(cl-convert (cadr e) fname lam namemap defined toplevel interp opaq toplevel-pure parsed-method-stack globals locals))
44744472
(else
44754473
(cons (car e)
4476-
(map-cl-convert (cdr e) fname lam namemap defined toplevel interp opaq toplevel-pure parsed-method-stack globals locals))))))))
4474+
(map-cl-convert (cdr e) fname lam namemap defined (and toplevel (toplevel-preserving? e)) interp opaq toplevel-pure parsed-method-stack globals locals))))))))
44774475

44784476
;; wrapper for `cl-convert-`
44794477
(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)