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

Require right-hand side of one-to-many assignments to be Indexable #11545

Conversation

HertzDevil
Copy link
Contributor

@HertzDevil HertzDevil commented Dec 7, 2021

Fixes #11414. The entire right-hand side needs to be an Indexable, not a union of Indexable and non-Indexable types (maybe this could be relaxed like in #11145, but since the literal expander cannot decide whether the RHS can possibly be such a union, every MultiAssign will end up having a runtime is_a?(::Indexable) check).

class Foo
  def [](index)
  end

  def size
    3
  end
end

a, b, c = Foo.new  # => [nil, nil, nil]
d, *e, f = Foo.new # Error: right-hand side of one-to-many assignment must be an Indexable, not Foo

If -Dstrict_multi_assign is provided, the first assignment also produces the same compilation error.

The first commit makes Enumerable and Indexable built-in types. This does little other than making sure these modules exist even with an empty prelude; nothing else is done in Crystal::Program (in particular Indexable does not even include Enumerable there), and the standard library continues to be responsible for those additional definitions.

@straight-shoota straight-shoota added this to the 1.3.0 milestone Dec 7, 2021
@straight-shoota straight-shoota merged commit 0f0852f into crystal-lang:master Dec 8, 2021
@HertzDevil HertzDevil deleted the feature/multi-assign-rhs-indexable branch December 9, 2021 03:12
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.

Require the right-hand side of 1-to-n assignmens to be Indexable
3 participants