Skip to content

Commit 8f38532

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 d710cfc commit 8f38532

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
@@ -423,8 +423,15 @@ namespace {
423423
return visitAbstractTypeParamType(type, origType, isSensitive);
424424
}
425425

426-
Type getConcreteReferenceStorageReferent(Type type) {
426+
Type getConcreteReferenceStorageReferent(Type type,
427+
AbstractionPattern origType) {
427428
if (type->isTypeParameter()) {
429+
auto genericSig = origType.getGenericSignature();
430+
if (auto concreteType = genericSig->getConcreteType(type))
431+
return concreteType;
432+
if (auto superclassType = genericSig->getSuperclassBound(type))
433+
return superclassType;
434+
assert(genericSig->requiresClass(type));
428435
return TC.Context.getAnyObjectType();
429436
}
430437

@@ -469,7 +476,7 @@ namespace {
469476
IsTypeExpansionSensitive_t isSensitive) { \
470477
auto referentType = \
471478
type->getReferentType()->lookThroughSingleOptionalType(); \
472-
auto concreteType = getConcreteReferenceStorageReferent(referentType); \
479+
auto concreteType = getConcreteReferenceStorageReferent(referentType, origType); \
473480
if (Name##StorageType::get(concreteType, TC.Context) \
474481
->isLoadable(Expansion.getResilienceExpansion())) { \
475482
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)