Skip to content

Commit 463cb02

Browse files
committed
Revert "[CSSolver] Use correct locator when matching function result types related to closures"
This reverts commit 503d2d8.
1 parent e6605bc commit 463cb02

File tree

5 files changed

+11
-111
lines changed

5 files changed

+11
-111
lines changed

lib/Sema/CSRanking.cpp

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,32 +1027,14 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
10271027
// Compare the type variable bindings.
10281028
auto &tc = cs.getTypeChecker();
10291029
for (auto &binding : diff.typeBindings) {
1030-
auto type1 = binding.bindings[idx1];
1031-
auto type2 = binding.bindings[idx2];
1032-
1033-
auto &impl = binding.typeVar->getImpl();
1034-
1035-
if (auto *locator = impl.getLocator()) {
1036-
auto path = locator->getPath();
1037-
if (!path.empty() &&
1038-
path.back().getKind() == ConstraintLocator::ClosureResult) {
1039-
// Since we support `() -> T` to `() -> Void` and
1040-
// `() -> Never` to `() -> T` conversions, it's always
1041-
// preferable to pick `T` rather than `Never` with
1042-
// all else being equal.
1043-
if (type2->isUninhabited())
1044-
++score1;
1045-
1046-
if (type1->isUninhabited())
1047-
++score2;
1048-
}
1049-
}
1050-
10511030
// If the type variable isn't one for which we should be looking at the
10521031
// bindings, don't.
1053-
if (!impl.prefersSubtypeBinding())
1032+
if (!binding.typeVar->getImpl().prefersSubtypeBinding())
10541033
continue;
10551034

1035+
auto type1 = binding.bindings[idx1];
1036+
auto type2 = binding.bindings[idx2];
1037+
10561038
// If the types are equivalent, there's nothing more to do.
10571039
if (type1->isEqual(type2))
10581040
continue;

lib/Sema/CSSimplify.cpp

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,8 +1235,10 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
12351235
}
12361236

12371237
// Result type can be covariant (or equal).
1238-
return matchTypes(func1->getResult(), func2->getResult(), subKind, subflags,
1239-
locator.withPathElement(ConstraintLocator::FunctionResult));
1238+
return matchTypes(func1->getResult(), func2->getResult(), subKind,
1239+
subflags,
1240+
locator.withPathElement(
1241+
ConstraintLocator::FunctionResult));
12401242
}
12411243

12421244
ConstraintSystem::SolutionKind
@@ -1562,8 +1564,6 @@ ConstraintSystem::SolutionKind
15621564
ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
15631565
TypeMatchOptions flags,
15641566
ConstraintLocatorBuilder locator) {
1565-
auto origType1 = type1;
1566-
15671567
bool isArgumentTupleConversion
15681568
= kind == ConstraintKind::ArgumentTupleConversion ||
15691569
kind == ConstraintKind::OperatorArgumentTupleConversion;
@@ -2324,32 +2324,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
23242324
// Allow '() -> T' to '() -> ()' and '() -> Never' to '() -> T' for closure
23252325
// literals.
23262326
if (auto elt = locator.last()) {
2327-
auto isClosureResult = [&]() {
2328-
if (elt->getKind() == ConstraintLocator::ClosureResult)
2329-
return true;
2330-
2331-
// If constraint is matching function results where
2332-
// left-hand side is a 'closure result' we need to allow
2333-
// certain implicit conversions.
2334-
if (elt->getKind() != ConstraintLocator::FunctionResult)
2335-
return false;
2336-
2337-
if (auto *typeVar = origType1->getAs<TypeVariableType>()) {
2338-
auto *locator = typeVar->getImpl().getLocator();
2339-
if (!locator)
2340-
return false;
2341-
2342-
auto path = locator->getPath();
2343-
if (path.empty())
2344-
return false;
2345-
2346-
return path.back().getKind() == ConstraintLocator::ClosureResult;
2347-
}
2348-
2349-
return false;
2350-
};
2351-
2352-
if (isClosureResult()) {
2327+
if (elt->getKind() == ConstraintLocator::ClosureResult) {
23532328
if (concrete && kind >= ConstraintKind::Subtype &&
23542329
(type1->isUninhabited() || type2->isVoid())) {
23552330
increaseScore(SK_FunctionConversion);

test/Constraints/closures.swift

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@ func rdar33429010_2() {
630630
let iter = I_33429010()
631631
var acc: Int = 0 // expected-warning {{}}
632632
let _: Int = AnySequence { iter }.rdar33429010(into: acc, { $0 + $1 })
633+
// expected-warning@-1 {{result of operator '+' is unused}}
633634
let _: Int = AnySequence { iter }.rdar33429010(into: acc, { $0.rdar33429010_incr($1) })
634635
}
635636

@@ -651,16 +652,3 @@ func rdar36054961() {
651652
str.replaceSubrange(range, with: str[range].reversed())
652653
}])
653654
}
654-
655-
func rdar37790062() {
656-
struct S<T> {
657-
init(_ a: () -> T, _ b: () -> T) {}
658-
}
659-
660-
func foo() -> Int { return 42 }
661-
func bar() -> Void {}
662-
func baz() -> (String, Int) { return ("question", 42) }
663-
664-
_ = S({ foo() }, { bar() }) // Ok, should infer T to be 'Void'
665-
_ = S({ baz() }, { bar() }) // Ok, should infer T to be 'Void'
666-
}

test/Constraints/rdar37790062.swift

Lines changed: 0 additions & 46 deletions
This file was deleted.

test/expr/closure/closures.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ func rdar19179412() -> (Int) -> Int {
259259
func takesVoidFunc(_ f: () -> ()) {}
260260
var i: Int = 1
261261

262+
// expected-warning @+1 {{expression of type 'Int' is unused}}
262263
takesVoidFunc({i})
263264
// expected-warning @+1 {{expression of type 'Int' is unused}}
264265
var f1: () -> () = {i}

0 commit comments

Comments
 (0)