Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler: fix incorrect var type insisde nested exception handler #11926

Closed
wants to merge 3 commits into from

Conversation

asterite
Copy link
Member

@asterite asterite commented Mar 24, 2022

Fixes #9769

@asterite asterite marked this pull request as draft March 24, 2022 12:07
@asterite
Copy link
Member Author

Well, this was not right...

@asterite asterite closed this Mar 24, 2022
@asterite asterite reopened this Mar 24, 2022
@asterite
Copy link
Member Author

This was actually good because I'm starting to understand the problem... but it'll take me longer to fix this.

@asterite asterite marked this pull request as ready for review March 24, 2022 15:37
@asterite asterite marked this pull request as draft March 24, 2022 15:37
@asterite asterite marked this pull request as ready for review March 24, 2022 15:50
@asterite
Copy link
Member Author

What happened with CI here? 😕

@HertzDevil HertzDevil added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:semantic labels Mar 24, 2022
@straight-shoota
Copy link
Member

GitHub actions seems to have a service disruption.

Comment on lines +717 to +726
a = 1.5
begin
a = 2
a = 'a'
a = "hello"
rescue ex
a = false
end
a
)) { union_of [float64, int32, char, string, bool] of Type }
Copy link
Member

Choose a reason for hiding this comment

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

This looks a bit odd. I realize this spec is not new. But could the type be more restricted?
After the begin/end it can really only be String (last type from begin branch) or Bool (last type from rescue branch).
Just mentioning this. It's probably a separate issue.

Copy link
Member Author

Choose a reason for hiding this comment

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

The compiler doesn't check what's actually happening in the begin body. All it does is to assume that if a variable is assigned a value inside it, well, it might never be assigned a value at all because an exception could be thrown in the middle. And that exception could also be thrown in-between assignments. I know that a simple value like 2 or "hello" can't raise, but the compiler doesn't take that into account. It's an improvement for sure. But I think this assumption is actually simpler. If you don't want that to happen, extract the variables outside of the exception handler: problem solved, and cleaner code.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually this is quite tricky to change. For example:

a = 2
raise "OH NO!" if something
a = 'a'

So it's of course not just about them being simple assignments. It's about knowing that an exception could be thrown somewhere in the middle. It's a pretty complex change, and I'm not actually sure it's either doable or worth doing. Nobody complained about this so far, so I think we shouldn't change this.

property exception_handler_vars : MetaVars? = nil
property all_exception_handler_vars : Array(MetaVars)? = nil
Copy link
Member Author

Choose a reason for hiding this comment

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

To explain the change with the minimum amount of words: instead of tracking a single set of variables to track on exception handlers, we need a list of them to take into account nested contexts.

@asterite
Copy link
Member Author

Replaced by #11928

@asterite asterite closed this Mar 25, 2022
@straight-shoota
Copy link
Member

I suppose you might've triggered CI by just adding an empty commit to the branch. But new PR is fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:semantic
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expr in nested begin..rescue doesn't update type union
3 participants