ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:117833] [Ruby master Bug#20482] nil variables in a guard clause of a standalone => or in expression
@ 2024-05-11 16:45 Soilent (Konstantin x) via ruby-core
  2024-05-12  0:05 ` [ruby-core:117836] " nobu (Nobuyoshi Nakada) via ruby-core
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Soilent (Konstantin x) via ruby-core @ 2024-05-11 16:45 UTC (permalink / raw
  To: ruby-core; +Cc: Soilent (Konstantin x)

Issue #20482 has been reported by Soilent (Konstantin x).

----------------------------------------
Bug #20482: nil variables in a guard clause of a standalone => or in expression
https://bugs.ruby-lang.org/issues/20482

* 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/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [ruby-core:117836] [Ruby master Bug#20482] nil variables in a guard clause of a standalone => or in expression
  2024-05-11 16:45 [ruby-core:117833] [Ruby master Bug#20482] nil variables in a guard clause of a standalone => or in expression Soilent (Konstantin x) via ruby-core
@ 2024-05-12  0:05 ` nobu (Nobuyoshi Nakada) via ruby-core
  2024-05-12  6:08 ` [ruby-core:117841] " Soilent (Konstantin x) via ruby-core
  2024-05-12  7:05 ` [ruby-core:117844] " nobu (Nobuyoshi Nakada) via ruby-core
  2 siblings, 0 replies; 4+ messages in thread
From: nobu (Nobuyoshi Nakada) via ruby-core @ 2024-05-12  0:05 UTC (permalink / raw
  To: ruby-core; +Cc: nobu (Nobuyoshi Nakada)

Issue #20482 has been updated by nobu (Nobuyoshi Nakada).


One-line pattern matching can not have a guard clause, so the `if` in the first example is a modifier `if` and is evaluated before the pattern matching.
In short, it should be intentional, I think.

----------------------------------------
Bug #20482: nil variables in a guard clause of a standalone => or in expression
https://bugs.ruby-lang.org/issues/20482#change-108240

* 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/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [ruby-core:117841] [Ruby master Bug#20482] nil variables in a guard clause of a standalone => or in expression
  2024-05-11 16:45 [ruby-core:117833] [Ruby master Bug#20482] nil variables in a guard clause of a standalone => or in expression Soilent (Konstantin x) via ruby-core
  2024-05-12  0:05 ` [ruby-core:117836] " nobu (Nobuyoshi Nakada) via ruby-core
@ 2024-05-12  6:08 ` Soilent (Konstantin x) via ruby-core
  2024-05-12  7:05 ` [ruby-core:117844] " nobu (Nobuyoshi Nakada) via ruby-core
  2 siblings, 0 replies; 4+ messages in thread
From: Soilent (Konstantin x) via ruby-core @ 2024-05-12  6:08 UTC (permalink / raw
  To: ruby-core; +Cc: Soilent (Konstantin x)

Issue #20482 has been updated by Soilent (Konstantin x).


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
```

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?

----------------------------------------
Bug #20482: nil variables in a guard clause of a standalone => or in expression
https://bugs.ruby-lang.org/issues/20482#change-108245

* 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/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [ruby-core:117844] [Ruby master Bug#20482] nil variables in a guard clause of a standalone => or in expression
  2024-05-11 16:45 [ruby-core:117833] [Ruby master Bug#20482] nil variables in a guard clause of a standalone => or in expression Soilent (Konstantin x) via ruby-core
  2024-05-12  0:05 ` [ruby-core:117836] " nobu (Nobuyoshi Nakada) via ruby-core
  2024-05-12  6:08 ` [ruby-core:117841] " Soilent (Konstantin x) via ruby-core
@ 2024-05-12  7:05 ` nobu (Nobuyoshi Nakada) via ruby-core
  2 siblings, 0 replies; 4+ messages in thread
From: nobu (Nobuyoshi Nakada) via ruby-core @ 2024-05-12  7:05 UTC (permalink / raw
  To: ruby-core; +Cc: nobu (Nobuyoshi Nakada)

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/

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-05-12  7:06 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-11 16:45 [ruby-core:117833] [Ruby master Bug#20482] nil variables in a guard clause of a standalone => or in expression Soilent (Konstantin x) via ruby-core
2024-05-12  0:05 ` [ruby-core:117836] " nobu (Nobuyoshi Nakada) via ruby-core
2024-05-12  6:08 ` [ruby-core:117841] " Soilent (Konstantin x) via ruby-core
2024-05-12  7:05 ` [ruby-core:117844] " nobu (Nobuyoshi Nakada) via ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).