ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "nobu (Nobuyoshi Nakada) via ruby-core" <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: "nobu (Nobuyoshi Nakada)" <noreply@ruby-lang.org>
Subject: [ruby-core:117844] [Ruby master Bug#20482] nil variables in a guard clause of a standalone => or in expression
Date: Sun, 12 May 2024 07:05:52 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-108249.20240512070552.7613@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-20482.20240511164529.7613@ruby-lang.org

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/

      parent reply	other threads:[~2024-05-12  7:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-108249.20240512070552.7613@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    --cc=noreply@ruby-lang.org \
    --cc=ruby-core@ml.ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).