Skip to content

SIL: Fix type lowering of unowned reference to class-bound generic parameter [5.4] #35403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/SIL/IR/TypeLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,15 @@ namespace {
return visitAbstractTypeParamType(type, origType, isSensitive);
}

Type getConcreteReferenceStorageReferent(Type type) {
Type getConcreteReferenceStorageReferent(Type type,
AbstractionPattern origType) {
if (type->isTypeParameter()) {
auto genericSig = origType.getGenericSignature();
if (auto concreteType = genericSig->getConcreteType(type))
return concreteType;
if (auto superclassType = genericSig->getSuperclassBound(type))
return superclassType;
assert(genericSig->requiresClass(type));
return TC.Context.getAnyObjectType();
}

Expand Down Expand Up @@ -460,7 +467,7 @@ namespace {
IsTypeExpansionSensitive_t isSensitive) { \
auto referentType = \
type->getReferentType()->lookThroughSingleOptionalType(); \
auto concreteType = getConcreteReferenceStorageReferent(referentType); \
auto concreteType = getConcreteReferenceStorageReferent(referentType, origType); \
if (Name##StorageType::get(concreteType, TC.Context) \
->isLoadable(Expansion.getResilienceExpansion())) { \
return asImpl().visitLoadable##Name##StorageType(type, origType, \
Expand Down
23 changes: 23 additions & 0 deletions test/SILGen/unowned-class-bound-generic-parameter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %target-swift-emit-silgen %s -enable-objc-interop | %FileCheck %s

protocol ClassProtocol: AnyObject {}

class BaseClass {}

func makeGenericClosureWithUnknownClass<T>(t: T) where T : ClassProtocol {
_ = { [unowned t] in _ = t }
}

// 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>) -> () {

func makeGenericClosureWithNativeClass1<T>(t: T) where T : BaseClass {
_ = { [unowned t] in _ = t }
}

// CHECK-LABEL: sil private [ossa] @$s4main34makeGenericClosureWithNativeClass11tyx_tAA9BaseClassCRbzlFyycfU_ : $@convention(thin) <T where T : BaseClass> (@guaranteed @sil_unowned T) -> () {

func makeGenericClosureWithNativeClass2<T>(t: T) where T : ClassProtocol, T : BaseClass {
_ = { [unowned t] in _ = t }
}

// CHECK-LABEL: sil private [ossa] @$s4main34makeGenericClosureWithNativeClass21tyx_tAA9BaseClassCRbzAA0I8ProtocolRzlFyycfU_ : $@convention(thin) <T where T : BaseClass, T : ClassProtocol> (@guaranteed @sil_unowned T) -> () {