Skip to content

🍒[5.10][Distributed] Don't crash in thunk generation when missing SR conformance #69502

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
20 changes: 3 additions & 17 deletions include/swift/AST/DistributedDecl.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ Type getDistributedActorIDType(NominalTypeDecl *actor);
/// Similar to `getDistributedSerializationRequirementType`, however, from the
/// perspective of a concrete function. This way we're able to get the
/// serialization requirement for specific members, also in protocols.
Type getConcreteReplacementForMemberSerializationRequirement(ValueDecl *member);
Type getSerializationRequirementTypesForMember(
ValueDecl *member, llvm::SmallPtrSet<ProtocolDecl *, 2> &serializationRequirements);

/// Get specific 'SerializationRequirement' as defined in 'nominal'
/// type, which must conform to the passed 'protocol' which is expected
Expand Down Expand Up @@ -91,19 +92,13 @@ llvm::SmallPtrSet<ProtocolDecl *, 2>
getDistributedSerializationRequirementProtocols(
NominalTypeDecl *decl, ProtocolDecl* protocol);

/// Desugar and flatten the `SerializationRequirement` type into a set of
/// specific protocol declarations.
llvm::SmallPtrSet<ProtocolDecl *, 2>
flattenDistributedSerializationTypeToRequiredProtocols(
TypeBase *serializationRequirement);

/// Check if the `allRequirements` represent *exactly* the
/// `Encodable & Decodable` (also known as `Codable`) requirement.
///
/// If so, we can emit slightly nicer diagnostics.
bool checkDistributedSerializationRequirementIsExactlyCodable(
ASTContext &C,
const llvm::SmallPtrSetImpl<ProtocolDecl *> &allRequirements);
Type type);

/// Get the `SerializationRequirement`, explode it into the specific
/// protocol requirements and insert them into `requirements`.
Expand All @@ -120,15 +115,6 @@ getDistributedSerializationRequirements(
ProtocolDecl *protocol,
llvm::SmallPtrSetImpl<ProtocolDecl *> &requirementProtos);

/// Given any set of generic requirements, locate those which are about the
/// `SerializationRequirement`. Those need to be applied in the parameter and
/// return type checking of distributed targets.
llvm::SmallPtrSet<ProtocolDecl *, 2>
extractDistributedSerializationRequirements(
ASTContext &C, ArrayRef<Requirement> allRequirements);

}

// ==== ------------------------------------------------------------------------

#endif /* SWIFT_DECL_DISTRIBUTEDDECL_H */
168 changes: 42 additions & 126 deletions lib/AST/DistributedDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ Type swift::getConcreteReplacementForProtocolActorSystemType(ValueDecl *member)
llvm_unreachable("Unable to fetch ActorSystem type!");
}

