Files
swift-mirror/test/Interpreter/SDK/objc_failable_initializers.swift
Doug Gregor 3fb06d7442 [Swift runtime] Deallocation of partial class instances for Objective-C-derived classes.
Teach swift_deallocPartialClassInstance how to deal with classes that
have pure Objective-C classes in their hierarchy. In such cases, we
need to make sure a few things happen:

1) We deallocate via objc_release rather than
swift_deallocClassInstance.
2) We only attempt to find an execute ivar destroyers for
Swift-defined classes in the hierarchy
3) When we hit the most-derived pure Objective-C class, make sure that we
only execute the dealloc of that class and not any of the subclasses
(which would end up trying to destroy ivars again).

Fixes rdar://problem/25023544.
2016-03-08 16:47:57 -08:00

90 lines
1.4 KiB
Swift

// RUN: %target-run-simple-swift
// REQUIRES: executable_test
// REQUIRES: objc_interop
import StdlibUnittest
import Foundation
var ObjCFailableInitTestSuite = TestSuite("ObjCFailableInit")
class Canary {
static var count: Int = 0
init() {
Canary.count += 1
}
deinit {
Canary.count -= 1
}
}
extension NSDate {
convenience init?(b: Bool) {
guard b else { return nil }
self.init()
}
}
class MyDate : NSDate {
var derivedCanary = Canary()
static var count = 0
override init() {
MyDate.count += 1
super.init()
}
required convenience init(coder: NSCoder) {
fatalError("not implemented")
}
deinit {
MyDate.count -= 1
}
@objc convenience init?(b: Bool) {
guard b else { return nil }
self.init()
}
}
class MyDerivedDate : MyDate {
var canary = Canary()
static var derivedCount = 0
override init() {
MyDerivedDate.count += 1
}
deinit {
MyDerivedDate.count -= 1
}
}
func mustFail<T>(f: () -> T?) {
if f() != nil {
preconditionFailure("Didn't fail")
}
}
ObjCFailableInitTestSuite.test("InitFailure_Before") {
mustFail { NSDate(b: false) }
expectEqual(0, Canary.count)
mustFail { MyDate(b: false) }
expectEqual(0, Canary.count)
expectEqual(0, MyDate.count)
mustFail { MyDerivedDate(b: false) }
expectEqual(0, Canary.count)
expectEqual(0, MyDate.count)
expectEqual(0, MyDerivedDate.derivedCount)
}
runAllTests()