Skip to content
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

Closed
groue opened this issue Jul 3, 2020 · 17 comments
Closed
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression swift 5.3 type checker Area → compiler: Semantic analysis

Comments

@groue
Copy link

groue commented Jul 3, 2020

Previous ID SR-13137
Radar rdar://problem/65088901
Original Reporter @groue
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

Xcode version 12.0 beta (12A6159)

macOS version 10.15.5 (19F101)

Additional Detail from JIRA
Votes 2
Component/s Compiler
Labels Bug, 5.3Regression, TypeChecker
Assignee @CodaFi
Priority Medium

md5: 345e61a2cfeef941ea3ba20008f3dace

Issue Description:

Hello,

Swift 5.2 would compile those two files fine:

// A.swift
struct A: Codable {
    var property: String
    static let propertyName = CodingKeys.property.stringValue
}

// B.swift
struct B { 
    static let propertyName = A.propertyName
} 

However, Swift 5.3 won't compile the same files, with the following error:

$ swift build 
/Users/groue/CodingKeysBug/Sources/CodingKeysBug/A.swift:3:31: error: cannot find 'CodingKeys' in scope
     static let propertyName = CodingKeys.property.stringValue
                               ^~~~~~~~~~ 
[2/2] Compiling CodingKeysBug A.swift 

This is a source breaking change that, I believe, qualified as a bug.

Please find a sample package attached.

@groue
Copy link
Author

groue commented Jul 3, 2020

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)
    }
}

@groue
Copy link
Author

groue commented Jul 3, 2020

Known workaround: groue/GRDB.swift#804

@typesanitizer
Copy link

@swift-ci create

@theblixguy
Copy link
Collaborator

I can't seem to reproduce this on master. Could you verify using latest trunk snapshot from swift.org?

@groue
Copy link
Author

groue commented Jul 20, 2020

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
                              ^~~~~~~~~~

@groue
Copy link
Author

groue commented Jul 23, 2020

@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)

@theblixguy
Copy link
Collaborator

Okay, I suppose I made a mistake with my swiftc invocation. I can reproduce it as well.

@groue
Copy link
Author

groue commented Aug 19, 2020

The issue is still not fixed in Xcode 12 beta 5.

@CodaFi
Copy link
Member

CodaFi commented Aug 19, 2020

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.

@groue
Copy link
Author

groue commented Aug 20, 2020

@CodaFi, thank you. Yes, there are workarounds. The problem is that:

  • this is a regression: it breaks existing code

  • the extra qualifier is only needed when another file uses the static variable

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!
}

@groue
Copy link
Author

groue commented Aug 20, 2020

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 🙂

@CodaFi
Copy link
Member

CodaFi commented Aug 21, 2020

#33582

@CodaFi
Copy link
Member

CodaFi commented Aug 21, 2020

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 {

@groue
Copy link
Author

groue commented Aug 21, 2020

A big, big, thank you, Robert!

@CodaFi
Copy link
Member

CodaFi commented Aug 25, 2020

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.

@groue
Copy link
Author

groue commented Sep 21, 2020

@CodaFi, FYI the package attached to this ticket still won't compile in Xcode 12.0 (12A7209) and 12.2 beta (12B5018i).

@groue
Copy link
Author

groue commented Sep 21, 2020

My apologies, I was wrong: Xcode 12.2 beta (12B5018i) compiles the attached package fine![]( Thank you all)

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression swift 5.3 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

5 participants