Skip to content

Support initial values for stored properties of @_objcImplementation. #64040

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/SILGen/SILGenType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1332,8 +1332,13 @@ class SILGenExtension : public TypeMemberVisitor<SILGenExtension> {
void visitPatternBindingDecl(PatternBindingDecl *pd) {
// Emit initializers for static variables.
for (auto i : range(pd->getNumPatternEntries())) {
if (pd->getExecutableInit(i) && pd->isStatic()) {
SGM.emitGlobalInitialization(pd, i);
if (pd->getExecutableInit(i)) {
if (pd->isStatic())
SGM.emitGlobalInitialization(pd, i);
else if (isa<ExtensionDecl>(pd->getDeclContext()) &&
cast<ExtensionDecl>(pd->getDeclContext())
->isObjCImplementation())
SGM.emitStoredPropertyInitialization(pd, i);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions test/Interpreter/Inputs/objc_implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)init;

@property (assign) NSInteger implProperty;
@property (assign) NSInteger defaultIntProperty;

+ (void)runTests;
- (nonnull NSString *)someMethod;
Expand Down
6 changes: 6 additions & 0 deletions test/Interpreter/objc_implementation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class LastWords {

@objc var implProperty: Int
final var object: LastWords
final weak var defaultNilProperty: AnyObject?
@objc var defaultIntProperty: Int = 17

@objc class func runTests() {
do {
Expand All @@ -35,6 +37,7 @@ class LastWords {
print("implProperty =", impl.implProperty)
impl.implProperty = 42
print("implProperty =", impl.implProperty)
print("defaultIntProperty =", impl.defaultIntProperty)
print("description =", impl.description)
}

Expand All @@ -44,6 +47,7 @@ class LastWords {
print("implProperty =", swiftSub.implProperty)
swiftSub.implProperty = 42
print("implProperty =", swiftSub.implProperty)
print("defaultIntProperty =", swiftSub.defaultIntProperty)

print("otherProperty =", swiftSub.otherProperty)
swiftSub.otherProperty = 13
Expand Down Expand Up @@ -75,11 +79,13 @@ ImplClass.runTests()
// CHECK: someMethod = ImplClass.someMethod()
// CHECK: implProperty = 0
// CHECK: implProperty = 42
// CHECK: defaultIntProperty = 17
// CHECK: description = ImplClass(implProperty: 42, object: main.LastWords)
// CHECK: ImplClass It's better to burn out than to fade away.
// CHECK: someMethod = SwiftSubclass.someMethod()
// CHECK: implProperty = 0
// CHECK: implProperty = 42
// CHECK: defaultIntProperty = 17
// CHECK: otherProperty = 1
// CHECK: otherProperty = 13
// CHECK: implProperty = 42
Expand Down