Skip to content

Commit deac82a

Browse files
authored
lowering: don't reverse handler order in (pop-handler-list ...) (JuliaLang#55871)
We were accidentally emitting a different pop order for `Expr(:leave, ...)` if you uncomment the `nothing` below: ```julia let src = Meta.@lower let try try return 1 catch end finally # nothing # <- uncomment me end end println.(filter(stmt->Base.isexpr(stmt, :leave), src.args[1].code)) nothing end ```
1 parent aff6512 commit deac82a

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

src/julia-syntax.scm

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4447,15 +4447,16 @@ f(x) = yt(x)
44474447
(define (pop-handler-list src-tokens dest-tokens lab)
44484448
(if (eq? src-tokens dest-tokens)
44494449
#f
4450-
(let loop ((s src-tokens)
4451-
(l '()))
4452-
(if (not (pair? s))
4453-
(if (null? lab)
4454-
(error "Attempt to jump into catch block")
4455-
(error (string "cannot goto label \"" lab "\" inside try/catch block"))))
4456-
(if (eq? (cdr s) dest-tokens)
4457-
(cons (car s) l)
4458-
(loop (cdr s) (cons (car s) l))))))
4450+
(reverse
4451+
(let loop ((s src-tokens)
4452+
(l '()))
4453+
(if (not (pair? s))
4454+
(if (null? lab)
4455+
(error "Attempt to jump into catch block")
4456+
(error (string "cannot goto label \"" lab "\" inside try/catch block"))))
4457+
(if (eq? (cdr s) dest-tokens)
4458+
(cons (car s) l)
4459+
(loop (cdr s) (cons (car s) l)))))))
44594460
(define (emit-return tail x)
44604461
(define (emit- x)
44614462
(let* ((tmp (if ((if (null? catch-token-stack) valid-ir-return? simple-atom?) x)

test/syntax.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3865,6 +3865,29 @@ end
38653865
end
38663866
end
38673867

3868+
let src = Meta.@lower let
3869+
try
3870+
try
3871+
return 1
3872+
catch
3873+
end
3874+
finally
3875+
nothing
3876+
end
3877+
end
3878+
code = src.args[1].code
3879+
for stmt in code
3880+
if Meta.isexpr(stmt, :leave) && length(stmt.args) > 1
3881+
# Expr(:leave, ...) should list the arguments to pop from
3882+
# inner-most scope to outer-most
3883+
@test issorted(Int[
3884+
(arg::Core.SSAValue).id
3885+
for arg in stmt.args
3886+
]; rev=true)
3887+
end
3888+
end
3889+
end
3890+
38683891
# Test that globals can be `using`'d even if they are not yet defined
38693892
module UndefGlobal54954
38703893
global theglobal54954::Int

0 commit comments

Comments
 (0)