Skip to content

Commit dc63bdf

Browse files
authored
Revert "Implement backward-compatible closure capture behavior with parser lookup disabled"
1 parent 95c4a97 commit dc63bdf

File tree

11 files changed

+139
-233
lines changed

11 files changed

+139
-233
lines changed

include/swift/AST/ASTScope.h

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#define SWIFT_AST_AST_SCOPE_H
3030

3131
#include "swift/AST/ASTNode.h"
32-
#include "swift/AST/NameLookup.h"
32+
#include "swift/AST/NameLookup.h" // for DeclVisibilityKind
3333
#include "swift/AST/SimpleRequest.h"
3434
#include "swift/Basic/Compiler.h"
3535
#include "swift/Basic/Debug.h"
@@ -444,6 +444,7 @@ class ASTScopeImpl {
444444
// It is not an instance variable or inherited type.
445445

446446
static bool lookupLocalBindingsInPattern(const Pattern *p,
447+
DeclVisibilityKind vis,
447448
DeclConsumer consumer);
448449

449450
/// When lookup must stop before the outermost scope, return the scope to stop
@@ -1023,10 +1024,10 @@ class AbstractPatternEntryScope : public ASTScopeImpl {
10231024
public:
10241025
PatternBindingDecl *const decl;
10251026
const unsigned patternEntryIndex;
1026-
const bool isLocalBinding;
1027+
const DeclVisibilityKind vis;
10271028

10281029
AbstractPatternEntryScope(PatternBindingDecl *, unsigned entryIndex,
1029-
bool);
1030+
DeclVisibilityKind);
10301031
virtual ~AbstractPatternEntryScope() {}
10311032

10321033
const PatternBindingEntry &getPatternEntry() const;
@@ -1043,8 +1044,8 @@ class AbstractPatternEntryScope : public ASTScopeImpl {
10431044
class PatternEntryDeclScope final : public AbstractPatternEntryScope {
10441045
public:
10451046
PatternEntryDeclScope(PatternBindingDecl *pbDecl, unsigned entryIndex,
1046-
bool isLocalBinding)
1047-
: AbstractPatternEntryScope(pbDecl, entryIndex, isLocalBinding) {}
1047+
DeclVisibilityKind vis)
1048+
: AbstractPatternEntryScope(pbDecl, entryIndex, vis) {}
10481049
virtual ~PatternEntryDeclScope() {}
10491050

10501051
protected:
@@ -1071,8 +1072,8 @@ class PatternEntryInitializerScope final : public AbstractPatternEntryScope {
10711072

10721073
public:
10731074
PatternEntryInitializerScope(PatternBindingDecl *pbDecl, unsigned entryIndex,
1074-
bool isLocalBinding)
1075-
: AbstractPatternEntryScope(pbDecl, entryIndex, isLocalBinding),
1075+
DeclVisibilityKind vis)
1076+
: AbstractPatternEntryScope(pbDecl, entryIndex, vis),
10761077
initAsWrittenWhenCreated(pbDecl->getOriginalInit(entryIndex)) {}
10771078
virtual ~PatternEntryInitializerScope() {}
10781079

@@ -1657,22 +1658,10 @@ class CaseStmtBodyScope final : public ASTScopeImpl {
16571658
};
16581659

16591660
class BraceStmtScope final : public AbstractStmtScope {
1660-
BraceStmt *const stmt;
1661-
1662-
/// Declarations which are in scope from the beginning of the statement.
1663-
SmallVector<ValueDecl *, 2> localFuncsAndTypes;
1664-
1665-
/// Declarations that are normally in scope only after their
1666-
/// definition.
1667-
SmallVector<VarDecl *, 2> localVars;
16681661

16691662
public:
1670-
BraceStmtScope(BraceStmt *e,
1671-
SmallVector<ValueDecl *, 2> localFuncsAndTypes,
1672-
SmallVector<VarDecl *, 2> localVars)
1673-
: stmt(e),
1674-
localFuncsAndTypes(localFuncsAndTypes),
1675-
localVars(localVars) {}
1663+
BraceStmt *const stmt;
1664+
BraceStmtScope(BraceStmt *e) : stmt(e) {}
16761665
virtual ~BraceStmtScope() {}
16771666

16781667
protected:

include/swift/AST/NameLookup.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ class AbstractASTScopeDeclConsumer {
603603
/// of type -vs- instance lookup results.
604604
///
605605
/// \return true if the lookup should be stopped at this point.
606-
virtual bool consume(ArrayRef<ValueDecl *> values,
606+
virtual bool consume(ArrayRef<ValueDecl *> values, DeclVisibilityKind vis,
607607
NullablePtr<DeclContext> baseDC = nullptr) = 0;
608608

609609
/// Look for members of a nominal type or extension scope.
@@ -613,14 +613,6 @@ class AbstractASTScopeDeclConsumer {
613613
lookInMembers(DeclContext *const scopeDC,
614614
NominalTypeDecl *const nominal) = 0;
615615

616-
/// Called for local VarDecls that might not yet be in scope.
617-
///
618-
/// Note that the set of VarDecls visited here are going to be a
619-
/// superset of those visited in consume().
620-
virtual bool consumePossiblyNotInScope(ArrayRef<VarDecl *> values) {
621-
return false;
622-
}
623-
624616
/// Called right before looking at the parent scope of a BraceStmt.
625617
///
626618
/// \return true if the lookup should be stopped at this point.
@@ -644,7 +636,7 @@ class ASTScopeDeclGatherer : public AbstractASTScopeDeclConsumer {
644636
public:
645637
virtual ~ASTScopeDeclGatherer() = default;
646638

647-
bool consume(ArrayRef<ValueDecl *> values,
639+
bool consume(ArrayRef<ValueDecl *> values, DeclVisibilityKind vis,
648640
NullablePtr<DeclContext> baseDC = nullptr) override;
649641

650642
/// Eventually this functionality should move into ASTScopeLookup

lib/AST/ASTScopeCreation.cpp

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -667,30 +667,10 @@ class NodeAdder
667667

668668
NullablePtr<ASTScopeImpl> visitBraceStmt(BraceStmt *bs, ASTScopeImpl *p,
669669
ScopeCreator &scopeCreator) {
670-
SmallVector<ValueDecl *, 2> localFuncsAndTypes;
671-
SmallVector<VarDecl *, 2> localVars;
672-
673-
// All types and functions are visible anywhere within a brace statement
674-
// scope. When ordering matters (i.e. var decl) we will have split the brace
675-
// statement into nested scopes.
676-
for (auto braceElement : bs->getElements()) {
677-
if (auto localBinding = braceElement.dyn_cast<Decl *>()) {
678-
if (auto *vd = dyn_cast<ValueDecl>(localBinding)) {
679-
if (isa<FuncDecl>(vd) || isa<TypeDecl>(vd)) {
680-
localFuncsAndTypes.push_back(vd);
681-
} else if (auto *var = dyn_cast<VarDecl>(localBinding)) {
682-
localVars.push_back(var);
683-
}
684-
}
685-
}
686-
}
687-
688670
auto maybeBraceScope =
689-
scopeCreator.ifUniqueConstructExpandAndInsert<BraceStmtScope>(
690-
p, bs, std::move(localFuncsAndTypes), std::move(localVars));
671+
scopeCreator.ifUniqueConstructExpandAndInsert<BraceStmtScope>(p, bs);
691672
if (auto *s = scopeCreator.getASTContext().Stats)
692673
++s->getFrontendCounters().NumBraceStmtASTScopes;
693-
694674
return maybeBraceScope.getPtrOr(p);
695675
}
696676

@@ -701,24 +681,24 @@ class NodeAdder
701681
if (auto *var = patternBinding->getSingleVar())
702682
scopeCreator.addChildrenForKnownAttributes(var, parentScope);
703683

684+
const bool isLocalBinding = patternBinding->getDeclContext()->isLocalContext();
685+
686+
const DeclVisibilityKind vis =
687+
isLocalBinding ? DeclVisibilityKind::LocalVariable
688+
: DeclVisibilityKind::MemberOfCurrentNominal;
704689
auto *insertionPoint = parentScope;
705690
for (auto i : range(patternBinding->getNumPatternEntries())) {
706-
bool isLocalBinding = false;
707-
if (auto *varDecl = patternBinding->getAnchoringVarDecl(i)) {
708-
isLocalBinding = varDecl->getDeclContext()->isLocalContext();
709-
}
710-
711691
insertionPoint =
712692
scopeCreator
713693
.ifUniqueConstructExpandAndInsert<PatternEntryDeclScope>(
714-
insertionPoint, patternBinding, i, isLocalBinding)
694+
insertionPoint, patternBinding, i, vis)
715695
.getPtrOr(insertionPoint);
716-
717-
ASTScopeAssert(isLocalBinding || insertionPoint == parentScope,
718-
"Bindings at the top-level or members of types should "
719-
"not change the insertion point");
720696
}
721697

698+
ASTScopeAssert(isLocalBinding || insertionPoint == parentScope,
699+
"Bindings at the top-level or members of types should "
700+
"not change the insertion point");
701+
722702
return insertionPoint;
723703
}
724704

@@ -991,7 +971,7 @@ PatternEntryDeclScope::expandAScopeThatCreatesANewInsertionPoint(
991971
"Original inits are always after the '='");
992972
scopeCreator
993973
.constructExpandAndInsertUncheckable<PatternEntryInitializerScope>(
994-
this, decl, patternEntryIndex, isLocalBinding);
974+
this, decl, patternEntryIndex, vis);
995975
}
996976

997977
// Add accessors for the variables in this pattern.
@@ -1002,7 +982,7 @@ PatternEntryDeclScope::expandAScopeThatCreatesANewInsertionPoint(
1002982
// In local context, the PatternEntryDeclScope becomes the insertion point, so
1003983
// that all any bindings introduecd by the pattern are in scope for subsequent
1004984
// lookups.
1005-
if (isLocalBinding)
985+
if (vis == DeclVisibilityKind::LocalVariable)
1006986
return {this, "All code that follows is inside this scope"};
1007987

1008988
return {getParent().get(), "Global and type members do not introduce scopes"};
@@ -1378,9 +1358,8 @@ ASTScopeImpl *LabeledConditionalStmtScope::createNestedConditionalClauseScopes(
13781358

13791359
AbstractPatternEntryScope::AbstractPatternEntryScope(
13801360
PatternBindingDecl *declBeingScoped, unsigned entryIndex,
1381-
bool isLocalBinding)
1382-
: decl(declBeingScoped), patternEntryIndex(entryIndex),
1383-
isLocalBinding(isLocalBinding) {
1361+
DeclVisibilityKind vis)
1362+
: decl(declBeingScoped), patternEntryIndex(entryIndex), vis(vis) {
13841363
ASTScopeAssert(entryIndex < declBeingScoped->getPatternList().size(),
13851364
"out of bounds");
13861365
}

lib/AST/ASTScopeLookup.cpp

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ bool ASTScopeImpl::lookInGenericParametersOf(
222222
SmallVector<ValueDecl *, 32> bindings;
223223
for (auto *param : paramList.get()->getParams())
224224
bindings.push_back(param);
225-
if (consumer.consume(bindings))
225+
if (consumer.consume(bindings, DeclVisibilityKind::GenericParameter))
226226
return true;
227227
return false;
228228
}
@@ -289,26 +289,28 @@ PatternEntryInitializerScope::getLookupParent() const {
289289

290290
bool GenericParamScope::lookupLocalsOrMembers(DeclConsumer consumer) const {
291291
auto *param = paramList->getParams()[index];
292-
return consumer.consume({param});
292+
return consumer.consume({param}, DeclVisibilityKind::GenericParameter);
293293
}
294294

295295
bool PatternEntryDeclScope::lookupLocalsOrMembers(DeclConsumer consumer) const {
296-
if (!isLocalBinding)
297-
return false;
298-
return lookupLocalBindingsInPattern(getPattern(), consumer);
296+
if (vis != DeclVisibilityKind::LocalVariable)
297+
return false; // look in self type will find this later
298+
return lookupLocalBindingsInPattern(getPattern(), vis, consumer);
299299
}
300300

301301
bool ForEachPatternScope::lookupLocalsOrMembers(DeclConsumer consumer) const {
302-
return lookupLocalBindingsInPattern(stmt->getPattern(), consumer);
302+
return lookupLocalBindingsInPattern(
303+
stmt->getPattern(), DeclVisibilityKind::LocalVariable, consumer);
303304
}
304305

305306
bool CaseLabelItemScope::lookupLocalsOrMembers(DeclConsumer consumer) const {
306-
return lookupLocalBindingsInPattern(item.getPattern(), consumer);
307+
return lookupLocalBindingsInPattern(
308+
item.getPattern(), DeclVisibilityKind::LocalVariable, consumer);
307309
}
308310

309311
bool CaseStmtBodyScope::lookupLocalsOrMembers(DeclConsumer consumer) const {
310312
for (auto *var : stmt->getCaseBodyVariablesOrEmptyArray())
311-
if (consumer.consume({var}))
313+
if (consumer.consume({var}, DeclVisibilityKind::LocalVariable))
312314
return true;
313315

314316
return false;
@@ -318,12 +320,13 @@ bool FunctionBodyScope::lookupLocalsOrMembers(
318320
DeclConsumer consumer) const {
319321
if (auto *paramList = decl->getParameters()) {
320322
for (auto *paramDecl : *paramList)
321-
if (consumer.consume({paramDecl}))
323+
if (consumer.consume({paramDecl}, DeclVisibilityKind::FunctionParameter))
322324
return true;
323325
}
324326

325327
if (decl->getDeclContext()->isTypeContext()) {
326-
return consumer.consume({decl->getImplicitSelfDecl()});
328+
return consumer.consume({decl->getImplicitSelfDecl()},
329+
DeclVisibilityKind::FunctionParameter);
327330
}
328331

329332
// Consider \c var t: T { (did/will/)get/set { ... t }}
@@ -332,7 +335,7 @@ bool FunctionBodyScope::lookupLocalsOrMembers(
332335
// then t needs to be found as a local binding:
333336
if (auto *accessor = dyn_cast<AccessorDecl>(decl)) {
334337
if (auto *storage = accessor->getStorage())
335-
if (consumer.consume({storage}))
338+
if (consumer.consume({storage}, DeclVisibilityKind::LocalVariable))
336339
return true;
337340
}
338341

@@ -343,7 +346,7 @@ bool SpecializeAttributeScope::lookupLocalsOrMembers(
343346
DeclConsumer consumer) const {
344347
if (auto *params = whatWasSpecialized->getGenericParams())
345348
for (auto *param : params->getParams())
346-
if (consumer.consume({param}))
349+
if (consumer.consume({param}, DeclVisibilityKind::GenericParameter))
347350
return true;
348351
return false;
349352
}
@@ -353,7 +356,7 @@ bool DifferentiableAttributeScope::lookupLocalsOrMembers(
353356
auto visitAbstractFunctionDecl = [&](AbstractFunctionDecl *afd) {
354357
if (auto *params = afd->getGenericParams())
355358
for (auto *param : params->getParams())
356-
if (consumer.consume({param}))
359+
if (consumer.consume({param}, DeclVisibilityKind::GenericParameter))
357360
return true;
358361
return false;
359362
};
@@ -368,10 +371,20 @@ bool DifferentiableAttributeScope::lookupLocalsOrMembers(
368371
}
369372

370373
bool BraceStmtScope::lookupLocalsOrMembers(DeclConsumer consumer) const {
371-
if (consumer.consume(localFuncsAndTypes))
372-
return true;
373-
374-
if (consumer.consumePossiblyNotInScope(localVars))
374+
// All types and functions are visible anywhere within a brace statement
375+
// scope. When ordering matters (i.e. var decl) we will have split the brace
376+
// statement into nested scopes.
377+
//
378+
// Don't stop at the first one, there may be local funcs with same base name
379+
// and want them all.
380+
SmallVector<ValueDecl *, 32> localBindings;
381+
for (auto braceElement : stmt->getElements()) {
382+
if (auto localBinding = braceElement.dyn_cast<Decl *>()) {
383+
if (auto *vd = dyn_cast<ValueDecl>(localBinding))
384+
localBindings.push_back(vd);
385+
}
386+
}
387+
if (consumer.consume(localBindings, DeclVisibilityKind::LocalVariable))
375388
return true;
376389

377390
if (consumer.finishLookupInBraceStmt(stmt))
@@ -387,15 +400,18 @@ bool PatternEntryInitializerScope::lookupLocalsOrMembers(
387400
decl->getInitContext(0));
388401
if (initContext) {
389402
if (auto *selfParam = initContext->getImplicitSelfDecl()) {
390-
return consumer.consume({selfParam});
403+
return consumer.consume({selfParam},
404+
DeclVisibilityKind::FunctionParameter);
391405
}
392406
}
393407
return false;
394408
}
395409

396410
bool CaptureListScope::lookupLocalsOrMembers(DeclConsumer consumer) const {
397411
for (auto &e : expr->getCaptureList()) {
398-
if (consumer.consume({e.Var}))
412+
if (consumer.consume(
413+
{e.Var},
414+
DeclVisibilityKind::LocalVariable)) // or FunctionParameter??
399415
return true;
400416
}
401417
return false;
@@ -404,24 +420,26 @@ bool CaptureListScope::lookupLocalsOrMembers(DeclConsumer consumer) const {
404420
bool ClosureParametersScope::lookupLocalsOrMembers(
405421
DeclConsumer consumer) const {
406422
for (auto param : *closureExpr->getParameters())
407-
if (consumer.consume({param}))
423+
if (consumer.consume({param}, DeclVisibilityKind::FunctionParameter))
408424
return true;
409425
return false;
410426
}
411427

412428
bool ConditionalClausePatternUseScope::lookupLocalsOrMembers(
413429
DeclConsumer consumer) const {
414-
return lookupLocalBindingsInPattern(pattern, consumer);
430+
return lookupLocalBindingsInPattern(
431+
pattern, DeclVisibilityKind::LocalVariable, consumer);
415432
}
416433

417434
bool ASTScopeImpl::lookupLocalBindingsInPattern(const Pattern *p,
435+
DeclVisibilityKind vis,
418436
DeclConsumer consumer) {
419437
if (!p)
420438
return false;
421439
bool isDone = false;
422440
p->forEachVariable([&](VarDecl *var) {
423441
if (!isDone)
424-
isDone = consumer.consume({var});
442+
isDone = consumer.consume({var}, vis);
425443
});
426444
return isDone;
427445
}

lib/AST/Decl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,8 +1447,7 @@ void PatternBindingEntry::setInit(Expr *E) {
14471447
VarDecl *PatternBindingEntry::getAnchoringVarDecl() const {
14481448
SmallVector<VarDecl *, 8> variables;
14491449
getPattern()->collectVariables(variables);
1450-
if (variables.empty())
1451-
return nullptr;
1450+
assert(!variables.empty());
14521451
return variables[0];
14531452
}
14541453

0 commit comments

Comments
 (0)