Project

General

Profile

Actions

Bug #20785

open

Should `a in b, and c` `a in b, or c` `a in b, rescue c` be syntax ok?

Added by tompng (tomoya ishida) about 1 month ago. Updated 30 days ago.

Status:
Open
Target version:
-
ruby -v:
ruby 3.4.0dev (2024-10-04T03:22:53Z master 939ec9f080) +YJIT +MN +PRISM [arm64-darwin22]
[ruby-core:119462]

Description

This code is accepted in parse.y but rejected in prism

tap do
  a in b, and c
  a in b, or c
  a in b, rescue c
end

# parsed as
tap do
  (a in b,;) and c
  (a in b,;) or c
  a in b,;
rescue c
end

I think these should be rejected like prism (parse.y accepts)

a in b, and c

a in b,
and c

tap do
  a in b, rescue c
end

I think these should be accepted like parse.y (prism rejects)

tap do
  a in b,
end

tap do
  a in b,
rescue
end

Updated by nobu (Nobuyoshi Nakada) about 1 month ago

  • Assignee set to matz (Yukihiro Matsumoto)

Updated by nobu (Nobuyoshi Nakada) about 1 month ago

  • Assignee changed from matz (Yukihiro Matsumoto) to ktsj (Kazuki Tsujimoto)

Updated by Dan0042 (Daniel DeLorme) 30 days ago

tompng (tomoya ishida) wrote:

I think these should be accepted like parse.y (prism rejects)

tap do
  a in b,
end

tap do
  a in b,
rescue
end

Can you explain your reasoning here? I'm guessing that a in b, is parsed as a in [b,] but a trailing comma like that is just too wild for me; it totally breaks my intuitions about Ruby syntax, and I think it should be rejected.

Updated by ktsj (Kazuki Tsujimoto) 30 days ago

  • Assignee changed from ktsj (Kazuki Tsujimoto) to matz (Yukihiro Matsumoto)

I agree with tompng's proposal.

I'm guessing that a in b, is parsed as a in [b,] but a trailing comma like that is just too wild for me

in (=>) can be considered right assignment.
And, I think it makes sense that a trailing comma is allowed in right assignment, just as a trailing comma is allowed in normal assignment.

a => b, # one-line pattern matching as right assignment
b, = a  # normal assignment

What do you think, @matz (Yukihiro Matsumoto)?

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0