-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[awaiting evolution] Allow convenience initializers to reassign self. #19311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -697,7 +697,12 @@ class DestructureInputs { | |
|
||
bool isInout = false; | ||
if (auto inoutType = dyn_cast<InOutType>(substType)) { | ||
isInout = true; | ||
// The `self` parameter of convenience initializers is considered 'inout' | ||
// for semantic purposes in the AST, but at the ABI level is always | ||
// passed by value in and returned out. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'd only end up here when computing the lowered initializing entry point type right? In which case this check should not matter since we only do this for designated inits. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Convenience inits still have initializing entry points for ObjC interop. This is necessary so that the lowering for the ObjC thunk still works. |
||
if (!forSelf || rep != SILFunctionTypeRepresentation::ObjCMethod) { | ||
isInout = true; | ||
} | ||
substType = inoutType.getObjectType(); | ||
origType = origType.getWithoutSpecifierType(); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// RUN: %target-swift-emit-sil(mock-sdk: %clang-importer-sdk) -swift-version 5 -verify %s | ||
|
||
import Foundation | ||
|
||
class X: NSObject { | ||
override init() {} | ||
|
||
static func foo() -> Self { fatalError() } | ||
|
||
@objc convenience init(x: Int) { | ||
self = type(of: self).foo() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// RUN: %target-typecheck-verify-swift -swift-version 4 | ||
|
||
final class A { | ||
static let a = A() | ||
convenience init(a: ()) { | ||
self = A.a // expected-error{{immutable}} | ||
} | ||
} | ||
|
||
private func _forceCastToSelf<T: AnyObject>(_ object: AnyObject) -> T { | ||
return object as! T | ||
} | ||
|
||
class B { | ||
static let b = B() | ||
|
||
convenience init(bWrong: ()) { | ||
self = B.b // expected-error{{immutable}} | ||
} | ||
|
||
convenience init(b: ()) { | ||
self = _forceCastToSelf(B.b) // expected-error{{immutable}} | ||
} | ||
} | ||
|
||
class C: B { } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// RUN: %target-typecheck-verify-swift -swift-version 5 | ||
|
||
final class A { | ||
static let a = A() | ||
convenience init(a: ()) { | ||
self = A.a | ||
} | ||
} | ||
|
||
private func _forceCastToSelf<T: AnyObject>(_ object: AnyObject) -> T { | ||
return object as! T | ||
} | ||
|
||
protocol P {} | ||
|
||
extension P { | ||
static var selfProperty: Self { fatalError() } | ||
static func selfReturningMethod() -> Self { fatalError() } | ||
static func selfLaunderingMethod(_: Self) -> Self { fatalError() } | ||
|
||
init(protocolExt: ()) { fatalError() } | ||
} | ||
|
||
class B: P { | ||
static let b = B() | ||
|
||
convenience init(otherInstance: B) { | ||
self = B.b // expected-error{{cannot assign value of type 'B' to type 'Self'}} | ||
self = otherInstance // expected-error{{cannot assign value of type 'B' to type 'Self'}} | ||
self = _forceCastToSelf(B.b) | ||
self = type(of: self).selfProperty | ||
self = type(of: self).selfReturningMethod() | ||
self = type(of: self).selfLaunderingMethod(otherInstance) // expected-error{{cannot assign value of type 'B' to type 'Self'}} | ||
self = type(of: self).selfLaunderingMethod(type(of: self).selfReturningMethod()) | ||
self = type(of: self).init(protocolExt: ()) | ||
} | ||
} | ||
|
||
class C: B { } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be clearer to say CD->isConvenienceInit()?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we ever expose factory inits as a user facing feature, they’d have the same behavior, wouldn’t they?