Skip to content

Make local scope for else blocks in try/catch/else#51785

Merged
KristofferC merged 1 commit intoJuliaLang:masterfrom
Pangoraw:else_local_scope
Oct 24, 2023
Merged

Make local scope for else blocks in try/catch/else#51785
KristofferC merged 1 commit intoJuliaLang:masterfrom
Pangoraw:else_local_scope

Conversation

@Pangoraw
Copy link
Copy Markdown
Contributor

Docs 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> try
       catch
       else
           z = 1
       end
1

julia> z
1

This change actually makes else blocks have their own scope block:

julia> try
       catch
       else
           z = 1
       end
1

julia> z
ERROR: UndefVarError: `z` not defined

[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> try
       catch
       else
           z = 1
       end
1

julia> z
ERROR: UndefVarError: `z` not defined
```
Copy link
Copy Markdown
Member

@simeonschaub simeonschaub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

@simeonschaub simeonschaub added compiler:lowering Syntax lowering (compiler front end, 2nd stage) bugfix This change fixes an existing bug backport 1.9 Change should be backported to release-1.9 backport 1.10 Change should be backported to the 1.10 release labels Oct 19, 2023
@KristofferC KristofferC merged commit 17a36ee into JuliaLang:master Oct 24, 2023
KristofferC pushed a commit that referenced this pull request Oct 24, 2023
[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
```

(cherry picked from commit 17a36ee)
@Pangoraw Pangoraw deleted the else_local_scope branch October 25, 2023 12:31
@KristofferC KristofferC removed the backport 1.10 Change should be backported to the 1.10 release label Nov 6, 2023
@KristofferC KristofferC mentioned this pull request Mar 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport 1.9 Change should be backported to release-1.9 bugfix This change fixes an existing bug compiler:lowering Syntax lowering (compiler front end, 2nd stage)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants