Skip to content

Commit 761657f

Browse files
committed
[Diagnostics] Extract common logic for raw representable diagnostics into AbstractRawRepresentableFailure
1 parent 942d967 commit 761657f

File tree

2 files changed

+44
-17
lines changed

2 files changed

+44
-17
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6319,23 +6319,27 @@ bool UnableToInferKeyPathRootFailure::diagnoseAsError() {
63196319
return true;
63206320
}
63216321

6322-
bool MissingRawRepresentativeInitFailure::diagnoseAsError() {
6322+
Optional<Diag<Type, Type>>
6323+
AbstractRawRepresentableFailure::getDiagnostic() const {
63236324
auto *locator = getLocator();
63246325

6325-
Optional<Diag<Type, Type>> message;
6326-
63276326
if (locator->isForContextualType()) {
6328-
message = diag::cannot_convert_initializer_value;
6327+
return diag::cannot_convert_initializer_value;
63296328
} else if (locator->isForAssignment()) {
6330-
message = diag::cannot_convert_assign;
6329+
return diag::cannot_convert_assign;
63316330
} else if (locator->isLastElement<LocatorPathElt::ApplyArgToParam>()) {
6332-
message = diag::cannot_convert_argument_value;
6331+
return diag::cannot_convert_argument_value;
63336332
}
63346333

6334+
return None;
6335+
}
6336+
6337+
bool AbstractRawRepresentableFailure::diagnoseAsError() {
6338+
auto message = getDiagnostic();
63356339
if (!message)
63366340
return false;
63376341

6338-
auto diagnostic = emitDiagnostic(*message, ValueType, RawReprType);
6342+
auto diagnostic = emitDiagnostic(*message, getFromType(), getToType());
63396343
fixIt(diagnostic);
63406344
return true;
63416345
}

lib/Sema/CSDiagnostics.h

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,6 +2102,30 @@ class UnableToInferKeyPathRootFailure final : public FailureDiagnostic {
21022102
bool diagnoseAsError() override;
21032103
};
21042104

2105+
class AbstractRawRepresentableFailure : public FailureDiagnostic {
2106+
protected:
2107+
Type RawReprType;
2108+
Type ValueType;
2109+
2110+
AbstractRawRepresentableFailure(const Solution &solution, Type rawReprType,
2111+
Type valueType, ConstraintLocator *locator)
2112+
: FailureDiagnostic(solution, locator),
2113+
RawReprType(resolveType(rawReprType)),
2114+
ValueType(resolveType(valueType)) {}
2115+
2116+
public:
2117+
virtual Type getFromType() const = 0;
2118+
virtual Type getToType() const = 0;
2119+
2120+
bool diagnoseAsError() override;
2121+
bool diagnoseAsNote() override { return false; }
2122+
2123+
protected:
2124+
Optional<Diag<Type, Type>> getDiagnostic() const;
2125+
2126+
virtual void fixIt(InFlightDiagnostic &diagnostic) const = 0;
2127+
};
2128+
21052129
/// Diagnose an attempt to initialize raw representable type or convert to it
21062130
/// a value of some other type that matches its `RawValue` type.
21072131
///
@@ -2115,23 +2139,22 @@ class UnableToInferKeyPathRootFailure final : public FailureDiagnostic {
21152139
///
21162140
/// `0` has to be wrapped into `E(rawValue: 0)` and either defaulted via `??` or
21172141
/// force unwrapped to constitute a valid binding.
2118-
class MissingRawRepresentativeInitFailure final : public FailureDiagnostic {
2119-
Type RawReprType;
2120-
Type ValueType;
2121-
2142+
class MissingRawRepresentativeInitFailure final
2143+
: public AbstractRawRepresentableFailure {
21222144
public:
21232145
MissingRawRepresentativeInitFailure(const Solution &solution,
21242146
Type rawReprType, Type valueType,
21252147
ConstraintLocator *locator)
2126-
: FailureDiagnostic(solution, locator),
2127-
RawReprType(resolveType(rawReprType)),
2128-
ValueType(resolveType(valueType)) {}
2148+
: AbstractRawRepresentableFailure(solution, rawReprType, valueType,
2149+
locator) {}
2150+
2151+
Type getFromType() const override { return ValueType; }
2152+
Type getToType() const override { return RawReprType; }
21292153

2130-
bool diagnoseAsError() override;
21312154
bool diagnoseAsNote() override;
21322155

2133-
private:
2134-
void fixIt(InFlightDiagnostic &diagnostic) const;
2156+
protected:
2157+
void fixIt(InFlightDiagnostic &diagnostic) const override;
21352158
};
21362159

21372160
} // end namespace constraints

0 commit comments

Comments
 (0)