@@ -423,6 +423,27 @@ class FailureDiagnosis :public ASTVisitor<FailureDiagnosis, /*exprresult*/bool>{
423
423
const CalleeCandidateInfo &candidates,
424
424
TCCOptions options = TCCOptions());
425
425
426
+ void getPossibleTypesOfExpressionWithoutApplying (
427
+ Expr *&expr, DeclContext *dc, SmallPtrSetImpl<TypeBase *> &types,
428
+ FreeTypeVariableBinding allowFreeTypeVariables =
429
+ FreeTypeVariableBinding::Disallow,
430
+ ExprTypeCheckListener *listener = nullptr ) {
431
+ CS.TC .getPossibleTypesOfExpressionWithoutApplying (
432
+ expr, dc, types, allowFreeTypeVariables, listener);
433
+ CS.cacheExprTypes (expr);
434
+ }
435
+
436
+ Type getTypeOfExpressionWithoutApplying (
437
+ Expr *&expr, DeclContext *dc, ConcreteDeclRef &referencedDecl,
438
+ FreeTypeVariableBinding allowFreeTypeVariables =
439
+ FreeTypeVariableBinding::Disallow,
440
+ ExprTypeCheckListener *listener = nullptr ) {
441
+ auto type = CS.TC .getTypeOfExpressionWithoutApplying (expr, dc, referencedDecl,
442
+ allowFreeTypeVariables, listener);
443
+ CS.cacheExprTypes (expr);
444
+ return type;
445
+ }
446
+
426
447
// / Diagnose common failures due to applications of an argument list to an
427
448
// / ApplyExpr or SubscriptExpr.
428
449
bool diagnoseParameterErrors (CalleeCandidateInfo &CCI,
@@ -452,6 +473,10 @@ class FailureDiagnosis :public ASTVisitor<FailureDiagnosis, /*exprresult*/bool>{
452
473
Type contextualType,
453
474
ContextualTypePurpose CTP);
454
475
476
+ bool diagnoseImplicitSelfErrors (Expr *fnExpr, Expr *argExpr,
477
+ CalleeCandidateInfo &CCI,
478
+ ArrayRef<Identifier> argLabels);
479
+
455
480
private:
456
481
// / Validate potential contextual type for type-checking one of the
457
482
// / sub-expressions, usually correct/valid types are the ones which
@@ -3194,10 +3219,9 @@ decomposeArgType(Type argType, ArrayRef<Identifier> argLabels) {
3194
3219
return result;
3195
3220
}
3196
3221
3197
- static bool diagnoseImplicitSelfErrors (Expr *fnExpr, Expr *argExpr,
3198
- CalleeCandidateInfo &CCI,
3199
- ArrayRef<Identifier> argLabels,
3200
- ConstraintSystem &CS) {
3222
+ bool FailureDiagnosis::diagnoseImplicitSelfErrors (
3223
+ Expr *fnExpr, Expr *argExpr, CalleeCandidateInfo &CCI,
3224
+ ArrayRef<Identifier> argLabels) {
3201
3225
// If candidate list is empty it means that problem is somewhere else,
3202
3226
// since we need to have candidates which might be shadowing other funcs.
3203
3227
if (CCI.empty () || !CCI[0 ].getDecl ())
@@ -3251,8 +3275,7 @@ static bool diagnoseImplicitSelfErrors(Expr *fnExpr, Expr *argExpr,
3251
3275
for (unsigned i = 0 , e = argTuple->getNumElements (); i < e; ++i) {
3252
3276
ConcreteDeclRef ref = nullptr ;
3253
3277
auto *el = argTuple->getElement (i);
3254
- auto typeResult =
3255
- TC.getTypeOfExpressionWithoutApplying (el, CS.DC , ref);
3278
+ auto typeResult = getTypeOfExpressionWithoutApplying (el, CS.DC , ref);
3256
3279
if (!typeResult)
3257
3280
return false ;
3258
3281
auto flags = ParameterTypeFlags ().withInOut (typeResult->is <InOutType>());
@@ -4226,7 +4249,7 @@ bool FailureDiagnosis::diagnoseParameterErrors(CalleeCandidateInfo &CCI,
4226
4249
}
4227
4250
4228
4251
// Try to diagnose errors related to the use of implicit self reference.
4229
- if (diagnoseImplicitSelfErrors (fnExpr, argExpr, CCI, argLabels, CS ))
4252
+ if (diagnoseImplicitSelfErrors (fnExpr, argExpr, CCI, argLabels))
4230
4253
return true ;
4231
4254
4232
4255
if (diagnoseInstanceMethodAsCurriedMemberOnType (CCI, fnExpr, argExpr))
@@ -4368,7 +4391,7 @@ bool FailureDiagnosis::diagnoseSubscriptErrors(SubscriptExpr *SE,
4368
4391
ConcreteDeclRef decl = nullptr ;
4369
4392
message = diag::cannot_subscript_with_index;
4370
4393
4371
- if (CS. TC . getTypeOfExpressionWithoutApplying (expr, CS.DC , decl))
4394
+ if (getTypeOfExpressionWithoutApplying (expr, CS.DC , decl))
4372
4395
return false ;
4373
4396
4374
4397
// If we are down to a single candidate but with an unresolved
@@ -4932,7 +4955,7 @@ bool FailureDiagnosis::diagnoseTrailingClosureErrors(ApplyExpr *callExpr) {
4932
4955
if (currentType->hasTypeVariable () || currentType->hasUnresolvedType ()) {
4933
4956
auto contextualType = CS.getContextualType ();
4934
4957
CallResultListener listener (contextualType);
4935
- CS. TC . getPossibleTypesOfExpressionWithoutApplying (
4958
+ getPossibleTypesOfExpressionWithoutApplying (
4936
4959
fnExpr, CS.DC , possibleTypes, FreeTypeVariableBinding::UnresolvedType,
4937
4960
&listener);
4938
4961
@@ -5037,9 +5060,9 @@ bool FailureDiagnosis::diagnoseCallContextualConversionErrors(
5037
5060
auto &TC = CS.TC ;
5038
5061
auto *DC = CS.DC ;
5039
5062
5040
- auto typeCheckExpr = [](TypeChecker &TC, Expr *expr, DeclContext *DC,
5041
- SmallPtrSetImpl<TypeBase *> &types) {
5042
- TC. getPossibleTypesOfExpressionWithoutApplying (
5063
+ auto typeCheckExpr = [& ](TypeChecker &TC, Expr *expr, DeclContext *DC,
5064
+ SmallPtrSetImpl<TypeBase *> &types) {
5065
+ getPossibleTypesOfExpressionWithoutApplying (
5043
5066
expr, DC, types, FreeTypeVariableBinding::Disallow);
5044
5067
};
5045
5068
@@ -5165,14 +5188,14 @@ bool FailureDiagnosis::diagnoseSubscriptMisuse(ApplyExpr *callExpr) {
5165
5188
// unresolved result let's not re-typecheck the function expression,
5166
5189
// because it might produce unrelated diagnostics due to lack of
5167
5190
// contextual information.
5168
- static bool shouldTypeCheckFunctionExpr (TypeChecker &TC , DeclContext *DC,
5191
+ static bool shouldTypeCheckFunctionExpr (FailureDiagnosis &FD , DeclContext *DC,
5169
5192
Expr *fnExpr) {
5170
5193
if (!isa<UnresolvedDotExpr>(fnExpr))
5171
5194
return true ;
5172
5195
5173
5196
SmallPtrSet<TypeBase *, 4 > fnTypes;
5174
- TC .getPossibleTypesOfExpressionWithoutApplying (fnExpr, DC, fnTypes,
5175
- FreeTypeVariableBinding::UnresolvedType);
5197
+ FD .getPossibleTypesOfExpressionWithoutApplying (
5198
+ fnExpr, DC, fnTypes, FreeTypeVariableBinding::UnresolvedType);
5176
5199
5177
5200
if (fnTypes.size () == 1 ) {
5178
5201
// Some member types depend on the arguments to produce a result type,
@@ -5232,7 +5255,7 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
5232
5255
auto *fnExpr = callExpr->getFn ();
5233
5256
auto originalFnType = CS.getType (callExpr->getFn ());
5234
5257
5235
- if (shouldTypeCheckFunctionExpr (CS. TC , CS.DC , fnExpr)) {
5258
+ if (shouldTypeCheckFunctionExpr (* this , CS.DC , fnExpr)) {
5236
5259
5237
5260
// If we are misusing a subscript, diagnose that and provide a fixit.
5238
5261
// We diagnose this here to have enough context to offer an appropriate fixit.
@@ -5269,7 +5292,7 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
5269
5292
fnExpr = callExpr->getFn ();
5270
5293
5271
5294
SmallPtrSet<TypeBase *, 4 > types;
5272
- CS. TC . getPossibleTypesOfExpressionWithoutApplying (fnExpr, CS.DC , types);
5295
+ getPossibleTypesOfExpressionWithoutApplying (fnExpr, CS.DC , types);
5273
5296
5274
5297
auto isFunctionType = [getFuncType](Type type) -> bool {
5275
5298
return type && getFuncType (type)->is <AnyFunctionType>();
@@ -5309,7 +5332,7 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
5309
5332
" unexpected declaration reference" );
5310
5333
5311
5334
ConcreteDeclRef decl = nullptr ;
5312
- Type type = CS. TC . getTypeOfExpressionWithoutApplying (
5335
+ Type type = getTypeOfExpressionWithoutApplying (
5313
5336
fnExpr, CS.DC , decl, FreeTypeVariableBinding::UnresolvedType,
5314
5337
&listener);
5315
5338
@@ -5893,7 +5916,7 @@ bool FailureDiagnosis::visitAssignExpr(AssignExpr *assignExpr) {
5893
5916
ExprTypeSaverAndEraser eraser (srcExpr);
5894
5917
5895
5918
ConcreteDeclRef ref = nullptr ;
5896
- auto type = CS. TC . getTypeOfExpressionWithoutApplying (srcExpr, CS.DC , ref);
5919
+ auto type = getTypeOfExpressionWithoutApplying (srcExpr, CS.DC , ref);
5897
5920
5898
5921
if (type && !type->isEqual (contextualType))
5899
5922
return diagnoseContextualConversionError (
@@ -6391,7 +6414,7 @@ bool FailureDiagnosis::diagnoseClosureExpr(
6391
6414
// diagnose situations where contextual type expected one result
6392
6415
// type but actual closure produces a different one without explicitly
6393
6416
// declaring it (e.g. by using anonymous parameters).
6394
- auto type = CS. TC . getTypeOfExpressionWithoutApplying (
6417
+ auto type = getTypeOfExpressionWithoutApplying (
6395
6418
closure, CS.DC , decl, FreeTypeVariableBinding::Disallow);
6396
6419
6397
6420
if (type && resultTypeProcessor (type, expectedResultType))
@@ -6900,7 +6923,7 @@ bool FailureDiagnosis::visitKeyPathExpr(KeyPathExpr *KPE) {
6900
6923
KeyPathListener listener (klass, parentType, rootType);
6901
6924
ConcreteDeclRef concreteDecl;
6902
6925
6903
- auto derivedType = CS. TC . getTypeOfExpressionWithoutApplying (
6926
+ auto derivedType = getTypeOfExpressionWithoutApplying (
6904
6927
expr, CS.DC , concreteDecl, FreeTypeVariableBinding::Disallow,
6905
6928
&listener);
6906
6929
@@ -8043,7 +8066,7 @@ diagnoseAmbiguousMultiStatementClosure(ClosureExpr *closure) {
8043
8066
// successfully type-checked its type cleanup is going to be disabled
8044
8067
// (we are allowing unresolved types), and as a side-effect it might
8045
8068
// also be transformed e.g. OverloadedDeclRefExpr -> DeclRefExpr.
8046
- auto type = CS. TC . getTypeOfExpressionWithoutApplying (
8069
+ auto type = getTypeOfExpressionWithoutApplying (
8047
8070
resultExpr, CS.DC , decl, FreeTypeVariableBinding::UnresolvedType);
8048
8071
if (type)
8049
8072
resultType = type;
0 commit comments