Skip to content

[ResultBuilders] A couple pattern matching fixes to make it consistent with TypeCheckPattern #39458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2405,7 +2405,19 @@ namespace {
Type memberType = CS.createTypeVariable(
CS.getConstraintLocator(locator),
TVO_CanBindToLValue | TVO_CanBindToNoEscape);
FunctionRefKind functionRefKind = FunctionRefKind::Compound;

// Tuple splat is still allowed for patterns (with a warning in Swift 5)
// so we need to start here from single-apply to make sure that e.g.
// `case test(x: Int, y: Int)` gets the labels preserved when matched
// with `case let .test(tuple)`.
FunctionRefKind functionRefKind = FunctionRefKind::SingleApply;
// If sub-pattern is a tuple we'd need to mark reference as compound,
// that would make sure that the labels are dropped in cases
// when `case` has a single tuple argument (tuple explosion) or multiple
// arguments (tuple-to-tuple conversion).
if (dyn_cast_or_null<TuplePattern>(enumPattern->getSubPattern()))
functionRefKind = FunctionRefKind::Compound;

if (enumPattern->getParentType() || enumPattern->getParentTypeRepr()) {
// Resolve the parent type.
const auto parentType = [&] {
Expand Down
26 changes: 23 additions & 3 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2290,9 +2290,29 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
increaseScore(SK_FunctionConversion);
}
}
} else if (last->getKind() == ConstraintLocator::PatternMatch &&
isa<EnumElementPattern>(
last->castTo<LocatorPathElt::PatternMatch>().getPattern())) {
} else if (last->is<LocatorPathElt::PatternMatch>() &&
isa<EnumElementPattern>(
last->castTo<LocatorPathElt::PatternMatch>()
.getPattern())) {
// 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`).
if (func1Params.size() == 1 && !func1Params.front().hasLabel() &&
func2Params.size() == 1 && func2Params.front().hasLabel()) {
auto param = func1Params.front();
auto label = func2Params.front().getLabel();

auto labeledParam = FunctionType::Param(param.getPlainType(), label,
param.getParameterFlags());

func1Params.clear();
func1Params.push_back(labeledParam);
}

// Consider following example:
//
// enum E {
Expand Down
58 changes: 58 additions & 0 deletions test/Constraints/result_builder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,64 @@ func getE(_ i: Int) -> E {
}
}

func test_labeled_splats() {
enum E {
case multi(a: String, b: String)
case tuple((a: Int, b: Int))
case single(result: Int)
case single_multi(result: (a: Int, q: String))
}

func test_answer(_: String) -> Int { 42 }
func test_question(_: Int) -> String { "ultimate question" }

let e: E = E.single(result: 42)

tuplify(true) { _ in
switch e {
case .single(let result):
test_question(result)
case let .single_multi(result):
test_answer(result.q)
test_question(result.a)
case let .multi(value): // tuple splat preserves labels
test_answer(value.a)
test_answer(value.b)
case let .tuple(a: a, b: b): // un-splat preserves labels too
test_question(a)
test_question(b)
}

// compound names still work with and without splat
switch e {
case .single(_): 42
case .single_multi(result: (let a, let q)):
test_answer(q)
test_question(a)
case let .multi(a: a, b: b):
test_answer(a)
test_answer(b)
case let .tuple((a: a, b: b)):
test_question(a)
test_question(b)
}

// no labels, no problem regardless of splatting
switch e {
case .single(_): 42
case let .single_multi(result: (a, q)):
test_question(a)
test_answer(q)
case let .multi(a, b):
test_answer(a)
test_answer(b)
case let .tuple((a, b)):
test_question(a)
test_question(b)
}
}
}

tuplify(true) { c in
"testIfLetMatching"
if let theValue = getOptionalInt(c) {
Expand Down