Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird attribute assignment syntax #12830

Open
FnControlOption opened this issue Dec 9, 2022 · 3 comments · May be fixed by #14815
Open

Weird attribute assignment syntax #12830

FnControlOption opened this issue Dec 9, 2022 · 3 comments · May be fixed by #14815
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:parser

Comments

@FnControlOption
Copy link
Contributor

In Ruby, the following code is invalid:

a = [:foo] * 3
a[0] = :bar
a.[](1) = :baz # syntax error

However, this works fine (by accident?) in Crystal, where a.[](1) = :baz is the same as a.[]=(1, :baz) (or a[1] = :baz)

Parser#can_be_assigned? probably should check if Call#has_parentheses is false when Call#name is "[]"

Here's my crystal -v:

Crystal 1.6.2 (2022-11-03)

LLVM: 14.0.6
Default target: x86_64-apple-macosx

@FnControlOption FnControlOption added the kind:bug A bug in the code. Does not apply to documentation, specs, etc. label Dec 9, 2022
@FnControlOption
Copy link
Contributor Author

FnControlOption commented Dec 10, 2022

Addendum: a.[] 1 = :baz also works in Crystal but doesn't in Ruby (a.[] 1 is a method call to [] with argument 1)

@straight-shoota
Copy link
Member

a.[](1) = :baz and a.[] 1 = :baz should be invalid. The combination of standard call syntax with an operator method name and assignment is confusing.

Note that a.[]=(1, :baz) should continue to work, though.

@FnControlOption
Copy link
Contributor Author

FnControlOption commented Jul 16, 2024

a.[](1) = :baz is now a syntax error (most likely fixed in #14527)

All that's left now before this issue can be closed is a.[] 1 = :baz

@FnControlOption FnControlOption linked a pull request Jul 16, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:parser
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants