@@ -544,10 +544,11 @@ namespace {
544
544
545
545
public:
546
546
// / \brief Build a reference to the given declaration.
547
- Expr *buildDeclRef (ValueDecl *decl , DeclNameLoc loc, Type openedType,
547
+ Expr *buildDeclRef (OverloadChoice choice , DeclNameLoc loc, Type openedType,
548
548
ConstraintLocatorBuilder locator, bool implicit,
549
549
FunctionRefKind functionRefKind,
550
550
AccessSemantics semantics) {
551
+ auto *decl = choice.getDecl ();
551
552
// Determine the declaration selected for this overloaded reference.
552
553
auto &ctx = cs.getASTContext ();
553
554
@@ -612,7 +613,7 @@ namespace {
612
613
if (auto fnConv = dyn_cast<FunctionConversionExpr>(refExpr))
613
614
refExpr = fnConv->getSubExpr ();
614
615
615
- return forceUnwrapIfExpected (refExpr, decl , locator);
616
+ return forceUnwrapIfExpected (refExpr, choice , locator);
616
617
}
617
618
}
618
619
}
@@ -623,7 +624,7 @@ namespace {
623
624
TypeExpr::createImplicitHack (loc.getBaseNameLoc (), baseTy, ctx);
624
625
cs.cacheExprTypes (base);
625
626
626
- return buildMemberRef (base, openedType, SourceLoc (), decl , loc,
627
+ return buildMemberRef (base, openedType, SourceLoc (), choice , loc,
627
628
openedFnType->getResult (), locator, locator,
628
629
implicit, functionRefKind, semantics,
629
630
/* isDynamic=*/ false );
@@ -659,7 +660,7 @@ namespace {
659
660
loc, implicit, semantics, type);
660
661
cs.cacheType (declRefExpr);
661
662
declRefExpr->setFunctionRefKind (functionRefKind);
662
- return forceUnwrapIfExpected (declRefExpr, decl , locator);
663
+ return forceUnwrapIfExpected (declRefExpr, choice , locator);
663
664
}
664
665
665
666
// / Describes an opened existential that has not yet been closed.
@@ -899,11 +900,12 @@ namespace {
899
900
900
901
// / \brief Build a new member reference with the given base and member.
901
902
Expr *buildMemberRef (Expr *base, Type openedFullType, SourceLoc dotLoc,
902
- ValueDecl *member , DeclNameLoc memberLoc,
903
+ OverloadChoice choice , DeclNameLoc memberLoc,
903
904
Type openedType, ConstraintLocatorBuilder locator,
904
905
ConstraintLocatorBuilder memberLocator, bool Implicit,
905
906
FunctionRefKind functionRefKind,
906
907
AccessSemantics semantics, bool isDynamic) {
908
+ ValueDecl *member = choice.getDecl ();
907
909
auto &tc = cs.getTypeChecker ();
908
910
auto &context = tc.Context ;
909
911
@@ -949,7 +951,7 @@ namespace {
949
951
ref->setFunctionRefKind (functionRefKind);
950
952
auto *DSBI = cs.cacheType (new (context) DotSyntaxBaseIgnoredExpr (
951
953
base, dotLoc, ref, cs.getType (ref)));
952
- return forceUnwrapIfExpected (DSBI, member , memberLocator);
954
+ return forceUnwrapIfExpected (DSBI, choice , memberLocator);
953
955
}
954
956
955
957
// The formal type of the 'self' value for the member's declaration.
@@ -1088,8 +1090,9 @@ namespace {
1088
1090
// We also need to handle the implicitly unwrap of the result
1089
1091
// of the called function if that's the type checking solution
1090
1092
// we ended up with.
1091
- return forceUnwrapIfExpected (ref, member, memberLocator,
1092
- member->getAttrs ().hasAttribute <OptionalAttr>());
1093
+ return forceUnwrapIfExpected (
1094
+ ref, choice, memberLocator,
1095
+ member->getAttrs ().hasAttribute <OptionalAttr>());
1093
1096
}
1094
1097
1095
1098
// For types and properties, build member references.
@@ -1115,7 +1118,7 @@ namespace {
1115
1118
cs.setType (memberRefExpr, simplifyType (openedType));
1116
1119
Expr *result = memberRefExpr;
1117
1120
closeExistential (result, locator);
1118
- return forceUnwrapIfExpected (result, member , memberLocator);
1121
+ return forceUnwrapIfExpected (result, choice , memberLocator);
1119
1122
}
1120
1123
1121
1124
// Handle all other references.
@@ -1135,7 +1138,7 @@ namespace {
1135
1138
ApplyExpr *apply;
1136
1139
if (isa<ConstructorDecl>(member)) {
1137
1140
// FIXME: Provide type annotation.
1138
- ref = forceUnwrapIfExpected (ref, member , memberLocator);
1141
+ ref = forceUnwrapIfExpected (ref, choice , memberLocator);
1139
1142
apply = new (context) ConstructorRefCallExpr (ref, base);
1140
1143
} else if (!baseIsInstance && member->isInstanceMember ()) {
1141
1144
// Reference to an unbound instance method.
@@ -1144,11 +1147,11 @@ namespace {
1144
1147
cs.getType (ref));
1145
1148
cs.cacheType (result);
1146
1149
closeExistential (result, locator, /* force=*/ openedExistential);
1147
- return forceUnwrapIfExpected (result, member , memberLocator);
1150
+ return forceUnwrapIfExpected (result, choice , memberLocator);
1148
1151
} else {
1149
1152
assert ((!baseIsInstance || member->isInstanceMember ()) &&
1150
1153
" can't call a static method on an instance" );
1151
- ref = forceUnwrapIfExpected (ref, member , memberLocator);
1154
+ ref = forceUnwrapIfExpected (ref, choice , memberLocator);
1152
1155
apply = new (context) DotSyntaxCallExpr (ref, dotLoc, base);
1153
1156
if (Implicit) {
1154
1157
apply->setImplicit ();
@@ -1449,10 +1452,10 @@ namespace {
1449
1452
if (selected->choice .getKind () ==
1450
1453
OverloadChoiceKind::DynamicMemberLookup)
1451
1454
locatorKind = ConstraintLocator::Member;
1452
-
1453
- newSubscript = forceUnwrapIfExpected (
1454
- newSubscript, selected->choice . getDecl () ,
1455
- locator.withPathElement (locatorKind));
1455
+
1456
+ newSubscript =
1457
+ forceUnwrapIfExpected ( newSubscript, selected->choice ,
1458
+ locator.withPathElement (locatorKind));
1456
1459
}
1457
1460
1458
1461
return newSubscript;
@@ -2471,8 +2474,9 @@ namespace {
2471
2474
}
2472
2475
}
2473
2476
2474
- bool shouldForceUnwrapResult (Decl *decl, ConstraintLocatorBuilder locator) {
2475
- if (!decl->getAttrs ().hasAttribute <ImplicitlyUnwrappedOptionalAttr>())
2477
+ bool shouldForceUnwrapResult (OverloadChoice choice,
2478
+ ConstraintLocatorBuilder locator) {
2479
+ if (!choice.isImplicitlyUnwrappedValueOrReturnValue ())
2476
2480
return false ;
2477
2481
2478
2482
auto *choiceLocator = cs.getConstraintLocator (locator.withPathElement (
@@ -2481,10 +2485,10 @@ namespace {
2481
2485
return solution.getDisjunctionChoice (choiceLocator);
2482
2486
}
2483
2487
2484
- Expr *forceUnwrapIfExpected (Expr *expr, Decl *decl ,
2488
+ Expr *forceUnwrapIfExpected (Expr *expr, OverloadChoice choice ,
2485
2489
ConstraintLocatorBuilder locator,
2486
- bool forForcedOptional =false ) {
2487
- if (!shouldForceUnwrapResult (decl , locator))
2490
+ bool forForcedOptional = false ) {
2491
+ if (!shouldForceUnwrapResult (choice , locator))
2488
2492
return expr;
2489
2493
2490
2494
// Force the expression if required for the solution.
@@ -2503,12 +2507,9 @@ namespace {
2503
2507
cs.setType (expr, varDecl->getType ());
2504
2508
return expr;
2505
2509
}
2506
-
2507
- auto choice = selected->choice ;
2508
- auto decl = choice.getDecl ();
2509
2510
2510
- return buildDeclRef (decl , expr->getNameLoc (), selected-> openedFullType ,
2511
- locator, expr->isImplicit (),
2511
+ return buildDeclRef (selected-> choice , expr->getNameLoc (),
2512
+ selected-> openedFullType , locator, expr->isImplicit (),
2512
2513
expr->getFunctionRefKind (),
2513
2514
expr->getAccessSemantics ());
2514
2515
}
@@ -2539,9 +2540,8 @@ namespace {
2539
2540
auto locator = cs.getConstraintLocator (expr);
2540
2541
auto selected = getOverloadChoice (locator);
2541
2542
auto choice = selected.choice ;
2542
- auto decl = choice.getDecl ();
2543
2543
2544
- return buildDeclRef (decl , expr->getNameLoc (), selected.openedFullType ,
2544
+ return buildDeclRef (choice , expr->getNameLoc (), selected.openedFullType ,
2545
2545
locator, expr->isImplicit (),
2546
2546
choice.getFunctionRefKind (),
2547
2547
AccessSemantics::Ordinary);
@@ -2566,7 +2566,7 @@ namespace {
2566
2566
= selected.choice .getKind () == OverloadChoiceKind::DeclViaDynamic;
2567
2567
return buildMemberRef (
2568
2568
expr->getBase (), selected.openedFullType , expr->getDotLoc (),
2569
- selected.choice . getDecl () , expr->getNameLoc (), selected.openedType ,
2569
+ selected.choice , expr->getNameLoc (), selected.openedType ,
2570
2570
cs.getConstraintLocator (expr), memberLocator, expr->isImplicit (),
2571
2571
selected.choice .getFunctionRefKind (), expr->getAccessSemantics (),
2572
2572
isDynamic);
@@ -2594,7 +2594,6 @@ namespace {
2594
2594
auto memberLocator = cs.getConstraintLocator (
2595
2595
expr, ConstraintLocator::UnresolvedMember);
2596
2596
auto selected = getOverloadChoice (memberLocator);
2597
- auto member = selected.choice .getDecl ();
2598
2597
2599
2598
// If the member came by optional unwrapping, then unwrap the base type.
2600
2599
if (selected.choice .getKind ()
@@ -2614,7 +2613,7 @@ namespace {
2614
2613
bool isDynamic
2615
2614
= selected.choice .getKind () == OverloadChoiceKind::DeclViaDynamic;
2616
2615
auto result = buildMemberRef (
2617
- base, selected.openedFullType , expr->getDotLoc (), member ,
2616
+ base, selected.openedFullType , expr->getDotLoc (), selected. choice ,
2618
2617
expr->getNameLoc (), selected.openedType ,
2619
2618
cs.getConstraintLocator (expr), memberLocator, expr->isImplicit (),
2620
2619
selected.choice .getFunctionRefKind (), AccessSemantics::Ordinary,
@@ -2651,9 +2650,10 @@ namespace {
2651
2650
Expr *applyCtorRefExpr (Expr *expr, Expr *base, SourceLoc dotLoc,
2652
2651
DeclNameLoc nameLoc, bool implicit,
2653
2652
ConstraintLocator *ctorLocator,
2654
- ConstructorDecl *ctor,
2655
- FunctionRefKind functionRefKind,
2656
- Type openedType) {
2653
+ OverloadChoice choice,
2654
+ FunctionRefKind functionRefKind, Type openedType) {
2655
+
2656
+ auto *ctor = cast<ConstructorDecl>(choice.getDecl ());
2657
2657
2658
2658
// If the member is a constructor, verify that it can be legally
2659
2659
// referenced from this base.
@@ -2665,7 +2665,7 @@ namespace {
2665
2665
// constructor.
2666
2666
if (cs.getType (base)->is <AnyMetatypeType>()) {
2667
2667
return buildMemberRef (
2668
- base, openedType, dotLoc, ctor , nameLoc, cs.getType (expr),
2668
+ base, openedType, dotLoc, choice , nameLoc, cs.getType (expr),
2669
2669
ConstraintLocatorBuilder (cs.getConstraintLocator (expr)),
2670
2670
ctorLocator, implicit, functionRefKind, AccessSemantics::Ordinary,
2671
2671
/* isDynamic=*/ false );
@@ -2731,10 +2731,9 @@ namespace {
2731
2731
ConstraintLocator::ConstructorMember);
2732
2732
if (auto selected = getOverloadChoiceIfAvailable (ctorLocator)) {
2733
2733
auto choice = selected->choice ;
2734
- auto *ctor = cast<ConstructorDecl>(choice.getDecl ());
2735
- return applyCtorRefExpr (expr, base, dotLoc, nameLoc, implicit,
2736
- ctorLocator, ctor, choice.getFunctionRefKind (),
2737
- selected->openedFullType );
2734
+ return applyCtorRefExpr (
2735
+ expr, base, dotLoc, nameLoc, implicit, ctorLocator, choice,
2736
+ choice.getFunctionRefKind (), selected->openedFullType );
2738
2737
}
2739
2738
2740
2739
// Determine the declaration selected for this overloaded reference.
@@ -2783,11 +2782,11 @@ namespace {
2783
2782
case OverloadChoiceKind::DeclViaDynamic: {
2784
2783
bool isDynamic
2785
2784
= selected.choice .getKind () == OverloadChoiceKind::DeclViaDynamic;
2786
- return buildMemberRef (
2787
- base, selected.openedFullType , dotLoc , selected.choice . getDecl () ,
2788
- nameLoc, selected. openedType , cs.getConstraintLocator (expr),
2789
- memberLocator, implicit, selected.choice .getFunctionRefKind (),
2790
- AccessSemantics::Ordinary, isDynamic);
2785
+ return buildMemberRef (base, selected. openedFullType , dotLoc,
2786
+ selected.choice , nameLoc , selected.openedType ,
2787
+ cs.getConstraintLocator (expr), memberLocator ,
2788
+ implicit, selected.choice .getFunctionRefKind (),
2789
+ AccessSemantics::Ordinary, isDynamic);
2791
2790
}
2792
2791
2793
2792
case OverloadChoiceKind::TupleIndex: {
@@ -4370,7 +4369,7 @@ namespace {
4370
4369
baseTy = component.getComponentType ();
4371
4370
resolvedComponents.push_back (component);
4372
4371
4373
- if (shouldForceUnwrapResult (property , locator)) {
4372
+ if (shouldForceUnwrapResult (foundDecl-> choice , locator)) {
4374
4373
auto objectTy = getObjectType (baseTy);
4375
4374
auto loc = origComponent.getLoc ();
4376
4375
component = KeyPathExpr::Component::forOptionalForce (objectTy, loc);
@@ -4451,7 +4450,7 @@ namespace {
4451
4450
baseTy = component.getComponentType ();
4452
4451
resolvedComponents.push_back (component);
4453
4452
4454
- if (shouldForceUnwrapResult (subscript , locator)) {
4453
+ if (shouldForceUnwrapResult (foundDecl-> choice , locator)) {
4455
4454
auto objectTy = getObjectType (baseTy);
4456
4455
auto loc = origComponent.getLoc ();
4457
4456
component = KeyPathExpr::Component::forOptionalForce (objectTy, loc);
@@ -7469,17 +7468,16 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
7469
7468
7470
7469
// We have the constructor.
7471
7470
auto choice = selected->choice ;
7472
- auto decl = choice.getDecl ();
7473
7471
7474
7472
// Consider the constructor decl reference expr 'implicit', but the
7475
7473
// constructor call expr itself has the apply's 'implicitness'.
7476
7474
bool isDynamic = choice.getKind () == OverloadChoiceKind::DeclViaDynamic;
7477
- Expr *declRef =
7478
- buildMemberRef (fn, selected-> openedFullType ,
7479
- /* dotLoc= */ SourceLoc (), decl, DeclNameLoc (fn->getEndLoc ()),
7480
- selected->openedType , locator, ctorLocator,
7481
- /* Implicit=*/ true , choice.getFunctionRefKind (),
7482
- AccessSemantics::Ordinary, isDynamic);
7475
+ Expr *declRef = buildMemberRef (fn, selected-> openedFullType ,
7476
+ /* dotLoc= */ SourceLoc (), choice ,
7477
+ DeclNameLoc (fn->getEndLoc ()),
7478
+ selected->openedType , locator, ctorLocator,
7479
+ /* Implicit=*/ true , choice.getFunctionRefKind (),
7480
+ AccessSemantics::Ordinary, isDynamic);
7483
7481
if (!declRef)
7484
7482
return nullptr ;
7485
7483
declRef->setImplicit (apply->isImplicit ());
@@ -8173,8 +8171,10 @@ Expr *TypeChecker::callWitness(Expr *base, DeclContext *dc,
8173
8171
ExprRewriter rewriter (cs, solution,
8174
8172
/* suppressDiagnostics=*/ false );
8175
8173
8174
+ auto choice =
8175
+ OverloadChoice (openedFullType, witness, FunctionRefKind::SingleApply);
8176
8176
auto memberRef = rewriter.buildMemberRef (
8177
- base, openedFullType, base->getStartLoc (), witness ,
8177
+ base, openedFullType, base->getStartLoc (), choice ,
8178
8178
DeclNameLoc (base->getEndLoc ()), openedType, dotLocator, dotLocator,
8179
8179
/* Implicit=*/ true , FunctionRefKind::SingleApply,
8180
8180
AccessSemantics::Ordinary,
0 commit comments