Swift: instantiated generic instance is wrong type

Originator:thephatmann
Number:rdar://19274114 Date Originated:16-Dec-2014 09:03 P
Status:Duplicate of 18537723 Resolved:06-Jan-2014
Product:Xcode Product Version:6.1.1
Classification:Serious Bug Reproducible:Always
 
Summary:
Given a generic class that takes a type T constrained to base class A. If T is set to a subclass B of A, creating an instance of T incorrectly creates an instance of A. The compiler thinks it has an instance of B. Chaos ensues.

Steps to Reproduce:

class A { init() {} }
class B : A { var prop = “prop” }

class G<T: A> {
    func t() -> T {
        return T() // incorrectly returns an instance of A even when T == B
    }
}

let g = G<B>()
let t = g.t()        // The compiler thinks it has an instance of B but it really has an instance of A
println t.prop    // Causes an error

Expected Results:
Prints “prop”

Actual Results:
BAD_ACCESS error

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!