Skip to content

Conversation

@IanButterworth
Copy link
Member

@IanButterworth IanButterworth commented Jan 12, 2021

It could be generally useful to evaluate a try in the local scope, as is the case in #39138

Based on a suggestion by @vtjnash this adds @noscope for making try blocks evaluate in the local scope

julia> x
ERROR: UndefVarError: x not defined

julia> try
            x = 1
            error()
        catch err
            display(err)
        finally
            println("finally")
        end
ErrorException()
finally

julia> x
ERROR: UndefVarError: x not defined

julia> @noscope try
            x = 1
            error()
        catch err
            display(err)
        finally
            println("finally")
        end
ErrorException()
finally

julia> x
1

If this is supported, there may be a better place for the code and tests to sit?

@vtjnash vtjnash added the triage This should be discussed on a triage call label Jan 12, 2021
1
```
"""
macro noscope(ex)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe

Suggested change
macro noscope(ex)
macro noscope360(ex)

?

Expr(:trycatch,
:($(esc(ex.args[1]))),
quote
begin
Copy link
Member Author

@IanButterworth IanButterworth Jan 13, 2021

Choose a reason for hiding this comment

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

Actually, I think this and all the finally and catch blocks should still be scoped

Suggested change
begin
let

Copy link

@f0103 f0103 left a comment

Choose a reason for hiding this comment

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

@JeffBezanson
Copy link
Member

From triage:

  • Basically yes
  • Would be nice to implement it properly in the front-end, and handle all kinds of scope-introducing blocks
  • We probably still want the variable in catch e to be local to its block. Similarly for @noscope let x = ..., the x should still be local, but nothing inside should introduce new variables to the scope. That will require some surgery in the scope pass.

@StefanKarpinski StefanKarpinski added feature Indicates new feature / enhancement requests compiler:lowering Syntax lowering (compiler front end, 2nd stage) and removed triage This should be discussed on a triage call labels Apr 29, 2021
@IanButterworth
Copy link
Member Author

With #48434 being open and it being unlikely that I'd take this forward, I'll close this

@IanButterworth IanButterworth deleted the ib/noscope_try branch December 7, 2023 18:24
@oOosys

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler:lowering Syntax lowering (compiler front end, 2nd stage) feature Indicates new feature / enhancement requests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants