17
17
// ===----------------------------------------------------------------------===//
18
18
19
19
#include " ConstraintSystem.h"
20
+ #include " CSDiagnostics.h"
20
21
#include " MiscDiagnostics.h"
21
22
#include " TypeCheckProtocol.h"
22
23
#include " swift/AST/ASTVisitor.h"
@@ -1278,23 +1279,6 @@ namespace {
1278
1279
ConstraintLocatorBuilder locator);
1279
1280
1280
1281
private:
1281
- // / \brief Retrieve the overload choice associated with the given
1282
- // / locator.
1283
- SelectedOverload getOverloadChoice (ConstraintLocator *locator) {
1284
- return *getOverloadChoiceIfAvailable (locator);
1285
- }
1286
-
1287
- // / \brief Retrieve the overload choice associated with the given
1288
- // / locator.
1289
- Optional<SelectedOverload>
1290
- getOverloadChoiceIfAvailable (ConstraintLocator *locator) {
1291
- auto known = solution.overloadChoices .find (locator);
1292
- if (known != solution.overloadChoices .end ())
1293
- return known->second ;
1294
-
1295
- return None;
1296
- }
1297
-
1298
1282
// / \brief Simplify the given type by substituting all occurrences of
1299
1283
// / type variables for their fixed types.
1300
1284
Type simplifyType (Type type) {
@@ -1395,7 +1379,7 @@ namespace {
1395
1379
1396
1380
// Determine the declaration selected for this subscript operation.
1397
1381
if (!selected)
1398
- selected = getOverloadChoiceIfAvailable (
1382
+ selected = solution. getOverloadChoiceIfAvailable (
1399
1383
cs.getConstraintLocator (
1400
1384
locator.withPathElement (
1401
1385
ConstraintLocator::SubscriptMember)));
@@ -2505,7 +2489,7 @@ namespace {
2505
2489
auto locator = cs.getConstraintLocator (expr);
2506
2490
2507
2491
// Find the overload choice used for this declaration reference.
2508
- auto selected = getOverloadChoiceIfAvailable (locator);
2492
+ auto selected = solution. getOverloadChoiceIfAvailable (locator);
2509
2493
if (!selected.hasValue ()) {
2510
2494
auto *varDecl = cast<VarDecl>(expr->getDecl ());
2511
2495
assert (varDecl->getType ()->is <UnresolvedType>() &&
@@ -2544,7 +2528,7 @@ namespace {
2544
2528
Expr *visitOverloadedDeclRefExpr (OverloadedDeclRefExpr *expr) {
2545
2529
// Determine the declaration selected for this overloaded reference.
2546
2530
auto locator = cs.getConstraintLocator (expr);
2547
- auto selected = getOverloadChoice (locator);
2531
+ auto selected = solution. getOverloadChoice (locator);
2548
2532
auto choice = selected.choice ;
2549
2533
2550
2534
return buildDeclRef (choice, expr->getNameLoc (), selected.openedFullType ,
@@ -2567,7 +2551,7 @@ namespace {
2567
2551
Expr *visitMemberRefExpr (MemberRefExpr *expr) {
2568
2552
auto memberLocator = cs.getConstraintLocator (expr,
2569
2553
ConstraintLocator::Member);
2570
- auto selected = getOverloadChoice (memberLocator);
2554
+ auto selected = solution. getOverloadChoice (memberLocator);
2571
2555
bool isDynamic
2572
2556
= selected.choice .getKind () == OverloadChoiceKind::DeclViaDynamic;
2573
2557
return buildMemberRef (
@@ -2599,7 +2583,7 @@ namespace {
2599
2583
// Find the selected member.
2600
2584
auto memberLocator = cs.getConstraintLocator (
2601
2585
expr, ConstraintLocator::UnresolvedMember);
2602
- auto selected = getOverloadChoice (memberLocator);
2586
+ auto selected = solution. getOverloadChoice (memberLocator);
2603
2587
2604
2588
// If the member came by optional unwrapping, then unwrap the base type.
2605
2589
if (selected.choice .getKind ()
@@ -2740,7 +2724,7 @@ namespace {
2740
2724
auto ctorLocator = cs.getConstraintLocator (
2741
2725
expr,
2742
2726
ConstraintLocator::ConstructorMember);
2743
- if (auto selected = getOverloadChoiceIfAvailable (ctorLocator)) {
2727
+ if (auto selected = solution. getOverloadChoiceIfAvailable (ctorLocator)) {
2744
2728
auto choice = selected->choice ;
2745
2729
return applyCtorRefExpr (
2746
2730
expr, base, dotLoc, nameLoc, implicit, ctorLocator, choice,
@@ -2750,7 +2734,7 @@ namespace {
2750
2734
// Determine the declaration selected for this overloaded reference.
2751
2735
auto memberLocator = cs.getConstraintLocator (expr,
2752
2736
ConstraintLocator::Member);
2753
- auto selectedElt = getOverloadChoiceIfAvailable (memberLocator);
2737
+ auto selectedElt = solution. getOverloadChoiceIfAvailable (memberLocator);
2754
2738
2755
2739
if (!selectedElt) {
2756
2740
// If constraint solving resolved this to an UnresolvedType, then we're
@@ -4360,7 +4344,7 @@ namespace {
4360
4344
// If this is an unresolved link, make sure we resolved it.
4361
4345
if (kind == KeyPathExpr::Component::Kind::UnresolvedProperty ||
4362
4346
kind == KeyPathExpr::Component::Kind::UnresolvedSubscript) {
4363
- foundDecl = getOverloadChoiceIfAvailable (locator);
4347
+ foundDecl = solution. getOverloadChoiceIfAvailable (locator);
4364
4348
// Leave the component unresolved if the overload was not resolved.
4365
4349
if (foundDecl) {
4366
4350
// If this was a @dynamicMemberLookup property, then we actually
@@ -7589,7 +7573,7 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
7589
7573
auto ctorLocator = cs.getConstraintLocator (
7590
7574
locator.withPathElement (ConstraintLocator::ApplyFunction)
7591
7575
.withPathElement (ConstraintLocator::ConstructorMember));
7592
- auto selected = getOverloadChoiceIfAvailable (ctorLocator);
7576
+ auto selected = solution. getOverloadChoiceIfAvailable (ctorLocator);
7593
7577
if (!selected) {
7594
7578
assert (ty->hasError () || ty->hasUnresolvedType ());
7595
7579
cs.setType (apply, ty);
@@ -8124,99 +8108,18 @@ bool ConstraintSystem::applySolutionFix(
8124
8108
}
8125
8109
8126
8110
case FixKind::RelabelArguments: {
8127
- auto *call = cast<CallExpr>(locator->getAnchor ());
8128
- return diagnoseArgumentLabelError (getASTContext (), call->getArg (),
8129
- fix.first .getArgumentLabels (*this ),
8130
- isa<SubscriptExpr>(call->getFn ()));
8111
+ LabelingFailure failure (solution, fix.second ,
8112
+ fix.first .getArgumentLabels (*this ));
8113
+ return failure.diagnose ();
8131
8114
}
8132
8115
8133
8116
case FixKind::AddConformance: {
8134
- auto getMissingConformance = [&](ConstraintLocator *locator) {
8135
- auto *anchor = locator->getAnchor ();
8136
- auto &requirement = locator->getPath ().back ();
8137
- auto result = MissingConformances.find ({anchor, requirement.getValue ()});
8138
- assert (result != MissingConformances.end ());
8139
- return result->getSecond ();
8140
- };
8141
-
8142
- auto conformance = getMissingConformance (locator);
8143
-
8144
8117
auto *anchor = locator->getAnchor ();
8145
- auto owner = solution.simplifyType (getType (anchor))->getRValueInstanceType ();
8146
-
8147
- auto type = conformance.first ;
8148
- auto protocolType = conformance.second ->getDeclaredType ();
8149
-
8150
- // Find `ApplyExpr` based on a function expression attached to it.
8151
- auto findApplyExpr = [](Expr *parent, Expr *fnExpr) -> ApplyExpr * {
8152
- ApplyExpr *applyExpr = nullptr ;
8153
- parent->forEachChildExpr ([&applyExpr, &fnExpr](Expr *subExpr) -> Expr * {
8154
- auto *AE = dyn_cast<ApplyExpr>(subExpr);
8155
- if (!AE || AE->getFn () != fnExpr)
8156
- return subExpr;
8157
-
8158
- applyExpr = AE;
8159
- return nullptr ;
8160
- });
8161
- return applyExpr;
8162
- };
8163
-
8164
- auto getArgumentAt = [](ApplyExpr *AE, unsigned index) -> Expr * {
8165
- assert (AE);
8166
-
8167
- auto *arg = AE->getArg ();
8168
- if (auto *TE = dyn_cast<TupleExpr>(arg))
8169
- return TE->getElement (index);
8170
-
8171
- assert (index == 0 );
8172
- if (auto *PE = dyn_cast<ParenExpr>(arg))
8173
- return PE->getSubExpr ();
8174
-
8175
- return arg;
8176
- };
8177
-
8178
- auto *applyExpr = findApplyExpr (expr, anchor);
8179
-
8180
- Optional<unsigned > atParameterPos;
8181
- // Sometimes fix is recorded by type-checking sub-expression
8182
- // during normal diagnostics, in such case call expression
8183
- // is unavailable.
8184
- if (applyExpr) {
8185
- // If this is a static, initializer or operator call,
8186
- // let's not try to diagnose it here, but refer to expression
8187
- // diagnostics.
8188
- if (isa<BinaryExpr>(applyExpr) || isa<TypeExpr>(anchor))
8189
- return false ;
8190
-
8191
- if (auto *fnType = owner->getAs <AnyFunctionType>()) {
8192
- auto parameters = fnType->getParams ();
8193
- for (auto index : indices (parameters)) {
8194
- if (parameters[index].getType ()->isEqual (type)) {
8195
- atParameterPos = index;
8196
- break ;
8197
- }
8198
- }
8199
- }
8200
- }
8201
-
8202
- if (type->isExistentialType ()) {
8203
- auto diagnostic = diag::protocol_does_not_conform_objc;
8204
- if (type->isObjCExistentialType ())
8205
- diagnostic = diag::protocol_does_not_conform_static;
8206
-
8207
- TC.diagnose (anchor->getLoc (), diagnostic, type, protocolType);
8208
- } else if (atParameterPos) {
8209
- // Requirement comes from one of the parameter types,
8210
- // let's try to point diagnostic to the argument expression.
8211
- auto *argExpr = getArgumentAt (applyExpr, *atParameterPos);
8212
- TC.diagnose (argExpr->getLoc (),
8213
- diag::cannot_convert_argument_value_protocol, type,
8214
- protocolType);
8215
- } else {
8216
- TC.diagnose (anchor->getLoc (), diag::type_does_not_conform_owner, owner,
8217
- type, protocolType);
8218
- }
8219
- return true ;
8118
+ auto &reqLoc = locator->getPath ().back ();
8119
+ MissingConformanceFailure failure (
8120
+ expr, solution, fix.second ,
8121
+ MissingConformances[{anchor, reqLoc.getValue ()}]);
8122
+ return failure.diagnose ();
8220
8123
}
8221
8124
}
8222
8125
0 commit comments