Skip to content

Commit 3559047

Browse files
committed
[ConstraintSolver] Unify value-to-pointer conversion scoring
Instead of having three different scores for - string, array, inout, let's unify them under a single value-to-pointer score and use it in appropriate places when simplifying restricted constraints.
1 parent d834929 commit 3559047

File tree

3 files changed

+11
-21
lines changed

3 files changed

+11
-21
lines changed

lib/Sema/CSRanking.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,14 @@ void ConstraintSystem::increaseScore(ScoreKind kind, unsigned value) {
7070
case SK_ValueToOptional:
7171
log << "value to optional";
7272
break;
73-
74-
case SK_ArrayPointerConversion:
75-
log << "array-to-pointer conversion";
76-
break;
77-
case SK_ScalarPointerConversion:
78-
log << "scalar-to-pointer conversion";
79-
break;
8073
case SK_EmptyExistentialConversion:
8174
log << "empty-existential conversion";
8275
break;
8376
case SK_KeyPathSubscript:
8477
log << "key path subscript";
8578
break;
86-
87-
case SK_StringToPointerConversion:
88-
log << "string-to-pointer conversion";
79+
case SK_ValueToPointerConversion:
80+
log << "value-to-pointer conversion";
8981
break;
9082
}
9183
log << ")\n";

lib/Sema/CSSimplify.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2080,15 +2080,15 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
20802080
// Favor an UnsafeMutablePointer-to-UnsafeMutablePointer
20812081
// conversion.
20822082
if (type1PointerKind != pointerKind)
2083-
increaseScore(ScoreKind::SK_ScalarPointerConversion);
2083+
increaseScore(ScoreKind::SK_ValueToPointerConversion);
20842084
conversionsOrFixes.push_back(
20852085
ConversionRestrictionKind::PointerToPointer);
20862086
}
20872087
// UnsafeMutableRawPointer -> UnsafeRawPointer
20882088
else if (type1PointerKind == PTK_UnsafeMutableRawPointer &&
20892089
pointerKind == PTK_UnsafeRawPointer) {
20902090
if (type1PointerKind != pointerKind)
2091-
increaseScore(ScoreKind::SK_ScalarPointerConversion);
2091+
increaseScore(ScoreKind::SK_ValueToPointerConversion);
20922092
conversionsOrFixes.push_back(
20932093
ConversionRestrictionKind::PointerToPointer);
20942094
}
@@ -4378,6 +4378,7 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
43784378
auto baseType1 = getFixedTypeRecursive(*isArrayType(obj1), false, false);
43794379
auto baseType2 = getBaseTypeForPointer(*this, t2);
43804380

4381+
increaseScore(ScoreKind::SK_ValueToPointerConversion);
43814382
return matchTypes(baseType1, baseType2,
43824383
ConstraintKind::BindToPointerType,
43834384
subflags, locator);
@@ -4397,7 +4398,7 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
43974398
// If we haven't resolved the element type, generate constraints.
43984399
if (baseType2->isTypeVariableOrMember()) {
43994400
if (flags.contains(TMF_GenerateConstraints)) {
4400-
increaseScore(SK_StringToPointerConversion);
4401+
increaseScore(ScoreKind::SK_ValueToPointerConversion);
44014402

44024403
auto int8Con = Constraint::create(*this, ConstraintKind::Bind,
44034404
baseType2, TC.getInt8Type(DC),
@@ -4421,7 +4422,7 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
44214422
return SolutionKind::Error;
44224423
}
44234424

4424-
increaseScore(SK_StringToPointerConversion);
4425+
increaseScore(ScoreKind::SK_ValueToPointerConversion);
44254426
return SolutionKind::Solved;
44264427
}
44274428

@@ -4436,6 +4437,7 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
44364437

44374438
// Set up the disjunction for the array or scalar cases.
44384439

4440+
increaseScore(ScoreKind::SK_ValueToPointerConversion);
44394441
return matchTypes(baseType1, baseType2,
44404442
ConstraintKind::BindToPointerType,
44414443
subflags, locator);

lib/Sema/ConstraintSystem.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -408,18 +408,14 @@ enum ScoreKind {
408408
SK_CollectionUpcastConversion,
409409
/// A value-to-optional conversion.
410410
SK_ValueToOptional,
411-
/// A conversion from an inout to a pointer of matching element type.
412-
SK_ScalarPointerConversion,
413-
/// A conversion from an array to a pointer of matching element type.
414-
SK_ArrayPointerConversion,
415411
/// A conversion to an empty existential type ('Any' or '{}').
416412
SK_EmptyExistentialConversion,
417413
/// A key path application subscript.
418414
SK_KeyPathSubscript,
419-
// A conversion from a string to a pointer.
420-
SK_StringToPointerConversion,
415+
/// A conversion from a string, array, or inout to a pointer.
416+
SK_ValueToPointerConversion,
421417

422-
SK_LastScoreKind = SK_StringToPointerConversion,
418+
SK_LastScoreKind = SK_ValueToPointerConversion,
423419
};
424420

425421
/// The number of score kinds.

0 commit comments

Comments
 (0)