Skip to content

Commit 7034fb3

Browse files
committed
[Diagnostic] Add a note to missing explicit raw representative init diagnostic
1 parent 87a0079 commit 7034fb3

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6407,7 +6407,12 @@ bool MissingRawRepresentativeInitFailure::diagnoseAsError() {
64076407
return false;
64086408

64096409
auto diagnostic = emitDiagnostic(*message, ValueType, RawReprType);
6410+
fixIt(diagnostic);
6411+
return true;
6412+
}
64106413

6414+
void MissingRawRepresentativeInitFailure::fixIt(
6415+
InFlightDiagnostic &diagnostic) const {
64116416
if (auto *E = getAsExpr(getAnchor())) {
64126417
auto range = E->getSourceRange();
64136418
auto rawReprObjType = RawReprType->getOptionalObjectType();
@@ -6455,6 +6460,33 @@ bool MissingRawRepresentativeInitFailure::diagnoseAsError() {
64556460
.fixItInsertAfter(range.End, ") ?? <#default value#>");
64566461
}
64576462
}
6463+
}
64586464

6459-
return true;
6465+
bool MissingRawRepresentativeInitFailure::diagnoseAsNote() {
6466+
auto *locator = getLocator();
6467+
6468+
Optional<InFlightDiagnostic> diagnostic;
6469+
if (locator->isForContextualType()) {
6470+
auto overload = getCalleeOverloadChoiceIfAvailable(locator);
6471+
if (!overload)
6472+
return false;
6473+
6474+
if (auto *decl = overload->choice.getDeclOrNull()) {
6475+
diagnostic.emplace(emitDiagnosticAt(
6476+
decl, diag::cannot_convert_candidate_result_to_contextual_type,
6477+
decl->getName(), ValueType, RawReprType));
6478+
}
6479+
} else if (auto argConv =
6480+
locator->getLastElementAs<LocatorPathElt::ApplyArgToParam>()) {
6481+
diagnostic.emplace(
6482+
emitDiagnostic(diag::candidate_has_invalid_argument_at_position,
6483+
RawReprType, argConv->getParamIdx(), /*inOut=*/false));
6484+
}
6485+
6486+
if (diagnostic) {
6487+
fixIt(*diagnostic);
6488+
return true;
6489+
}
6490+
6491+
return false;
64606492
}

lib/Sema/CSDiagnostics.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,6 +2128,10 @@ class MissingRawRepresentativeInitFailure final : public FailureDiagnostic {
21282128
ValueType(resolveType(valueType)) {}
21292129

21302130
bool diagnoseAsError() override;
2131+
bool diagnoseAsNote() override;
2132+
2133+
private:
2134+
void fixIt(InFlightDiagnostic &diagnostic) const;
21312135
};
21322136

21332137
} // end namespace constraints

0 commit comments

Comments
 (0)