Skip to content

Commit 0db2231

Browse files
committed
[ConstraintSystem] Record conformances synthesized for ad-hoc distributed requirements
1 parent 171fb0a commit 0db2231

File tree

5 files changed

+39
-14
lines changed

5 files changed

+39
-14
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,6 +1627,11 @@ class Solution {
16271627
llvm::DenseMap<ConstraintLocator *, UnresolvedDotExpr *>
16281628
ImplicitCallAsFunctionRoots;
16291629

1630+
/// The set of conformances synthesized during solving (i.e. for
1631+
/// ad-hoc distributed `SerializationRequirement` conformances).
1632+
llvm::MapVector<ConstraintLocator *, ProtocolConformanceRef>
1633+
SynthesizedConformances;
1634+
16301635
/// Record a new argument matching choice for given locator that maps a
16311636
/// single argument to a single parameter.
16321637
void recordSingleArgMatchingChoice(ConstraintLocator *locator);
@@ -2415,6 +2420,11 @@ class ConstraintSystem {
24152420
llvm::SmallMapVector<ConstraintLocator *, UnresolvedDotExpr *, 2>
24162421
ImplicitCallAsFunctionRoots;
24172422

2423+
/// The set of conformances synthesized during solving (i.e. for
2424+
/// ad-hoc distributed `SerializationRequirement` conformances).
2425+
llvm::MapVector<ConstraintLocator *, ProtocolConformanceRef>
2426+
SynthesizedConformances;
2427+
24182428
private:
24192429
/// Describe the candidate expression for partial solving.
24202430
/// This class used by shrink & solve methods which apply
@@ -2938,6 +2948,9 @@ class ConstraintSystem {
29382948
/// The length of \c ImplicitCallAsFunctionRoots.
29392949
unsigned numImplicitCallAsFunctionRoots;
29402950

2951+
/// The length of \c SynthesizedConformances.
2952+
unsigned numSynthesizedConformances;
2953+
29412954
/// The previous score.
29422955
Score PreviousScore;
29432956

lib/Sema/CSApply.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ Solution::computeSubstitutions(NullablePtr<ValueDecl> decl,
105105
subs[opened.first] = type;
106106
}
107107

108-
auto *DC = constraintSystem->DC;
109108
auto lookupConformanceFn =
110109
[&](CanType original, Type replacement,
111110
ProtocolDecl *protoType) -> ProtocolConformanceRef {
@@ -121,17 +120,9 @@ Solution::computeSubstitutions(NullablePtr<ValueDecl> decl,
121120
replacement, protoType, /*allowMissing=*/true);
122121

123122
if (conformance.isInvalid()) {
124-
if (auto *funcDecl = dyn_cast<FuncDecl>(decl.getPtrOrNull())) {
125-
if (funcDecl->isDistributedActorSystemRemoteCall(
126-
/*isVoidResult=*/false)) {
127-
// `Res` conformances would be looked by at runtime but are
128-
// guaranteed to be there by Sema because all distributed
129-
// methods and accessors are checked to conform to
130-
// `SerializationRequirement` of `DistributedActorSystem`.
131-
if (original->isEqual(funcDecl->getResultInterfaceType()))
132-
return ProtocolConformanceRef(protoType);
133-
}
134-
}
123+
auto synthesized = SynthesizedConformances.find(locator);
124+
if (synthesized != SynthesizedConformances.end())
125+
return synthesized->second;
135126
}
136127

137128
return conformance;

lib/Sema/CSSimplify.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8583,9 +8583,17 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
85838583
// but they are verified by Sema so it's okay to omit them here and lookup
85848584
// dynamically during IRGen.
85858585
if (auto *witness = dyn_cast<FuncDecl>(witnessInfo->first)) {
8586+
auto synthesizeConformance = [&]() {
8587+
ProtocolConformanceRef synthesized(protocol);
8588+
auto witnessLoc = getConstraintLocator(
8589+
/*anchor=*/{}, LocatorPathElt::Witness(witness));
8590+
SynthesizedConformances.insert({witnessLoc, synthesized});
8591+
return recordConformance(synthesized);
8592+
};
8593+
85868594
if (witness->isDistributedActorSystemRemoteCall(/*isVoidReturn=*/false)) {
85878595
if (GP->isEqual(cast<FuncDecl>(witness)->getResultInterfaceType()))
8588-
return recordConformance(ProtocolConformanceRef(protocol));
8596+
return synthesizeConformance();
85898597
}
85908598
}
85918599
}

lib/Sema/CSSolver.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ Solution ConstraintSystem::finalize() {
254254
for (const auto &packEltGenericEnv : PackElementGenericEnvironments)
255255
solution.PackElementGenericEnvironments.push_back(packEltGenericEnv);
256256

257+
for (const auto &synthesized : SynthesizedConformances) {
258+
solution.SynthesizedConformances.insert(synthesized);
259+
}
260+
257261
return solution;
258262
}
259263

@@ -406,6 +410,10 @@ void ConstraintSystem::applySolution(const Solution &solution) {
406410
ImplicitCallAsFunctionRoots.insert(implicitRoot);
407411
}
408412

413+
for (auto &synthesized : solution.SynthesizedConformances) {
414+
SynthesizedConformances.insert(synthesized);
415+
}
416+
409417
// Register any fixes produced along this path.
410418
Fixes.insert(solution.Fixes.begin(), solution.Fixes.end());
411419
}
@@ -686,6 +694,7 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
686694
numImplicitValueConversions = cs.ImplicitValueConversions.size();
687695
numArgumentLists = cs.ArgumentLists.size();
688696
numImplicitCallAsFunctionRoots = cs.ImplicitCallAsFunctionRoots.size();
697+
numSynthesizedConformances = cs.SynthesizedConformances.size();
689698

690699
PreviousScore = cs.CurrentScore;
691700

@@ -832,6 +841,9 @@ ConstraintSystem::SolverScope::~SolverScope() {
832841
// which are no longer in scope.
833842
truncate(cs.ImplicitCallAsFunctionRoots, numImplicitCallAsFunctionRoots);
834843

844+
// Remove any implicitly synthesized conformances.
845+
truncate(cs.SynthesizedConformances, numSynthesizedConformances);
846+
835847
// Reset the previous score.
836848
cs.CurrentScore = PreviousScore;
837849

lib/Sema/ConstraintSystem.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4431,7 +4431,8 @@ size_t Solution::getTotalMemory() const {
44314431
size_in_bytes(resultBuilderTransformed) +
44324432
size_in_bytes(appliedPropertyWrappers) +
44334433
size_in_bytes(argumentLists) +
4434-
ImplicitCallAsFunctionRoots.getMemorySize();
4434+
ImplicitCallAsFunctionRoots.getMemorySize() +
4435+
size_in_bytes(SynthesizedConformances);
44354436
}
44364437

44374438
DeclContext *Solution::getDC() const { return constraintSystem->DC; }

0 commit comments

Comments
 (0)