@@ -55,13 +55,16 @@ using namespace rewriting;
55
55
// / side into a series of same-type and concrete-type requirements where the
56
56
// / left hand side is always a type parameter.
57
57
static void desugarSameTypeRequirement (Type lhs, Type rhs,
58
- SmallVectorImpl<Requirement> &result) {
58
+ SmallVectorImpl<Requirement> &result,
59
+ SmallVectorImpl<RequirementError> &errors) {
59
60
class Matcher : public TypeMatcher <Matcher> {
60
61
SmallVectorImpl<Requirement> &result;
62
+ SmallVectorImpl<RequirementError> &errors;
61
63
62
64
public:
63
- explicit Matcher (SmallVectorImpl<Requirement> &result)
64
- : result(result) {}
65
+ explicit Matcher (SmallVectorImpl<Requirement> &result,
66
+ SmallVectorImpl<RequirementError> &errors)
67
+ : result(result), errors(errors) {}
65
68
66
69
bool mismatch (TypeBase *firstType, TypeBase *secondType,
67
70
Type sugaredFirstType) {
@@ -86,7 +89,7 @@ static void desugarSameTypeRequirement(Type lhs, Type rhs,
86
89
// FIXME: Record concrete type conflict, diagnose upstream
87
90
return true ;
88
91
}
89
- } matcher (result);
92
+ } matcher (result, errors );
90
93
91
94
if (lhs->hasError () || rhs->hasError ())
92
95
return ;
@@ -97,7 +100,8 @@ static void desugarSameTypeRequirement(Type lhs, Type rhs,
97
100
98
101
static void desugarSuperclassRequirement (Type subjectType,
99
102
Type constraintType,
100
- SmallVectorImpl<Requirement> &result) {
103
+ SmallVectorImpl<Requirement> &result,
104
+ SmallVectorImpl<RequirementError> &errors) {
101
105
if (!subjectType->isTypeParameter ()) {
102
106
// FIXME: Perform unification, diagnose redundancy or conflict upstream
103
107
return ;
@@ -108,7 +112,8 @@ static void desugarSuperclassRequirement(Type subjectType,
108
112
109
113
static void desugarLayoutRequirement (Type subjectType,
110
114
LayoutConstraint layout,
111
- SmallVectorImpl<Requirement> &result) {
115
+ SmallVectorImpl<Requirement> &result,
116
+ SmallVectorImpl<RequirementError> &errors) {
112
117
if (!subjectType->isTypeParameter ()) {
113
118
// FIXME: Diagnose redundancy or conflict upstream
114
119
return ;
@@ -133,7 +138,8 @@ static Type lookupMemberType(Type subjectType, ProtocolDecl *protoDecl,
133
138
// / compositions on the right hand side into conformance and superclass
134
139
// / requirements.
135
140
static void desugarConformanceRequirement (Type subjectType, Type constraintType,
136
- SmallVectorImpl<Requirement> &result) {
141
+ SmallVectorImpl<Requirement> &result,
142
+ SmallVectorImpl<RequirementError> &errors) {
137
143
// Fast path.
138
144
if (constraintType->is <ProtocolType>()) {
139
145
if (!subjectType->isTypeParameter ()) {
@@ -152,7 +158,7 @@ static void desugarConformanceRequirement(Type subjectType, Type constraintType,
152
158
153
159
// Introduce conditional requirements if the subject type is concrete.
154
160
for (auto req : concrete->getConditionalRequirements ()) {
155
- desugarRequirement (req, result);
161
+ desugarRequirement (req, result, errors );
156
162
}
157
163
return ;
158
164
}
@@ -166,28 +172,29 @@ static void desugarConformanceRequirement(Type subjectType, Type constraintType,
166
172
auto *protoDecl = paramType->getBaseType ()->getDecl ();
167
173
168
174
desugarConformanceRequirement (subjectType, paramType->getBaseType (),
169
- result);
175
+ result, errors );
170
176
171
177
auto *assocType = protoDecl->getPrimaryAssociatedType ();
172
178
173
179
auto memberType = lookupMemberType (subjectType, protoDecl, assocType);
174
180
desugarSameTypeRequirement (memberType, paramType->getArgumentType (),
175
- result);
181
+ result, errors );
176
182
return ;
177
183
}
178
184
179
185
auto *compositionType = constraintType->castTo <ProtocolCompositionType>();
180
186
if (compositionType->hasExplicitAnyObject ()) {
181
187
desugarLayoutRequirement (subjectType,
182
188
LayoutConstraint::getLayoutConstraint (
183
- LayoutConstraintKind::Class), result);
189
+ LayoutConstraintKind::Class),
190
+ result, errors);
184
191
}
185
192
186
193
for (auto memberType : compositionType->getMembers ()) {
187
194
if (memberType->isExistentialType ())
188
- desugarConformanceRequirement (subjectType, memberType, result);
195
+ desugarConformanceRequirement (subjectType, memberType, result, errors );
189
196
else
190
- desugarSuperclassRequirement (subjectType, memberType, result);
197
+ desugarSuperclassRequirement (subjectType, memberType, result, errors );
191
198
}
192
199
}
193
200
@@ -197,24 +204,29 @@ static void desugarConformanceRequirement(Type subjectType, Type constraintType,
197
204
// / converted into rewrite rules by the RuleBuilder.
198
205
void
199
206
swift::rewriting::desugarRequirement (Requirement req,
200
- SmallVectorImpl<Requirement> &result) {
207
+ SmallVectorImpl<Requirement> &result,
208
+ SmallVectorImpl<RequirementError> &errors) {
201
209
auto firstType = req.getFirstType ();
202
210
203
211
switch (req.getKind ()) {
204
212
case RequirementKind::Conformance:
205
- desugarConformanceRequirement (firstType, req.getSecondType (), result);
213
+ desugarConformanceRequirement (firstType, req.getSecondType (),
214
+ result, errors);
206
215
break ;
207
216
208
217
case RequirementKind::Superclass:
209
- desugarSuperclassRequirement (firstType, req.getSecondType (), result);
218
+ desugarSuperclassRequirement (firstType, req.getSecondType (),
219
+ result, errors);
210
220
break ;
211
221
212
222
case RequirementKind::Layout:
213
- desugarLayoutRequirement (firstType, req.getLayoutConstraint (), result);
223
+ desugarLayoutRequirement (firstType, req.getLayoutConstraint (),
224
+ result, errors);
214
225
break ;
215
226
216
227
case RequirementKind::SameType:
217
- desugarSameTypeRequirement (firstType, req.getSecondType (), result);
228
+ desugarSameTypeRequirement (firstType, req.getSecondType (),
229
+ result, errors);
218
230
break ;
219
231
}
220
232
}
@@ -235,10 +247,10 @@ static void realizeTypeRequirement(Type subjectType, Type constraintType,
235
247
236
248
if (constraintType->isConstraintType ()) {
237
249
// Handle conformance requirements.
238
- desugarConformanceRequirement (subjectType, constraintType, reqs);
250
+ desugarConformanceRequirement (subjectType, constraintType, reqs, errors );
239
251
} else if (constraintType->getClassOrBoundGenericClass ()) {
240
252
// Handle superclass requirements.
241
- desugarSuperclassRequirement (subjectType, constraintType, reqs);
253
+ desugarSuperclassRequirement (subjectType, constraintType, reqs, errors );
242
254
} else {
243
255
errors.push_back (
244
256
RequirementError::forInvalidConformance (subjectType,
@@ -258,6 +270,7 @@ namespace {
258
270
struct InferRequirementsWalker : public TypeWalker {
259
271
ModuleDecl *module ;
260
272
SmallVector<Requirement, 2 > reqs;
273
+ SmallVector<RequirementError, 2 > errors;
261
274
262
275
explicit InferRequirementsWalker (ModuleDecl *module ) : module(module ) {}
263
276
@@ -277,7 +290,7 @@ struct InferRequirementsWalker : public TypeWalker {
277
290
auto subMap = typeAlias->getSubstitutionMap ();
278
291
for (const auto &rawReq : decl->getGenericSignature ().getRequirements ()) {
279
292
if (auto req = rawReq.subst (subMap))
280
- desugarRequirement (*req, reqs);
293
+ desugarRequirement (*req, reqs, errors );
281
294
}
282
295
283
296
return Action::Continue;
@@ -297,14 +310,14 @@ struct InferRequirementsWalker : public TypeWalker {
297
310
auto addConformanceConstraint = [&](Type type, ProtocolDecl *protocol) {
298
311
Requirement req (RequirementKind::Conformance, type,
299
312
protocol->getDeclaredInterfaceType ());
300
- desugarRequirement (req, reqs);
313
+ desugarRequirement (req, reqs, errors );
301
314
};
302
315
auto addSameTypeConstraint = [&](Type firstType,
303
316
AssociatedTypeDecl *assocType) {
304
317
auto *protocol = assocType->getProtocol ();
305
318
auto secondType = lookupMemberType (firstType, protocol, assocType);
306
319
Requirement req (RequirementKind::SameType, firstType, secondType);
307
- desugarRequirement (req, reqs);
320
+ desugarRequirement (req, reqs, errors );
308
321
};
309
322
auto *tangentVectorAssocType =
310
323
differentiableProtocol->getAssociatedType (ctx.Id_TangentVector );
@@ -343,7 +356,7 @@ struct InferRequirementsWalker : public TypeWalker {
343
356
// FIXME: Inaccurate TypeReprs.
344
357
for (const auto &rawReq : genericSig.getRequirements ()) {
345
358
if (auto req = rawReq.subst (subMap))
346
- desugarRequirement (*req, reqs);
359
+ desugarRequirement (*req, reqs, errors );
347
360
}
348
361
349
362
return Action::Continue;
@@ -408,7 +421,8 @@ void swift::rewriting::realizeRequirement(
408
421
}
409
422
410
423
SmallVector<Requirement, 2 > reqs;
411
- desugarLayoutRequirement (firstType, req.getLayoutConstraint (), reqs);
424
+ desugarLayoutRequirement (firstType, req.getLayoutConstraint (),
425
+ reqs, errors);
412
426
413
427
for (auto req : reqs)
414
428
result.push_back ({req, loc, /* wasInferred=*/ false });
@@ -429,7 +443,7 @@ void swift::rewriting::realizeRequirement(
429
443
}
430
444
431
445
SmallVector<Requirement, 2 > reqs;
432
- desugarSameTypeRequirement (req.getFirstType (), secondType, reqs);
446
+ desugarSameTypeRequirement (req.getFirstType (), secondType, reqs, errors );
433
447
434
448
for (auto req : reqs)
435
449
result.push_back ({req, loc, /* wasInferred=*/ false });
@@ -615,6 +629,7 @@ TypeAliasRequirementsRequest::evaluate(Evaluator &evaluator,
615
629
assert (!proto->hasLazyRequirementSignature ());
616
630
617
631
SmallVector<Requirement, 2 > result;
632
+ SmallVector<RequirementError, 2 > errors;
618
633
619
634
auto &ctx = proto->getASTContext ();
620
635
@@ -656,7 +671,8 @@ TypeAliasRequirementsRequest::evaluate(Evaluator &evaluator,
656
671
// within this protocol or a protocol it inherits.
657
672
auto recordInheritedTypeRequirement = [&](TypeDecl *first, TypeDecl *second) {
658
673
desugarSameTypeRequirement (getStructuralType (first),
659
- getStructuralType (second), result);
674
+ getStructuralType (second),
675
+ result, errors);
660
676
};
661
677
662
678
// Local function to find the insertion point for the protocol's "where"
0 commit comments