Skip to content

Commit 17a36ee

Browse files
authored
Make local scope for else blocks in try/catch/else (#51785)
[Docs](https://docs.julialang.org/en/v1/manual/control-flow/#else-Clauses) state: > The try, catch, else, and finally clauses each introduce their own > scope blocks. But it is currently not the case for `else` blocks ```julia julia> try catch else z = 1 end 1 julia> z 1 ``` This change actually makes `else` blocks have their own scope block: ```julia julia> try catch else z = 1 end 1 julia> z ERROR: UndefVarError: `z` not defined ```
1 parent 0907858 commit 17a36ee

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/julia-syntax.scm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,7 @@
14241424
(scope-block ,finalb)))))
14251425
((length> e 3)
14261426
(and (length> e 6) (error "invalid \"try\" form"))
1427-
(let ((elseb (if (length= e 6) (cdddddr e) '())))
1427+
(let ((elseb (if (length= e 6) `((scope-block ,@(cdddddr e))) '())))
14281428
(expand-forms
14291429
`(,(if (null? elseb) 'trycatch 'trycatchelse)
14301430
(scope-block ,tryb)

test/syntax.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3187,6 +3187,22 @@ end
31873187
end
31883188
@test err == 5 + 6
31893189
@test x == 1
3190+
3191+
x = 0
3192+
try
3193+
catch
3194+
else
3195+
x = 1
3196+
end
3197+
@test x == 1
3198+
3199+
try
3200+
catch
3201+
else
3202+
tryelse_in_local_scope = true
3203+
end
3204+
3205+
@test !@isdefined(tryelse_in_local_scope)
31903206
end
31913207

31923208
@test_parseerror """

0 commit comments

Comments
 (0)