-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Mutable variables - let 1 become 0 #6873
Comments
This also incorrectly succeeds and rebinds a variable with other expressions in the match tail, not just plain variables, e.g. test2() ->
Zero = 0,
Result = Zero = self(),
io:format("~p~n", [Zero]),
Result. 1> test:test2().
<0.81.0>
<0.81.0> |
It is also incorrect in OTP 23 and probably in many earlier releases. It seems that the bug was inadvertently corrected in #6415. My current plan is to backport part of that PR to OTP 23 through 25 in order to fix this bug. |
Looks like the comment I was replying to is gone now
test3() ->
Zero = 0,
One = 1,
F = fun(One) -> io:format("~p~n", [One]) end,
F(Zero). The compiler also issues a warning about this:
|
The compiler would generate incorrect code for the following type of expression: Pattern = BoundVar1 = . . . = BoundVarN = Expression An exception should be raised if any of the bound variables have different values than `Expression`. The compiler would generate code that would cause the bound variables to be bound to the value of `Expression` whether the value matched or not. For example: t() -> Zero = 0, One = 1, Result = One = Zero, {Result,One,Zero}. There should be a `badmatch` exception, but instead, the `t/0` function returned `{0,0,0}`. Closes erlang#6873
The compiler would generate incorrect code for the following type of expression: Pattern = BoundVar1 = . . . = BoundVarN = Expression An exception should be raised if any of the bound variables have different values than `Expression`. The compiler would generate code that would cause the bound variables to be bound to the value of `Expression` whether the value matched or not. For example: t() -> Zero = 0, One = 1, Result = One = Zero, {Result,One,Zero}. There should be a `badmatch` exception, but instead, the `t/0` function returned `{0,0,0}`. Closes erlang#6873
…70' into maint * bjorn/compiler/fix-mutable-variables/24/GH-6873/OTP-18470: Eliminate "mutable variables"
Thanks for fixing this quickly! |
…70' into maint * bjorn/compiler/fix-mutable-variables/23/GH-6873/OTP-18470: Eliminate "mutable variables"
…70' into maint-24 * bjorn/compiler/fix-mutable-variables/24/GH-6873/OTP-18470: Eliminate "mutable variables"
…70' into maint-23 * bjorn/compiler/fix-mutable-variables/23/GH-6873/OTP-18470: Eliminate "mutable variables"
Describe the bug
In some specific cases pattern matching doesn't do what it's supposed to - instead it mutates variables.
To Reproduce
Given the following snippet:
Compiling the
test
module and runningtest:test()
from the shell produces:Expected behavior
The code should crash with match error, and variables should remain immutable.
Affected versions
Tested with the latest
maint-25
andmaint-24
branches. This correctly fails on current master, even emitting a warning during compilation witherlc
.Additional context
Originally discovered by @jcpetruzza
The text was updated successfully, but these errors were encountered: