Skip to content

Strip Sendable from existential types in @preconcurrency mangling. #59636

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
61 changes: 61 additions & 0 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,67 @@ Type TypeBase::stripConcurrency(bool recurse, bool dropGlobalActor) {
return newFnType;
}

if (auto existentialType = getAs<ExistentialType>()) {
auto newConstraintType = existentialType->getConstraintType()
->stripConcurrency(recurse, dropGlobalActor);
if (newConstraintType.getPointer() ==
existentialType->getConstraintType().getPointer())
return Type(this);

return ExistentialType::get(newConstraintType);
}

if (auto protocolType = getAs<ProtocolType>()) {
if (protocolType->getDecl()->isSpecificProtocol(
KnownProtocolKind::Sendable))
return ProtocolCompositionType::get(getASTContext(), { }, false);

return Type(this);
}

if (auto protocolCompositionType = getAs<ProtocolCompositionType>()) {
SmallVector<Type, 4> newMembers;
auto members = protocolCompositionType->getMembers();
for (unsigned i : indices(members)) {
auto memberType = members[i];
auto newMemberType =
memberType->stripConcurrency(recurse, dropGlobalActor);
if (!newMembers.empty()) {
newMembers.push_back(newMemberType);
continue;
}

if (memberType.getPointer() != newMemberType.getPointer()) {
newMembers.append(members.begin(), members.begin() + i);
newMembers.push_back(newMemberType);
continue;
}
}

if (!newMembers.empty()) {
return ProtocolCompositionType::get(
getASTContext(), newMembers,
protocolCompositionType->hasExplicitAnyObject());
}

return Type(this);
}

if (auto existentialMetatype = getAs<ExistentialMetatypeType>()) {
auto instanceType = existentialMetatype->getExistentialInstanceType();
auto newInstanceType =
instanceType->stripConcurrency(recurse, dropGlobalActor);
if (instanceType.getPointer() != newInstanceType.getPointer()) {
Optional<MetatypeRepresentation> repr;
if (existentialMetatype->hasRepresentation())
repr = existentialMetatype->getRepresentation();
return ExistentialMetatypeType::get(
newInstanceType, repr, getASTContext());
}

return Type(this);
}

return Type(this);
}

Expand Down
10 changes: 10 additions & 0 deletions test/SILGen/mangling_predates_concurrency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@
public func excitingFunction<T: Sendable>(value: T, body: (@Sendable () -> Void)?) -> (@MainActor () -> Void) {
{ }
}

public protocol P { }

// CHECK: sil [ossa] @$s29mangling_predates_concurrency13lotsOfChangesyyXlSgyp_AA1P_pypXpAaD_XlXptF
@preconcurrency public func lotsOfChanges(
_: Sendable, _: P & Sendable, _: Sendable.Type,
_: (AnyObject & Sendable & P).Type
) -> (AnyObject & Sendable)? {
nil
}