Skip to content

SIL: Fix type lowering of 'unowned T' where 'T' is a type parameter #79293

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
merged 1 commit into from
Feb 12, 2025
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
32 changes: 20 additions & 12 deletions lib/SIL/IR/TypeLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,19 +655,29 @@ namespace {
return visitAbstractTypeParamType(type, origType, isSensitive);
}

Type getConcreteReferenceStorageReferent(Type type,
Type getConcreteReferenceStorageReferent(Type substType,
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));
substType = substType->getReferenceStorageReferent();
origType = origType.getReferenceStorageReferentType();

if (auto objectType = substType->getOptionalObjectType()) {
substType = objectType;
origType = origType.getOptionalObjectType();
}

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

return type;
return substType;
}

#define NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
Expand Down Expand Up @@ -710,9 +720,7 @@ namespace {
RetTy visit##Name##StorageType(Can##Name##StorageType type, \
AbstractionPattern origType, \
IsTypeExpansionSensitive_t isSensitive) { \
auto referentType = \
type->getReferentType()->lookThroughSingleOptionalType(); \
auto concreteType = getConcreteReferenceStorageReferent(referentType, origType); \
auto concreteType = getConcreteReferenceStorageReferent(type, origType); \
if (Name##StorageType::get(concreteType, TC.Context) \
->isLoadable(Expansion.getResilienceExpansion())) { \
return asImpl().visitLoadable##Name##StorageType(type, origType, \
Expand Down
16 changes: 15 additions & 1 deletion test/SILGen/unowned-class-bound-generic-parameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,18 @@ func makeGenericClosureWithNativeClass2<T>(t: T) where T : ClassProtocol, T : Ba
_ = { [unowned t] in _ = t }
}

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

// https://github.com/swiftlang/swift/issues/79244

struct Wrapper<T: AnyObject> {
unowned var t: T
}

func f1<T: AnyObject>(t: T) {
_ = { Wrapper(t: t) }
}

func f2<T: AnyObject, U: AnyObject>(u: U, tt: Array<Wrapper<T>>) {
_ = tt.map { _ in Wrapper(t: u) }
}