@@ -1350,20 +1350,53 @@ ConstraintSystem::matchTupleTypes(TupleType *tuple1, TupleType *tuple2,
13501350 return getTypeMatchSuccess ();
13511351}
13521352
1353+ // Determine whether conversion is allowed between two function types
1354+ // based on their representations.
1355+ static bool
1356+ isConversionAllowedBetween (FunctionTypeRepresentation rep1,
1357+ FunctionTypeRepresentation rep2) {
1358+ auto isThin = [](FunctionTypeRepresentation rep) {
1359+ return rep == FunctionTypeRepresentation::CFunctionPointer ||
1360+ rep == FunctionTypeRepresentation::Thin;
1361+ };
1362+
1363+ // Allowing "thin" (c, thin) to "thin" conventions
1364+ if (isThin (rep1) && isThin (rep2))
1365+ return true ;
1366+
1367+ // Allowing all to "thick" (swift, block) conventions
1368+ // "thin" (c, thin) to "thick" or "thick" to "thick"
1369+ if (rep2 == FunctionTypeRepresentation::Swift ||
1370+ rep2 == FunctionTypeRepresentation::Block)
1371+ return true ;
1372+
1373+ return rep1 == rep2;
1374+ }
1375+
13531376// Returns 'false' (i.e. no error) if it is legal to match functions with the
13541377// corresponding function type representations and the given match kind.
13551378static bool matchFunctionRepresentations (FunctionTypeRepresentation rep1,
13561379 FunctionTypeRepresentation rep2,
1357- ConstraintKind kind) {
1380+ ConstraintKind kind,
1381+ ConstraintLocatorBuilder locator) {
13581382 switch (kind) {
13591383 case ConstraintKind::Bind:
13601384 case ConstraintKind::BindParam:
13611385 case ConstraintKind::BindToPointerType:
13621386 case ConstraintKind::Equal:
13631387 return rep1 != rep2;
1388+
1389+ case ConstraintKind::Subtype: {
1390+ auto last = locator.last ();
1391+ if (!(last && last->is <LocatorPathElt::FunctionArgument>()))
1392+ return false ;
1393+
1394+ // Inverting the result because matchFunctionRepresentations
1395+ // returns false in conversions are allowed.
1396+ return !isConversionAllowedBetween (rep1, rep2);
1397+ }
13641398
13651399 case ConstraintKind::OpaqueUnderlyingType:
1366- case ConstraintKind::Subtype:
13671400 case ConstraintKind::Conversion:
13681401 case ConstraintKind::BridgingConversion:
13691402 case ConstraintKind::ArgumentConversion:
@@ -1658,7 +1691,7 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
16581691
16591692 if (matchFunctionRepresentations (func1->getExtInfo ().getRepresentation (),
16601693 func2->getExtInfo ().getRepresentation (),
1661- kind)) {
1694+ kind, locator )) {
16621695 return getTypeMatchFailure (locator);
16631696 }
16641697
0 commit comments