@@ -2001,6 +2001,40 @@ bool GenericRequirementsCheckListener::diagnoseUnsatisfiedRequirement(
2001
2001
return false ;
2002
2002
}
2003
2003
2004
+ // / Whether the contextual type provided for the given purpose is only a
2005
+ // / hint, and not a requirement.
2006
+ static bool contextualTypeIsOnlyAHint (ContextualTypePurpose ctp,
2007
+ TypeCheckExprOptions options) {
2008
+ switch (ctp) {
2009
+ case CTP_Initialization:
2010
+ return !options.contains (
2011
+ TypeCheckExprFlags::ConvertTypeIsOpaqueReturnType);
2012
+ case CTP_ForEachStmt:
2013
+ return true ;
2014
+ case CTP_Unused:
2015
+ case CTP_ReturnStmt:
2016
+ case CTP_ReturnSingleExpr:
2017
+ case CTP_YieldByValue:
2018
+ case CTP_YieldByReference:
2019
+ case CTP_ThrowStmt:
2020
+ case CTP_EnumCaseRawValue:
2021
+ case CTP_DefaultParameter:
2022
+ case CTP_AutoclosureDefaultParameter:
2023
+ case CTP_CalleeResult:
2024
+ case CTP_CallArgument:
2025
+ case CTP_ClosureResult:
2026
+ case CTP_ArrayElement:
2027
+ case CTP_DictionaryKey:
2028
+ case CTP_DictionaryValue:
2029
+ case CTP_CoerceOperand:
2030
+ case CTP_AssignSource:
2031
+ case CTP_SubscriptAssignSource:
2032
+ case CTP_Condition:
2033
+ case CTP_CannotFail:
2034
+ return false ;
2035
+ }
2036
+ }
2037
+
2004
2038
#pragma mark High-level entry points
2005
2039
Type TypeChecker::typeCheckExpression (Expr *&expr, DeclContext *dc,
2006
2040
TypeLoc convertType,
@@ -2071,7 +2105,7 @@ Type TypeChecker::typeCheckExpression(Expr *&expr, DeclContext *dc,
2071
2105
2072
2106
// If the convertType is *only* provided for that hint, then null it out so
2073
2107
// that we don't later treat it as an actual conversion constraint.
2074
- if (options. contains (TypeCheckExprFlags::ConvertTypeIsOnlyAHint ))
2108
+ if (contextualTypeIsOnlyAHint (convertTypePurpose, options ))
2075
2109
convertType = TypeLoc ();
2076
2110
2077
2111
// If the client can handle unresolved type variables, leave them in the
@@ -2092,7 +2126,7 @@ Type TypeChecker::typeCheckExpression(Expr *&expr, DeclContext *dc,
2092
2126
2093
2127
// Attempt to solve the constraint system.
2094
2128
SolutionApplicationTarget target (
2095
- expr, convertTo,
2129
+ expr, convertTypePurpose, convertTo,
2096
2130
options.contains (TypeCheckExprFlags::IsDiscarded));
2097
2131
auto viable = cs.solve (target, listener, allowFreeTypeVariables);
2098
2132
if (!viable)
@@ -2149,7 +2183,7 @@ Type TypeChecker::typeCheckExpression(Expr *&expr, DeclContext *dc,
2149
2183
// Unless the client has disabled them, perform syntactic checks on the
2150
2184
// expression now.
2151
2185
if (!cs.shouldSuppressDiagnostics () &&
2152
- !options.contains (TypeCheckExprFlags::DisableStructuralChecks )) {
2186
+ !options.contains (TypeCheckExprFlags::SubExpressionDiagnostics )) {
2153
2187
bool isExprStmt = options.contains (TypeCheckExprFlags::IsExprStmt);
2154
2188
performSyntacticExprDiagnostics (result, dc, isExprStmt);
2155
2189
}
@@ -2192,7 +2226,8 @@ getTypeOfExpressionWithoutApplying(Expr *&expr, DeclContext *dc,
2192
2226
// re-check.
2193
2227
if (needClearType)
2194
2228
expr->setType (Type ());
2195
- SolutionApplicationTarget target (expr, Type (), /* isDiscarded=*/ false );
2229
+ SolutionApplicationTarget target (
2230
+ expr, CTP_Unused, Type (), /* isDiscarded=*/ false );
2196
2231
auto viable = cs.solve (target, listener, allowFreeTypeVariables);
2197
2232
if (!viable) {
2198
2233
recoverOriginalType ();
@@ -2272,7 +2307,8 @@ void TypeChecker::getPossibleTypesOfExpressionWithoutApplying(
2272
2307
if (originalType && originalType->hasError ())
2273
2308
expr->setType (Type ());
2274
2309
2275
- SolutionApplicationTarget target (expr, Type (), /* isDiscarded=*/ false );
2310
+ SolutionApplicationTarget target (
2311
+ expr, CTP_Unused, Type (), /* isDiscarded=*/ false );
2276
2312
if (auto viable = cs.solve (target, listener, allowFreeTypeVariables)) {
2277
2313
expr = target.getAsExpr ();
2278
2314
for (auto &solution : *viable) {
@@ -2598,7 +2634,7 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
2598
2634
2599
2635
TypeLoc contextualType;
2600
2636
auto contextualPurpose = CTP_Unused;
2601
- TypeCheckExprOptions flags = TypeCheckExprFlags::ConvertTypeIsOnlyAHint ;
2637
+ TypeCheckExprOptions flags = None ;
2602
2638
2603
2639
// Set the contextual purpose even if the pattern doesn't have a type so
2604
2640
// if there's an error we can use that information to inform diagnostics.
@@ -2617,7 +2653,6 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
2617
2653
// opaque type.
2618
2654
if (auto opaqueType = patternType->getAs <OpaqueTypeArchetypeType>()){
2619
2655
flags |= TypeCheckExprFlags::ConvertTypeIsOpaqueReturnType;
2620
- flags -= TypeCheckExprFlags::ConvertTypeIsOnlyAHint;
2621
2656
}
2622
2657
2623
2658
// Only provide a TypeLoc if it makes sense to allow diagnostics.
@@ -2985,7 +3020,7 @@ bool TypeChecker::typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt) {
2985
3020
// Type-check the for-each loop sequence and element pattern.
2986
3021
auto resultTy = TypeChecker::typeCheckExpression (
2987
3022
seq, dc, TypeLoc::withoutLoc (sequenceProto->getDeclaredType ()),
2988
- CTP_ForEachStmt, TypeCheckExprFlags::ConvertTypeIsOnlyAHint , &listener);
3023
+ CTP_ForEachStmt, None , &listener);
2989
3024
if (!resultTy)
2990
3025
return true ;
2991
3026
return false ;
0 commit comments