Skip to content

Commit 7a854ee

Browse files
committed
[CSSimplify] Make solver behavior consistent with TypeCheckPattern for paren patterns
A single paren pattern becomes a labeled tuple pattern e.g. `case .test(let value):` should be able to match `case test(result: Int)`. Note that it also means that: `cast test(result: (String, Int))` would be matched against e.g. `case .test((let x, let y))` but that fails during pattern coercion (behavior consistent with what happens in `TypeCheckPattern`).
1 parent 50e70fb commit 7a854ee

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,9 +2290,29 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
22902290
increaseScore(SK_FunctionConversion);
22912291
}
22922292
}
2293-
} else if (last->getKind() == ConstraintLocator::PatternMatch &&
2294-
isa<EnumElementPattern>(
2295-
last->castTo<LocatorPathElt::PatternMatch>().getPattern())) {
2293+
} else if (last->is<LocatorPathElt::PatternMatch>() &&
2294+
isa<EnumElementPattern>(
2295+
last->castTo<LocatorPathElt::PatternMatch>()
2296+
.getPattern())) {
2297+
// A single paren pattern becomes a labeled tuple pattern
2298+
// e.g. `case .test(let value):` should be able to match
2299+
// `case test(result: Int)`. Note that it also means that:
2300+
// `cast test(result: (String, Int))` would be matched against
2301+
// e.g. `case .test((let x, let y))` but that fails during
2302+
// pattern coercion (behavior consistent with what happens in
2303+
// `TypeCheckPattern`).
2304+
if (func1Params.size() == 1 && !func1Params.front().hasLabel() &&
2305+
func2Params.size() == 1 && func2Params.front().hasLabel()) {
2306+
auto param = func1Params.front();
2307+
auto label = func2Params.front().getLabel();
2308+
2309+
auto labeledParam = FunctionType::Param(param.getPlainType(), label,
2310+
param.getParameterFlags());
2311+
2312+
func1Params.clear();
2313+
func1Params.push_back(labeledParam);
2314+
}
2315+
22962316
// Consider following example:
22972317
//
22982318
// enum E {

0 commit comments

Comments
 (0)