Skip to content

Commit 42e510c

Browse files
committed
NFC, Sema: Derive base expr from locator in getTypeOfMemberReference
1 parent aa74b1b commit 42e510c

File tree

3 files changed

+27
-36
lines changed

3 files changed

+27
-36
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3916,8 +3916,7 @@ class ConstraintSystem {
39163916
Type baseTy, ValueDecl *decl, DeclContext *useDC,
39173917
bool isDynamicResult,
39183918
FunctionRefKind functionRefKind,
3919-
ConstraintLocatorBuilder locator,
3920-
const DeclRefExpr *base = nullptr,
3919+
ConstraintLocator *locator,
39213920
OpenedTypeMap *replacements = nullptr);
39223921

39233922
/// Retrieve a list of generic parameter types solver has "opened" (replaced

lib/Sema/ConstraintSystem.cpp

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,8 +1508,7 @@ ConstraintSystem::getTypeOfMemberReference(
15081508
Type baseTy, ValueDecl *value, DeclContext *useDC,
15091509
bool isDynamicResult,
15101510
FunctionRefKind functionRefKind,
1511-
ConstraintLocatorBuilder locator,
1512-
const DeclRefExpr *base,
1511+
ConstraintLocator *locator,
15131512
OpenedTypeMap *replacementsPtr) {
15141513
// Figure out the instance type used for the base.
15151514
Type resolvedBaseTy = getFixedTypeRecursive(baseTy, /*wantRValue=*/true);
@@ -1534,9 +1533,8 @@ ConstraintSystem::getTypeOfMemberReference(
15341533
bool isStaticMemberRefOnProtocol = false;
15351534
if (resolvedBaseTy->is<MetatypeType>() && baseObjTy->isExistentialType() &&
15361535
value->isStatic()) {
1537-
if (auto last = locator.last())
1538-
isStaticMemberRefOnProtocol =
1539-
last->is<LocatorPathElt::UnresolvedMember>();
1536+
isStaticMemberRefOnProtocol =
1537+
locator->isLastElement<LocatorPathElt::UnresolvedMember>();
15401538
}
15411539

15421540
if (auto *typeDecl = dyn_cast<TypeDecl>(value)) {
@@ -1568,6 +1566,18 @@ ConstraintSystem::getTypeOfMemberReference(
15681566
unsigned numRemovedArgumentLabels = getNumRemovedArgumentLabels(
15691567
value, /*isCurriedInstanceReference*/ !hasAppliedSelf, functionRefKind);
15701568

1569+
const auto getBaseAsDeclRefExpr = [&] {
1570+
if (auto *E = getAsExpr(locator->getAnchor())) {
1571+
if (auto *MRE = dyn_cast<MemberRefExpr>(E)) {
1572+
return dyn_cast<DeclRefExpr>(MRE->getBase());
1573+
} else if (auto *UDE = dyn_cast<UnresolvedDotExpr>(E)) {
1574+
return dyn_cast<DeclRefExpr>(UDE->getBase());
1575+
}
1576+
}
1577+
1578+
return (DeclRefExpr *)nullptr;
1579+
};
1580+
15711581
AnyFunctionType *funcType;
15721582

15731583
if (isa<AbstractFunctionDecl>(value) ||
@@ -1583,7 +1593,8 @@ ConstraintSystem::getTypeOfMemberReference(
15831593
if (auto *subscript = dyn_cast<SubscriptDecl>(value)) {
15841594
auto elementTy = subscript->getElementInterfaceType();
15851595

1586-
if (doesStorageProduceLValue(subscript, baseTy, useDC, base))
1596+
if (doesStorageProduceLValue(subscript, baseTy, useDC,
1597+
getBaseAsDeclRefExpr()))
15871598
elementTy = LValueType::get(elementTy);
15881599

15891600
// See ConstraintSystem::resolveOverload() -- optional and dynamic
@@ -1599,9 +1610,9 @@ ConstraintSystem::getTypeOfMemberReference(
15991610
->castTo<AnyFunctionType>()->getParams();
16001611
refType = FunctionType::get(indices, elementTy);
16011612
} else {
1602-
refType =
1603-
getUnopenedTypeOfReference(cast<VarDecl>(value), baseTy, useDC, base,
1604-
/*wantInterfaceType=*/true);
1613+
refType = getUnopenedTypeOfReference(cast<VarDecl>(value), baseTy, useDC,
1614+
getBaseAsDeclRefExpr(),
1615+
/*wantInterfaceType=*/true);
16051616
}
16061617

16071618
auto selfTy = outerDC->getSelfInterfaceType();
@@ -1757,7 +1768,7 @@ ConstraintSystem::getTypeOfMemberReference(
17571768
// (e.g. "colorLiteralRed:") by stripping all the redundant stuff about
17581769
// literals (leaving e.g. "red:").
17591770
{
1760-
auto anchor = locator.getAnchor();
1771+
auto anchor = locator->getAnchor();
17611772
if (auto *OLE = getAsExpr<ObjectLiteralExpr>(anchor)) {
17621773
auto fnType = type->castTo<FunctionType>();
17631774

@@ -2726,28 +2737,11 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
27262737
// Retrieve the type of a reference to the specific declaration choice.
27272738
assert(!baseTy->hasTypeParameter());
27282739

2729-
auto getDotBase = [](const Expr *E) -> const DeclRefExpr * {
2730-
if (E == nullptr) return nullptr;
2731-
switch (E->getKind()) {
2732-
case ExprKind::MemberRef: {
2733-
auto Base = cast<MemberRefExpr>(E)->getBase();
2734-
return dyn_cast<const DeclRefExpr>(Base);
2735-
}
2736-
case ExprKind::UnresolvedDot: {
2737-
auto Base = cast<UnresolvedDotExpr>(E)->getBase();
2738-
return dyn_cast<const DeclRefExpr>(Base);
2739-
}
2740-
default:
2741-
return nullptr;
2742-
}
2743-
};
2744-
auto *anchor = locator ? getAsExpr(locator->getAnchor()) : nullptr;
2745-
auto base = getDotBase(anchor);
27462740
std::tie(openedFullType, refType)
27472741
= getTypeOfMemberReference(baseTy, choice.getDecl(), useDC,
27482742
(kind == OverloadChoiceKind::DeclViaDynamic),
27492743
choice.getFunctionRefKind(),
2750-
locator, base, nullptr);
2744+
locator, nullptr);
27512745
} else {
27522746
std::tie(openedFullType, refType)
27532747
= getTypeOfReference(choice.getDecl(),

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -902,8 +902,7 @@ swift::matchWitness(WitnessChecker::RequirementEnvironmentCache &reqEnvCache,
902902
ConstraintLocator *reqLocator = nullptr;
903903
ConstraintLocator *witnessLocator = nullptr;
904904
Type witnessType, openWitnessType;
905-
Type openedFullWitnessType;
906-
Type reqType, openedFullReqType;
905+
Type reqType;
907906

908907
GenericSignature reqSig = proto->getGenericSignature();
909908
if (auto *funcDecl = dyn_cast<AbstractFunctionDecl>(req)) {
@@ -988,12 +987,11 @@ swift::matchWitness(WitnessChecker::RequirementEnvironmentCache &reqEnvCache,
988987
reqLocator = cs->getConstraintLocator(
989988
static_cast<Expr *>(nullptr), LocatorPathElt::ProtocolRequirement(req));
990989
OpenedTypeMap reqReplacements;
991-
std::tie(openedFullReqType, reqType)
990+
std::tie(std::ignore, reqType)
992991
= cs->getTypeOfMemberReference(selfTy, req, dc,
993992
/*isDynamicResult=*/false,
994993
FunctionRefKind::DoubleApply,
995994
reqLocator,
996-
/*base=*/nullptr,
997995
&reqReplacements);
998996
reqType = reqType->getRValueType();
999997

@@ -1022,13 +1020,13 @@ swift::matchWitness(WitnessChecker::RequirementEnvironmentCache &reqEnvCache,
10221020
witnessLocator = cs->getConstraintLocator({},
10231021
LocatorPathElt::Witness(witness));
10241022
if (witness->getDeclContext()->isTypeContext()) {
1025-
std::tie(openedFullWitnessType, openWitnessType)
1023+
std::tie(std::ignore, openWitnessType)
10261024
= cs->getTypeOfMemberReference(selfTy, witness, dc,
10271025
/*isDynamicResult=*/false,
10281026
FunctionRefKind::DoubleApply,
10291027
witnessLocator);
10301028
} else {
1031-
std::tie(openedFullWitnessType, openWitnessType)
1029+
std::tie(std::ignore, openWitnessType)
10321030
= cs->getTypeOfReference(witness,
10331031
FunctionRefKind::DoubleApply,
10341032
witnessLocator, /*useDC=*/nullptr);

0 commit comments

Comments
 (0)