Skip to content

Evaluation order when using mutating operations with destructuring assignment #40574

@simonbyrne

Description

@simonbyrne
julia> x = [1,2]
2-element Vector{Int64}:
 1
 2

julia> x[2],x[1] = x
2-element Vector{Int64}:
 1
 1

This is caused by mutating the iterator as it is being iterated: i.e. it is being lowered to

julia> x = [1,2]
2-element Vector{Int64}:
 1
 2

julia> x[2], state = iterate(x)
(1, 2)

julia> x[1], state = iterate(x, state)
(1, 3)

julia> x
2-element Vector{Int64}:
 1
 1

From @simeonschaub on Slack:

For a non-trivial lhs, we should probably always assign the rhs to temporary variables first and only then call setindex! etc.

cc: @JeffBezanson @vtjnash

Metadata

Metadata

Assignees

No one assigned

    Labels

    compiler:loweringSyntax lowering (compiler front end, 2nd stage)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions