Skip to content

Commit 1eaa0c1

Browse files
authored
Merge pull request #4877 from slavapestov/fix-generic-subclass-nsobject
2 parents 81ee414 + 584316d commit 1eaa0c1

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

lib/IRGen/GenClass.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,16 +230,13 @@ namespace {
230230
assert(superclass);
231231

232232
if (superclass->hasClangNode()) {
233-
// As a special case, assume NSObject has a fixed layout.
234-
if (superclass->getName() !=
235-
IGM.Context.getSwiftId(KnownFoundationEntity::NSObject)) {
236-
// If the superclass was imported from Objective-C, its size is
237-
// not known at compile time. However, since the field offset
238-
// vector only stores offsets of stored properties defined in
239-
// Swift, we don't have to worry about indirect indexing of
240-
// the field offset vector.
241-
ClassHasFixedSize = false;
242-
}
233+
// If the superclass was imported from Objective-C, its size is
234+
// not known at compile time. However, since the field offset
235+
// vector only stores offsets of stored properties defined in
236+
// Swift, we don't have to worry about indirect indexing of
237+
// the field offset vector.
238+
ClassHasFixedSize = false;
239+
243240
} else if (IGM.isResilient(superclass, ResilienceExpansion::Maximal)) {
244241
ClassMetadataRequiresDynamicInitialization = true;
245242

test/IRGen/class_resilience_objc.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
import Foundation
88

99
public class FixedLayoutObjCSubclass : NSObject {
10-
// This field uses constant direct access because NSObject has fixed layout.
10+
// This field could use constant direct access because NSObject has
11+
// fixed layout, but we don't allow that right now.
1112
public final var field: Int32 = 0
1213
};
1314

1415
// CHECK-LABEL: define hidden void @_TF21class_resilience_objc29testConstantDirectFieldAccessFCS_23FixedLayoutObjCSubclassT_(%C21class_resilience_objc23FixedLayoutObjCSubclass*)
15-
// CHECK: [[FIELD_ADDR:%.*]] = getelementptr inbounds %C21class_resilience_objc23FixedLayoutObjCSubclass, %C21class_resilience_objc23FixedLayoutObjCSubclass* %0, i32 0, i32 1
16-
// CHECK-NEXT: [[PAYLOAD_ADDR:%.*]] = getelementptr inbounds %Vs5Int32, %Vs5Int32* %1, i32 0, i32 0
16+
// CHECK: [[OFFSET:%.*]] = load [[INT]], [[INT]]* @_TWvdvC21class_resilience_objc23FixedLayoutObjCSubclass5fieldVs5Int32
17+
// CHECK-NEXT: [[OBJECT:%.*]] = bitcast %C21class_resilience_objc23FixedLayoutObjCSubclass* %0 to i8*
18+
// CHECK-NEXT: [[ADDR:%.*]] = getelementptr inbounds i8, i8* [[OBJECT]], [[INT]] [[OFFSET]]
19+
// CHECK-NEXT: [[FIELD_ADDR:%.*]] = bitcast i8* [[ADDR]] to %Vs5Int32*
20+
// CHECK-NEXT: [[PAYLOAD_ADDR:%.*]] = getelementptr inbounds %Vs5Int32, %Vs5Int32* [[FIELD_ADDR]], i32 0, i32 0
1721
// CHECK-NEXT: store i32 10, i32* [[PAYLOAD_ADDR]]
1822

1923
func testConstantDirectFieldAccess(_ o: FixedLayoutObjCSubclass) {

test/Interpreter/generic_objc_subclass.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,19 @@ fixedB.third = 17
233233

234234
// CHECK: (101, 0, 0, 0, 16, [19, 84], 17)
235235
print(fixedG())
236+
237+
// Problem with field alignment in direct generic subclass of NSObject -
238+
// <https://bugs.swift.org/browse/SR-2586>
239+
public class PandorasBox<T>: NSObject {
240+
final public var value: T
241+
242+
public init(_ value: T) {
243+
// Uses ConstantIndirect access pattern
244+
self.value = value
245+
}
246+
}
247+
248+
let c = PandorasBox(30)
249+
// CHECK: 30
250+
// Uses ConstantDirect access pattern
251+
print(c.value)

0 commit comments

Comments
 (0)