Skip to content

Commit 03322bf

Browse files
committed
Move implicit self lookup from resolveDeclRefExpr into ASTScope::unqualifiedLookup implementation
1 parent 4abca2b commit 03322bf

File tree

5 files changed

+33
-25
lines changed

5 files changed

+33
-25
lines changed

include/swift/AST/NameLookup.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,18 @@ struct LookupResultEntry {
103103
/// extension (if it found something at that level).
104104
DeclContext *BaseDC;
105105

106+
/// The declaration that defines the base of the call to `Value`
107+
ValueDecl *BaseDecl;
108+
106109
/// The declaration corresponds to the given name; i.e. the decl we are
107110
/// looking up.
108111
ValueDecl *Value;
109112

110113
public:
111-
LookupResultEntry(ValueDecl *value) : BaseDC(nullptr), Value(value) {}
114+
LookupResultEntry(ValueDecl *value) : BaseDC(nullptr), BaseDecl(nullptr), Value(value) {}
112115

113-
LookupResultEntry(DeclContext *baseDC, ValueDecl *value)
114-
: BaseDC(baseDC), Value(value) {}
116+
LookupResultEntry(DeclContext *baseDC, ValueDecl *baseDecl, ValueDecl *value)
117+
: BaseDC(baseDC), BaseDecl(baseDecl), Value(value) {}
115118

116119
ValueDecl *getValueDecl() const { return Value; }
117120

lib/AST/NameLookup.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ void VectorDeclConsumer::anchor() {}
5555
ValueDecl *LookupResultEntry::getBaseDecl() const {
5656
if (BaseDC == nullptr)
5757
return nullptr;
58+
59+
if (BaseDecl)
60+
return BaseDecl;
5861

5962
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(BaseDC))
6063
return AFD->getImplicitSelfDecl();

lib/AST/UnqualifiedLookup.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,22 @@ void UnqualifiedLookupFactory::ResultFinderForTypeContext::findResults(
343343
SmallVector<ValueDecl *, 4> Lookup;
344344
contextForLookup->lookupQualified(selfBounds, Name, baseNLOptions, Lookup);
345345
for (auto Result : Lookup) {
346-
results.emplace_back(const_cast<DeclContext *>(whereValueIsMember(Result)),
347-
Result);
346+
auto baseDC = const_cast<DeclContext *>(whereValueIsMember(Result));
347+
348+
// Perform an unqualified lookup for the base decl of this result. This handles cases
349+
// where self was rebound (e.g. `guard let self = self`) earlier in the scope.
350+
// Only do this in closures, since implicit self isn't allowed to be rebound
351+
// in other contexts. Otherwise, we use the `ParamDecl` from `baseDC` if applicable.
352+
ValueDecl* localBaseDecl = nullptr;
353+
if (factory->DC->getContextKind() == DeclContextKind::AbstractClosureExpr && baseDC) {
354+
auto *localDecl = ASTScope::lookupSingleLocalDecl(factory->DC->getParentSourceFile(),
355+
DeclName(factory->Ctx.Id_self),
356+
factory->Loc);
357+
if (localDecl)
358+
localBaseDecl = localDecl;
359+
}
360+
361+
results.emplace_back(baseDC, localBaseDecl, Result);
348362
#ifndef NDEBUG
349363
factory->addedResult(results.back());
350364
#endif

lib/Sema/PreCheckExpr.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -728,22 +728,7 @@ Expr *TypeChecker::resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE,
728728
ValueDecl *Base = nullptr;
729729
DeclContext *BaseDC = nullptr;
730730
for (auto Result : Lookup) {
731-
// Perform an unqualified lookup for the base decl. This handles cases
732-
// where self was rebound (e.g. `guard let self = self`) earlier in this scope.
733-
// Only do this in closures, since implicit self isn't allowed to be rebound
734-
// in other contexts.
735-
ValueDecl* ThisBase = nullptr;
736-
if (DC->getContextKind() == DeclContextKind::AbstractClosureExpr) {
737-
auto &ctx = DC->getASTContext();
738-
auto localDecl = ASTScope::lookupSingleLocalDecl(DC->getParentSourceFile(),
739-
DeclName(ctx.Id_self),
740-
UDRE->getLoc());
741-
if (localDecl)
742-
ThisBase = localDecl;
743-
}
744-
745-
if (!ThisBase)
746-
ThisBase = Result.getBaseDecl();
731+
auto ThisBase = Result.getBaseDecl();
747732

748733
// Track the base for member declarations.
749734
if (ThisBase && !isa<ModuleDecl>(ThisBase)) {

lib/Sema/TypeCheckNameLookup.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,21 @@ namespace {
9292
/// \param baseDC The declaration context through which we found the
9393
/// declaration.
9494
///
95+
/// \param baseDecl The declaration that defines the base of the
96+
/// call to `found`
97+
///
9598
/// \param foundInType The type through which we found the
9699
/// declaration.
97100
///
98101
/// \param isOuter Whether this is an outer result (i.e. a result that isn't
99102
/// from the innermost scope with results)
100-
void add(ValueDecl *found, DeclContext *baseDC, Type foundInType,
103+
void add(ValueDecl *found, DeclContext *baseDC, ValueDecl *baseDecl, Type foundInType,
101104
bool isOuter) {
102105
DeclContext *foundDC = found->getDeclContext();
103106

104107
auto addResult = [&](ValueDecl *result) {
105108
if (Known.insert({{result, baseDC}, false}).second) {
106-
Result.add(LookupResultEntry(baseDC, result), isOuter);
109+
Result.add(LookupResultEntry(baseDC, baseDecl, result), isOuter);
107110
if (isOuter)
108111
FoundOuterDecls.push_back(result);
109112
else
@@ -267,7 +270,7 @@ LookupResult TypeChecker::lookupUnqualified(DeclContext *dc, DeclNameRef name,
267270
assert(foundInType && "bogus base declaration?");
268271
}
269272

270-
builder.add(found.getValueDecl(), found.getDeclContext(), foundInType,
273+
builder.add(found.getValueDecl(), found.getDeclContext(), found.getBaseDecl(), foundInType,
271274
/*isOuter=*/idx >= lookup.getIndexOfFirstOuterResult());
272275
}
273276
return result;
@@ -327,7 +330,7 @@ LookupResult TypeChecker::lookupMember(DeclContext *dc,
327330
dc->lookupQualified(type, name, subOptions, lookupResults);
328331

329332
for (auto found : lookupResults)
330-
builder.add(found, nullptr, type, /*isOuter=*/false);
333+
builder.add(found, nullptr, nullptr, type, /*isOuter=*/false);
331334

332335
return result;
333336
}

0 commit comments

Comments
 (0)