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

Allow variables bound in 'try' to be used in 'of' clauses #2662

Merged

Conversation

bjorng
Copy link
Contributor

@bjorng bjorng commented Jun 17, 2020

The following program would not compile:

-module(foo).
baz() ->
    try Bar = 42 of
        _ -> Bar
    after
    ok
    end.

The error message is "foo.erl:4: variable 'Bar' unsafe in 'try' (line 3)".

There is no reason for 'Bar' to be unsafe, because if the pattern
after of is reached, Bar = 42 must have been successfully evaluated.

https://bugs.erlang.org/browse/ERL-1281

The following program would not compile:

    -module(foo).
    baz() ->
        try Bar = 42 of
            _ -> Bar
        after
	    ok
        end.

The error message is "foo.erl:4: variable 'Bar' unsafe in 'try' (line 3)".

There is no reason for 'Bar' to be unsafe, because if the pattern
after `of` is reached, `Bar = 42` must have been successfully evaluated.

https://bugs.erlang.org/browse/ERL-1281
@bjorng bjorng added team:VM Assigned to OTP team VM feature testing currently being tested, tag is used by OTP internal CI labels Jun 17, 2020
@bjorng bjorng self-assigned this Jun 17, 2020
@bjorng bjorng merged commit b3b1648 into erlang:master Jun 18, 2020
@bjorng bjorng deleted the bjorn/compiler/try-export/ERL-1281/OTP-16706 branch June 18, 2020 06:46
bjorng added a commit to bjorng/otp that referenced this pull request Jan 12, 2021
In commit 8a9a443 introduced by
erlang#2662, a restriction regarding
variables in try/catch was lifted, allowing variables bound
between `try` and `of` to be used later:

    baz() ->
        try Bar = 0 of
          _ -> Bar
        after 0
        end.

(In OTP 23 and earlier that would be a compilation error because `Bar`
would be considered unsafe.)

The lifted restriction was not properly handled by dialyzer. Attempting
to analyze the the following module would crash dialyzer:

    -module(dialyzer_bug).
    -export([main/0]).

    main() ->
        try A = foo() of
            _ -> A
        after ok
        end.

    foo() -> 1.

https://bugs.erlang.org/browse/ERL-1454
bjorng added a commit to bjorng/otp that referenced this pull request Jan 12, 2021
In commit 8a9a443 introduced by
erlang#2662, a restriction regarding
variables in try/catch was lifted, allowing variables bound
between `try` and `of` to be used later:

    baz() ->
        try Bar = 0 of
          _ -> Bar
        after 0
        end.

(In OTP 23 and earlier that would be a compilation error because `Bar`
would be considered unsafe.)

The lifted restriction was not properly handled by dialyzer. Attempting
to analyze the the following module would crash dialyzer:

    -module(dialyzer_bug).
    -export([main/0]).

    main() ->
        try A = foo() of
            _ -> A
        after ok
        end.

    foo() -> 1.

https://bugs.erlang.org/browse/ERL-1454
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature team:VM Assigned to OTP team VM testing currently being tested, tag is used by OTP internal CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant