Skip to content

SIL: Fix regression with unowned capture of generic parameter #17784

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
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
19 changes: 10 additions & 9 deletions lib/SIL/TypeLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,13 @@ namespace {
return visitAbstractTypeParamType(type);
}

bool hasNativeReferenceCounting(CanType type) {
Type getConcreteReferenceStorageReferent(Type type) {
if (type->isTypeParameter()) {
auto signature = getGenericSignature();
assert(signature && "dependent type without generic signature?!");

if (auto concreteType = signature->getConcreteType(type))
return hasNativeReferenceCounting(concreteType->getCanonicalType());
return concreteType->getCanonicalType();

assert(signature->requiresClass(type));

Expand All @@ -288,17 +288,14 @@ namespace {
// at some point the type-checker should prove acyclic-ness.
auto bound = signature->getSuperclassBound(type);
if (bound) {
return hasNativeReferenceCounting(bound->getCanonicalType());
return getConcreteReferenceStorageReferent(bound->getCanonicalType());
}

// Ask whether Builtin.UnknownObject uses native reference counting.
auto &ctx = M.getASTContext();
return ctx.TheUnknownObjectType->
usesNativeReferenceCounting(ResilienceExpansion::Maximal);
return ctx.TheUnknownObjectType;
}

// FIXME: resilience
return type->usesNativeReferenceCounting(ResilienceExpansion::Maximal);
return type;
}

#define NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
Expand All @@ -321,7 +318,11 @@ namespace {
IsAddressOnly}); \
} \
RetTy visit##Name##StorageType(Can##Name##StorageType type) { \
if (type->isLoadable(ResilienceExpansion::Maximal)) { \
auto referentType = type->getReferentType(); \
auto concreteType = getConcreteReferenceStorageReferent(referentType); \
auto &ctx = M.getASTContext(); \
if (Name##StorageType::get(concreteType, ctx) \
->isLoadable(ResilienceExpansion::Maximal)) { \
return asImpl().visitLoadable##Name##StorageType(type); \
} else { \
return asImpl().visitAddressOnly##Name##StorageType(type); \
Expand Down
9 changes: 9 additions & 0 deletions test/SILGen/unowned.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,12 @@ struct Unowned<T: AnyObject> {
}
func takesUnownedStruct(_ z: Unowned<C>) {}
// CHECK-LABEL: sil hidden @$S7unowned18takesUnownedStructyyAA0C0VyAA1CCGF : $@convention(thin) (@guaranteed Unowned<C>) -> ()

// Make sure we don't crash here
struct UnownedGenericCapture<T : AnyObject> {
var object: T

func f() -> () -> () {
return { [unowned object] in _ = object }
}
}