swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.72k stars 10.39k forks source link

[SR-10263] Compile error when generic function with `where` clause is called on weak variable. #52663

Open swift-ci opened 5 years ago

swift-ci commented 5 years ago
Previous ID SR-10263
Radar None
Original Reporter maciek2w@gmail.com (JIRA User)
Type Bug
Environment Reproduced on: - Xcode 10.2 Swift 5 - Swift-4.2.1
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, TypeChecker | |Assignee | None | |Priority | Medium | md5: eec2bcf4fac3d33b4daaa8c1e24287c3

Issue Description:

Got error "cannot convert value of type '[Base.Type]' to expected argument type '[_.Type]'"
when generic function with `where` clause is called on weak variable.

import Foundation

// Lets create base class and two classes that inherits base class.
class Base {}
class A: Base {}
class B: Base {}

// Lets create class with generic function with `where` clauses
class Printer {
    func printAll<T>(classes: [T.Type]) where T: Base {
        for c in classes {
            print(String(describing: c))
        }
    }
}

// Lets create the instance
let printer = Printer()

// Compiles
printer.printAll(classes: [A.self, B.self])

// Lets create weak variable
weak var weakPrinter: Printer! = printer

// Compiles
weakPrinter.printAll(classes: [A.self])

// Does not compile with error:
// "cannot convert value of type '[Base.Type]' to expected argument type '[_.Type]'"
weakPrinter.printAll(classes: [A.self, B.self])
belkadan commented 5 years ago

Hm. Explicitly specifying as [Base.Type] makes everything work, so there must be something weird going on in the type checker. cc @xedin