Type swift::getConcreteReplacementForMemberSerializationRequirement(
ValueDecl *member) {
Type swift::getSerializationRequirementTypesForMember(
ValueDecl *member,
llvm::SmallPtrSet<ProtocolDecl *, 2> &serializationRequirements) {
auto &C = member->getASTContext();
auto *DC = member->getDeclContext();
auto DA = C.getDistributedActorDecl();
Expand All @@ -106,17 +107,21 @@ Type swift::getConcreteReplacementForMemberSerializationRequirement(
return getDistributedSerializationRequirementType(classDecl, C.getDistributedActorDecl());
}

/// === Maybe the value is declared in a protocol?
if (auto protocol = DC->getSelfProtocolDecl()) {
auto SerReqAssocType = DA->getAssociatedType(C.Id_SerializationRequirement)
->getDeclaredInterfaceType();

if (DC->getSelfProtocolDecl()) {
GenericSignature signature;
if (auto *genericContext = member->getAsGenericContext()) {
signature = genericContext->getGenericSignature();
} else {
signature = DC->getGenericSignatureOfContext();
}

auto SerReqAssocType = DA->getAssociatedType(C.Id_SerializationRequirement)
->getDeclaredInterfaceType();
// Also store all `SerializationRequirement : SomeProtocol` requirements
for (auto proto: signature->getRequiredProtocols(SerReqAssocType)) {
serializationRequirements.insert(proto);
}

// Note that this may be null, e.g. if we're a distributed func inside
// a protocol that did not declare a specific actor system requirement.
Expand Down Expand Up @@ -178,13 +183,7 @@ Type swift::getDistributedActorSystemResultHandlerType(
auto module = system->getParentModule();
Type selfType = system->getSelfInterfaceType();
auto conformance = module->lookupConformance(selfType, DAS);
auto witness =
conformance.getTypeWitnessByName(selfType, ctx.Id_ResultHandler);
if (auto alias = dyn_cast<TypeAliasType>(witness.getPointer())) {
return alias->getDecl()->getUnderlyingType();
} else {
return witness;
}
return conformance.getTypeWitnessByName(selfType, ctx.Id_ResultHandler);
}

Type swift::getDistributedActorSystemInvocationEncoderType(NominalTypeDecl *system) {
Expand Down Expand Up @@ -346,63 +345,39 @@ swift::getDistributedSerializationRequirements(
if (existentialRequirementTy->isAny())
return true; // we're done here, any means there are no requirements

if (!existentialRequirementTy->isExistentialType()) {
// SerializationRequirement must be an existential type
return false;
}

ExistentialType *serialReqType = existentialRequirementTy
->castTo<ExistentialType>();
auto *serialReqType = existentialRequirementTy->getAs<ExistentialType>();
if (!serialReqType || serialReqType->hasError()) {
return false;
}

auto desugaredTy = serialReqType->getConstraintType()->getDesugaredType();
auto flattenedRequirements =
flattenDistributedSerializationTypeToRequiredProtocols(
desugaredTy);
for (auto p : flattenedRequirements) {
auto layout = serialReqType->getExistentialLayout();
for (auto p : layout.getProtocols()) {
requirementProtos.insert(p);
}

return true;
}

llvm::SmallPtrSet<ProtocolDecl *, 2>
swift::flattenDistributedSerializationTypeToRequiredProtocols(
TypeBase *serializationRequirement) {
llvm::SmallPtrSet<ProtocolDecl *, 2> serializationReqs;
if (auto composition =
serializationRequirement->getAs<ProtocolCompositionType>()) {
for (auto member : composition->getMembers()) {
if (auto comp = member->getAs<ProtocolCompositionType>()) {
for (auto protocol :
flattenDistributedSerializationTypeToRequiredProtocols(comp)) {
serializationReqs.insert(protocol);
}
} else if (auto *protocol = member->getAs<ProtocolType>()) {
serializationReqs.insert(protocol->getDecl());
}
}
} else {
auto protocol = serializationRequirement->castTo<ProtocolType>()->getDecl();
serializationReqs.insert(protocol);
}

return serializationReqs;
}

bool swift::checkDistributedSerializationRequirementIsExactlyCodable(
ASTContext &C,
const llvm::SmallPtrSetImpl<ProtocolDecl *> &allRequirements) {
Type type) {
if (!type)
return false;

if (type->hasError())
return false;

auto encodable = C.getProtocol(KnownProtocolKind::Encodable);
auto decodable = C.getProtocol(KnownProtocolKind::Decodable);

if (allRequirements.size() != 2)
auto layout = type->getExistentialLayout();
auto protocols = layout.getProtocols();

if (protocols.size() != 2)
return false;

return allRequirements.count(encodable) &&
allRequirements.count(decodable);
return std::count(protocols.begin(), protocols.end(), encodable) == 1 &&
std::count(protocols.begin(), protocols.end(), decodable) == 1;
}

/******************************************************************************/
Expand Down Expand Up @@ -571,25 +546,19 @@ bool AbstractFunctionDecl::isDistributedActorSystemRemoteCall(bool isVoidReturn)

// --- Check requirement: conforms_to: Act DistributedActor
auto actorReq = requirements[0];
auto distActorTy = C.getProtocol(KnownProtocolKind::DistributedActor)
->getInterfaceType()
->getMetatypeInstanceType();
if (actorReq.getKind() != RequirementKind::Conformance) {
return false;
}
if (!actorReq.getSecondType()->isEqual(distActorTy)) {
if (!actorReq.getProtocolDecl()->isSpecificProtocol(KnownProtocolKind::DistributedActor)) {
return false;
}

// --- Check requirement: conforms_to: Err Error
auto errorReq = requirements[1];
auto errorTy = C.getProtocol(KnownProtocolKind::Error)
->getInterfaceType()
->getMetatypeInstanceType();
if (errorReq.getKind() != RequirementKind::Conformance) {
return false;
}
if (!errorReq.getSecondType()->isEqual(errorTy)) {
if (!errorReq.getProtocolDecl()->isSpecificProtocol(KnownProtocolKind::Error)) {
return false;
}

Expand All @@ -604,10 +573,9 @@ bool AbstractFunctionDecl::isDistributedActorSystemRemoteCall(bool isVoidReturn)
assert(ResParam && "Non void function, yet no Res generic parameter found");
if (auto func = dyn_cast<FuncDecl>(this)) {
auto resultType = func->mapTypeIntoContext(func->getResultInterfaceType())
->getMetatypeInstanceType()
->getDesugaredType();
->getMetatypeInstanceType();
auto resultParamType = func->mapTypeIntoContext(
ResParam->getInterfaceType()->getMetatypeInstanceType());
ResParam->getDeclaredInterfaceType());
// The result of the function must be the `Res` generic argument.
if (!resultType->isEqual(resultParamType)) {
return false;
Expand Down Expand Up @@ -803,12 +771,10 @@ AbstractFunctionDecl::isDistributedTargetInvocationEncoderRecordArgument() const

// the <Value> of the RemoteCallArgument<Value>
auto remoteCallArgValueGenericTy =
mapTypeIntoContext(argGenericParams[0]->getInterfaceType())
->getDesugaredType()
->getMetatypeInstanceType();
mapTypeIntoContext(argGenericParams[0]->getDeclaredInterfaceType());
// expected (the <Value> from the recordArgument<Value>)
auto expectedGenericParamTy = mapTypeIntoContext(
ArgumentParam->getInterfaceType()->getMetatypeInstanceType());
ArgumentParam->getDeclaredInterfaceType());

if (!remoteCallArgValueGenericTy->isEqual(expectedGenericParamTy)) {
return false;
Expand Down Expand Up @@ -938,11 +904,10 @@ AbstractFunctionDecl::isDistributedTargetInvocationEncoderRecordReturnType() con
// ...

auto resultType = func->mapTypeIntoContext(argumentParam->getInterfaceType())
->getMetatypeInstanceType()
->getDesugaredType();
->getMetatypeInstanceType();

auto resultParamType = func->mapTypeIntoContext(
ArgumentParam->getInterfaceType()->getMetatypeInstanceType());
ArgumentParam->getDeclaredInterfaceType());

// The result of the function must be the `Res` generic argument.
if (!resultType->isEqual(resultParamType)) {
Expand Down Expand Up @@ -1052,13 +1017,10 @@ AbstractFunctionDecl::isDistributedTargetInvocationEncoderRecordErrorType() cons

// --- Check requirement: conforms_to: Err Error
auto errorReq = requirements[0];
auto errorTy = C.getProtocol(KnownProtocolKind::Error)
->getInterfaceType()
->getMetatypeInstanceType();
if (errorReq.getKind() != RequirementKind::Conformance) {
return false;
}
if (!errorReq.getSecondType()->isEqual(errorTy)) {
if (!errorReq.getProtocolDecl()->isSpecificProtocol(KnownProtocolKind::Error)) {
return false;
}

Expand Down Expand Up @@ -1145,10 +1107,9 @@ AbstractFunctionDecl::isDistributedTargetInvocationDecoderDecodeNextArgument() c
// --- Check: Argument: SerializationRequirement
GenericTypeParamDecl *ArgumentParam = genericParams->getParams()[0];
auto resultType = func->mapTypeIntoContext(func->getResultInterfaceType())
->getMetatypeInstanceType()
->getDesugaredType();
->getMetatypeInstanceType();
auto resultParamType = func->mapTypeIntoContext(
ArgumentParam->getInterfaceType()->getMetatypeInstanceType());
ArgumentParam->getDeclaredInterfaceType());
// The result of the function must be the `Res` generic argument.
if (!resultType->isEqual(resultParamType)) {
return false;
Expand Down Expand Up @@ -1243,11 +1204,10 @@ AbstractFunctionDecl::isDistributedTargetInvocationResultHandlerOnReturn() const
// === Check generic parameters in detail
// --- Check: Argument: SerializationRequirement
GenericTypeParamDecl *ArgumentParam = genericParams->getParams()[0];
auto argumentType = func->mapTypeIntoContext(valueParam->getInterfaceType())
->getMetatypeInstanceType()
->getDesugaredType();
auto argumentType = func->mapTypeIntoContext(
valueParam->getInterfaceType()->getMetatypeInstanceType());
auto resultParamType = func->mapTypeIntoContext(
ArgumentParam->getInterfaceType()->getMetatypeInstanceType());
ArgumentParam->getDeclaredInterfaceType());
// The result of the function must be the `Res` generic argument.
if (!argumentType->isEqual(resultParamType)) {
return false;
Expand All @@ -1268,50 +1228,6 @@ AbstractFunctionDecl::isDistributedTargetInvocationResultHandlerOnReturn() const
return true;
}

llvm::SmallPtrSet<ProtocolDecl *, 2>
swift::extractDistributedSerializationRequirements(
ASTContext &C, ArrayRef<Requirement> allRequirements) {
llvm::SmallPtrSet<ProtocolDecl *, 2> serializationReqs;
auto DA = C.getDistributedActorDecl();
auto daSerializationReqAssocType =
DA->getAssociatedType(C.Id_SerializationRequirement);
auto daSystemSerializationReqTy = daSerializationReqAssocType->getInterfaceType();

for (auto req : allRequirements) {
if (req.getSecondType()->isAny()) {
continue;
}
if (!req.getFirstType()->hasDependentMember())
continue;

if (auto dependentMemberType =
req.getFirstType()->castTo<DependentMemberType>()) {
auto dependentTy =
dependentMemberType->getAssocType()->getInterfaceType();

if (dependentTy->isEqual(daSystemSerializationReqTy)) {
auto requirementProto = req.getSecondType();
if (auto proto = dyn_cast_or_null<ProtocolDecl>(
requirementProto->getAnyNominal())) {
serializationReqs.insert(proto);
} else {
auto serialReqType = requirementProto->castTo<ExistentialType>()
->getConstraintType()
->getDesugaredType();
auto flattenedRequirements =
flattenDistributedSerializationTypeToRequiredProtocols(
serialReqType);
for (auto p : flattenedRequirements) {
serializationReqs.insert(p);
}
}
}
}
}

return serializationReqs;
}

/******************************************************************************/
/********************** Distributed Functions *********************************/
/******************************************************************************/
Expand Down
8 changes: 7 additions & 1 deletion lib/Sema/CodeSynthesisDistributedActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -842,9 +842,15 @@ FuncDecl *GetDistributedThunkRequest::evaluate(Evaluator &evaluator,
if (!distributedTarget->isDistributed())
return nullptr;
}

assert(distributedTarget);

// This evaluation type-check by now was already computed and cached;
// We need to check in order to avoid emitting a THUNK for a distributed func
// which had errors; as the thunk then may also cause un-addressable issues and confusion.
if (swift::checkDistributedFunction(distributedTarget)) {
return nullptr;
}

auto &C = distributedTarget->getASTContext();

if (!getConcreteReplacementForProtocolActorSystemType(distributedTarget)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckDeclOverride.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2067,7 +2067,7 @@ static bool checkSingleOverride(ValueDecl *override, ValueDecl *base) {
return (prop &&
prop->isFinal() &&
isa<ClassDecl>(prop->getDeclContext()) &&
cast<ClassDecl>(prop->getDeclContext())->isActor() &&
cast<ClassDecl>(prop->getDeclContext())->isAnyActor() &&
!prop->isStatic() &&
prop->getName() == ctx.Id_unownedExecutor &&
prop->getInterfaceType()->getAnyNominal() == ctx.getUnownedSerialExecutorDecl());
Expand Down
Loading