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

Disallow weird assignments #14815

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

FnControlOption
Copy link
Contributor

Closes #12830

Added Call#dot_location so that the AST for a[0] can be differentiated from a.[] 0. This makes it easier to disallow a.[] 0 = 1

Added a check for whether Call#named_args is nil so that a b: 0 = 1 is disallowed and not treated like an assignment to a (where b: 0 was ignored)

@FnControlOption FnControlOption marked this pull request as draft July 20, 2024 04:23
@FnControlOption
Copy link
Contributor Author

FnControlOption commented Jul 20, 2024

Found some more edge cases:

foo = [1, 2, 3]
#foo.[]? = 1         # Error: undefined method '[]?='
#foo.[]? += 1        # Error: wrong number of arguments
#foo.[0]? = 1        # Error: unexpected token: "="
#foo.[0]? += 1       # Error: unexpected token: "+="
#foo[0]? = 1         # Error: unexpected token: "="
#foo[0]? += 1        # Error: unexpected token: "+="
#foo.tap &.[0]? = 1  # Error: undefined method '[]?='
#foo.tap &.[] 0 = 1  # Error: no overload matches
#foo.tap &.[] = 1    # Error: no overload matches

class Foo
  def []=(value : Int)
    puts value
  end
end
foo = Foo.new
#foo.tap &.[]? = 1   # Error: undefined method '[]?='
#foo.tap &.[0]? = 1  # Error: undefined method '[]?='
foo.tap(&.[]=(1))    # 1
foo.tap &.[] = 1     # 1
foo.tap &.[]=(1)     # 1
foo.tap &.[] 0 = 1   # 1
foo.tap &.[](0) = 1  # 1

class Foo
  def foo
    Foo.new
  end
  def []=(index : Int, value : Int)
    puts [index, value]
  end
end
foo.tap &.foo[0] = 1    # [0, 1]
foo.tap &.foo.[] 0 = 1  # [0, 1]
#foo.tap &.foo[0]? = 1  # Error: unexpected token: "="

Maybe instead of dot_location, a field named args_in_brackets could be added

@FnControlOption FnControlOption marked this pull request as ready for review July 20, 2024 22:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Weird attribute assignment syntax
4 participants