[ruby-core:117844] [Ruby master Bug#20482] nil variables in a guard clause of a standalone => or in expression
From:
"nobu (Nobuyoshi Nakada) via ruby-core" <ruby-core@...>
Date:
2024-05-12 07:05:52 UTC
List:
ruby-core #117844
Issue #20482 has been updated by nobu (Nobuyoshi Nakada).
Soilent (Konstantin x) wrote in #note-2:
> Thank you, nobu! That explains it.
>
> Just to clarify, is this how the expression is parsed?
>
> ```ruby
> ( [1, 2] in a, b ) if b == 2*a
> ```
Yes, you can confirm the AST by `ruby --dump=parsetree -e '1 in a if a'` (the code is simplified).
> And since this is a modifier `if`, the parser creates variables a and b first, which is why they are defined in the test expression?
Yes, `a` and `b` are created by `[1, 2] in a, b` and available in the `if` condition.
----------------------------------------
Bug #20482: nil variables in a guard clause of a standalone => or in expression
https://bugs.ruby-lang.org/issues/20482#change-108249
* Author: Soilent (Konstantin x)
* Status: Open
* ruby -v: ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23]
* Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
The following expression produces a TypeError, which is quite unexpected:
```ruby
[1, 2] in a, b if b == 2*a
```
```
x.rb:1:in `*': nil can't be coerced into Integer (TypeError)
[1, 2] in a, b if b == 2*a
^
from x.rb:1:in `<main>'
```
The expression above should be equivalent to the following one, which works as expected:
```ruby
case [1, 2]
in a, b if b == 2*a
true
else
false
end
# => true
```
Apologies in advance if this is intentional or has been reported before.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/