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-15360] Invalid conformance to RangeReplaceableCollection compiles and causes crash #57682

Closed
swift-ci opened this issue Oct 20, 2021 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-15360
Radar None
Original Reporter Stiivi (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Environment
swift-driver version: 1.26.9 Apple Swift version 5.5 (swiftlang-1300.0.31.1 clang-1300.0.29.1)
Target: x86_64-apple-macosx11.0
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 465e049fde0bdaab0b80c1035988256e

duplicates:

  • SR-6501 RangeReplaceableCollection default implementations cause infinite recursion

Issue Description:

The following code compiles and causes program to crash:

import Cocoa

typealias Thing = String
class Item: Equatable {
    static func ==(lhs: Item, rhs: Item) -> Bool { return true }
    
}
final class CustomList {
    var things: [Thing]
    
    required init(things: [Thing]) {
        self.things = things
    }
}

extension Array where Element: Item {
    func isTop(_ item: Element) -> Bool {
        guard let topItem = self.last else {
            return false
        }
        return topItem == item
    }
}


extension CustomList: RangeReplaceableCollection {
    public convenience init() {
        self.init(things: [])
    }
    
    public var startIndex: Int { things.startIndex }
    public var endIndex: Int { things.endIndex }

    public subscript(key: Int) -> Thing {
        get { return things[key] }
    }
    
    public func index(after: Int) -> Int {
        return things.index(after:after)
    }

    func replaceSubrange(_ subrange: Range<Int>, with newElements: [Thing]) {
        things.replaceSubrange(subrange, with: newElements)
    }
}

var list = CustomList()

// This yields: illegal hardware instruction  swift CollectionError.swift
list += CustomList(["thing"])

Swift version:

Context: Swift Forums Post

@glessard
Copy link
Contributor

This issue is fixed in recent swift snapshots. (Tested on the October 18th snapshot).

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

No branches or pull requests

2 participants