swiftlang / swift

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

[SR-12529] @NSCopying attribute not working in final class #54972

Open swift-ci opened 4 years ago

swift-ci commented 4 years ago
Previous ID SR-12529
Radar rdar://problem/62201660
Original Reporter sh-a-n (JIRA User)
Type Bug

Attachment: Download

Environment xCode 11.4
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, PropertyWrappers | |Assignee | None | |Priority | Medium | md5: 0a3413c9b5880fdad2a5640dfab27de7

Issue Description:

Marking class as final makes @NSCopying attribute not working at all.

Example of bug in attachments.

//
import UIKit

class MyClass: NSCopying {
    var a: Int

    init(a: Int) {
        self.a = a
    }

    func copy(with zone: NSZone? = nil) -> Any {
        print(#function)
        return MyClass(a: self.a)
    }
}

class CopyingContainerWithoutFinal {
    @NSCopying var value: MyClass?
}

final class CopyingContainerWithFinal {
    @NSCopying var value: MyClass?
}

class ContainerWithoutNSCopying {
    var value: MyClass?
}

let c1 = MyClass(a: 0)

let container1 = CopyingContainerWithoutFinal()
let container2 = CopyingContainerWithFinal()
let container3 = ContainerWithoutNSCopying()

container1.value = c1
container2.value = c1
container3.value = c1

let value1 = container1.value
let value2 = container2.value

c1.a = 1

print(container1.value!.a) // Must print 0.
print(container2.value!.a) // Must print 0, but actually prints 1. Bug?
print(container3.value!.a) // Must print 1.
beccadax commented 4 years ago

@swift-ci create