@@ -2058,6 +2058,40 @@ bool GenericRequirementsCheckListener::diagnoseUnsatisfiedRequirement(
2058
2058
return false ;
2059
2059
}
2060
2060
2061
+ // / Whether the contextual type provided for the given purpose is only a
2062
+ // / hint, and not a requirement.
2063
+ static bool contextualTypeIsOnlyAHint (ContextualTypePurpose ctp,
2064
+ TypeCheckExprOptions options) {
2065
+ switch (ctp) {
2066
+ case CTP_Initialization:
2067
+ return !options.contains (
2068
+ TypeCheckExprFlags::ConvertTypeIsOpaqueReturnType);
2069
+ case CTP_ForEachStmt:
2070
+ return true ;
2071
+ case CTP_Unused:
2072
+ case CTP_ReturnStmt:
2073
+ case CTP_ReturnSingleExpr:
2074
+ case CTP_YieldByValue:
2075
+ case CTP_YieldByReference:
2076
+ case CTP_ThrowStmt:
2077
+ case CTP_EnumCaseRawValue:
2078
+ case CTP_DefaultParameter:
2079
+ case CTP_AutoclosureDefaultParameter:
2080
+ case CTP_CalleeResult:
2081
+ case CTP_CallArgument:
2082
+ case CTP_ClosureResult:
2083
+ case CTP_ArrayElement:
2084
+ case CTP_DictionaryKey:
2085
+ case CTP_DictionaryValue:
2086
+ case CTP_CoerceOperand:
2087
+ case CTP_AssignSource:
2088
+ case CTP_SubscriptAssignSource:
2089
+ case CTP_Condition:
2090
+ case CTP_CannotFail:
2091
+ return false ;
2092
+ }
2093
+ }
2094
+
2061
2095
#pragma mark High-level entry points
2062
2096
Type TypeChecker::typeCheckExpression (Expr *&expr, DeclContext *dc,
2063
2097
TypeLoc convertType,
@@ -2128,7 +2162,7 @@ Type TypeChecker::typeCheckExpression(Expr *&expr, DeclContext *dc,
2128
2162
2129
2163
// If the convertType is *only* provided for that hint, then null it out so
2130
2164
// that we don't later treat it as an actual conversion constraint.
2131
- if (options. contains (TypeCheckExprFlags::ConvertTypeIsOnlyAHint ))
2165
+ if (contextualTypeIsOnlyAHint (convertTypePurpose, options ))
2132
2166
convertType = TypeLoc ();
2133
2167
2134
2168
// If the client can handle unresolved type variables, leave them in the
@@ -2655,7 +2689,7 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
2655
2689
2656
2690
TypeLoc contextualType;
2657
2691
auto contextualPurpose = CTP_Unused;
2658
- TypeCheckExprOptions flags = TypeCheckExprFlags::ConvertTypeIsOnlyAHint ;
2692
+ TypeCheckExprOptions flags = None ;
2659
2693
2660
2694
// Set the contextual purpose even if the pattern doesn't have a type so
2661
2695
// if there's an error we can use that information to inform diagnostics.
@@ -2674,7 +2708,6 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
2674
2708
// opaque type.
2675
2709
if (auto opaqueType = patternType->getAs <OpaqueTypeArchetypeType>()){
2676
2710
flags |= TypeCheckExprFlags::ConvertTypeIsOpaqueReturnType;
2677
- flags -= TypeCheckExprFlags::ConvertTypeIsOnlyAHint;
2678
2711
}
2679
2712
2680
2713
// Only provide a TypeLoc if it makes sense to allow diagnostics.
@@ -3042,7 +3075,7 @@ bool TypeChecker::typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt) {
3042
3075
// Type-check the for-each loop sequence and element pattern.
3043
3076
auto resultTy = TypeChecker::typeCheckExpression (
3044
3077
seq, dc, TypeLoc::withoutLoc (sequenceProto->getDeclaredType ()),
3045
- CTP_ForEachStmt, TypeCheckExprFlags::ConvertTypeIsOnlyAHint , &listener);
3078
+ CTP_ForEachStmt, None , &listener);
3046
3079
if (!resultTy)
3047
3080
return true ;
3048
3081
return false ;
0 commit comments