Skip to content

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

Merged
straight-shoota merged 2 commits intocrystal-lang:masterfrom
HertzDevil:feature/multi-assign-rhs-indexable
Dec 8, 2021
Merged

Require right-hand side of one-to-many assignments to be Indexable#11545
straight-shoota merged 2 commits intocrystal-lang:masterfrom
HertzDevil:feature/multi-assign-rhs-indexable

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

Labels

breaking-change May have breaking effect in some edge cases. Mostly negligible, but still noteworthy. kind:feature topic:compiler:semantic topic:stdlib:collection

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