swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.58k stars 10.36k forks source link

[SR-9893] Swift 5 regression: cannot assign through '!': '$0' is immutable #52299

Open groue opened 5 years ago

groue commented 5 years ago
Previous ID SR-9893
Radar rdar://problem/47967277
Original Reporter @groue
Type Bug
Environment Xcode Version 10.2 beta 2 Swift 5 toolchain 06/02/2019 macOS 10.14.3 (18D109)
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, 5.0Regression, SourceCompatibility | |Assignee | None | |Priority | Medium | md5: ac90bc1287842cec66ca4d2c3e628f0b

Issue Description:

I have a test which won't compile with the Swift 5 compiler (2019-02-06 snapshot), both in Swift 5 and Swift 4.2 modes:

XCTAssertEqual(
    Set(fetchedRecords.map { $0.name! }),
    Set([record1, record2].map { $0.name! }))

The compiler emits the following error:

/Users/groue/Documents/git/groue/GRDB.swift/Tests/GRDBTests/RecordPrimaryKeyNoneTests.swift:155:106: error: cannot assign through '!': '$0' is immutable
                XCTAssertEqual(Set(fetchedRecords.map { $0.name! }), Set([record1, record2].map { $0.name! }))
                                                                                                  ~~     ^

The error vanishes when the code is rewritten in the equivalent form below:

let fetchedNames = Set(fetchedRecords.map { $0.name! })
let expectedNames = Set([record1, record2].map { $0.name! })
XCTAssertEqual(fetchedNames, expectedNames)

For information, the record1, record2 variables are of type `Item`, and the fetchedRecords variable is of type `[Item]`:

private class Item: Record {
    var name: String?
    ...
}

To reproduce:

1. Clone https://github.com/groue/GRDB.swift.git

  1. Checkout the 493575a47bc7feffecd91bdd411358e15f92e118 commit
  2. Open GRDB.xcworkspace
  3. Select the GRDBOSX scheme
  4. Build tests with the 2019-02-06 snapshot > there is an error :(
  5. Build tests with Xcode 10.1 > there is no error :)
bc7072e8-7d37-4a38-8fee-cab463d443b7 commented 5 years ago

Should be fixed as of https://github.com/apple/swift/pull/22526 and https://github.com/apple/swift/pull/22563. The fix is available in the last Swift release.

@groue, Could you verify if the problem is fixed and if so move the JIRA to "Closed"?

Thanks!
Anna