Skip to content

Commit 29258b4

Browse files
committed
Split function type matching into a separate function.
1 parent cad0722 commit 29258b4

File tree

1 file changed

+45
-33
lines changed

1 file changed

+45
-33
lines changed

lib/AST/Type.cpp

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,6 +2178,49 @@ namespace {
21782178
};
21792179
} // end anonymous namespace
21802180

2181+
static bool matches(CanType t1, CanType t2, TypeMatchOptions matchMode,
2182+
ParameterPosition paramPosition,
2183+
OptionalUnwrapping insideOptional);
2184+
2185+
static bool matchFunctionTypes(CanAnyFunctionType fn1, CanAnyFunctionType fn2,
2186+
TypeMatchOptions matchMode,
2187+
ParameterPosition paramPosition,
2188+
OptionalUnwrapping insideOptional) {
2189+
// FIXME: Handle generic functions in non-ABI matches.
2190+
if (!matchMode.contains(TypeMatchFlags::AllowABICompatible)) {
2191+
if (!isa<FunctionType>(fn1) || !isa<FunctionType>(fn2))
2192+
return false;
2193+
}
2194+
2195+
// When checking overrides, allow the base type to be throwing even if the
2196+
// overriding type isn't.
2197+
auto ext1 = fn1->getExtInfo();
2198+
auto ext2 = fn2->getExtInfo();
2199+
if (matchMode.contains(TypeMatchFlags::AllowOverride)) {
2200+
if (ext2.throws()) {
2201+
ext1 = ext1.withThrows(true);
2202+
}
2203+
}
2204+
// If specified, allow an escaping function parameter to override a
2205+
// non-escaping function parameter when the parameter is optional.
2206+
// Note that this is checking 'ext2' rather than 'ext1' because parameters
2207+
// must be contravariant for the containing function to be covariant.
2208+
if (matchMode.contains(
2209+
TypeMatchFlags::IgnoreNonEscapingForOptionalFunctionParam) &&
2210+
insideOptional == OptionalUnwrapping::OptionalToOptional) {
2211+
if (!ext2.isNoEscape())
2212+
ext1 = ext1.withNoEscape(false);
2213+
}
2214+
if (ext1 != ext2)
2215+
return false;
2216+
2217+
// Inputs are contravariant, results are covariant.
2218+
return (matches(fn2.getInput(), fn1.getInput(), matchMode,
2219+
ParameterPosition::Parameter, OptionalUnwrapping::None) &&
2220+
matches(fn1.getResult(), fn2.getResult(), matchMode,
2221+
ParameterPosition::NotParameter, OptionalUnwrapping::None));
2222+
}
2223+
21812224
static bool matches(CanType t1, CanType t2, TypeMatchOptions matchMode,
21822225
ParameterPosition paramPosition,
21832226
OptionalUnwrapping insideOptional) {
@@ -2255,39 +2298,8 @@ static bool matches(CanType t1, CanType t2, TypeMatchOptions matchMode,
22552298
if (!fn1)
22562299
return false;
22572300

2258-
// FIXME: Handle generic functions in non-ABI matches.
2259-
if (!matchMode.contains(TypeMatchFlags::AllowABICompatible)) {
2260-
if (!isa<FunctionType>(t1) || !isa<FunctionType>(t2))
2261-
return false;
2262-
}
2263-
2264-
// When checking overrides, allow the base type to be throwing even if the
2265-
// overriding type isn't.
2266-
auto ext1 = fn1->getExtInfo();
2267-
auto ext2 = fn2->getExtInfo();
2268-
if (matchMode.contains(TypeMatchFlags::AllowOverride)) {
2269-
if (ext2.throws()) {
2270-
ext1 = ext1.withThrows(true);
2271-
}
2272-
}
2273-
// If specified, allow an escaping function parameter to override a
2274-
// non-escaping function parameter when the parameter is optional.
2275-
// Note that this is checking 'ext2' rather than 'ext1' because parameters
2276-
// must be contravariant for the containing function to be covariant.
2277-
if (matchMode.contains(
2278-
TypeMatchFlags::IgnoreNonEscapingForOptionalFunctionParam) &&
2279-
insideOptional == OptionalUnwrapping::OptionalToOptional) {
2280-
if (!ext2.isNoEscape())
2281-
ext1 = ext1.withNoEscape(false);
2282-
}
2283-
if (ext1 != ext2)
2284-
return false;
2285-
2286-
// Inputs are contravariant, results are covariant.
2287-
return (matches(fn2.getInput(), fn1.getInput(), matchMode,
2288-
ParameterPosition::Parameter, OptionalUnwrapping::None) &&
2289-
matches(fn1.getResult(), fn2.getResult(), matchMode,
2290-
ParameterPosition::NotParameter, OptionalUnwrapping::None));
2301+
return matchFunctionTypes(fn1, fn2, matchMode, paramPosition,
2302+
insideOptional);
22912303
}
22922304

22932305
if (matchMode.contains(TypeMatchFlags::AllowNonOptionalForIUOParam) &&

0 commit comments

Comments
 (0)