@@ -535,7 +535,8 @@ namespace {
535
535
cs.cacheExprTypes (base);
536
536
537
537
return buildMemberRef (base, SourceLoc (), overload, loc, locator,
538
- locator, implicit, semantics);
538
+ locator, implicit, /* extraUncurryLevel=*/ false ,
539
+ semantics);
539
540
}
540
541
541
542
if (isa<TypeDecl>(decl) && !isa<ModuleDecl>(decl)) {
@@ -675,11 +676,6 @@ namespace {
675
676
prev = result;
676
677
}
677
678
678
- // Invalid case -- direct call of a metatype. Has one less argument
679
- // application because there's no ".init".
680
- if (isa<ApplyExpr>(ExprStack.back ()))
681
- argCount--;
682
-
683
679
return argCount;
684
680
}
685
681
@@ -737,6 +733,11 @@ namespace {
737
733
unsigned maxArgCount = getNaturalArgumentCount (member);
738
734
unsigned depth = ExprStack.size () - getArgCount (maxArgCount);
739
735
736
+ // Invalid case -- direct call of a metatype. Has one less argument
737
+ // application because there's no ".init".
738
+ if (isa<ApplyExpr>(ExprStack.back ()))
739
+ depth++;
740
+
740
741
// Create the opaque opened value. If we started with a
741
742
// metatype, it's a metatype.
742
743
Type opaqueType = archetype;
@@ -804,7 +805,7 @@ namespace {
804
805
SelectedOverload overload, DeclNameLoc memberLoc,
805
806
ConstraintLocatorBuilder locator,
806
807
ConstraintLocatorBuilder memberLocator, bool Implicit,
807
- AccessSemantics semantics) {
808
+ bool extraUncurryLevel, AccessSemantics semantics) {
808
809
auto choice = overload.choice ;
809
810
auto openedType = overload.openedType ;
810
811
auto openedFullType = overload.openedFullType ;
@@ -830,15 +831,14 @@ namespace {
830
831
831
832
// Build a member reference.
832
833
auto memberRef = resolveConcreteDeclRef (member, memberLocator);
833
- auto refTy = solution.simplifyType (openedFullType);
834
834
835
835
// If we're referring to the member of a module, it's just a simple
836
836
// reference.
837
837
if (baseTy->is <ModuleType>()) {
838
838
assert (semantics == AccessSemantics::Ordinary &&
839
839
" Direct property access doesn't make sense for this" );
840
840
auto ref = new (context) DeclRefExpr (memberRef, memberLoc, Implicit);
841
- cs.setType (ref, refTy );
841
+ cs.setType (ref, simplifyType (openedFullType) );
842
842
ref->setFunctionRefKind (choice.getFunctionRefKind ());
843
843
auto *DSBI = cs.cacheType (new (context) DotSyntaxBaseIgnoredExpr (
844
844
base, dotLoc, ref, cs.getType (ref)));
@@ -857,6 +857,11 @@ namespace {
857
857
return result;
858
858
}
859
859
860
+ bool isUnboundInstanceMember =
861
+ (!baseIsInstance && member->isInstanceMember ());
862
+
863
+ auto refTy = simplifyType (openedFullType);
864
+
860
865
// The formal type of the 'self' value for the member's declaration.
861
866
Type containerTy = getBaseType (refTy->castTo <FunctionType>());
862
867
@@ -866,39 +871,21 @@ namespace {
866
871
867
872
// If we opened up an existential when referencing this member, update
868
873
// the base accordingly.
874
+ bool openedExistential = false ;
875
+
876
+ // For a partial application, we have to open the existential inside
877
+ // the thunk itself.
869
878
auto knownOpened = solution.OpenedExistentialTypes .find (
870
879
getConstraintSystem ().getConstraintLocator (
871
880
memberLocator));
872
- bool openedExistential = false ;
873
881
if (knownOpened != solution.OpenedExistentialTypes .end ()) {
882
+ // Open the existential before performing the member reference.
874
883
base = openExistentialReference (base, knownOpened->second , member);
875
884
baseTy = knownOpened->second ;
876
885
selfTy = baseTy;
877
886
openedExistential = true ;
878
887
}
879
888
880
- // If this is a method whose result type is dynamic Self, or a
881
- // construction, replace the result type with the actual object type.
882
- Type dynamicSelfFnType;
883
- if (!member->getDeclContext ()->getSelfProtocolDecl ()) {
884
- if (auto func = dyn_cast<AbstractFunctionDecl>(member)) {
885
- if (func->hasDynamicSelfResult () &&
886
- !baseTy->getOptionalObjectType ()) {
887
- refTy = refTy->replaceCovariantResultType (containerTy, 2 );
888
- if (!baseTy->isEqual (containerTy)) {
889
- dynamicSelfFnType = refTy->replaceCovariantResultType (baseTy, 2 );
890
- }
891
- }
892
- } else if (auto *decl = dyn_cast<VarDecl>(member)) {
893
- if (decl->getValueInterfaceType ()->hasDynamicSelfType ()) {
894
- refTy = refTy->replaceCovariantResultType (containerTy, 1 );
895
- if (!baseTy->isEqual (containerTy)) {
896
- dynamicSelfFnType = refTy->replaceCovariantResultType (baseTy, 1 );
897
- }
898
- }
899
- }
900
- }
901
-
902
889
// References to properties with accessors and storage usually go
903
890
// through the accessors, but sometimes are direct.
904
891
if (auto *VD = dyn_cast<VarDecl>(member)) {
@@ -952,9 +939,9 @@ namespace {
952
939
953
940
// If the base was an opened existential, erase the opened
954
941
// existential.
955
- if (openedExistential &&
956
- refType->hasOpenedExistential (knownOpened-> second )) {
957
- refType = refType-> eraseOpenedExistential (knownOpened-> second );
942
+ if (openedExistential) {
943
+ refType = refType->eraseOpenedExistential (
944
+ baseTy-> castTo <OpenedArchetypeType>() );
958
945
}
959
946
960
947
cs.setType (ref, refType);
@@ -1003,7 +990,7 @@ namespace {
1003
990
1004
991
// For properties, build member references.
1005
992
if (isa<VarDecl>(member)) {
1006
- if (!baseIsInstance && member-> isInstanceMember () ) {
993
+ if (isUnboundInstanceMember ) {
1007
994
assert (memberLocator.getBaseLocator () &&
1008
995
cs.UnevaluatedRootExprs .count (
1009
996
memberLocator.getBaseLocator ()->getAnchor ()) &&
@@ -1020,39 +1007,60 @@ namespace {
1020
1007
memberLoc, Implicit, semantics);
1021
1008
memberRefExpr->setIsSuper (isSuper);
1022
1009
1023
- // Skip the synthesized 'self' input type of the opened type.
1024
1010
cs.setType (memberRefExpr, simplifyType (openedType));
1025
1011
Expr *result = memberRefExpr;
1026
1012
closeExistential (result, locator);
1027
- if (dynamicSelfFnType) {
1028
- result = new (context) CovariantReturnConversionExpr (result,
1029
- dynamicSelfFnType);
1030
- cs.cacheType (result);
1031
- cs.setType (result, simplifyType (openedType));
1013
+
1014
+ if (cast<VarDecl>(member)->getValueInterfaceType ()
1015
+ ->hasDynamicSelfType ()) {
1016
+ if (!baseTy->isEqual (containerTy)) {
1017
+ result = new (context) CovariantReturnConversionExpr (
1018
+ result, simplifyType (openedType));
1019
+ cs.cacheType (result);
1020
+ }
1032
1021
}
1033
1022
return forceUnwrapIfExpected (result, choice, memberLocator);
1034
1023
}
1035
-
1024
+
1025
+ if (member->getInterfaceType ()->hasDynamicSelfType ())
1026
+ refTy = refTy->replaceCovariantResultType (containerTy, 2 );
1027
+
1036
1028
// Handle all other references.
1037
1029
auto declRefExpr = new (context) DeclRefExpr (memberRef, memberLoc,
1038
1030
Implicit, semantics);
1039
1031
declRefExpr->setFunctionRefKind (choice.getFunctionRefKind ());
1032
+ declRefExpr->setType (refTy);
1040
1033
cs.setType (declRefExpr, refTy);
1041
1034
Expr *ref = declRefExpr;
1042
1035
1043
- // If the reference needs to be converted, do so now.
1044
- if (dynamicSelfFnType) {
1045
- ref = new (context) CovariantFunctionConversionExpr (ref,
1036
+ // If this is a method whose result type is dynamic Self, or a
1037
+ // construction, replace the result type with the actual object type.
1038
+ if (!member->getDeclContext ()->getSelfProtocolDecl ()) {
1039
+ if (auto func = dyn_cast<AbstractFunctionDecl>(member)) {
1040
+ if (func->hasDynamicSelfResult () &&
1041
+ !baseTy->getOptionalObjectType ()) {
1042
+ if (!baseTy->isEqual (containerTy)) {
1043
+ auto dynamicSelfFnType = refTy->replaceCovariantResultType (baseTy, 2 );
1044
+ ref = new (context) CovariantFunctionConversionExpr (ref,
1046
1045
dynamicSelfFnType);
1047
- cs.cacheType (ref);
1046
+ cs.cacheType (ref);
1047
+ }
1048
+ }
1049
+ }
1048
1050
}
1049
1051
1050
1052
ApplyExpr *apply;
1051
1053
if (isa<ConstructorDecl>(member)) {
1052
1054
// FIXME: Provide type annotation.
1053
1055
ref = forceUnwrapIfExpected (ref, choice, memberLocator);
1054
1056
apply = new (context) ConstructorRefCallExpr (ref, base);
1055
- } else if (!baseIsInstance && member->isInstanceMember ()) {
1057
+ } else if (isUnboundInstanceMember) {
1058
+ auto refType = cs.simplifyType (openedType);
1059
+ if (!cs.getType (ref)->isEqual (refType)) {
1060
+ ref = new (context) FunctionConversionExpr (ref, refType);
1061
+ cs.cacheType (ref);
1062
+ }
1063
+
1056
1064
// Reference to an unbound instance method.
1057
1065
Expr *result = new (context) DotSyntaxBaseIgnoredExpr (base, dotLoc,
1058
1066
ref,
@@ -2339,7 +2347,7 @@ namespace {
2339
2347
return buildMemberRef (
2340
2348
expr->getBase (), expr->getDotLoc (), selected, expr->getNameLoc (),
2341
2349
cs.getConstraintLocator (expr), memberLocator, expr->isImplicit (),
2342
- expr->getAccessSemantics ());
2350
+ /* extraUncurryLevel= */ false , expr->getAccessSemantics ());
2343
2351
}
2344
2352
2345
2353
Expr *visitDynamicMemberRefExpr (DynamicMemberRefExpr *expr) {
@@ -2382,7 +2390,8 @@ namespace {
2382
2390
auto *exprLoc = cs.getConstraintLocator (expr);
2383
2391
auto result = buildMemberRef (
2384
2392
base, expr->getDotLoc (), selected, expr->getNameLoc (), exprLoc,
2385
- memberLocator, expr->isImplicit (), AccessSemantics::Ordinary);
2393
+ memberLocator, expr->isImplicit (), /* extraUncurryLevel=*/ true ,
2394
+ AccessSemantics::Ordinary);
2386
2395
if (!result)
2387
2396
return nullptr ;
2388
2397
@@ -2530,7 +2539,8 @@ namespace {
2530
2539
if (cs.getType (base)->is <AnyMetatypeType>()) {
2531
2540
return buildMemberRef (
2532
2541
base, dotLoc, overload, nameLoc, cs.getConstraintLocator (expr),
2533
- ctorLocator, implicit, AccessSemantics::Ordinary);
2542
+ ctorLocator, implicit, /* extraUncurryLevel=*/ true ,
2543
+ AccessSemantics::Ordinary);
2534
2544
}
2535
2545
2536
2546
// The subexpression must be either 'self' or 'super'.
@@ -2703,7 +2713,8 @@ namespace {
2703
2713
case OverloadChoiceKind::DeclViaDynamic:
2704
2714
return buildMemberRef (base, dotLoc, selected, nameLoc,
2705
2715
cs.getConstraintLocator (expr), memberLocator,
2706
- implicit, AccessSemantics::Ordinary);
2716
+ implicit, /* extraUncurryLevel=*/ false ,
2717
+ AccessSemantics::Ordinary);
2707
2718
2708
2719
case OverloadChoiceKind::TupleIndex: {
2709
2720
Type toType = simplifyType (cs.getType (expr));
@@ -6547,7 +6558,8 @@ static Expr *buildCallAsFunctionMethodRef(
6547
6558
auto *fn = apply->getFn ();
6548
6559
auto *declRef = rewriter.buildMemberRef (
6549
6560
fn, /* dotLoc*/ SourceLoc (), selected, DeclNameLoc (fn->getEndLoc ()),
6550
- calleeLoc, calleeLoc, /* implicit*/ true , AccessSemantics::Ordinary);
6561
+ calleeLoc, calleeLoc, /* implicit*/ true ,
6562
+ /* extraUncurryLevel=*/ true , AccessSemantics::Ordinary);
6551
6563
if (!declRef)
6552
6564
return nullptr ;
6553
6565
declRef->setImplicit (apply->isImplicit ());
@@ -6581,7 +6593,8 @@ ExprRewriter::finishApplyDynamicCallable(ApplyExpr *apply,
6581
6593
// Construct expression referencing the `dynamicallyCall` method.
6582
6594
auto member = buildMemberRef (fn, SourceLoc (), selected,
6583
6595
DeclNameLoc (method->getNameLoc ()), loc, loc,
6584
- /* implicit*/ true , AccessSemantics::Ordinary);
6596
+ /* implicit=*/ true , /* extraUncurryLevel=*/ true ,
6597
+ AccessSemantics::Ordinary);
6585
6598
6586
6599
// Construct argument to the method (either an array or dictionary
6587
6600
// expression).
@@ -6942,7 +6955,8 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
6942
6955
// constructor call expr itself has the apply's 'implicitness'.
6943
6956
Expr *declRef = buildMemberRef (fn, /* dotLoc=*/ SourceLoc (), *selected,
6944
6957
DeclNameLoc (fn->getEndLoc ()), locator,
6945
- ctorLocator, /* Implicit=*/ true ,
6958
+ ctorLocator, /* implicit=*/ true ,
6959
+ /* extraUncurryLevel=*/ true ,
6946
6960
AccessSemantics::Ordinary);
6947
6961
if (!declRef)
6948
6962
return nullptr ;
0 commit comments