Skip to content

Commit 02150f1

Browse files
authored
Merge pull request #17049 from rudkx/sr7884-4.2
[4.2] [ConstraintSystem] Remove an unsound optimization hack for generic fu…
2 parents 78e9497 + 0af774d commit 02150f1

File tree

5 files changed

+22
-63
lines changed

5 files changed

+22
-63
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -1410,64 +1410,6 @@ ConstraintSystem::getTypeOfMemberReference(
14101410
return { openedType, type };
14111411
}
14121412

1413-
// Performance hack: if there are two generic overloads, and one is
1414-
// more specialized than the other, prefer the more-specialized one.
1415-
static void tryOptimizeGenericDisjunction(ConstraintSystem &cs,
1416-
ArrayRef<OverloadChoice> choices,
1417-
OverloadChoice *&favoredChoice) {
1418-
if (favoredChoice || choices.size() != 2)
1419-
return;
1420-
1421-
const auto &choiceA = choices[0];
1422-
const auto &choiceB = choices[1];
1423-
1424-
if (!choiceA.isDecl() || !choiceB.isDecl())
1425-
return;
1426-
1427-
auto isViable = [](ValueDecl *decl) -> bool {
1428-
assert(decl);
1429-
1430-
auto *AFD = dyn_cast<AbstractFunctionDecl>(decl);
1431-
if (!AFD || !AFD->isGeneric())
1432-
return false;
1433-
1434-
auto funcType = AFD->getInterfaceType();
1435-
auto hasAny = funcType.findIf([](Type type) -> bool {
1436-
if (auto objType = type->getOptionalObjectType())
1437-
return objType->isAny();
1438-
1439-
return type->isAny();
1440-
});
1441-
1442-
// If function declaration references `Any` or `Any?` type
1443-
// let's not attempt it, because it's unclear
1444-
// without solving which overload is going to be better.
1445-
return !hasAny;
1446-
};
1447-
1448-
auto *declA = choiceA.getDecl();
1449-
auto *declB = choiceB.getDecl();
1450-
1451-
if (!isViable(declA) || !isViable(declB))
1452-
return;
1453-
1454-
auto &TC = cs.TC;
1455-
auto *DC = cs.DC;
1456-
1457-
switch (TC.compareDeclarations(DC, declA, declB)) {
1458-
case Comparison::Better:
1459-
favoredChoice = const_cast<OverloadChoice *>(&choiceA);
1460-
break;
1461-
1462-
case Comparison::Worse:
1463-
favoredChoice = const_cast<OverloadChoice *>(&choiceB);
1464-
break;
1465-
1466-
case Comparison::Unordered:
1467-
break;
1468-
}
1469-
}
1470-
14711413
void ConstraintSystem::addOverloadSet(Type boundType,
14721414
ArrayRef<OverloadChoice> choices,
14731415
DeclContext *useDC,
@@ -1482,8 +1424,6 @@ void ConstraintSystem::addOverloadSet(Type boundType,
14821424
return;
14831425
}
14841426

1485-
tryOptimizeGenericDisjunction(*this, choices, favoredChoice);
1486-
14871427
SmallVector<Constraint *, 4> overloads;
14881428

14891429
// As we do for other favored constraints, if a favored overload has been

test/Constraints/optional.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func test9(_ i: Int, io: Int?) {
9494
let _: Double = result2
9595

9696
let result3 = test9_helper2(i)
97-
var _: Double = result3
97+
var _: Int = result3
9898
let result4 = test9_helper2(io)
9999
let _: Double = result4
100100
}

test/Constraints/sr7884.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
// SR-7884
4+
func f<T>(_ x: T) -> T {
5+
return x
6+
}
7+
8+
func f<T>(_ x: T?) -> T? {
9+
return x
10+
}
11+
12+
let r = f(1) // calls optional variant
13+
let _ = r! // expected-error {{cannot force unwrap value of non-optional type 'Int'}}
14+
15+
// SR-7899
16+
let optSeq: LazySequence<[Int]>? = nil
17+
let lazySequence = optSeq?.lazy
18+
let value = lazySequence?.compactMap({ $0 as? Int }).first // expected-warning {{conditional cast from 'Int' to 'Int' always succeeds}}
19+
let _: Int = value!

validation-test/Sema/type_checker_perf/fast/more_specialized_generic_func.swift.gyb renamed to validation-test/Sema/type_checker_perf/slow/more_specialized_generic_func.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %scale-test --begin 1 --end 10 --step 1 --select incrementScopeCounter %s --expected-exit-code 0
1+
// RUN: %scale-test --invert-result --begin 1 --end 10 --step 1 --select incrementScopeCounter %s --expected-exit-code 0
22
// REQUIRES: OS=macosx
33
// REQUIRES: asserts
44

0 commit comments

Comments
 (0)