Skip to content

Commit 340d1e8

Browse files
committed
[ConstraintSystem] Record trailing choice match choice when arguments/result are equivalent to applied function
1 parent faa6289 commit 340d1e8

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8802,14 +8802,6 @@ ConstraintSystem::simplifyApplicableFnConstraint(
88028802
return false;
88038803
};
88048804

8805-
// If the types are obviously equivalent, we're done. This optimization
8806-
// is not valid for operators though, where an inout parameter does not
8807-
// have an explicit inout argument.
8808-
if (type1.getPointer() == desugar2) {
8809-
if (!isOperator || !hasInOut())
8810-
return SolutionKind::Solved;
8811-
}
8812-
88138805
// Local function to form an unsolved result.
88148806
auto formUnsolved = [&] {
88158807
if (flags.contains(TMF_GenerateConstraints)) {
@@ -8838,6 +8830,19 @@ ConstraintSystem::simplifyApplicableFnConstraint(
88388830
ConstraintLocatorBuilder outerLocator =
88398831
getConstraintLocator(anchor, parts, locator.getSummaryFlags());
88408832

8833+
// If the types are obviously equivalent, we're done. This optimization
8834+
// is not valid for operators though, where an inout parameter does not
8835+
// have an explicit inout argument.
8836+
if (type1.getPointer() == desugar2) {
8837+
if (!isOperator || !hasInOut()) {
8838+
recordTrailingClosureMatch(
8839+
getConstraintLocator(
8840+
outerLocator.withPathElement(ConstraintLocator::ApplyArgument)),
8841+
TrailingClosureMatching::Forward);
8842+
return SolutionKind::Solved;
8843+
}
8844+
}
8845+
88418846
// Handle applications of types with `callAsFunction` methods.
88428847
// Do this before stripping optional types below, when `shouldAttemptFixes()`
88438848
// is true.

unittests/Sema/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

22
add_swift_unittest(swiftSemaTests
33
SemaFixture.cpp
4-
BindingInferenceTests.cpp)
4+
BindingInferenceTests.cpp
5+
ConstraintSimplificationTests.cpp)
56

67
target_link_libraries(swiftSemaTests
78
PRIVATE
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===--- ConstraintSimplificationTests.cpp --------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "SemaFixture.h"
14+
#include "swift/Sema/ConstraintSystem.h"
15+
16+
using namespace swift;
17+
using namespace swift::unittest;
18+
using namespace swift::constraints;
19+
20+
TEST_F(SemaTest, TestTrailingClosureMatchRecordingForIdenticalFunctions) {
21+
ConstraintSystem cs(DC, ConstraintSystemOptions());
22+
23+
auto intType = getStdlibType("Int");
24+
auto floatType = getStdlibType("Float");
25+
26+
auto func = FunctionType::get({FunctionType::Param(intType)}, floatType);
27+
28+
cs.addConstraint(
29+
ConstraintKind::ApplicableFunction, func, func,
30+
cs.getConstraintLocator({}, ConstraintLocator::ApplyFunction));
31+
32+
SmallVector<Solution, 2> solutions;
33+
cs.solve(solutions);
34+
35+
ASSERT_EQ(solutions.size(), (unsigned)1);
36+
37+
const auto &solution = solutions.front();
38+
39+
auto *locator = cs.getConstraintLocator({}, ConstraintLocator::ApplyArgument);
40+
auto choice = solution.trailingClosureMatchingChoices.find(locator);
41+
ASSERT_TRUE(choice != solution.trailingClosureMatchingChoices.end());
42+
ASSERT_EQ(choice->second, TrailingClosureMatching::Forward);
43+
}

0 commit comments

Comments
 (0)