Skip to content

Sequence/Iterator conformance broken in 5.7 #60514

Closed
@MaxDesiatov

Description

@MaxDesiatov

Describe the bug

Code utilizing Sequence and Iterator protocols that worked in previous versions of Swift no longer type-checks in 5.7 snapshots.

Steps To Reproduce

Try to build WasmTransformer with Xcode 14.0 beta 5.

Expected behavior

This code type-checks and builds as it did Swift 5.5/5.6

Screenshots

Screenshot 2022-08-11 at 19 38 05

Environment (please fill out the following information)

  • OS: macOS 12.5 and Ubuntu 20.04
  • Xcode Version/Tag/Branch: 14.0 beta 5 (14A5294e), also reproducible on Linux with swiftlang/swift:nightly-5.7-focal image and 6fa8c6b08e85 hash.

Additional context

Here's the code snippet (not self-contained, but I hope it helps to pinpoint the issue) that causes these new errors:

public protocol VectorSectionReader: Sequence where Element == Result<Item, Error> {
    associatedtype Item
    var count: UInt32 { get }
    mutating func read() throws -> Item
}

public struct VectorSectionIterator<Reader: VectorSectionReader>: IteratorProtocol {
    private(set) var reader: Reader
    private(set) var left: UInt32
    init(reader: Reader, count: UInt32) {
        self.reader = reader
        self.left = count
    }
    private var end: Bool = false
    public mutating func next() -> Reader.Element? {
        guard !end else { return nil }
        guard left != 0 else { return nil }
        let result = Result(catching: { try reader.read() })
        left -= 1
        switch result {
        case .success: return result
        case .failure:
            end = true
            return result
        }
    }
}

extension VectorSectionReader {
    __consuming public func makeIterator() -> VectorSectionIterator<Self> {
        VectorSectionIterator(reader: self, count: count)
    }

    public func collect() throws -> [Item] {
        var items: [Item] = []
        items.reserveCapacity(Int(count))
        for result in self {
            try items.append(result.get())
        }
        return items
    }
}

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfconformancesFeature → protocol: protocol conformancesregressiontype checkerArea → compiler: Semantic analysis

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions