[swift-users] Static type when expecting dynamic type

Slava Pestov spestov at apple.com
Thu Feb 2 15:57:27 CST 2017


The problem is were not marking the materializeForSet accessor in DefaultMutableReference.value as an override. Definitely a bug:

sil_vtable DefaultMutableReference {
  #MutableReference.init!initializer.1: _TFC1d23DefaultMutableReferencecfT_GS0_x_       // DefaultMutableReference.init() -> DefaultMutableReference<A>
  #MutableReference.value!getter.1: _TFC1d23DefaultMutableReferenceg5valuex     // DefaultMutableReference.value.getter
  #MutableReference.value!setter.1: _TFC1d23DefaultMutableReferences5valuex     // DefaultMutableReference.value.setter
  #MutableReference.value!materializeForSet.1: _TFC1d16MutableReferencem5valuex // MutableReference.value.materializeForSet
  #DefaultMutableReference.value!materializeForSet.1: _TFC1d23DefaultMutableReferencem5valuex   // DefaultMutableReference.value.materializeForSet

The last entry should not exist.

I’ll have a fix shortly.

Slava

> On Feb 2, 2017, at 9:04 AM, Jordan Rose via swift-users <swift-users at swift.org> wrote:
> 
> Seems like a bug, and moreover it seems like a regression from Swift 3.0. Mind filing a report at bugs.swift.org <http://bugs.swift.org/>?
> 
> Thanks!
> Jordan
> 
>> On Feb 1, 2017, at 20:49, Howard Lovatt via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>> 
>> Hi All,
>> 
>> Anyone know what is going on here:
>> 
>>     //: Closure picks up static type not dynamic
>>     
>>     class MutableReference<T> {
>>         init() {
>>             guard type(of: self) != MutableReference.self else {
>>                 fatalError("MutableReference is an abstract class; create a derrivative of MutableReference")
>>             }
>>         }
>>         var value: T {
>>             get {
>>                 fatalError("Calculated property value getter should be overridden")
>>             }
>>             set {
>>                 fatalError("Calculated property value setter should be overridden")
>>             }
>>         }
>>     }
>>     
>>     class DefaultMutableReference<T>: MutableReference<T> {
>>         private var _value: T
>>         override var value: T {
>>             get {
>>                 return _value
>>             }
>>             set {
>>                 _value = newValue
>>             }
>>         }
>>         init(_ value: T) {
>>             _value = value
>>         }
>>     }
>> 
>>     let e: (MutableReference<[Int]>, Int) -> Void = { $0.value.append($1) }
>>     let dmr = DefaultMutableReference([2])
>>     dmr.value // [2]
>>     e(dmr, 2) // fatal error: Calculated property value getter should be overridden
>>     dmr.value // Expect [2, 2]  
>> If I change `e` to:
>> 
>>     let e: (DefaultMutableReference<[Int]>, Int) -> Void = { $0.value.append($1) }
>> It works fine.
>> 
>> IE the closure is using the static type of its first argument and not dynamically dispatching.
>> 
>> Am I doing something wrong? Is there a way round where I can still use the base class for `e`?
>> 
>> 
>> 
>> Thanks for any help in advance,
>> 
>> -- Howard.
>> 
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org <mailto:swift-users at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-users
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170202/20fa5305/attachment.html>


More information about the swift-users mailing list