Skip to content

Commit d106dc1

Browse files
authored
Merge pull request #62640 from eeckstein/fix-letpropertyopt
LetPropertiesOpts: don't crash for properties in ObjC extensions
2 parents b206c76 + d442155 commit d106dc1

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

lib/AST/Attr.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,7 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
941941
case DAK_Optimize:
942942
case DAK_Exclusivity:
943943
case DAK_NonSendable:
944+
case DAK_ObjCImplementation:
944945
if (getKind() == DAK_Effects &&
945946
cast<EffectsAttr>(this)->getKind() == EffectsKind::Custom) {
946947
Printer.printAttrName("@_effects");

lib/SILOptimizer/IPO/LetPropertiesOpts.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,10 @@ static bool isAssignableExternally(VarDecl *Property, SILModule *Module) {
361361

362362
auto *Ty = dyn_cast<NominalTypeDecl>(Property->getDeclContext());
363363

364+
// Check for "unusual" decl contexts, e.g. ObjC extensions.
365+
if (!Ty)
366+
return true;
367+
364368
// Initializer for a let property of a class cannot exist externally.
365369
// It cannot be defined by an extension or a derived class.
366370
if (isa<ClassDecl>(Ty))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#import <Foundation/NSObject.h>
2+
3+
@interface ObjcInterface: NSObject
4+
@end
5+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %target-swift-frontend -module-name test -primary-file %s -import-objc-header %S/Inputs/let_properties_opts.h -O -wmo -emit-sil | %FileCheck %s
2+
3+
// REQUIRES: objc_interop
4+
5+
// CHECK-LABEL: @_objcImplementation extension ObjcInterface
6+
@_objcImplementation extension ObjcInterface {
7+
final public let i: Int
8+
}
9+
10+
// Check that it doesn't crash with properties in ObjC extensions
11+
12+
// CHECK-LABEL: sil @$s4test0A13ObjcInterfaceySiSo0bC0CF
13+
// CHECK: ref_element_addr %0 : $ObjcInterface, #ObjcInterface.i
14+
// CHECK: } // end sil function '$s4test0A13ObjcInterfaceySiSo0bC0CF'
15+
public func testObjcInterface(_ x: ObjcInterface) -> Int {
16+
return x.i
17+
}
18+

0 commit comments

Comments
 (0)