Uploaded image for project: 'Swift'
  1. Swift
  2. SR-6103

Law of Exclusivity is not applied for extension of Array with closure which returns Element

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Medium
    • Resolution: Done
    • Component/s: Compiler
    • Labels:
      None
    • Environment:

      I confirmed issue in those environment.

      • Xcode 9.0 (9A235)
      • swift-DEVELOPMENT-SNAPSHOT-2017-10-09-a.xctoolchain

      Description

      This code can be compiled incorrectly.

      extension Array {
          mutating func update(_ f: () -> Element) {}
      }
      
      func main() {
          var xs: [Int] = [1, 2, 3]
          var ys: [Int] = []
          xs.update {
              ys = xs
              return 3
          }
      }
      
      main()
      

      Calling mutating func of `xs` and reading `xs` on right hand of assignment should be conflict.

      Strangely, this problem occurs only when `Element` is used at closure result.
      If return type changed to `Void`, `Array<Element>` or `Int`,
      all cases will be compile error correctly.

      This is `Void` case.

      extension Array {
          mutating func update(_ f: () -> Void) {}
      }
      
      func main() {
          var xs: [Int] = [1, 2, 3]
          var ys: [Int] = []
          xs.update {
              ys = xs
          }
      }
      
      main()
      
      $ swift run
      Compile Swift Module 'swex' (1 sources)
      /Users/omochi/temp/swex/Sources/swex/main.swift:7:9: warning: variable 'ys' was written to, but never read
          var ys: [Int] = []
              ^
      /Users/omochi/temp/swex/Sources/swex/main.swift:8:5: error: overlapping accesses to 'xs', but modification requires exclusive access; consider copying to a local variable
          xs.update {
          ^~
      /Users/omochi/temp/swex/Sources/swex/main.swift:9:12: note: conflicting access is here
              ys = xs
              ~~~^~~~
      

        Attachments

          Activity

            People

            Assignee:
            omochimetaru omochimetaru
            Reporter:
            omochimetaru omochimetaru
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: