Skip to content

Commit 5998cd6

Browse files
committed
[ConstraintSolver] Penalize conversions from String to UnsafePointer
There are possible situations when we find solutions with String and String -> UnsafePointer conversions at the same time for expressions with default string literals. In order to disambiguite such situations let's prefer solutions without String -> UnsafePointer conversions if possible.
1 parent 1bc7a1e commit 5998cd6

File tree

5 files changed

+14
-3
lines changed

5 files changed

+14
-3
lines changed

lib/Sema/CSRanking.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ void ConstraintSystem::increaseScore(ScoreKind kind, unsigned value) {
8383
case SK_KeyPathSubscript:
8484
log << "key path subscript";
8585
break;
86+
87+
case SK_StringToPointerConversion:
88+
log << "string-to-pointer conversion";
89+
break;
8690
}
8791
log << ")\n";
8892
}

lib/Sema/CSSimplify.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4397,6 +4397,8 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
43974397
// If we haven't resolved the element type, generate constraints.
43984398
if (baseType2->isTypeVariableOrMember()) {
43994399
if (flags.contains(TMF_GenerateConstraints)) {
4400+
increaseScore(SK_StringToPointerConversion);
4401+
44004402
auto int8Con = Constraint::create(*this, ConstraintKind::Bind,
44014403
baseType2, TC.getInt8Type(DC),
44024404
getConstraintLocator(locator));
@@ -4418,6 +4420,8 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
44184420
if (!isStringCompatiblePointerBaseType(TC, DC, baseType2)) {
44194421
return SolutionKind::Error;
44204422
}
4423+
4424+
increaseScore(SK_StringToPointerConversion);
44214425
return SolutionKind::Solved;
44224426
}
44234427

lib/Sema/ConstraintSystem.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,10 @@ enum ScoreKind {
416416
SK_EmptyExistentialConversion,
417417
/// A key path application subscript.
418418
SK_KeyPathSubscript,
419-
420-
SK_LastScoreKind = SK_KeyPathSubscript,
419+
// A conversion from a string to a pointer.
420+
SK_StringToPointerConversion,
421+
422+
SK_LastScoreKind = SK_StringToPointerConversion,
421423
};
422424

423425
/// The number of score kinds.

test/Constraints/overload.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,4 @@ func curry<F, S, T, R>(_ f: @escaping (F, S, T) -> R) -> (F) -> (S) -> (T) -> R
222222
// Ensure that we consider these unambiguous
223223
let _ = curry(+)(1)
224224
let _ = [0].reduce(0, +)
225+
let _ = curry(+)("string vs. pointer")

validation-test/Sema/rdar32204609.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
let x: Int! = nil
99
let y: Int! = 1
1010

11-
print(x == y)
11+
print(x == y)

0 commit comments

Comments
 (0)