@@ -5774,13 +5774,42 @@ bool CollectionElementContextualFailure::diagnoseAsError() {
5774
5774
auto eltType = getFromType ();
5775
5775
auto contextualType = getToType ();
5776
5776
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
+
5777
5783
auto diagnoseAllOccurrences = [&](Diag<Type, Type> diagnostic) {
5778
5784
assert (AffectedElements.size () > 1 );
5779
5785
for (auto *element : AffectedElements) {
5780
5786
emitDiagnosticAt (element->getLoc (), diagnostic, eltType, contextualType);
5781
5787
}
5782
5788
};
5783
5789
5790
+ if (locator->isForSequenceElementType ()) {
5791
+ auto purpose = FailureDiagnostic::getContextualTypePurpose (getAnchor ());
5792
+ // If this is a conversion failure related to binding of `for-each`
5793
+ // statement it has to be diagnosed as pattern match if there are
5794
+ // holes present in the contextual type.
5795
+ if ((purpose == ContextualTypePurpose::CTP_ForEachStmt ||
5796
+ purpose == ContextualTypePurpose::CTP_ForEachSequence) &&
5797
+ contextualType->hasUnresolvedType ()) {
5798
+ auto diagnostic = emitDiagnostic (
5799
+ (contextualType->is <TupleType>() && !eltType->is <TupleType>())
5800
+ ? diag::cannot_match_expr_tuple_pattern_with_nontuple_value
5801
+ : diag::cannot_match_unresolved_expr_pattern_with_value,
5802
+ eltType);
5803
+ (void )trySequenceSubsequenceFixIts (diagnostic);
5804
+ } else {
5805
+ diagnoseSingleElement (contextualType->isExistentialType ()
5806
+ ? diag::cannot_convert_sequence_element_protocol
5807
+ : diag::cannot_convert_sequence_element_value,
5808
+ eltType, contextualType);
5809
+ }
5810
+ return true ;
5811
+ }
5812
+
5784
5813
auto isFixedToDictionary = [&](ArrayExpr *anchor) {
5785
5814
return llvm::any_of (getSolution ().Fixes , [&](ConstraintFix *fix) {
5786
5815
auto *fixAnchor = getAsExpr<ArrayExpr>(fix->getAnchor ());
@@ -5790,16 +5819,16 @@ bool CollectionElementContextualFailure::diagnoseAsError() {
5790
5819
};
5791
5820
5792
5821
bool treatAsDictionary = false ;
5793
- Optional<InFlightDiagnostic> diagnostic;
5794
5822
if (auto *AE = getAsExpr<ArrayExpr>(anchor)) {
5795
5823
if (!(treatAsDictionary = isFixedToDictionary (AE))) {
5796
5824
if (AffectedElements.size () > 1 ) {
5797
5825
diagnoseAllOccurrences (diag::cannot_convert_array_element);
5798
5826
return true ;
5799
5827
}
5800
5828
5801
- diagnostic.emplace (emitDiagnostic (diag::cannot_convert_array_element,
5802
- eltType, contextualType));
5829
+ diagnoseSingleElement (diag::cannot_convert_array_element, eltType,
5830
+ contextualType);
5831
+ return true ;
5803
5832
}
5804
5833
}
5805
5834
@@ -5812,9 +5841,9 @@ bool CollectionElementContextualFailure::diagnoseAsError() {
5812
5841
return true ;
5813
5842
}
5814
5843
5815
- diagnostic. emplace ( emitDiagnostic (diag::cannot_convert_dict_key, eltType,
5816
- contextualType) );
5817
- break ;
5844
+ diagnoseSingleElement (diag::cannot_convert_dict_key, eltType,
5845
+ contextualType);
5846
+ return true ;
5818
5847
}
5819
5848
5820
5849
case 1 : { // value
@@ -5823,43 +5852,17 @@ bool CollectionElementContextualFailure::diagnoseAsError() {
5823
5852
return true ;
5824
5853
}
5825
5854
5826
- diagnostic. emplace ( emitDiagnostic ( diag::cannot_convert_dict_value,
5827
- eltType, contextualType) );
5828
- break ;
5855
+ diagnoseSingleElement ( diag::cannot_convert_dict_value, eltType ,
5856
+ contextualType);
5857
+ return true ;
5829
5858
}
5830
5859
5831
5860
default :
5832
5861
break ;
5833
5862
}
5834
5863
}
5835
5864
5836
- if (locator->isForSequenceElementType ()) {
5837
- auto purpose = FailureDiagnostic::getContextualTypePurpose (getAnchor ());
5838
- // If this is a conversion failure related to binding of `for-each`
5839
- // statement it has to be diagnosed as pattern match if there are
5840
- // holes present in the contextual type.
5841
- if ((purpose == ContextualTypePurpose::CTP_ForEachStmt ||
5842
- purpose == ContextualTypePurpose::CTP_ForEachSequence) &&
5843
- contextualType->hasUnresolvedType ()) {
5844
- diagnostic.emplace (emitDiagnostic (
5845
- (contextualType->is <TupleType>() && !eltType->is <TupleType>())
5846
- ? diag::cannot_match_expr_tuple_pattern_with_nontuple_value
5847
- : diag::cannot_match_unresolved_expr_pattern_with_value,
5848
- eltType));
5849
- } 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));
5855
- }
5856
- }
5857
-
5858
- if (!diagnostic)
5859
- return false ;
5860
-
5861
- (void )trySequenceSubsequenceFixIts (*diagnostic);
5862
- return true ;
5865
+ return false ;
5863
5866
}
5864
5867
5865
5868
bool MissingContextualConformanceFailure::diagnoseAsError () {
0 commit comments