Skip to content

Commit 2139ea7

Browse files
committed
SIL: Fix type lowering of unowned reference to class-bound generic parameter
Unowned references lower as address-only or loadable, depending on whether the underlying class type is known to be native Swift or not. In the case where we were lowering an interface type, we would unconditionally erase the type to AnyObject, producing an incorrect lowering when the generic signature constrained the type parameter to a native Swift class. Fixes <rdar://problem/73083179>.
1 parent a144c20 commit 2139ea7

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

lib/SIL/IR/TypeLowering.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,15 @@ namespace {
414414
return visitAbstractTypeParamType(type, origType, isSensitive);
415415
}
416416

417-
Type getConcreteReferenceStorageReferent(Type type) {
417+
Type getConcreteReferenceStorageReferent(Type type,
418+
AbstractionPattern origType) {
418419
if (type->isTypeParameter()) {
420+
auto genericSig = origType.getGenericSignature();
421+
if (auto concreteType = genericSig->getConcreteType(type))
422+
return concreteType;
423+
if (auto superclassType = genericSig->getSuperclassBound(type))
424+
return superclassType;
425+
assert(genericSig->requiresClass(type));
419426
return TC.Context.getAnyObjectType();
420427
}
421428

@@ -460,7 +467,7 @@ namespace {
460467
IsTypeExpansionSensitive_t isSensitive) { \
461468
auto referentType = \
462469
type->getReferentType()->lookThroughSingleOptionalType(); \
463-
auto concreteType = getConcreteReferenceStorageReferent(referentType); \
470+
auto concreteType = getConcreteReferenceStorageReferent(referentType, origType); \
464471
if (Name##StorageType::get(concreteType, TC.Context) \
465472
->isLoadable(Expansion.getResilienceExpansion())) { \
466473
return asImpl().visitLoadable##Name##StorageType(type, origType, \
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %target-swift-emit-silgen %s -enable-objc-interop | %FileCheck %s
2+
3+
protocol ClassProtocol: AnyObject {}
4+
5+
class BaseClass {}
6+
7+
func makeGenericClosureWithUnknownClass<T>(t: T) where T : ClassProtocol {
8+
_ = { [unowned t] in _ = t }
9+
}
10+
11+
// CHECK-LABEL: sil private [ossa] @$s4main34makeGenericClosureWithUnknownClass1tyx_tAA0G8ProtocolRzlFyycfU_ : $@convention(thin) <T where T : ClassProtocol> (@guaranteed <τ_0_0 where τ_0_0 : ClassProtocol> { var @sil_unowned τ_0_0 } <T>) -> () {
12+
13+
func makeGenericClosureWithNativeClass1<T>(t: T) where T : BaseClass {
14+
_ = { [unowned t] in _ = t }
15+
}
16+
17+
// CHECK-LABEL: sil private [ossa] @$s4main34makeGenericClosureWithNativeClass11tyx_tAA9BaseClassCRbzlFyycfU_ : $@convention(thin) <T where T : BaseClass> (@guaranteed @sil_unowned T) -> () {
18+
19+
func makeGenericClosureWithNativeClass2<T>(t: T) where T : ClassProtocol, T : BaseClass {
20+
_ = { [unowned t] in _ = t }
21+
}
22+
23+
// CHECK-LABEL: sil private [ossa] @$s4main34makeGenericClosureWithNativeClass21tyx_tAA9BaseClassCRbzAA0I8ProtocolRzlFyycfU_ : $@convention(thin) <T where T : BaseClass, T : ClassProtocol> (@guaranteed @sil_unowned T) -> () {

0 commit comments

Comments
 (0)