@@ -1677,73 +1677,29 @@ bool FailureDiagnosis::diagnoseContextualConversionError(
1677
1677
return failure.diagnose ();
1678
1678
}
1679
1679
1680
- // Try to find the contextual type in a variety of ways. If the constraint
1681
- // system had a contextual type specified, we use it - it will have a purpose
1682
- // indicator which allows us to give a very "to the point" diagnostic .
1683
- Diag<Type> nilDiag;
1684
- std::function< void ( void )> nilFollowup ;
1680
+ // If we don't have a type for the expression, then we cannot use it in
1681
+ // conversion constraint diagnostic generation. If the types match, then it
1682
+ // must not be the contextual type that is the problem .
1683
+ if ( isUnresolvedOrTypeVarType (exprType) || exprType-> isEqual (contextualType))
1684
+ return false ;
1685
1685
1686
1686
// If this is conversion failure due to a return statement with an argument
1687
1687
// that cannot be coerced to the result type of the function, emit a
1688
1688
// specific error.
1689
- switch (CTP) {
1690
- case CTP_Unused:
1691
- case CTP_CannotFail:
1692
- llvm_unreachable (" These contextual type purposes cannot fail with a "
1693
- " conversion type specified!" );
1694
- case CTP_CalleeResult:
1695
- llvm_unreachable (" CTP_CalleeResult does not actually install a "
1696
- " contextual type" );
1697
- case CTP_Initialization:
1698
- nilDiag = diag::cannot_convert_initializer_value_nil;
1699
- nilFollowup = [this ] {
1700
- TypeRepr *patternTR = CS.getContextualTypeLoc ().getTypeRepr ();
1701
- if (!patternTR)
1702
- return ;
1703
- auto diag = diagnose (patternTR->getLoc (), diag::note_make_optional,
1704
- OptionalType::get (CS.getContextualType ()));
1705
- if (patternTR->isSimple ()) {
1706
- diag.fixItInsertAfter (patternTR->getEndLoc (), " ?" );
1707
- } else {
1708
- diag.fixItInsert (patternTR->getStartLoc (), " (" );
1709
- diag.fixItInsertAfter (patternTR->getEndLoc (), " )?" );
1710
- }
1711
- };
1712
- break ;
1713
- case CTP_ReturnSingleExpr:
1714
- case CTP_ReturnStmt:
1715
- // Special case the "conversion to void" case.
1716
- if (contextualType->isVoid ()) {
1717
- diagnose (expr->getLoc (), diag::cannot_return_value_from_void_func)
1718
- .highlight (expr->getSourceRange ());
1719
- return true ;
1720
- }
1721
-
1722
- nilDiag = diag::cannot_convert_to_return_type_nil;
1723
- break ;
1724
- case CTP_ThrowStmt: {
1725
- if (isa<NilLiteralExpr>(expr->getValueProvidingExpr ())) {
1726
- diagnose (expr->getLoc (), diag::cannot_throw_nil);
1727
- return true ;
1728
- }
1729
-
1730
- if (isUnresolvedOrTypeVarType (exprType) ||
1731
- exprType->isEqual (contextualType))
1732
- return false ;
1733
-
1689
+ if (CTP == CTP_ThrowStmt) {
1734
1690
// If we tried to throw the error code of an error type, suggest object
1735
1691
// construction.
1736
1692
auto &TC = CS.getTypeChecker ();
1737
1693
if (auto errorCodeProtocol =
1738
1694
TC.Context .getProtocol (KnownProtocolKind::ErrorCodeProtocol)) {
1739
- if (auto conformance =
1740
- TypeChecker::conformsToProtocol ( CS.getType (expr), errorCodeProtocol, CS.DC ,
1741
- ConformanceCheckFlags::InExpression)) {
1695
+ if (auto conformance = TypeChecker::conformsToProtocol (
1696
+ CS.getType (expr), errorCodeProtocol, CS.DC ,
1697
+ ConformanceCheckFlags::InExpression)) {
1742
1698
Type errorCodeType = CS.getType (expr);
1743
1699
Type errorType =
1744
- conformance-> getTypeWitnessByName (errorCodeType,
1745
- TC.Context .Id_ErrorType )
1746
- ->getCanonicalType ();
1700
+ conformance
1701
+ -> getTypeWitnessByName (errorCodeType, TC.Context .Id_ErrorType )
1702
+ ->getCanonicalType ();
1747
1703
if (errorType) {
1748
1704
auto diag = diagnose (expr->getLoc (), diag::cannot_throw_error_code,
1749
1705
errorCodeType, errorType);
@@ -1760,18 +1716,11 @@ bool FailureDiagnosis::diagnoseContextualConversionError(
1760
1716
// if this ever expands, this should be a specific form like () is for
1761
1717
// return.
1762
1718
diagnose (expr->getLoc (), diag::cannot_convert_thrown_type, exprType)
1763
- .highlight (expr->getSourceRange ());
1719
+ .highlight (expr->getSourceRange ());
1764
1720
return true ;
1765
1721
}
1766
1722
1767
- case CTP_EnumCaseRawValue:
1768
- nilDiag = diag::cannot_convert_raw_initializer_value_nil;
1769
- break ;
1770
- case CTP_DefaultParameter:
1771
- nilDiag = diag::cannot_convert_default_arg_value_nil;
1772
- break ;
1773
-
1774
- case CTP_YieldByReference:
1723
+ if (CTP == CTP_YieldByReference) {
1775
1724
if (auto contextualLV = contextualType->getAs <LValueType>())
1776
1725
contextualType = contextualLV->getObjectType ();
1777
1726
if (auto exprLV = exprType->getAs <LValueType>()) {
@@ -1781,58 +1730,10 @@ bool FailureDiagnosis::diagnoseContextualConversionError(
1781
1730
diagnose (expr->getLoc (), diag::cannot_yield_rvalue_by_reference_same_type,
1782
1731
exprType);
1783
1732
} else {
1784
- diagnose (expr->getLoc (), diag::cannot_yield_rvalue_by_reference,
1785
- exprType, contextualType);
1733
+ diagnose (expr->getLoc (), diag::cannot_yield_rvalue_by_reference, exprType,
1734
+ contextualType);
1786
1735
}
1787
1736
return true ;
1788
- case CTP_YieldByValue:
1789
- nilDiag = diag::cannot_convert_yield_value_nil;
1790
- break ;
1791
- case CTP_CallArgument:
1792
- nilDiag = diag::cannot_convert_argument_value_nil;
1793
- break ;
1794
- case CTP_ClosureResult:
1795
- nilDiag = diag::cannot_convert_closure_result_nil;
1796
- break ;
1797
- case CTP_ArrayElement:
1798
- nilDiag = diag::cannot_convert_array_element_nil;
1799
- break ;
1800
- case CTP_DictionaryKey:
1801
- nilDiag = diag::cannot_convert_dict_key_nil;
1802
- break ;
1803
- case CTP_DictionaryValue:
1804
- nilDiag = diag::cannot_convert_dict_value_nil;
1805
- break ;
1806
- case CTP_CoerceOperand:
1807
- nilDiag = diag::cannot_convert_coerce_nil;
1808
- break ;
1809
- case CTP_AssignSource:
1810
- nilDiag = diag::cannot_convert_assign_nil;
1811
- break ;
1812
- case CTP_SubscriptAssignSource:
1813
- nilDiag = diag::cannot_convert_subscript_assign_nil;
1814
- break ;
1815
- }
1816
-
1817
- // If we're diagnostic an issue with 'nil', produce a specific diagnostic,
1818
- // instead of uttering ExpressibleByNilLiteral.
1819
- if (isa<NilLiteralExpr>(expr->getValueProvidingExpr ())) {
1820
- // If the source type is some kind of optional, the contextual conversion
1821
- // to 'nil' didn't fail, something else did.
1822
- if (contextualType->getOptionalObjectType ())
1823
- return false ;
1824
- diagnose (expr->getLoc (), nilDiag, contextualType);
1825
- if (nilFollowup)
1826
- nilFollowup ();
1827
- return true ;
1828
- }
1829
-
1830
- // If we don't have a type for the expression, then we cannot use it in
1831
- // conversion constraint diagnostic generation. If the types match, then it
1832
- // must not be the contextual type that is the problem.
1833
- if (isUnresolvedOrTypeVarType (exprType) ||
1834
- exprType->isEqual (contextualType)) {
1835
- return false ;
1836
1737
}
1837
1738
1838
1739
exprType = exprType->getRValueType ();
0 commit comments