@@ -1186,6 +1186,31 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
1186
1186
if (kind != TypeMatchKind::ConformsTo && desugar1->isEqual (desugar2))
1187
1187
return SolutionKind::Solved;
1188
1188
1189
+ // Local function that should be used to produce the return value whenever
1190
+ // this function was unable to resolve the constraint. It should be used
1191
+ // within \c matchTypes() as
1192
+ //
1193
+ // return formUnsolvedResult();
1194
+ //
1195
+ // along any unsolved path. No other returns should produce
1196
+ // SolutionKind::Unsolved or inspect TMF_GenerateConstraints.
1197
+ auto formUnsolvedResult = [&] {
1198
+ // If we're supposed to generate constraints (i.e., this is a
1199
+ // newly-generated constraint), do so now.
1200
+ if (flags & TMF_GenerateConstraints) {
1201
+ // Add a new constraint between these types. We consider the current
1202
+ // type-matching problem to the "solved" by this addition, because
1203
+ // this new constraint will be solved at a later point.
1204
+ // Obviously, this must not happen at the top level, or the
1205
+ // algorithm would not terminate.
1206
+ addConstraint (getConstraintKind (kind), type1, type2,
1207
+ getConstraintLocator (locator));
1208
+ return SolutionKind::Solved;
1209
+ }
1210
+
1211
+ return SolutionKind::Unsolved;
1212
+ };
1213
+
1189
1214
// If either (or both) types are type variables, unify the type variables.
1190
1215
if (typeVar1 || typeVar2) {
1191
1216
switch (kind) {
@@ -1204,24 +1229,8 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
1204
1229
// If exactly one of the type variables can bind to an lvalue, we
1205
1230
// can't merge these two type variables.
1206
1231
if (rep1->getImpl ().canBindToLValue ()
1207
- != rep2->getImpl ().canBindToLValue ()) {
1208
- if (flags & TMF_GenerateConstraints) {
1209
- if (kind == TypeMatchKind::BindToPointerType) {
1210
- increaseScore (ScoreKind::SK_ScalarPointerConversion);
1211
- }
1212
-
1213
- // Add a new constraint between these types. We consider the current
1214
- // type-matching problem to the "solved" by this addition, because
1215
- // this new constraint will be solved at a later point.
1216
- // Obviously, this must not happen at the top level, or the
1217
- // algorithm would not terminate.
1218
- addConstraint (getConstraintKind (kind), rep1, rep2,
1219
- getConstraintLocator (locator));
1220
- return SolutionKind::Solved;
1221
- }
1222
-
1223
- return SolutionKind::Unsolved;
1224
- }
1232
+ != rep2->getImpl ().canBindToLValue ())
1233
+ return formUnsolvedResult ();
1225
1234
1226
1235
// Merge the equivalence classes corresponding to these two variables.
1227
1236
mergeEquivalenceClasses (rep1, rep2);
@@ -1254,8 +1263,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
1254
1263
1255
1264
// A constraint that binds any pointer to a void pointer is
1256
1265
// ineffective, since any pointer can be converted to a void pointer.
1257
- if (kind == TypeMatchKind::BindToPointerType && desugar2->isVoid () &&
1258
- (flags & TMF_GenerateConstraints)) {
1266
+ if (kind == TypeMatchKind::BindToPointerType && desugar2->isVoid ()) {
1259
1267
// Bind type1 to Void only as a last resort.
1260
1268
addConstraint (ConstraintKind::Defaultable, typeVar1, type2,
1261
1269
getConstraintLocator (locator));
@@ -1309,7 +1317,8 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
1309
1317
return SolutionKind::Solved;
1310
1318
}
1311
1319
}
1312
- return SolutionKind::Unsolved;
1320
+
1321
+ return formUnsolvedResult ();
1313
1322
}
1314
1323
1315
1324
case TypeMatchKind::ArgumentTupleConversion:
@@ -1331,22 +1340,13 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
1331
1340
case TypeMatchKind::ArgumentConversion:
1332
1341
case TypeMatchKind::OperatorArgumentTupleConversion:
1333
1342
case TypeMatchKind::OperatorArgumentConversion:
1334
- if (flags & TMF_GenerateConstraints) {
1335
- // Add a new constraint between these types. We consider the current
1336
- // type-matching problem to the "solved" by this addition, because
1337
- // this new constraint will be solved at a later point.
1338
- // Obviously, this must not happen at the top level, or the algorithm
1339
- // would not terminate.
1340
- addConstraint (getConstraintKind (kind), type1, type2,
1341
- getConstraintLocator (locator));
1342
- return SolutionKind::Solved;
1343
- }
1344
-
1345
1343
// We couldn't solve this constraint. If only one of the types is a type
1346
1344
// variable, perhaps we can do something with it below.
1347
- if (typeVar1 && typeVar2)
1348
- return typeVar1 == typeVar2 ? SolutionKind::Solved
1349
- : SolutionKind::Unsolved;
1345
+ if (typeVar1 && typeVar2) {
1346
+ if (typeVar1 == typeVar2) return SolutionKind::Solved;
1347
+
1348
+ return formUnsolvedResult ();
1349
+ }
1350
1350
1351
1351
break ;
1352
1352
}
@@ -1363,18 +1363,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
1363
1363
return ::matchCallArguments (*this , kind, type1, type2, locator);
1364
1364
}
1365
1365
1366
- if (flags & TMF_GenerateConstraints) {
1367
- // Add a new constraint between these types. We consider the current
1368
- // type-matching problem to the "solved" by this addition, because
1369
- // this new constraint will be solved at a later point.
1370
- // Obviously, this must not happen at the top level, or the algorithm
1371
- // would not terminate.
1372
- addConstraint (getConstraintKind (kind), type1, type2,
1373
- getConstraintLocator (locator));
1374
- return SolutionKind::Solved;
1375
- }
1376
-
1377
- return SolutionKind::Unsolved;
1366
+ return formUnsolvedResult ();
1378
1367
}
1379
1368
1380
1369
// Decompose parallel structure.
@@ -2075,8 +2064,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
2075
2064
2076
2065
if (conversionsOrFixes.empty ()) {
2077
2066
// If one of the types is a type variable, we leave this unsolved.
2078
- if (typeVar1 || typeVar2)
2079
- return SolutionKind::Unsolved;
2067
+ if (typeVar1 || typeVar2) return formUnsolvedResult ();
2080
2068
2081
2069
return SolutionKind::Error;
2082
2070
}
0 commit comments