swift 4.0 complains/warns about swapping array elements


(Edward Connell) #1

I just switched to the 5/17 swift 4.0 tool chain on Linux, and I am getting
the following complaint when using swap on array elements. Is this warning
legitimate because of a copy on write issue? If so, is there some new swap
function for array elements? I realize I can turn 1 line into 3 and do the
swapping myself, it's just verbose.

*warning: simultaneous accesses to var 'dims', but modification requires
exclusive access; consider copying to a local variable*
* swap(&dims[lastIndex], &dims[lastIndex-1])*

Thanks, Ed


(David Sweeris) #2

Looks like it got changed to "swapAt": https://github.com/apple/swift-evolution/blob/master/proposals/0173-swap-indices.md

Hope that helps,
- Dave Sweeris

···

On May 19, 2017, at 10:10, Edward Connell via swift-users <swift-users@swift.org> wrote:

I just switched to the 5/17 swift 4.0 tool chain on Linux, and I am getting the following complaint when using swap on array elements. Is this warning legitimate because of a copy on write issue? If so, is there some new swap function for array elements? I realize I can turn 1 line into 3 and do the swapping myself, it's just verbose.

warning: simultaneous accesses to var 'dims', but modification requires exclusive access; consider copying to a local variable
                                swap(&dims[lastIndex], &dims[lastIndex-1])


(Devin Coughlin) #3

Hi Ed,

···

On May 19, 2017, at 10:10 AM, Edward Connell via swift-users <swift-users@swift.org> wrote:

I just switched to the 5/17 swift 4.0 tool chain on Linux, and I am getting the following complaint when using swap on array elements. Is this warning legitimate because of a copy on write issue? If so, is there some new swap function for array elements? I realize I can turn 1 line into 3 and do the swapping myself, it's just verbose.

warning: simultaneous accesses to var 'dims', but modification requires exclusive access; consider copying to a local variable
                                swap(&dims[lastIndex], &dims[lastIndex-1])

You can use the new swapAt() method:

   dims.swapAt(lastIndex, lastIndex-1)

I recently added a Fix-It for this — but we really should also say "consider using ‘swapAt()’” in the diagnostic text as well in this case rather then mentioning copying to a local.

Devin


(Edward Connell) #4

Great! Yes, changing the diagnostic message will save people from having to
ask this question.

Thanks, Ed

···

On Fri, May 19, 2017 at 10:30 AM, Devin Coughlin <dcoughlin@apple.com> wrote:

Hi Ed,

On May 19, 2017, at 10:10 AM, Edward Connell via swift-users < > swift-users@swift.org> wrote:

I just switched to the 5/17 swift 4.0 tool chain on Linux, and I am
getting the following complaint when using swap on array elements. Is this
warning legitimate because of a copy on write issue? If so, is there some
new swap function for array elements? I realize I can turn 1 line into 3
and do the swapping myself, it's just verbose.

*warning: simultaneous accesses to var 'dims', but modification requires
exclusive access; consider copying to a local variable*
* swap(&dims[lastIndex],
&dims[lastIndex-1])*

You can use the new swapAt() method:

   dims.swapAt(lastIndex, lastIndex-1)

I recently added a Fix-It for this — but we really should also say
"consider using ‘swapAt()’” in the diagnostic text as well in this case
rather then mentioning copying to a local.

Devin