Skip to content

Commit a0a37d2

Browse files
authored
Merge pull request swiftlang#34356 from xedin/record-trailing-even-if-types-are-equal
[ConstraintSystem] Record trailing choice match choice when arguments…
2 parents b654008 + 340d1e8 commit a0a37d2

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
@@ -8864,14 +8864,6 @@ ConstraintSystem::simplifyApplicableFnConstraint(
88648864
return false;
88658865
};
88668866

8867-
// If the types are obviously equivalent, we're done. This optimization
8868-
// is not valid for operators though, where an inout parameter does not
8869-
// have an explicit inout argument.
8870-
if (type1.getPointer() == desugar2) {
8871-
if (!isOperator || !hasInOut())
8872-
return SolutionKind::Solved;
8873-
}
8874-
88758867
// Local function to form an unsolved result.
88768868
auto formUnsolved = [&] {
88778869
if (flags.contains(TMF_GenerateConstraints)) {
@@ -8900,6 +8892,19 @@ ConstraintSystem::simplifyApplicableFnConstraint(
89008892
ConstraintLocatorBuilder outerLocator =
89018893
getConstraintLocator(anchor, parts, locator.getSummaryFlags());
89028894

8895+
// If the types are obviously equivalent, we're done. This optimization
8896+
// is not valid for operators though, where an inout parameter does not
8897+
// have an explicit inout argument.
8898+
if (type1.getPointer() == desugar2) {
8899+
if (!isOperator || !hasInOut()) {
8900+
recordTrailingClosureMatch(
8901+
getConstraintLocator(
8902+
outerLocator.withPathElement(ConstraintLocator::ApplyArgument)),
8903+
TrailingClosureMatching::Forward);
8904+
return SolutionKind::Solved;
8905+
}
8906+
}
8907+
89038908
// Handle applications of types with `callAsFunction` methods.
89048909
// Do this before stripping optional types below, when `shouldAttemptFixes()`
89058910
// 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)