New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SR-13137] Swift 5.3 regression: error "Cannot find 'CodingKeys' in scope" #55584
Comments
The attached package is as minimal as I could make it. Practically speaking, this bug affects many applications that use the GRDB SQLite library: import GRDB
extension AppState: FetchableRecord, PersistableRecord {
enum Columns {
// Compiler errors with Swift 5.3, as soon as those columns
// are used is other files.
static let targetProgramID = Column(CodingKeys.targetProgramID)
static let playableProgramID = Column(CodingKeys.playableProgramID)
}
} |
Known workaround: groue/GRDB.swift#804 |
@swift-ci create |
I can't seem to reproduce this on master. Could you verify using latest trunk snapshot from swift.org? |
Hello, when I compile the attached package with Xcode Version 12.0 beta 2 (12A6163b), Toolchain: Swift Development Snapshot 2020-07-17 (a) downloaded from https://swift.org/download/#snapshots, the error is still there. CompileSwiftSources normal x86_64 com.apple.xcode.tools.swift.compiler (in target 'CodingKeysBug' from project 'CodingKeysBug')
cd /Users/groue/Downloads/CodingKeysBug
export DEVELOPER_DIR\=/Applications/Xcode\ 12/Xcode-beta.app/Contents/Developer
export SDKROOT\=/Applications/Xcode\ 12/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.16.sdk
export TOOLCHAINS\=org.swift.50202007171a\
/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2020-07-17-a.xctoolchain/usr/bin/swiftc -incremental ...
/Users/groue/Downloads/CodingKeysBug/Sources/CodingKeysBug/A.swift:3:31: error: cannot find 'CodingKeys' in scope
static let propertyName = CodingKeys.property.stringValue
^~~~~~~~~~ |
@theblixguy The error is still there, with the package attached to this ticket, Xcode 12.0 beta 3 (12A8169g), and Swift Development Snapshot 2020-07-22 (a) |
Okay, I suppose I made a mistake with my swiftc invocation. I can reproduce it as well. |
The issue is still not fixed in Xcode 12 beta 5. |
To those encountering this issue, `Self.CodingKeys` will work around this. Qualified lookup must have lost a path that synthesized CodingKeys by forcing this conformance. |
@CodaFi, thank you. Yes, there are workarounds. The problem is that:
Maybe I should have focused more on this second point. The 1st is already an annoyance. But the second is just terrible. The code below works well UNTIL another file uses A.propertyName, and it is not clear at all how to fix the compiler error. struct A: Codable {
var property: String
static let propertyName = CodingKeys.property.stringValue
} Even when you figure out the fix, application code evolves odd exceptions like below: struct A: Codable {
var property1: String
var property2: String
var property3: String
static let property1Name = CodingKeys.property1.stringValue // ok
static let property2Name = CodingKeys.property2.stringValue // ok
static let property3Name = Self.CodingKeys.property3.stringValue // exception!
} |
Also, @CodaFi, @theblixguy, I'm lobbying for a fix to this issue as the maintainer of the GRDB library. Typical code written by GRDB users is: struct Player: Codable {
var score: Int
var name: String
...
}
extension Player: TableRecord {
enum Columns {
static let score = Column(CodingKeys.score)
static let name = Column(CodingKeys.name)
...
}
} The code above is fostered by the library documentation, because it allows to talk to a SQLite database and build queries with compiler-vetted column names. Because of the regression we are talking about, Swift 5.3 stops compiling some lines of this above code, those that define the columns that happen to be used by another file. This situation happens when a library user defines database queries that involve columns from several tables. Because those columns are declared along with record types which are usually defined in their own Swift file, the regression can express itself. Unfortunately, such queries are frequently needed, and this regression affects quite a few users. The Github issue that helps users fix this compiler error got some positive votes already. Please do not see this ticket as a pedantic one 🙂 |
Oh, I definitely agree. The diagnostics this produces are obnoxiously unhelpful, even for a compiler engineer like me: /Users/rwidmann/Swift-Reproducer/swift/test/decl/protocol/special/coding/Inputs/struct_codable_simple_multi1.swift:25:8: error: unexpected note produced: did you mean the implicitly-synthesized member 'CodingKeys'?
struct A: Codable {
^
/Users/rwidmann/Swift-Reproducer/swift/test/decl/protocol/special/coding/Inputs/struct_codable_simple_multi1.swift:27:31: error: unexpected error produced: cannot find 'CodingKeys' in scope
static let propertyName = CodingKeys.property.stringValue
^
Swift.CodingKey:1:17: note: diagnostic produced elsewhere: did you mean 'CodingKey'?
public protocol CodingKey : CustomDebugStringConvertible, CustomStringConvertible { |
A big, big, thank you, Robert! |
Resolved by 5.3 and master merges of the linked PRs. Please verify at your earliest convenience by installing a development toolchain with this PR available. |
@CodaFi, FYI the package attached to this ticket still won't compile in Xcode 12.0 (12A7209) and 12.2 beta (12B5018i). |
My apologies, I was wrong: Xcode 12.2 beta (12B5018i) compiles the attached package fine![]( Thank you all) |
Attachment: Download
Environment
Xcode version 12.0 beta (12A6159)
macOS version 10.15.5 (19F101)
Additional Detail from JIRA
md5: 345e61a2cfeef941ea3ba20008f3dace
Issue Description:
Hello,
Swift 5.2 would compile those two files fine:
However, Swift 5.3 won't compile the same files, with the following error:
This is a source breaking change that, I believe, qualified as a bug.
Please find a sample package attached.
The text was updated successfully, but these errors were encountered: