Skip to content

Commit 5946c66

Browse files
committed
Style updates
1 parent 9dd56f9 commit 5946c66

File tree

6 files changed

+172
-140
lines changed

6 files changed

+172
-140
lines changed

include/swift/AST/NameLookup.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,17 @@ struct LookupResultEntry {
106106
/// The declaration that defines the base of the call to `Value`.
107107
/// This is always available, as long as `BaseDC` is not null.
108108
ValueDecl *BaseDecl;
109-
109+
110110
/// The declaration corresponds to the given name; i.e. the decl we are
111111
/// looking up.
112112
ValueDecl *Value;
113113

114114
public:
115-
LookupResultEntry(ValueDecl *value) : BaseDC(nullptr), BaseDecl(nullptr), Value(value) {}
115+
LookupResultEntry(ValueDecl *value)
116+
: BaseDC(nullptr), BaseDecl(nullptr), Value(value) {}
116117

117118
LookupResultEntry(DeclContext *baseDC, ValueDecl *baseDecl, ValueDecl *value)
118-
: BaseDC(baseDC), BaseDecl(baseDecl), Value(value) {}
119+
: BaseDC(baseDC), BaseDecl(baseDecl), Value(value) {}
119120

120121
ValueDecl *getValueDecl() const { return Value; }
121122

lib/AST/NameLookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void VectorDeclConsumer::anchor() {}
5555
ValueDecl *LookupResultEntry::getBaseDecl() const {
5656
if (BaseDC == nullptr)
5757
return nullptr;
58-
58+
5959
return BaseDecl;
6060
}
6161

lib/AST/UnqualifiedLookup.cpp

Lines changed: 65 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
#include "swift/AST/ASTVisitor.h"
2020
#include "swift/AST/DebuggerClient.h"
2121
#include "swift/AST/ImportCache.h"
22+
#include "swift/AST/Initializer.h"
2223
#include "swift/AST/ModuleNameLookup.h"
2324
#include "swift/AST/NameLookup.h"
2425
#include "swift/AST/NameLookupRequests.h"
2526
#include "swift/AST/PropertyWrappers.h"
2627
#include "swift/AST/SourceFile.h"
27-
#include "swift/AST/Initializer.h"
2828
#include "swift/Basic/Debug.h"
2929
#include "swift/Basic/STLExtras.h"
3030
#include "swift/Basic/SourceManager.h"
@@ -77,6 +77,8 @@ namespace {
7777

7878
private:
7979
SelfBounds findSelfBounds(const DeclContext *dc);
80+
ValueDecl *lookupBaseDecl(const DeclContext *baseDC) const;
81+
ValueDecl *getBaseDeclForResult(const DeclContext *baseDC) const;
8082

8183
// Classify this declaration.
8284
// Types are formally members of the metatype.
@@ -345,64 +347,74 @@ void UnqualifiedLookupFactory::ResultFinderForTypeContext::findResults(
345347
contextForLookup->lookupQualified(selfBounds, Name, baseNLOptions, Lookup);
346348
for (auto Result : Lookup) {
347349
auto baseDC = const_cast<DeclContext *>(whereValueIsMember(Result));
348-
349-
// Retrieve the base decl for this lookup
350-
ValueDecl* baseDecl;
351-
352-
// Perform an unqualified lookup for the base decl of this result. This handles cases
353-
// where self was rebound (e.g. `guard let self = self`) earlier in the scope.
354-
// - Only do this in closures that capture self weakly, since implicit self isn't
355-
// allowed to be rebound in other contexts. In other contexts, implicit self
356-
// _always_ refers to the context's self `ParamDecl`, even if there is another
357-
// local decl with the name `self` that would be found by `lookupSingleLocalDecl`.
358-
ValueDecl* localBaseDecl = nullptr;
359-
bool isInWeakSelfClosure = false;
360-
if (auto closureExpr = dyn_cast<ClosureExpr>(factory->DC)) {
361-
if (auto selfDecl = closureExpr->getCapturedSelfDecl()) {
362-
auto *attr = selfDecl->getAttrs().getAttribute<ReferenceOwnershipAttr>();
363-
isInWeakSelfClosure = attr->get() == ReferenceOwnership::Weak;
364-
}
365-
}
366-
367-
if (isInWeakSelfClosure) {
368-
localBaseDecl = ASTScope::lookupSingleLocalDecl(factory->DC->getParentSourceFile(),
369-
DeclName(factory->Ctx.Id_self),
370-
factory->Loc);
371-
}
372-
373-
if (baseDC == nullptr)
374-
baseDecl = nullptr;
375-
376-
else if (localBaseDecl)
377-
baseDecl = localBaseDecl;
350+
auto baseDecl = getBaseDeclForResult(baseDC);
351+
results.emplace_back(baseDC, baseDecl, Result);
352+
#ifndef NDEBUG
353+
factory->addedResult(results.back());
354+
#endif
355+
}
356+
}
378357

379-
else if (auto *AFD = dyn_cast<AbstractFunctionDecl>(baseDC))
380-
baseDecl = AFD->getImplicitSelfDecl();
358+
ValueDecl *
359+
UnqualifiedLookupFactory::ResultFinderForTypeContext::getBaseDeclForResult(
360+
const DeclContext *baseDC) const {
361+
if (baseDC == nullptr) {
362+
return nullptr;
363+
}
381364

382-
else if (auto *PBI = dyn_cast<PatternBindingInitializer>(baseDC)) {
383-
auto *selfDecl = PBI->getImplicitSelfDecl();
384-
assert(selfDecl);
385-
baseDecl = selfDecl;
386-
}
365+
if (auto localBaseDecl = lookupBaseDecl(baseDC)) {
366+
return localBaseDecl;
367+
}
387368

388-
else if (auto *CE = dyn_cast<ClosureExpr>(baseDC)) {
389-
auto *selfDecl = CE->getCapturedSelfDecl();
390-
assert(selfDecl);
391-
assert(selfDecl->isSelfParamCapture());
392-
baseDecl = selfDecl;
393-
}
369+
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(baseDC)) {
370+
return const_cast<ParamDecl *>(AFD->getImplicitSelfDecl());
371+
}
394372

395-
else {
396-
auto *nominalDecl = baseDC->getSelfNominalTypeDecl();
397-
assert(nominalDecl);
398-
baseDecl = nominalDecl;
373+
if (auto *PBI = dyn_cast<PatternBindingInitializer>(baseDC)) {
374+
auto *selfDecl = PBI->getImplicitSelfDecl();
375+
assert(selfDecl);
376+
return selfDecl;
377+
}
378+
379+
else if (auto *CE = dyn_cast<ClosureExpr>(baseDC)) {
380+
auto *selfDecl = CE->getCapturedSelfDecl();
381+
assert(selfDecl);
382+
assert(selfDecl->isSelfParamCapture());
383+
return selfDecl;
384+
}
385+
386+
auto *nominalDecl = baseDC->getSelfNominalTypeDecl();
387+
assert(nominalDecl);
388+
return nominalDecl;
389+
}
390+
391+
ValueDecl *UnqualifiedLookupFactory::ResultFinderForTypeContext::lookupBaseDecl(
392+
const DeclContext *baseDC) const {
393+
// Perform an unqualified lookup for the base decl of this result. This
394+
// handles cases where self was rebound (e.g. `guard let self = self`)
395+
// earlier in the scope.
396+
//
397+
// Only do this in closures that capture self weakly, since implicit self
398+
// isn't allowed to be rebound in other contexts. In other contexts, implicit
399+
// self _always_ refers to the context's self `ParamDecl`, even if there
400+
// is another local decl with the name `self` that would be found by
401+
// `lookupSingleLocalDecl`.
402+
bool isInWeakSelfClosure = false;
403+
if (auto closureExpr = dyn_cast<ClosureExpr>(factory->DC)) {
404+
if (auto decl = closureExpr->getCapturedSelfDecl()) {
405+
if (auto a = decl->getAttrs().getAttribute<ReferenceOwnershipAttr>()) {
406+
isInWeakSelfClosure = a->get() == ReferenceOwnership::Weak;
407+
}
399408
}
400-
401-
results.emplace_back(baseDC, baseDecl, Result);
402-
#ifndef NDEBUG
403-
factory->addedResult(results.back());
404-
#endif
405409
}
410+
411+
if (isInWeakSelfClosure) {
412+
return ASTScope::lookupSingleLocalDecl(factory->DC->getParentSourceFile(),
413+
DeclName(factory->Ctx.Id_self),
414+
factory->Loc);
415+
}
416+
417+
return nullptr;
406418
}
407419

408420
// TODO (someday): Instead of adding unavailable entries to Results,

lib/Sema/CSDiagnostics.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,19 +1372,17 @@ bool MemberAccessOnOptionalBaseFailure::diagnoseAsError() {
13721372
}
13731373
} else {
13741374
// Check whether or not the base of this optional unwrap is implicit self
1375-
// - This can only happen with a [weak self] capture, and is not permitted.
1375+
// This can only happen with a [weak self] capture, and is not permitted.
13761376
if (auto dotExpr = getAsExpr<UnresolvedDotExpr>(locator->getAnchor())) {
1377-
if (auto base = dotExpr->getBase()) {
1378-
if (auto baseDeclRef = dyn_cast<DeclRefExpr>(base)) {
1379-
ASTContext &Ctx = baseDeclRef->getDecl()->getASTContext();
1380-
if (baseDeclRef->isImplicit()
1381-
&& baseDeclRef->getDecl()->getName().isSimpleName(Ctx.Id_self)) {
1382-
emitDiagnostic(diag::optional_self_not_unwrapped);
1383-
1384-
emitDiagnostic(diag::optional_self_chain)
1385-
.fixItInsertAfter(sourceRange.End, "self?.");
1386-
return true;
1387-
}
1377+
if (auto baseDeclRef = dyn_cast<DeclRefExpr>(dotExpr->getBase())) {
1378+
ASTContext &Ctx = baseDeclRef->getDecl()->getASTContext();
1379+
if (baseDeclRef->isImplicit() &&
1380+
baseDeclRef->getDecl()->getName().isSimpleName(Ctx.Id_self)) {
1381+
emitDiagnostic(diag::optional_self_not_unwrapped);
1382+
1383+
emitDiagnostic(diag::optional_self_chain)
1384+
.fixItInsertAfter(sourceRange.End, "self?.");
1385+
return true;
13881386
}
13891387
}
13901388
}

0 commit comments

Comments
 (0)