Swift: Instantiating an object of a generic function's type parameter yields base class instead of sub class

Originator:neocool2
Number:rdar://19357437 Date Originated:29-Dec-2014 09:51 PM
Status:Duplicate of 18537723 Resolved:
Product:Developer Tools Product Version:Swift 1.1.1
Classification:Serious Bug Reproducible:Always
 
Summary:

The attached code contains a generic function which should be specialised by return type. Unfortunately, despite the type parameter T referencing the subclass Sub, the instantiated object will be of the base type Base. Swift’s type inference is not aware of this fact which leads to runtime crashes.

Steps to Reproduce:

$ ./generics.swift 

Expected Results:

Output of the program:

param is: generics.Sub
result is: generics.Sub

o1 should be of type Sub. Optionally, the program should not compile because the result of the create() function has the wrong type.

Actual Results:

param is: generics.Sub
result is: generics.Base

o1 is of the wrong type. This can easily lead to runtime crashes, e.g. when calling functions of type Sub which do not exist in type Base.

Regression: -

Notes:

Code sample is attached.

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!