Skip to content

Commit 33d64db

Browse files
Merge pull request #5449 from aschwaighofer/irgen_fix_generic_element_ref
IRGen: Cast the type of class pointer to the storage type
2 parents 5687341 + 2d73b3c commit 33d64db

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/IRGen/GenClass.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,11 @@ OwnedAddress irgen::projectPhysicalClassMemberAddress(IRGenFunction &IGF,
456456
case FieldAccess::ConstantDirect: {
457457
Address baseAddr(base, baseClassTI.getHeapAlignment(IGF.IGM, baseType));
458458
auto &element = baseClassTI.getElements(IGF.IGM, baseType)[fieldIndex];
459+
// We might run into a case where the type of baseAddr is an opaque pointer.
460+
if (baseAddr->getType() != baseClassTI.getStorageType()) {
461+
baseAddr = IGF.Builder.CreateBitCast(baseAddr,
462+
baseClassTI.getStorageType());
463+
}
459464
Address memberAddr = element.project(IGF, baseAddr, None);
460465
// We may need to bitcast the address if the field is of a generic type.
461466
if (memberAddr.getType()->getElementType() != fieldTI.getStorageType())

test/IRGen/class_bounded_generics.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,15 @@ func class_bounded_metatype<T: SomeSwiftClass>(_ t : T) {
261261
class WeakRef<T: AnyObject> {
262262
weak var value: T?
263263
}
264+
265+
class A<T> {
266+
required init() {}
267+
}
268+
269+
class M<T, S: A<T>> {
270+
private var s: S
271+
init() {
272+
// Don't crash generating the reference to 's'.
273+
s = S.init()
274+
}
275+
}

0 commit comments

Comments
 (0)