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-10475] Protocol ordering in file matters in Swift 5 #52875

Closed
djehrlich opened this issue Apr 12, 2019 · 1 comment
Closed

[SR-10475] Protocol ordering in file matters in Swift 5 #52875

djehrlich opened this issue Apr 12, 2019 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression swift 5.0

Comments

@djehrlich
Copy link

Previous ID SR-10475
Radar rdar://problem/49906971
Original Reporter @djehrlich
Type Bug
Status Resolved
Resolution Cannot Reproduce
Environment

Tested in:

Xcode Version 10.1 (10B61)

Xcode Version 10.2 (10E125)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, 5.0Regression
Assignee None
Priority Medium

md5: 81c01089b0ad9a7b9e4a35dbc1d68c2b

Issue Description:

Three examples of very similar code.

All three compile in Xcode 10.1/Swift 4.2.

Two of three compile in Xcode 10.2/Swift 5.

Identification and differences among the three are described in inline comments.

// Example 1

// * Builds in Swift 4.2
// * Does NOT build in Swift 5
// * SectionModel defined _after_ Model
// * SectionModel contains RowID associatedtype referencing the
//   RowIDConstraint typealias which is Hashable 
protocol Example1TableViewModels {
    typealias SectionConstraint = Example1TableViewSectionModels
    associatedtype Section: SectionConstraint

    var sections: [Section] { get }
}

extension Example1TableViewModels {
    func cellIndexPath(
        forRowIdentifiedBy rowIdentifier: Section.RowID
    )
        -> (Int,Int)?
    {
        for indexAndSection in zip(0...,sections) {
            if let rowIDIndex = indexAndSection.1.rowIDs.firstIndex(
                   of: rowIdentifier // Argument type 'Self.Section.RowID' does not conform to expected type 'Equatable'
               )
            {
                return (indexAndSection.0,rowIDIndex)
            }
        }
        return nil
    }
}
protocol Example1TableViewSectionModels: Equatable {
    typealias RowIDConstraint = Hashable
    associatedtype RowID : RowIDConstraint

    var rowIDs: [RowID] { get }
}
// Example 2

// * Builds in Swift 4.2
// * Builds in Swift 5
// * SectionModel defined _after_ Model
// * SectionModel contains RowID associatedtype constrained to Hashable directly
protocol Example2TableViewModels {
    typealias SectionConstraint = Example2TableViewSectionModels
    associatedtype Section: SectionConstraint

    var sections: [Section] { get }
}
extension Example2TableViewModels {
    func cellIndexPath(
        forRowIdentifiedBy rowIdentifier: Section.RowID
    )
        -> (Int,Int)?
    {
        for indexAndSection in zip(0...,sections) {
            if let rowIDIndex = indexAndSection.1.rowIDs.firstIndex(
                   of: rowIdentifier
               )
            {
                return (indexAndSection.0,rowIDIndex)
            }
        }
        return nil
    }
}
protocol Example2TableViewSectionModels: Equatable {
    associatedtype RowID : Hashable

    var rowIDs: [RowID] { get }
}
// Example 3

// * Builds in Swift 4.2
// * Builds in Swift 5
// * SectionModel defined _before_ Model
// * SectionModel contains RowID associatedtype referencing the
//   RowIDConstraint typealias which is Hashable
protocol Example3TableViewSectionModels: Equatable {
    typealias RowIDConstraint = Hashable
    associatedtype RowID : RowIDConstraint

    var rowIDs: [RowID] { get }
}
protocol Example3TableViewModels {
    typealias SectionConstraint = Example3TableViewSectionModels
    associatedtype Section: SectionConstraint

    var sections: [Section] { get }
}
extension Example3TableViewModels {
    func cellIndexPath(
        forRowIdentifiedBy rowIdentifier: Section.RowID
    )
        -> (Int,Int)?
    {
        for indexAndSection in zip(0...,sections) {
            if let rowIDIndex = indexAndSection.1.rowIDs.firstIndex(
                   of: rowIdentifier
               )
            {
                return (indexAndSection.0,rowIDIndex)
            }
        }
        return nil
    }
}

If Example 1 is split into two files (one for each protocol and its extension, if appropriate), it compiles in Xcode 10.1/Swift 4.2 but does not compile in Xcode 10.2/Swift 5.0.

@slavapestov
Copy link
Member

Appears to have been fixed in Swift 5.2.

@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.0
Projects
None yet
Development

No branches or pull requests

3 participants