Skip to content

Commit 7bf14de

Browse files
committed
Handle module selectors with local vars right
1 parent b35aaef commit 7bf14de

File tree

10 files changed

+32
-21
lines changed

10 files changed

+32
-21
lines changed

include/swift/AST/Identifier.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,7 @@ class DeclNameRef {
769769
public:
770770
static DeclNameRef createSubscript();
771771
static DeclNameRef createConstructor();
772+
static DeclNameRef createSelf(const ASTContext &ctx);
772773

773774
DeclNameRef() : storage(DeclName()) { }
774775

include/swift/AST/NameLookup.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
namespace swift {
3535
class ASTContext;
36-
class DeclName;
36+
class DeclNameRef;
3737
class Type;
3838
class TypeDecl;
3939
class ValueDecl;
@@ -773,20 +773,20 @@ class ASTScope : public ASTAllocated<ASTScope> {
773773
///
774774
/// \param stopAfterInnermostBraceStmt If lookup should consider
775775
/// local declarations inside the innermost syntactic scope only.
776-
static void lookupLocalDecls(SourceFile *, DeclName, SourceLoc,
776+
static void lookupLocalDecls(SourceFile *, DeclNameRef, SourceLoc,
777777
bool stopAfterInnermostBraceStmt,
778778
ABIRole roleFilter,
779779
SmallVectorImpl<ValueDecl *> &);
780780

781-
static void lookupLocalDecls(SourceFile *sf, DeclName name, SourceLoc loc,
781+
static void lookupLocalDecls(SourceFile *sf, DeclNameRef name, SourceLoc loc,
782782
bool stopAfterInnermostBraceStmt,
783783
SmallVectorImpl<ValueDecl *> &results) {
784784
lookupLocalDecls(sf, name, loc, stopAfterInnermostBraceStmt,
785785
ABIRole::ProvidesAPI, results);
786786
}
787787

788788
/// Returns the result if there is exactly one, nullptr otherwise.
789-
static ValueDecl *lookupSingleLocalDecl(SourceFile *, DeclName, SourceLoc);
789+
static ValueDecl *lookupSingleLocalDecl(SourceFile *, DeclNameRef, SourceLoc);
790790

791791
/// Entry point to record the visible statement labels from the given
792792
/// point.

lib/AST/Identifier.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ llvm::raw_ostream &DeclName::printPretty(llvm::raw_ostream &os) const {
220220
return print(os, /*skipEmptyArgumentNames=*/!isSpecial());
221221
}
222222

223+
DeclNameRef DeclNameRef::createSelf(const ASTContext &ctx) {
224+
return DeclNameRef(ctx.Id_self);
225+
}
226+
223227
void DeclNameRef::dump() const {
224228
llvm::errs() << *this << "\n";
225229
}

lib/AST/UnqualifiedLookup.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,8 @@ ValueDecl *UnqualifiedLookupFactory::lookupBaseDecl(const DeclContext *baseDC) c
383383
return nullptr;
384384

385385
auto selfDecl = ASTScope::lookupSingleLocalDecl(DC->getParentSourceFile(),
386-
DeclName(Ctx.Id_self), Loc);
386+
DeclNameRef::createSelf(Ctx),
387+
Loc);
387388
if (!selfDecl) {
388389
return nullptr;
389390
}
@@ -910,14 +911,14 @@ namespace {
910911

911912
class ASTScopeDeclConsumerForLocalLookup
912913
: public AbstractASTScopeDeclConsumer {
913-
DeclName name;
914+
DeclNameRef name;
914915
bool stopAfterInnermostBraceStmt;
915916
ABIRole roleFilter;
916917
SmallVectorImpl<ValueDecl *> &results;
917918

918919
public:
919920
ASTScopeDeclConsumerForLocalLookup(
920-
DeclName name, bool stopAfterInnermostBraceStmt,
921+
DeclNameRef name, bool stopAfterInnermostBraceStmt,
921922
ABIRole roleFilter, SmallVectorImpl<ValueDecl *> &results)
922923
: name(name), stopAfterInnermostBraceStmt(stopAfterInnermostBraceStmt),
923924
roleFilter(roleFilter), results(results) {}
@@ -928,6 +929,8 @@ class ASTScopeDeclConsumerForLocalLookup
928929

929930
bool consume(ArrayRef<ValueDecl *> values,
930931
NullablePtr<DeclContext> baseDC) override {
932+
if (name.hasModuleSelector()) return false;
933+
931934
for (auto *value: values) {
932935
bool foundMatch = false;
933936
if (auto *varDecl = dyn_cast<VarDecl>(value)) {
@@ -941,7 +944,7 @@ class ASTScopeDeclConsumerForLocalLookup
941944
});
942945
}
943946

944-
if (!foundMatch && value->getName().matchesRef(name)
947+
if (!foundMatch && value->getName().matchesRef(name.getFullName())
945948
&& hasCorrectABIRole(value))
946949
results.push_back(value);
947950
}
@@ -968,7 +971,7 @@ class ASTScopeDeclConsumerForLocalLookup
968971

969972
/// Lookup that only finds local declarations and does not trigger
970973
/// interface type computation.
971-
void ASTScope::lookupLocalDecls(SourceFile *sf, DeclName name, SourceLoc loc,
974+
void ASTScope::lookupLocalDecls(SourceFile *sf, DeclNameRef name, SourceLoc loc,
972975
bool stopAfterInnermostBraceStmt,
973976
ABIRole roleFilter,
974977
SmallVectorImpl<ValueDecl *> &results) {
@@ -977,7 +980,7 @@ void ASTScope::lookupLocalDecls(SourceFile *sf, DeclName name, SourceLoc loc,
977980
ASTScope::unqualifiedLookup(sf, loc, consumer);
978981
}
979982

980-
ValueDecl *ASTScope::lookupSingleLocalDecl(SourceFile *sf, DeclName name,
983+
ValueDecl *ASTScope::lookupSingleLocalDecl(SourceFile *sf, DeclNameRef name,
981984
SourceLoc loc) {
982985
SmallVector<ValueDecl *, 1> result;
983986
ASTScope::lookupLocalDecls(sf, name, loc,

lib/Sema/CSDiagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1758,7 +1758,7 @@ class VarDeclMultipleReferencesChecker : public ASTWalker {
17581758
if (name.isSimpleName(varDecl->getName()) && loc.isValid()) {
17591759
auto *otherDecl =
17601760
ASTScope::lookupSingleLocalDecl(DC->getParentSourceFile(),
1761-
name.getFullName(), loc);
1761+
name, loc);
17621762
if (otherDecl == varDecl)
17631763
++count;
17641764
}

lib/Sema/CSGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@ TypeVarRefCollector::walkToExprPre(Expr *expr) {
897897
auto loc = declRef->getLoc();
898898
if (name.isSimpleName() && loc.isValid()) {
899899
auto *SF = CS.DC->getParentSourceFile();
900-
auto *D = ASTScope::lookupSingleLocalDecl(SF, name.getFullName(), loc);
900+
auto *D = ASTScope::lookupSingleLocalDecl(SF, name, loc);
901901
inferTypeVars(D);
902902
}
903903
}

lib/Sema/PreCheckTarget.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,7 @@ static Expr *resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *DC,
560560
}
561561
}
562562

563-
DeclName lookupName(context, Name.getBaseName(), lookupLabels);
564-
LookupName = DeclNameRef(lookupName);
563+
LookupName = Name.withArgumentLabels(context, lookupLabels);
565564
}
566565

567566
LookupResult Lookup;
@@ -571,8 +570,7 @@ static Expr *resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *DC,
571570

572571
// First, look for a local binding in scope.
573572
if (Loc.isValid() && !Name.isOperator()) {
574-
ASTScope::lookupLocalDecls(DC->getParentSourceFile(),
575-
LookupName.getFullName(), Loc,
573+
ASTScope::lookupLocalDecls(DC->getParentSourceFile(), LookupName, Loc,
576574
/*stopAfterInnermostBraceStmt=*/false,
577575
ResultValues);
578576
for (auto *localDecl : ResultValues) {
@@ -2177,8 +2175,9 @@ VarDecl *PreCheckTarget::getImplicitSelfDeclForSuperContext(SourceLoc Loc) {
21772175

21782176
// Do an actual lookup for 'self' in case it shows up in a capture list.
21792177
auto *methodSelf = methodContext->getImplicitSelfDecl();
2180-
auto *lookupSelf = ASTScope::lookupSingleLocalDecl(DC->getParentSourceFile(),
2181-
Ctx.Id_self, Loc);
2178+
auto *lookupSelf = ASTScope::lookupSingleLocalDecl(
2179+
DC->getParentSourceFile(),
2180+
DeclNameRef::createSelf(Ctx), Loc);
21822181
if (lookupSelf && lookupSelf != methodSelf) {
21832182
// FIXME: This is the wrong diagnostic for if someone manually declares a
21842183
// variable named 'self' using backticks.

lib/Sema/TypeCheckDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,8 +577,8 @@ BodyInitKindRequest::evaluate(Evaluator &evaluator,
577577
auto loc = declRef->getLoc();
578578
if (name.isSimpleName(ctx.Id_self)) {
579579
auto *otherSelfDecl =
580-
ASTScope::lookupSingleLocalDecl(Decl->getParentSourceFile(),
581-
name.getFullName(), loc);
580+
ASTScope::lookupSingleLocalDecl(Decl->getParentSourceFile(), name,
581+
loc);
582582
if (otherSelfDecl == Decl->getImplicitSelfDecl())
583583
myKind = BodyInitKind::Delegating;
584584
}

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,8 @@ CheckRedeclarationRequest::evaluate(Evaluator &eval, ValueDecl *current,
757757
else
758758
roleFilter = currentIsABIOnly ? ABIRole::ProvidesABI
759759
: ABIRole::ProvidesAPI;
760-
ASTScope::lookupLocalDecls(currentFile, current->getBaseName(),
760+
ASTScope::lookupLocalDecls(currentFile,
761+
DeclNameRef(current->getBaseName()),
761762
current->getLoc(),
762763
/*stopAfterInnermostBraceStmt=*/true,
763764
roleFilter, otherDefinitions);

test/NameLookup/module_selector.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,20 +309,23 @@ func decl1(
309309
label p3: @escaping () -> A
310310
) {
311311
switch Optional(main::p2) {
312+
// expected-error@-1 {{cannot find 'main::p2' in scope}}
312313
case Optional.some(let decl1i):
313314
break
314315
case .none:
315316
break
316317
}
317318

318319
switch Optional(main::p2) {
320+
// expected-error@-1 {{cannot find 'main::p2' in scope}}
319321
case let Optional.some(decl1j):
320322
break
321323
case .none:
322324
break
323325
}
324326

325327
switch Optional(main::p2) {
328+
// expected-error@-1 {{cannot find 'main::p2' in scope}}
326329
case let decl1k?:
327330
break
328331
case .none:

0 commit comments

Comments
 (0)