Project

General

Profile

Actions

Bug #19918

closed

Should `a[&b]=c` be syntax valid?

Added by tompng (tomoya ishida) 7 months ago. Updated about 1 month ago.

Status:
Closed
Target version:
-
ruby -v:
ruby 3.3.0dev (2023-10-11T04:46:58Z master 40ab7b8c24) [x86_64-linux]
[ruby-core:114997]

Description

These codes are syntax valid now. Prism parses it as syntax error.

a[&b]=c
a[&b]+=c
a[&b]&&=c
a[&b]||=c

Is this syntax intentional or should be error?

Issue of Prism
https://github.com/ruby/prism/issues/1636

It's added in test_parse.rb
https://github.com/ruby/ruby/blob/40ab7b8c244de20007cb45846f41de3a01f7ea0c/test/ruby/test_parse.rb#L502

Updated by mame (Yusuke Endoh) 7 months ago

Assuming this is intentionally valid, I wonder if a[&b] = c should evaluate b before c.

Updated by baweaver (Brandon Weaver) 7 months ago

Is it bad that my first consideration is how this might be abused to do bad things for code golfing reasons? I blame mame.

Updated by Eregon (Benoit Daloze) 7 months ago

mame (Yusuke Endoh) wrote in #note-1:

Assuming this is intentionally valid, I wonder if a[&b] = c should evaluate b before c.

That's the kind of complications which I think illustrates the Ruby syntax shouldn't support such edge cases.
It does not seem nearly useful or used enough to warrant those complications.
Literally I have never seen any code using []= with a block.
If a block is useful the method should not be named with []= but something clearer so it's easy to pass a block without such contortions.

Updated by nobu (Nobuyoshi Nakada) 7 months ago

Whether or not that is useful, it is at least intentional as there are some tests for them.

Updated by mame (Yusuke Endoh) 3 months ago

  • Status changed from Open to Assigned
  • Assignee set to yui-knk (Kaneko Yuichiro)

Discussed at the dev meeting. @matz (Yukihiro Matsumoto) wanted to allow a[&b] and forbid a[&b] = c if the implementation is not overly cumbersome.

@yui-knk (Kaneko Yuichiro) What do you think?

Note: During the meeting, we found that the compilation of a[&b], c[&d] = e, f is apparently buggy. If it is difficult to prohibit the grammar, we need to fix this bug.

Actions #7

Updated by nobu (Nobuyoshi Nakada) about 1 month ago

  • Status changed from Assigned to Closed

Applied in changeset git|df5ef282337764508a1e1d93459d7a280e46647c.


[Bug #19918] Reject block passing in index

Actions

Also available in: Atom PDF

Like0
Like0Like1Like0Like2Like0Like0Like0