@@ -2032,6 +2032,9 @@ bool ContextualFailure::diagnoseAsError() {
2032
2032
return true ;
2033
2033
}
2034
2034
2035
+ auto fromType = getFromType ();
2036
+ auto toType = getToType ();
2037
+
2035
2038
Diag<Type, Type> diagnostic;
2036
2039
switch (path.back ().getKind ()) {
2037
2040
case ConstraintLocator::ClosureResult: {
@@ -2040,9 +2043,8 @@ bool ContextualFailure::diagnoseAsError() {
2040
2043
closure->getExplicitResultTypeLoc ().getTypeRepr ()) {
2041
2044
auto resultRepr = closure->getExplicitResultTypeLoc ().getTypeRepr ();
2042
2045
emitDiagnostic (resultRepr->getStartLoc (),
2043
- diag::incorrect_explicit_closure_result, getFromType (),
2044
- getToType ())
2045
- .fixItReplace (resultRepr->getSourceRange (), getToType ().getString ());
2046
+ diag::incorrect_explicit_closure_result, fromType, toType)
2047
+ .fixItReplace (resultRepr->getSourceRange (), toType.getString ());
2046
2048
return true ;
2047
2049
}
2048
2050
@@ -2071,22 +2073,21 @@ bool ContextualFailure::diagnoseAsError() {
2071
2073
return true ;
2072
2074
2073
2075
if (CTP == CTP_ForEachStmt) {
2074
- if (FromType ->isAnyExistentialType ()) {
2076
+ if (fromType ->isAnyExistentialType ()) {
2075
2077
emitDiagnostic (anchor->getLoc (), diag::type_cannot_conform,
2076
- /* isExistentialType=*/ true , FromType, ToType );
2078
+ /* isExistentialType=*/ true , fromType, toType );
2077
2079
return true ;
2078
2080
}
2079
2081
2080
2082
emitDiagnostic (
2081
2083
anchor->getLoc (),
2082
2084
diag::foreach_sequence_does_not_conform_to_expected_protocol,
2083
- FromType, ToType , bool (FromType ->getOptionalObjectType ()))
2085
+ fromType, toType , bool (fromType ->getOptionalObjectType ()))
2084
2086
.highlight (anchor->getSourceRange ());
2085
2087
return true ;
2086
2088
}
2087
2089
2088
- auto contextualType = getToType ();
2089
- if (auto msg = getDiagnosticFor (CTP, contextualType->isExistentialType ())) {
2090
+ if (auto msg = getDiagnosticFor (CTP, toType->isExistentialType ())) {
2090
2091
diagnostic = *msg;
2091
2092
break ;
2092
2093
}
@@ -2102,11 +2103,11 @@ bool ContextualFailure::diagnoseAsError() {
2102
2103
return false ;
2103
2104
2104
2105
auto *choice = overload->choice .getDecl ();
2105
- auto fnType = FromType ->getAs <FunctionType>();
2106
+ auto fnType = fromType ->getAs <FunctionType>();
2106
2107
if (!fnType) {
2107
2108
emitDiagnostic (anchor->getLoc (),
2108
2109
diag::expected_result_in_contextual_member,
2109
- choice->getFullName (), FromType, ToType );
2110
+ choice->getFullName (), fromType, toType );
2110
2111
return true ;
2111
2112
}
2112
2113
@@ -2151,7 +2152,8 @@ bool ContextualFailure::diagnoseAsError() {
2151
2152
return false ;
2152
2153
}
2153
2154
2154
- auto diag = emitDiagnostic (anchor->getLoc (), diagnostic, FromType, ToType);
2155
+ auto diag =
2156
+ emitDiagnostic (anchor->getLoc (), diagnostic, fromType, toType);
2155
2157
diag.highlight (anchor->getSourceRange ());
2156
2158
2157
2159
(void )tryFixIts (diag);
@@ -2357,12 +2359,13 @@ bool ContextualFailure::diagnoseMissingFunctionCall() const {
2357
2359
if (getLocator ()->isLastElement <LocatorPathElt::RValueAdjustment>())
2358
2360
return false ;
2359
2361
2360
- auto *srcFT = FromType ->getAs <FunctionType>();
2362
+ auto *srcFT = getFromType () ->getAs <FunctionType>();
2361
2363
if (!srcFT || !srcFT->getParams ().empty ())
2362
2364
return false ;
2363
2365
2364
- if (ToType->is <AnyFunctionType>() ||
2365
- !TypeChecker::isConvertibleTo (srcFT->getResult (), ToType, getDC ()))
2366
+ auto toType = getToType ();
2367
+ if (toType->is <AnyFunctionType>() ||
2368
+ !TypeChecker::isConvertibleTo (srcFT->getResult (), toType, getDC ()))
2366
2369
return false ;
2367
2370
2368
2371
auto *anchor = getAnchor ();
@@ -2663,7 +2666,10 @@ bool ContextualFailure::tryRawRepresentableFixIts(
2663
2666
2664
2667
bool ContextualFailure::tryIntegerCastFixIts (
2665
2668
InFlightDiagnostic &diagnostic) const {
2666
- if (!isIntegerType (FromType) || !isIntegerType (ToType))
2669
+ auto fromType = getFromType ();
2670
+ auto toType = getToType ();
2671
+
2672
+ if (!isIntegerType (fromType) || !isIntegerType (toType))
2667
2673
return false ;
2668
2674
2669
2675
auto getInnerCastedExpr = [&](Expr *expr) -> Expr * {
@@ -2684,7 +2690,7 @@ bool ContextualFailure::tryIntegerCastFixIts(
2684
2690
auto *anchor = getAnchor ();
2685
2691
if (Expr *innerE = getInnerCastedExpr (anchor)) {
2686
2692
Type innerTy = getType (innerE);
2687
- if (TypeChecker::isConvertibleTo (innerTy, ToType , getDC ())) {
2693
+ if (TypeChecker::isConvertibleTo (innerTy, toType , getDC ())) {
2688
2694
// Remove the unnecessary cast.
2689
2695
diagnostic.fixItRemoveChars (anchor->getLoc (), innerE->getStartLoc ())
2690
2696
.fixItRemove (anchor->getEndLoc ());
@@ -2693,7 +2699,7 @@ bool ContextualFailure::tryIntegerCastFixIts(
2693
2699
}
2694
2700
2695
2701
// Add a wrapping integer cast.
2696
- std::string convWrapBefore = ToType .getString ();
2702
+ std::string convWrapBefore = toType .getString ();
2697
2703
convWrapBefore += " (" ;
2698
2704
std::string convWrapAfter = " )" ;
2699
2705
SourceRange exprRange = anchor->getSourceRange ();
@@ -2715,8 +2721,8 @@ bool ContextualFailure::trySequenceSubsequenceFixIts(
2715
2721
2716
2722
// Substring -> String conversion
2717
2723
// Wrap in String.init
2718
- if (FromType ->isEqual (Substring)) {
2719
- if (ToType ->isEqual (String)) {
2724
+ if (getFromType () ->isEqual (Substring)) {
2725
+ if (getToType () ->isEqual (String)) {
2720
2726
auto *anchor = getAnchor ()->getSemanticsProvidingExpr ();
2721
2727
auto range = anchor->getSourceRange ();
2722
2728
diagnostic.fixItInsert (range.Start , " String(" );
@@ -2775,10 +2781,11 @@ bool ContextualFailure::tryProtocolConformanceFixIt(
2775
2781
if (!nominal)
2776
2782
return false ;
2777
2783
2784
+ auto fromType = getFromType ();
2778
2785
// We need to get rid of optionals and parens as it's not relevant when
2779
2786
// printing the diagnostic and the fix-it.
2780
2787
auto unwrappedToType =
2781
- ToType ->lookThroughAllOptionalTypes ()->getWithoutParens ();
2788
+ getToType () ->lookThroughAllOptionalTypes ()->getWithoutParens ();
2782
2789
2783
2790
// If the protocol requires a class & we don't have one (maybe the context
2784
2791
// is a struct), then bail out instead of offering a broken fix-it later on.
@@ -2789,13 +2796,13 @@ bool ContextualFailure::tryProtocolConformanceFixIt(
2789
2796
requiresClass = layout.requiresClass ();
2790
2797
}
2791
2798
2792
- if (requiresClass && !FromType ->is <ClassType>()) {
2799
+ if (requiresClass && !fromType ->is <ClassType>()) {
2793
2800
return false ;
2794
2801
}
2795
2802
2796
2803
// We can only offer a fix-it if we're assigning to a protocol type and
2797
2804
// the type we're assigning is the same as the innermost type context.
2798
- bool shouldOfferFixIt = nominal->getSelfTypeInContext ()->isEqual (FromType ) &&
2805
+ bool shouldOfferFixIt = nominal->getSelfTypeInContext ()->isEqual (fromType ) &&
2799
2806
unwrappedToType->isExistentialType ();
2800
2807
if (!shouldOfferFixIt)
2801
2808
return false ;
@@ -2805,7 +2812,7 @@ bool ContextualFailure::tryProtocolConformanceFixIt(
2805
2812
// Let's build a list of protocols that the context does not conform to.
2806
2813
SmallVector<std::string, 8 > missingProtoTypeStrings;
2807
2814
for (auto protocol : layout.getProtocols ()) {
2808
- if (!TypeChecker::conformsToProtocol (FromType , protocol->getDecl (), getDC (),
2815
+ if (!TypeChecker::conformsToProtocol (fromType , protocol->getDecl (), getDC (),
2809
2816
ConformanceCheckFlags::InExpression)) {
2810
2817
missingProtoTypeStrings.push_back (protocol->getString ());
2811
2818
}
@@ -2833,7 +2840,7 @@ bool ContextualFailure::tryProtocolConformanceFixIt(
2833
2840
// TODO: Maybe also insert the requirement stubs?
2834
2841
auto conformanceDiag = emitDiagnostic (
2835
2842
getAnchor ()->getLoc (), diag::assign_protocol_conformance_fix_it,
2836
- unwrappedToType, nominal->getDescriptiveKind (), FromType );
2843
+ unwrappedToType, nominal->getDescriptiveKind (), fromType );
2837
2844
if (nominal->getInherited ().size () > 0 ) {
2838
2845
auto lastInherited = nominal->getInherited ().back ().getLoc ();
2839
2846
auto lastInheritedEndLoc =
0 commit comments