Skip to content

[5.8][RequirementMachine] Skip Sendable conformance requirements from @preconcurrency declarations in requirement inference. #63493

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
46 changes: 37 additions & 9 deletions lib/AST/RequirementMachine/RequirementLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,12 @@ namespace {
/// AST walker that infers requirements from type representations.
struct InferRequirementsWalker : public TypeWalker {
ModuleDecl *module;
DeclContext *dc;
SmallVector<Requirement, 2> reqs;
SmallVector<RequirementError, 2> errors;

explicit InferRequirementsWalker(ModuleDecl *module) : module(module) {}
explicit InferRequirementsWalker(ModuleDecl *module, DeclContext *dc)
: module(module), dc(dc) {}

Action walkToTypePre(Type ty) override {
// Unbound generic types are the result of recovered-but-invalid code, and
Expand All @@ -482,11 +484,32 @@ struct InferRequirementsWalker : public TypeWalker {
}

Action walkToTypePost(Type ty) override {
// Skip `Sendable` conformance requirements that are inferred from
// `@preconcurrency` declarations.
auto skipRequirement = [&](Requirement req, Decl *fromDecl) {
if (!fromDecl->preconcurrency())
return false;

// If this decl is `@preconcurrency`, include concurrency
// requirements. The explicit annotation directly on the decl
// will still exclude `Sendable` requirements from ABI.
auto *decl = dc->getAsDecl();
if (!decl || decl->preconcurrency())
return false;

return (req.getKind() == RequirementKind::Conformance &&
req.getSecondType()->castTo<ProtocolType>()->getDecl()
->isSpecificProtocol(KnownProtocolKind::Sendable));
};

// Infer from generic typealiases.
if (auto typeAlias = dyn_cast<TypeAliasType>(ty.getPointer())) {
auto decl = typeAlias->getDecl();
auto subMap = typeAlias->getSubstitutionMap();
for (const auto &rawReq : decl->getGenericSignature().getRequirements()) {
if (skipRequirement(rawReq, decl))
continue;

desugarRequirement(rawReq.subst(subMap), SourceLoc(), reqs, errors);
}

Expand Down Expand Up @@ -567,6 +590,9 @@ struct InferRequirementsWalker : public TypeWalker {
// Handle the requirements.
// FIXME: Inaccurate TypeReprs.
for (const auto &rawReq : genericSig.getRequirements()) {
if (skipRequirement(rawReq, decl))
continue;

auto req = rawReq.subst(subMap);
desugarRequirement(req, SourceLoc(), reqs, errors);
}
Expand All @@ -585,12 +611,13 @@ struct InferRequirementsWalker : public TypeWalker {
/// We automatically infer 'T : Hashable' from the fact that 'struct Set'
/// declares a Hashable requirement on its generic parameter.
void swift::rewriting::inferRequirements(
Type type, SourceLoc loc, ModuleDecl *module,
Type type, SourceLoc loc,
ModuleDecl *module, DeclContext *dc,
SmallVectorImpl<StructuralRequirement> &result) {
if (!type)
return;

InferRequirementsWalker walker(module);
InferRequirementsWalker walker(module, dc);
type.walk(walker);

for (const auto &req : walker.reqs)
Expand Down Expand Up @@ -619,11 +646,11 @@ void swift::rewriting::realizeRequirement(
if (shouldInferRequirements) {
auto firstLoc = (reqRepr ? reqRepr->getSubjectRepr()->getStartLoc()
: SourceLoc());
inferRequirements(firstType, firstLoc, moduleForInference, result);
inferRequirements(firstType, firstLoc, moduleForInference, dc, result);

auto secondLoc = (reqRepr ? reqRepr->getConstraintRepr()->getStartLoc()
: SourceLoc());
inferRequirements(secondType, secondLoc, moduleForInference, result);
inferRequirements(secondType, secondLoc, moduleForInference, dc, result);
}

realizeTypeRequirement(dc, firstType, secondType, loc, result, errors);
Expand All @@ -634,7 +661,7 @@ void swift::rewriting::realizeRequirement(
if (shouldInferRequirements) {
auto firstLoc = (reqRepr ? reqRepr->getSubjectRepr()->getStartLoc()
: SourceLoc());
inferRequirements(firstType, firstLoc, moduleForInference, result);
inferRequirements(firstType, firstLoc, moduleForInference, dc, result);
}

SmallVector<Requirement, 2> reqs;
Expand All @@ -652,11 +679,11 @@ void swift::rewriting::realizeRequirement(
if (shouldInferRequirements) {
auto firstLoc = (reqRepr ? reqRepr->getFirstTypeRepr()->getStartLoc()
: SourceLoc());
inferRequirements(firstType, firstLoc, moduleForInference, result);
inferRequirements(firstType, firstLoc, moduleForInference, dc, result);

auto secondLoc = (reqRepr ? reqRepr->getSecondTypeRepr()->getStartLoc()
: SourceLoc());
inferRequirements(secondType, secondLoc, moduleForInference, result);
inferRequirements(secondType, secondLoc, moduleForInference, dc, result);
}

SmallVector<Requirement, 2> reqs;
Expand Down Expand Up @@ -700,7 +727,8 @@ void swift::rewriting::realizeInheritedRequirements(
auto *typeRepr = inheritedTypes[index].getTypeRepr();
SourceLoc loc = (typeRepr ? typeRepr->getStartLoc() : SourceLoc());
if (shouldInferRequirements) {
inferRequirements(inheritedType, loc, moduleForInference, result);
inferRequirements(inheritedType, loc, moduleForInference,
decl->getInnermostDeclContext(), result);
}

realizeTypeRequirement(dc, type, inheritedType, loc, result, errors);
Expand Down
3 changes: 2 additions & 1 deletion lib/AST/RequirementMachine/RequirementLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ void desugarRequirement(Requirement req, SourceLoc loc,
SmallVectorImpl<Requirement> &result,
SmallVectorImpl<RequirementError> &errors);

void inferRequirements(Type type, SourceLoc loc, ModuleDecl *module,
void inferRequirements(Type type, SourceLoc loc,
ModuleDecl *module, DeclContext *dc,
SmallVectorImpl<StructuralRequirement> &result);

void realizeRequirement(DeclContext *dc,
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/RequirementMachine/RequirementMachineRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ InferredGenericSignatureRequest::evaluate(
loc = typeLoc;

inferRequirements(sourcePair.getType(), typeLoc, moduleForInference,
requirements);
lookupDC, requirements);
}

// Finish by adding any remaining requirements. This is used to introduce
Expand Down
21 changes: 21 additions & 0 deletions test/Generics/requirement_inference_preconcurrency.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %target-swift-frontend -typecheck %s -debug-generic-signatures 2>&1 | %FileCheck %s

// CHECK-LABEL: ExistingType
// CHECK: Canonical generic signature: <τ_0_0 where τ_0_0 : Sendable>
@preconcurrency
public struct ExistingType<T: Sendable> : Sendable {}

// CHECK-LABEL: existingClient(arg:)
// CHECK: Canonical generic signature: <τ_0_0>
public func existingClient<T>(arg: T.Type) -> ExistingType<T>? { nil }

public typealias Alias = ExistingType

// CHECK-LABEL: existingClient2(arg:)
// CHECK: Canonical generic signature: <τ_0_0>
public func existingClient2<T>(arg: T.Type) -> Alias<T>? { nil }

// CHECK-LABEL: preconcurrencyClient(arg:)
// CHECK: Canonical generic signature: <τ_0_0 where τ_0_0 : Sendable>
@preconcurrency
public func preconcurrencyClient<T>(arg: T.Type) -> ExistingType<T>? { nil }