Skip to content

Commit 942d967

Browse files
committed
[CSFix] Move type verification out of UseValueTypeOfRawRepresentative
1 parent 03938f9 commit 942d967

File tree

2 files changed

+26
-41
lines changed

2 files changed

+26
-41
lines changed

lib/Sema/CSFix.cpp

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,28 +1087,6 @@ AllowInOutConversion *AllowInOutConversion::create(ConstraintSystem &cs,
10871087
AllowInOutConversion(cs, argType, paramType, locator);
10881088
}
10891089

1090-
/// Check whether given `value` type is indeed a the same type as a `RawValue`
1091-
/// type of a given raw representable type.
1092-
static bool isValueOfRawRepresentable(ConstraintSystem &cs,
1093-
Type rawRepresentableType,
1094-
Type valueType) {
1095-
auto rawType = isRawRepresentable(cs, rawRepresentableType);
1096-
if (!rawType)
1097-
return false;
1098-
1099-
KnownProtocolKind protocols[] = {
1100-
KnownProtocolKind::ExpressibleByStringLiteral,
1101-
KnownProtocolKind::ExpressibleByIntegerLiteral};
1102-
1103-
for (auto protocol : protocols) {
1104-
if (conformsToKnownProtocol(cs, valueType, protocol) &&
1105-
valueType->isEqual(rawType))
1106-
return true;
1107-
}
1108-
1109-
return false;
1110-
}
1111-
11121090
ExpandArrayIntoVarargs *
11131091
ExpandArrayIntoVarargs::attempt(ConstraintSystem &cs, Type argType,
11141092
Type paramType,
@@ -1159,18 +1137,19 @@ ExplicitlyConstructRawRepresentable::create(ConstraintSystem &cs,
11591137
ExplicitlyConstructRawRepresentable(cs, rawReprType, valueType, locator);
11601138
}
11611139

1162-
UseValueTypeOfRawRepresentative *
1163-
UseValueTypeOfRawRepresentative::attempt(ConstraintSystem &cs, Type argType,
1164-
Type paramType,
1165-
ConstraintLocatorBuilder locator) {
1166-
auto rawRepresentableType = argType->lookThroughAllOptionalTypes();
1167-
auto valueType = paramType->lookThroughAllOptionalTypes();
1168-
1169-
if (isValueOfRawRepresentable(cs, rawRepresentableType, valueType))
1170-
return new (cs.getAllocator()) UseValueTypeOfRawRepresentative(
1171-
cs, rawRepresentableType, valueType, cs.getConstraintLocator(locator));
1140+
bool UseValueTypeOfRawRepresentative::diagnose(const Solution &solution,
1141+
bool asNote) const {
1142+
ArgumentMismatchFailure failure(solution, RawReprType, ValueType,
1143+
getLocator());
1144+
return failure.diagnose(asNote);
1145+
}
11721146

1173-
return nullptr;
1147+
UseValueTypeOfRawRepresentative *
1148+
UseValueTypeOfRawRepresentative::create(ConstraintSystem &cs, Type rawReprType,
1149+
Type valueType,
1150+
ConstraintLocator *locator) {
1151+
return new (cs.getAllocator())
1152+
UseValueTypeOfRawRepresentative(cs, rawReprType, valueType, locator);
11741153
}
11751154

11761155
unsigned AllowArgumentMismatch::getParamIdx() const {

lib/Sema/CSFix.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,20 +1617,26 @@ class ExplicitlyConstructRawRepresentable final : public ConstraintFix {
16171617
ConstraintLocator *locator);
16181618
};
16191619

1620-
class UseValueTypeOfRawRepresentative final : public AllowArgumentMismatch {
1621-
UseValueTypeOfRawRepresentative(ConstraintSystem &cs, Type argType,
1622-
Type paramType, ConstraintLocator *locator)
1623-
: AllowArgumentMismatch(cs, FixKind::UseValueTypeOfRawRepresentative,
1624-
argType, paramType, locator) {}
1620+
class UseValueTypeOfRawRepresentative final : public ConstraintFix {
1621+
Type RawReprType;
1622+
Type ValueType;
1623+
1624+
UseValueTypeOfRawRepresentative(ConstraintSystem &cs, Type rawReprType,
1625+
Type valueType, ConstraintLocator *locator)
1626+
: ConstraintFix(cs, FixKind::UseValueTypeOfRawRepresentative, locator),
1627+
RawReprType(rawReprType), ValueType(valueType) {}
16251628

16261629
public:
16271630
std::string getName() const override {
16281631
return "use `.rawValue` of a raw representable type";
16291632
}
16301633

1631-
static UseValueTypeOfRawRepresentative *
1632-
attempt(ConstraintSystem &cs, Type argType, Type paramType,
1633-
ConstraintLocatorBuilder locator);
1634+
bool diagnose(const Solution &solution, bool asNote = false) const override;
1635+
1636+
static UseValueTypeOfRawRepresentative *create(ConstraintSystem &cs,
1637+
Type rawReprType,
1638+
Type valueType,
1639+
ConstraintLocator *locator);
16341640
};
16351641

16361642
/// Replace a coercion ('as') with a forced checked cast ('as!').

0 commit comments

Comments
 (0)