@@ -1178,6 +1178,31 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
1178
1178
if (kind != TypeMatchKind::ConformsTo && desugar1->isEqual (desugar2))
1179
1179
return SolutionKind::Solved;
1180
1180
1181
+ // Local function that should be used to produce the return value whenever
1182
+ // this function was unable to resolve the constraint. It should be used
1183
+ // within \c matchTypes() as
1184
+ //
1185
+ // return formUnsolvedResult();
1186
+ //
1187
+ // along any unsolved path. No other returns should produce
1188
+ // SolutionKind::Unsolved or inspect TMF_GenerateConstraints.
1189
+ auto formUnsolvedResult = [&] {
1190
+ // If we're supposed to generate constraints (i.e., this is a
1191
+ // newly-generated constraint), do so now.
1192
+ if (flags & TMF_GenerateConstraints) {
1193
+ // Add a new constraint between these types. We consider the current
1194
+ // type-matching problem to the "solved" by this addition, because
1195
+ // this new constraint will be solved at a later point.
1196
+ // Obviously, this must not happen at the top level, or the
1197
+ // algorithm would not terminate.
1198
+ addConstraint (getConstraintKind (kind), type1, type2,
1199
+ getConstraintLocator (locator));
1200
+ return SolutionKind::Solved;
1201
+ }
1202
+
1203
+ return SolutionKind::Unsolved;
1204
+ };
1205
+
1181
1206
// If either (or both) types are type variables, unify the type variables.
1182
1207
if (typeVar1 || typeVar2) {
1183
1208
switch (kind) {
@@ -1196,24 +1221,8 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
1196
1221
// If exactly one of the type variables can bind to an lvalue, we
1197
1222
// can't merge these two type variables.
1198
1223
if (rep1->getImpl ().canBindToLValue ()
1199
- != rep2->getImpl ().canBindToLValue ()) {
1200
- if (flags & TMF_GenerateConstraints) {
1201
- if (kind == TypeMatchKind::BindToPointerType) {
1202
- increaseScore (ScoreKind::SK_ScalarPointerConversion);
1203
- }
1204
-
1205
- // Add a new constraint between these types. We consider the current
1206
- // type-matching problem to the "solved" by this addition, because
1207
- // this new constraint will be solved at a later point.
1208
- // Obviously, this must not happen at the top level, or the
1209
- // algorithm would not terminate.
1210
- addConstraint (getConstraintKind (kind), rep1, rep2,
1211
- getConstraintLocator (locator));
1212
- return SolutionKind::Solved;
1213
- }
1214
-
1215
- return SolutionKind::Unsolved;
1216
- }
1224
+ != rep2->getImpl ().canBindToLValue ())
1225
+ return formUnsolvedResult ();
1217
1226
1218
1227
// Merge the equivalence classes corresponding to these two variables.
1219
1228
mergeEquivalenceClasses (rep1, rep2);
@@ -1246,8 +1255,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
1246
1255
1247
1256
// A constraint that binds any pointer to a void pointer is
1248
1257
// ineffective, since any pointer can be converted to a void pointer.
1249
- if (kind == TypeMatchKind::BindToPointerType && desugar2->isVoid () &&
1250
- (flags & TMF_GenerateConstraints)) {
1258
+ if (kind == TypeMatchKind::BindToPointerType && desugar2->isVoid ()) {
1251
1259
// Bind type1 to Void only as a last resort.
1252
1260
addConstraint (ConstraintKind::Defaultable, typeVar1, type2,
1253
1261
getConstraintLocator (locator));
@@ -1301,7 +1309,8 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
1301
1309
return SolutionKind::Solved;
1302
1310
}
1303
1311
}
1304
- return SolutionKind::Unsolved;
1312
+
1313
+ return formUnsolvedResult ();
1305
1314
}
1306
1315
1307
1316
case TypeMatchKind::ArgumentTupleConversion:
@@ -1323,22 +1332,13 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
1323
1332
case TypeMatchKind::ArgumentConversion:
1324
1333
case TypeMatchKind::OperatorArgumentTupleConversion:
1325
1334
case TypeMatchKind::OperatorArgumentConversion:
1326
- if (flags & TMF_GenerateConstraints) {
1327
- // Add a new constraint between these types. We consider the current
1328
- // type-matching problem to the "solved" by this addition, because
1329
- // this new constraint will be solved at a later point.
1330
- // Obviously, this must not happen at the top level, or the algorithm
1331
- // would not terminate.
1332
- addConstraint (getConstraintKind (kind), type1, type2,
1333
- getConstraintLocator (locator));
1334
- return SolutionKind::Solved;
1335
- }
1336
-
1337
1335
// We couldn't solve this constraint. If only one of the types is a type
1338
1336
// variable, perhaps we can do something with it below.
1339
- if (typeVar1 && typeVar2)
1340
- return typeVar1 == typeVar2 ? SolutionKind::Solved
1341
- : SolutionKind::Unsolved;
1337
+ if (typeVar1 && typeVar2) {
1338
+ if (typeVar1 == typeVar2) return SolutionKind::Solved;
1339
+
1340
+ return formUnsolvedResult ();
1341
+ }
1342
1342
1343
1343
break ;
1344
1344
}
@@ -1355,18 +1355,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
1355
1355
return ::matchCallArguments (*this , kind, type1, type2, locator);
1356
1356
}
1357
1357
1358
- if (flags & TMF_GenerateConstraints) {
1359
- // Add a new constraint between these types. We consider the current
1360
- // type-matching problem to the "solved" by this addition, because
1361
- // this new constraint will be solved at a later point.
1362
- // Obviously, this must not happen at the top level, or the algorithm
1363
- // would not terminate.
1364
- addConstraint (getConstraintKind (kind), type1, type2,
1365
- getConstraintLocator (locator));
1366
- return SolutionKind::Solved;
1367
- }
1368
-
1369
- return SolutionKind::Unsolved;
1358
+ return formUnsolvedResult ();
1370
1359
}
1371
1360
1372
1361
// Decompose parallel structure.
@@ -2067,8 +2056,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
2067
2056
2068
2057
if (conversionsOrFixes.empty ()) {
2069
2058
// If one of the types is a type variable, we leave this unsolved.
2070
- if (typeVar1 || typeVar2)
2071
- return SolutionKind::Unsolved;
2059
+ if (typeVar1 || typeVar2) return formUnsolvedResult ();
2072
2060
2073
2061
return SolutionKind::Error;
2074
2062
}
0 commit comments