Skip to content

Commit adba283

Browse files
[CSSimplify] Abstract function conversion validation into static method
1 parent d6bf34e commit adba283

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,29 @@ ConstraintSystem::matchTupleTypes(TupleType *tuple1, TupleType *tuple2,
13491349
return getTypeMatchSuccess();
13501350
}
13511351

1352+
// Determine if a function representation conversion is allowed returning
1353+
// 'false' (i.e. no error) if the conversion is valid.
1354+
static bool
1355+
matchFunctionConversionRepresentations(FunctionTypeRepresentation rep1,
1356+
FunctionTypeRepresentation rep2) {
1357+
auto isThin = [](FunctionTypeRepresentation rep) {
1358+
return rep == FunctionTypeRepresentation::CFunctionPointer ||
1359+
rep == FunctionTypeRepresentation::Thin;
1360+
};
1361+
1362+
// Allowing "thin" (c, thin) to "thin" conventions
1363+
if (isThin(rep1) && isThin(rep2))
1364+
return false;
1365+
1366+
// Allowing all to "thick" (swift, block) conventions
1367+
// "thin" (c, thin) to "thick" or "thick" to "thick"
1368+
if (rep2 == FunctionTypeRepresentation::Swift ||
1369+
rep2 == FunctionTypeRepresentation::Block)
1370+
return false;
1371+
1372+
return rep1 != rep2;
1373+
}
1374+
13521375
// Returns 'false' (i.e. no error) if it is legal to match functions with the
13531376
// corresponding function type representations and the given match kind.
13541377
static bool matchFunctionRepresentations(FunctionTypeRepresentation rep1,
@@ -1367,22 +1390,7 @@ static bool matchFunctionRepresentations(FunctionTypeRepresentation rep1,
13671390
if (!(last && last->is<LocatorPathElt::FunctionArgument>()))
13681391
return false;
13691392

1370-
auto isThin = [](FunctionTypeRepresentation rep) {
1371-
return rep == FunctionTypeRepresentation::CFunctionPointer ||
1372-
rep == FunctionTypeRepresentation::Thin;
1373-
};
1374-
1375-
// Allowing "thin" (c, thin) to "thin" conventions
1376-
if (isThin(rep1) && isThin(rep2))
1377-
return false;
1378-
1379-
// Allowing all to "thick" (swift, block) conventions
1380-
// "thin" (c, thin) to "thick" or "thick" to "thick"
1381-
if (rep2 == FunctionTypeRepresentation::Swift ||
1382-
rep2 == FunctionTypeRepresentation::Block)
1383-
return false;
1384-
1385-
return rep1 != rep2;
1393+
return matchFunctionConversionRepresentations(rep1, rep2);
13861394
}
13871395

13881396
case ConstraintKind::OpaqueUnderlyingType:

0 commit comments

Comments
 (0)