Skip to content

Commit 5a7c70d

Browse files
committed
[Diagnostics] NFC: Add isRawRepresentable/conformsToKnownProtocol helpers into FailureDiagnostic
1 parent bf4c3b7 commit 5a7c70d

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,18 @@ Type FailureDiagnostic::restoreGenericParameters(
150150
});
151151
}
152152

153+
bool FailureDiagnostic::conformsToKnownProtocol(
154+
Type type, KnownProtocolKind protocol) const {
155+
auto &cs = getConstraintSystem();
156+
return constraints::conformsToKnownProtocol(cs, type, protocol);
157+
}
158+
159+
Type FailureDiagnostic::isRawRepresentable(Type type,
160+
KnownProtocolKind protocol) const {
161+
auto &cs = getConstraintSystem();
162+
return constraints::isRawRepresentable(cs, type, protocol);
163+
}
164+
153165
Type RequirementFailure::getOwnerType() const {
154166
auto anchor = getRawAnchor();
155167

@@ -2406,9 +2418,8 @@ bool ContextualFailure::diagnoseConversionToBool() const {
24062418

24072419
// If we're trying to convert something from optional type to an integer, then
24082420
// a comparison against nil was probably expected.
2409-
auto &cs = getConstraintSystem();
2410-
if (conformsToKnownProtocol(cs, fromType, KnownProtocolKind::BinaryInteger) &&
2411-
conformsToKnownProtocol(cs, fromType,
2421+
if (conformsToKnownProtocol(fromType, KnownProtocolKind::BinaryInteger) &&
2422+
conformsToKnownProtocol(fromType,
24122423
KnownProtocolKind::ExpressibleByIntegerLiteral)) {
24132424
StringRef prefix = "((";
24142425
StringRef suffix;
@@ -2503,7 +2514,6 @@ bool ContextualFailure::diagnoseYieldByReferenceMismatch() const {
25032514
bool ContextualFailure::tryRawRepresentableFixIts(
25042515
InFlightDiagnostic &diagnostic,
25052516
KnownProtocolKind rawRepresentableProtocol) const {
2506-
auto &CS = getConstraintSystem();
25072517
auto anchor = getAnchor();
25082518
auto fromType = getFromType();
25092519
auto toType = getToType();
@@ -2558,14 +2568,14 @@ bool ContextualFailure::tryRawRepresentableFixIts(
25582568
}
25592569
};
25602570

2561-
if (conformsToKnownProtocol(CS, fromType, rawRepresentableProtocol)) {
2562-
if (conformsToKnownProtocol(CS, fromType, KnownProtocolKind::OptionSet) &&
2571+
if (conformsToKnownProtocol(fromType, rawRepresentableProtocol)) {
2572+
if (conformsToKnownProtocol(fromType, KnownProtocolKind::OptionSet) &&
25632573
isExpr<IntegerLiteralExpr>(anchor) &&
25642574
castToExpr<IntegerLiteralExpr>(anchor)->getDigitsText() == "0") {
25652575
diagnostic.fixItReplace(::getSourceRange(anchor), "[]");
25662576
return true;
25672577
}
2568-
if (auto rawTy = isRawRepresentable(CS, toType, rawRepresentableProtocol)) {
2578+
if (auto rawTy = isRawRepresentable(toType, rawRepresentableProtocol)) {
25692579
// Produce before/after strings like 'Result(rawValue: RawType(<expr>))'
25702580
// or just 'Result(rawValue: <expr>)'.
25712581
std::string convWrapBefore = toType.getString();
@@ -2595,8 +2605,8 @@ bool ContextualFailure::tryRawRepresentableFixIts(
25952605
}
25962606
}
25972607

2598-
if (auto rawTy = isRawRepresentable(CS, fromType, rawRepresentableProtocol)) {
2599-
if (conformsToKnownProtocol(CS, toType, rawRepresentableProtocol)) {
2608+
if (auto rawTy = isRawRepresentable(fromType, rawRepresentableProtocol)) {
2609+
if (conformsToKnownProtocol(toType, rawRepresentableProtocol)) {
26002610
std::string convWrapBefore;
26012611
std::string convWrapAfter = ".rawValue";
26022612
if (!TypeChecker::isConvertibleTo(rawTy, toType, getDC())) {
@@ -2877,12 +2887,11 @@ void ContextualFailure::tryComputedPropertyFixIts() const {
28772887
}
28782888

28792889
bool ContextualFailure::isIntegerToStringIndexConversion() const {
2880-
auto &cs = getConstraintSystem();
28812890
auto kind = KnownProtocolKind::ExpressibleByIntegerLiteral;
28822891

28832892
auto fromType = getFromType();
28842893
auto toType = getToType()->getCanonicalType();
2885-
return (conformsToKnownProtocol(cs, fromType, kind) &&
2894+
return (conformsToKnownProtocol(fromType, kind) &&
28862895
toType.getString() == "String.CharacterView.Index");
28872896
}
28882897

lib/Sema/CSDiagnostics.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ class FailureDiagnostic {
216216
auto &cs = getConstraintSystem();
217217
return bool(cs.isArrayType(type));
218218
}
219+
220+
bool conformsToKnownProtocol(Type type, KnownProtocolKind protocol) const;
221+
Type isRawRepresentable(Type type, KnownProtocolKind protocol) const;
219222
};
220223

221224
/// Base class for all of the diagnostics related to generic requirement
@@ -672,8 +675,7 @@ class ContextualFailure : public FailureDiagnostic {
672675

673676
bool isIntegerType(Type type) const {
674677
return conformsToKnownProtocol(
675-
getConstraintSystem(), type,
676-
KnownProtocolKind::ExpressibleByIntegerLiteral);
678+
type, KnownProtocolKind::ExpressibleByIntegerLiteral);
677679
}
678680

679681
/// Return true if the conversion from fromType to toType is

0 commit comments

Comments
 (0)