Skip to content

Commit e3987be

Browse files
committed
[CSOptimizer] Literal arguments should cause score reset only for operators
Resetting score to `0.1` is intended to make sure that the solver picks the outermost disjunction in literal chains like `1 + 2 + 3 ...` because that would provide context to the inner choices. Resolves: #78371 Resolves: rdar://142105691
1 parent 6c82892 commit e3987be

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/Sema/CSOptimizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ static void determineBestChoicesInContext(
958958
// Preferring outer disjunction first works better in situations
959959
// when contextual type for the whole chain becomes available at
960960
// some point during solving at it would allow for faster pruning.
961-
if (score > 0 && onlyLiteralCandidates)
961+
if (score > 0 && onlyLiteralCandidates && decl->isOperator())
962962
score = 0.1;
963963

964964
// If one of the result types matches exactly, that's a good

validation-test/Sema/issue78371.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
// https://github.com/swiftlang/swift/issues/78371
4+
5+
// Note that the test has to use a standard operator because
6+
// custom ones are not optimized.
7+
8+
struct Scalar : Equatable {
9+
init(_: UInt8) {}
10+
init?(_: Int) {}
11+
}
12+
13+
func ==(_: Scalar, _: Scalar) -> Bool { }
14+
15+
extension Optional where Wrapped == Scalar {
16+
static func ==(_: Wrapped?, _: Wrapped?) -> Wrapped { }
17+
}
18+
19+
func test(a: Scalar) {
20+
let result = a == Scalar(0x07FD)
21+
let _: Scalar = result // Ok
22+
}

0 commit comments

Comments
 (0)