Skip to content

Commit c0e09cb

Browse files
committed
[CSDiagnostics] Simplify MissingContextualConformanceFailure and avoid storing diagnostic
Carrying diagnostic through the method is becoming unmanagable and prune to issues due to "in-flight" diagnostic replacement.
1 parent 89c032c commit c0e09cb

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5774,6 +5774,12 @@ bool CollectionElementContextualFailure::diagnoseAsError() {
57745774
auto eltType = getFromType();
57755775
auto contextualType = getToType();
57765776

5777+
auto diagnoseSingleElement = [&](Diag<Type, Type> msg, Type eltType,
5778+
Type contextualType) {
5779+
auto diagnostic = emitDiagnostic(msg, eltType, contextualType);
5780+
(void)trySequenceSubsequenceFixIts(diagnostic);
5781+
};
5782+
57775783
auto diagnoseAllOccurrences = [&](Diag<Type, Type> diagnostic) {
57785784
assert(AffectedElements.size() > 1);
57795785
for (auto *element : AffectedElements) {
@@ -5790,16 +5796,16 @@ bool CollectionElementContextualFailure::diagnoseAsError() {
57905796
};
57915797

57925798
bool treatAsDictionary = false;
5793-
Optional<InFlightDiagnostic> diagnostic;
57945799
if (auto *AE = getAsExpr<ArrayExpr>(anchor)) {
57955800
if (!(treatAsDictionary = isFixedToDictionary(AE))) {
57965801
if (AffectedElements.size() > 1) {
57975802
diagnoseAllOccurrences(diag::cannot_convert_array_element);
57985803
return true;
57995804
}
58005805

5801-
diagnostic.emplace(emitDiagnostic(diag::cannot_convert_array_element,
5802-
eltType, contextualType));
5806+
diagnoseSingleElement(diag::cannot_convert_array_element, eltType,
5807+
contextualType);
5808+
return true;
58035809
}
58045810
}
58055811

@@ -5812,9 +5818,9 @@ bool CollectionElementContextualFailure::diagnoseAsError() {
58125818
return true;
58135819
}
58145820

5815-
diagnostic.emplace(emitDiagnostic(diag::cannot_convert_dict_key, eltType,
5816-
contextualType));
5817-
break;
5821+
diagnoseSingleElement(diag::cannot_convert_dict_key, eltType,
5822+
contextualType);
5823+
return true;
58185824
}
58195825

58205826
case 1: { // value
@@ -5823,9 +5829,9 @@ bool CollectionElementContextualFailure::diagnoseAsError() {
58235829
return true;
58245830
}
58255831

5826-
diagnostic.emplace(emitDiagnostic(diag::cannot_convert_dict_value,
5827-
eltType, contextualType));
5828-
break;
5832+
diagnoseSingleElement(diag::cannot_convert_dict_value, eltType,
5833+
contextualType);
5834+
return true;
58295835
}
58305836

58315837
default:
@@ -5841,25 +5847,22 @@ bool CollectionElementContextualFailure::diagnoseAsError() {
58415847
if ((purpose == ContextualTypePurpose::CTP_ForEachStmt ||
58425848
purpose == ContextualTypePurpose::CTP_ForEachSequence) &&
58435849
contextualType->hasUnresolvedType()) {
5844-
diagnostic.emplace(emitDiagnostic(
5850+
auto diagnostic = emitDiagnostic(
58455851
(contextualType->is<TupleType>() && !eltType->is<TupleType>())
58465852
? diag::cannot_match_expr_tuple_pattern_with_nontuple_value
58475853
: diag::cannot_match_unresolved_expr_pattern_with_value,
5848-
eltType));
5854+
eltType);
5855+
(void)trySequenceSubsequenceFixIts(diagnostic);
58495856
} else {
5850-
diagnostic.emplace(
5851-
emitDiagnostic(contextualType->isExistentialType()
5852-
? diag::cannot_convert_sequence_element_protocol
5853-
: diag::cannot_convert_sequence_element_value,
5854-
eltType, contextualType));
5857+
diagnoseSingleElement(contextualType->isExistentialType()
5858+
? diag::cannot_convert_sequence_element_protocol
5859+
: diag::cannot_convert_sequence_element_value,
5860+
eltType, contextualType);
58555861
}
5862+
return true;
58565863
}
58575864

5858-
if (!diagnostic)
5859-
return false;
5860-
5861-
(void)trySequenceSubsequenceFixIts(*diagnostic);
5862-
return true;
5865+
return false;
58635866
}
58645867

58655868
bool MissingContextualConformanceFailure::diagnoseAsError() {

0 commit comments

Comments
 (0)