Skip to content

Commit 11d2837

Browse files
authored
Merge pull request swiftlang#39864 from slavapestov/rqm-refactor-protocol-linear-order
RequirementMachine: Protocol reduction order refactoring
2 parents 8979602 + e0a3344 commit 11d2837

21 files changed

+302
-567
lines changed

lib/AST/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ add_swift_host_library(swiftAST STATIC
7979
RequirementMachine/GenericSignatureQueries.cpp
8080
RequirementMachine/PropertyMap.cpp
8181
RequirementMachine/PropertyUnification.cpp
82-
RequirementMachine/ProtocolGraph.cpp
8382
RequirementMachine/RequirementMachine.cpp
8483
RequirementMachine/RequirementMachineRequests.cpp
8584
RequirementMachine/RewriteContext.cpp

lib/AST/RequirementMachine/GenericSignatureQueries.cpp

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,20 @@ RequirementMachine::getLocalRequirements(
3737
System.simplify(term);
3838
verify(term);
3939

40-
auto &protos = System.getProtocols();
41-
4240
GenericSignature::LocalRequirements result;
43-
result.anchor = Context.getTypeForTerm(term, genericParams, protos);
41+
result.anchor = Context.getTypeForTerm(term, genericParams);
4442

4543
auto *props = Map.lookUpProperties(term);
4644
if (!props)
4745
return result;
4846

4947
if (props->isConcreteType()) {
50-
result.concreteType = props->getConcreteType({}, term, protos, Context);
48+
result.concreteType = props->getConcreteType({}, term, Context);
5149
return result;
5250
}
5351

5452
if (props->hasSuperclassBound()) {
55-
result.superclass = props->getSuperclassBound({}, term, protos, Context);
53+
result.superclass = props->getSuperclassBound({}, term, Context);
5654
}
5755

5856
for (const auto *proto : props->getConformsToExcludingSuperclassConformances())
@@ -154,8 +152,7 @@ getSuperclassBound(Type depType,
154152
if (!props->hasSuperclassBound())
155153
return Type();
156154

157-
auto &protos = System.getProtocols();
158-
return props->getSuperclassBound(genericParams, term, protos, Context);
155+
return props->getSuperclassBound(genericParams, term, Context);
159156
}
160157

161158
bool RequirementMachine::isConcreteType(Type depType) const {
@@ -186,8 +183,7 @@ getConcreteType(Type depType,
186183
if (!props->isConcreteType())
187184
return Type();
188185

189-
auto &protos = System.getProtocols();
190-
return props->getConcreteType(genericParams, term, protos, Context);
186+
return props->getConcreteType(genericParams, term, Context);
191187
}
192188

193189
bool RequirementMachine::areSameTypeParameterInContext(Type depType1,
@@ -217,9 +213,6 @@ RequirementMachine::getLongestValidPrefix(const MutableTerm &term) const {
217213
case Symbol::Kind::Protocol:
218214
assert(prefix.empty() &&
219215
"Protocol symbol can only appear at the start of a type term");
220-
if (!System.getProtocols().isKnownProtocol(symbol.getProtocol()))
221-
return prefix;
222-
223216
break;
224217

225218
case Symbol::Kind::GenericParam:
@@ -235,9 +228,6 @@ RequirementMachine::getLongestValidPrefix(const MutableTerm &term) const {
235228
auto conformsTo = props->getConformsTo();
236229

237230
for (const auto *proto : symbol.getProtocols()) {
238-
if (!System.getProtocols().isKnownProtocol(proto))
239-
return prefix;
240-
241231
// T.[P:A] is valid iff T conforms to P.
242232
if (std::find(conformsTo.begin(), conformsTo.end(), proto)
243233
== conformsTo.end())
@@ -268,8 +258,6 @@ RequirementMachine::getLongestValidPrefix(const MutableTerm &term) const {
268258
/// not considered canonical, since they can be replaced with their
269259
/// concrete type).
270260
bool RequirementMachine::isCanonicalTypeInContext(Type type) const {
271-
auto &protos = System.getProtocols();
272-
273261
// Look for non-canonical type parameters.
274262
return !type.findIf([&](Type component) -> bool {
275263
if (!component->isTypeParameter())
@@ -288,7 +276,7 @@ bool RequirementMachine::isCanonicalTypeInContext(Type type) const {
288276
if (props->isConcreteType())
289277
return true;
290278

291-
auto anchor = Context.getTypeForTerm(term, {}, protos);
279+
auto anchor = Context.getTypeForTerm(term, {});
292280
return CanType(anchor) != CanType(component);
293281
});
294282
}
@@ -304,7 +292,6 @@ bool RequirementMachine::isCanonicalTypeInContext(Type type) const {
304292
Type RequirementMachine::getCanonicalTypeInContext(
305293
Type type,
306294
TypeArrayView<GenericTypeParamType> genericParams) const {
307-
const auto &protos = System.getProtocols();
308295

309296
return type.transformRec([&](Type t) -> Optional<Type> {
310297
if (!t->isTypeParameter())
@@ -353,8 +340,7 @@ Type RequirementMachine::getCanonicalTypeInContext(
353340
if (props) {
354341
if (props->isConcreteType()) {
355342
auto concreteType = props->getConcreteType(genericParams,
356-
prefix, protos,
357-
Context);
343+
prefix, Context);
358344
if (!concreteType->hasTypeParameter())
359345
return concreteType;
360346

@@ -369,8 +355,7 @@ Type RequirementMachine::getCanonicalTypeInContext(
369355
if (props->hasSuperclassBound() &&
370356
prefix.size() != term.size()) {
371357
auto superclass = props->getSuperclassBound(genericParams,
372-
prefix, protos,
373-
Context);
358+
prefix, Context);
374359
if (!superclass->hasTypeParameter())
375360
return superclass;
376361

@@ -379,7 +364,7 @@ Type RequirementMachine::getCanonicalTypeInContext(
379364
}
380365
}
381366

382-
return Context.getTypeForTerm(prefix, genericParams, protos);
367+
return Context.getTypeForTerm(prefix, genericParams);
383368
}();
384369

385370
// If T is already valid, the longest valid prefix U of T is T itself, and
@@ -404,8 +389,7 @@ Type RequirementMachine::getCanonicalTypeInContext(
404389

405390
// Compute the type of the unresolved suffix term V, rooted in the
406391
// generic parameter τ_0_0.
407-
auto origType = Context.getRelativeTypeForTerm(
408-
term, prefix, System.getProtocols());
392+
auto origType = Context.getRelativeTypeForTerm(term, prefix);
409393

410394
// Substitute τ_0_0 in the above relative type with the concrete type
411395
// for U.

lib/AST/RequirementMachine/HomotopyReduction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ findRuleToDelete(bool firstPass,
676676
const auto &otherRule = getRule(*found);
677677

678678
// Prefer to delete "less canonical" rules.
679-
if (rule.compare(otherRule, Protos) > 0)
679+
if (rule.compare(otherRule, Context) > 0)
680680
found = ruleID;
681681
}
682682

lib/AST/RequirementMachine/PropertyMap.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,11 @@ PropertyBag::getPrefixAfterStrippingKey(const MutableTerm &lookupTerm) const {
152152
Type PropertyBag::getSuperclassBound(
153153
TypeArrayView<GenericTypeParamType> genericParams,
154154
const MutableTerm &lookupTerm,
155-
const ProtocolGraph &protos,
156155
RewriteContext &ctx) const {
157156
MutableTerm prefix = getPrefixAfterStrippingKey(lookupTerm);
158157
return ctx.getTypeFromSubstitutionSchema(Superclass->getSuperclass(),
159158
Superclass->getSubstitutions(),
160-
genericParams, prefix,
161-
protos);
159+
genericParams, prefix);
162160
}
163161

164162
/// Get the concrete type of the term represented by this property bag.
@@ -173,13 +171,11 @@ Type PropertyBag::getSuperclassBound(
173171
Type PropertyBag::getConcreteType(
174172
TypeArrayView<GenericTypeParamType> genericParams,
175173
const MutableTerm &lookupTerm,
176-
const ProtocolGraph &protos,
177174
RewriteContext &ctx) const {
178175
MutableTerm prefix = getPrefixAfterStrippingKey(lookupTerm);
179176
return ctx.getTypeFromSubstitutionSchema(ConcreteType->getConcreteType(),
180177
ConcreteType->getSubstitutions(),
181-
genericParams, prefix,
182-
protos);
178+
genericParams, prefix);
183179
}
184180

185181
void PropertyBag::copyPropertiesFrom(const PropertyBag *next,

lib/AST/RequirementMachine/PropertyMap.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include <vector>
2727

2828
#include "Debug.h"
29-
#include "ProtocolGraph.h"
3029
#include "RewriteContext.h"
3130
#include "RewriteSystem.h"
3231

@@ -102,7 +101,6 @@ class PropertyBag {
102101
Type getSuperclassBound(
103102
TypeArrayView<GenericTypeParamType> genericParams,
104103
const MutableTerm &lookupTerm,
105-
const ProtocolGraph &protos,
106104
RewriteContext &ctx) const;
107105

108106
bool isConcreteType() const {
@@ -116,7 +114,6 @@ class PropertyBag {
116114
Type getConcreteType(
117115
TypeArrayView<GenericTypeParamType> genericParams,
118116
const MutableTerm &lookupTerm,
119-
const ProtocolGraph &protos,
120117
RewriteContext &ctx) const;
121118

122119
LayoutConstraint getLayoutConstraint() const {
@@ -146,7 +143,6 @@ class PropertyMap {
146143
using ConcreteTypeInDomain = std::pair<CanType, ArrayRef<const ProtocolDecl *>>;
147144
llvm::DenseMap<ConcreteTypeInDomain, Term> ConcreteTypeInDomainMap;
148145

149-
const ProtocolGraph &Protos;
150146
DebugOptions Debug;
151147

152148
PropertyBag *getOrCreateProperties(Term key);
@@ -159,8 +155,7 @@ class PropertyMap {
159155
public:
160156
explicit PropertyMap(RewriteSystem &system)
161157
: Context(system.getRewriteContext()),
162-
System(system),
163-
Protos(system.getProtocols()) {
158+
System(system) {
164159
Debug = Context.getDebugOptions();
165160
}
166161

lib/AST/RequirementMachine/PropertyUnification.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ void PropertyMap::concretizeNestedTypesFromConcreteParent(
501501
// PropertyBag::getConformsToExcludingSuperclassConformances().
502502
conformances.push_back(concrete);
503503

504-
auto assocTypes = Protos.getProtocolInfo(proto).AssociatedTypes;
504+
auto assocTypes = proto->getAssociatedTypeMembers();
505505
if (assocTypes.empty())
506506
continue;
507507

@@ -543,7 +543,7 @@ void PropertyMap::concretizeNestedTypesFromConcreteParent(
543543
auto term = Context.getRelativeTermForType(t->getCanonicalType(),
544544
substitutions);
545545
System.simplify(term);
546-
return Context.getTypeForTerm(term, { }, Protos);
546+
return Context.getTypeForTerm(term, { });
547547
}));
548548
};
549549

0 commit comments

Comments
 (0)