Skip to content

Commit

Permalink
[ConstraintSolver] Penalize conversions from String to UnsafePointer
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
xedin committed Jun 1, 2017
1 parent 1bc7a1e commit 5998cd6
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/Sema/CSRanking.cpp
Expand Up @@ -83,6 +83,10 @@ void ConstraintSystem::increaseScore(ScoreKind kind, unsigned value) {
case SK_KeyPathSubscript:
log << "key path subscript";
break;

case SK_StringToPointerConversion:
log << "string-to-pointer conversion";
break;
}
log << ")\n";
}
Expand Down
4 changes: 4 additions & 0 deletions lib/Sema/CSSimplify.cpp
Expand Up @@ -4397,6 +4397,8 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
// If we haven't resolved the element type, generate constraints.
if (baseType2->isTypeVariableOrMember()) {
if (flags.contains(TMF_GenerateConstraints)) {
increaseScore(SK_StringToPointerConversion);

auto int8Con = Constraint::create(*this, ConstraintKind::Bind,
baseType2, TC.getInt8Type(DC),
getConstraintLocator(locator));
Expand All @@ -4418,6 +4420,8 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
if (!isStringCompatiblePointerBaseType(TC, DC, baseType2)) {
return SolutionKind::Error;
}

increaseScore(SK_StringToPointerConversion);
return SolutionKind::Solved;
}

Expand Down
6 changes: 4 additions & 2 deletions lib/Sema/ConstraintSystem.h
Expand Up @@ -416,8 +416,10 @@ enum ScoreKind {
SK_EmptyExistentialConversion,
/// A key path application subscript.
SK_KeyPathSubscript,

SK_LastScoreKind = SK_KeyPathSubscript,
// A conversion from a string to a pointer.
SK_StringToPointerConversion,

SK_LastScoreKind = SK_StringToPointerConversion,
};

/// The number of score kinds.
Expand Down
1 change: 1 addition & 0 deletions test/Constraints/overload.swift
Expand Up @@ -222,3 +222,4 @@ func curry<F, S, T, R>(_ f: @escaping (F, S, T) -> R) -> (F) -> (S) -> (T) -> R
// Ensure that we consider these unambiguous
let _ = curry(+)(1)
let _ = [0].reduce(0, +)
let _ = curry(+)("string vs. pointer")
2 changes: 1 addition & 1 deletion validation-test/Sema/rdar32204609.swift
Expand Up @@ -8,4 +8,4 @@
let x: Int! = nil
let y: Int! = 1

print(x == y)
print(x == y)

0 comments on commit 5998cd6

Please sign in to comment.