Skip to content

Commit 125abed

Browse files
committed
[CSOptimizer] Detect when candidate comes from string interpolation
Instead of checking both protocols, check one that matches best depending on where `String` literal candidate came from.
1 parent 5aa3859 commit 125abed

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

lib/Sema/CSOptimizer.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,7 @@ static void determineBestChoicesInContext(
10701070
Literal = 0x02,
10711071
ExactOnly = 0x04,
10721072
DisableCGFloatDoubleConversion = 0x08,
1073+
StringInterpolation = 0x10,
10731074
};
10741075

10751076
using MatchOptions = OptionSet<MatchFlag>;
@@ -1177,13 +1178,16 @@ static void determineBestChoicesInContext(
11771178
paramType, KnownProtocolKind::ExpressibleByBooleanLiteral))
11781179
return 0.3;
11791180

1180-
if (candidateType->isString() &&
1181-
(TypeChecker::conformsToKnownProtocol(
1182-
paramType, KnownProtocolKind::ExpressibleByStringLiteral) ||
1183-
TypeChecker::conformsToKnownProtocol(
1184-
paramType,
1185-
KnownProtocolKind::ExpressibleByStringInterpolation)))
1186-
return 0.3;
1181+
if (candidateType->isString()) {
1182+
auto literalProtocol =
1183+
options.contains(MatchFlag::StringInterpolation)
1184+
? KnownProtocolKind::ExpressibleByStringInterpolation
1185+
: KnownProtocolKind::ExpressibleByStringLiteral;
1186+
1187+
if (TypeChecker::conformsToKnownProtocol(paramType,
1188+
literalProtocol))
1189+
return 0.3;
1190+
}
11871191

11881192
auto &ctx = cs.getASTContext();
11891193

@@ -1523,6 +1527,11 @@ static void determineBestChoicesInContext(
15231527
candidate.fromInitializerCall)
15241528
options |= MatchFlag::DisableCGFloatDoubleConversion;
15251529

1530+
if (isExpr<InterpolatedStringLiteralExpr>(
1531+
argumentList->getExpr(argIdx)
1532+
->getSemanticsProvidingExpr()))
1533+
options |= MatchFlag::StringInterpolation;
1534+
15261535
// The specifier for a candidate only matters for `inout` check.
15271536
auto candidateScore = scoreCandidateMatch(
15281537
genericSig, decl, candidate.type->getWithoutSpecifierType(),

0 commit comments

Comments
 (0)