Skip to content

Commit 6bf9312

Browse files
committed
Handle module selectors with local vars right
1 parent 75d3825 commit 6bf9312

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
@@ -724,6 +724,7 @@ class DeclNameRef {
724724
public:
725725
static DeclNameRef createSubscript();
726726
static DeclNameRef createConstructor();
727+
static DeclNameRef createSelf(const ASTContext &ctx);
727728

728729
DeclNameRef() : storage(DeclName()) { }
729730

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;
@@ -768,20 +768,20 @@ class ASTScope : public ASTAllocated<ASTScope> {
768768
///
769769
/// \param stopAfterInnermostBraceStmt If lookup should consider
770770
/// local declarations inside the innermost syntactic scope only.
771-
static void lookupLocalDecls(SourceFile *, DeclName, SourceLoc,
771+
static void lookupLocalDecls(SourceFile *, DeclNameRef, SourceLoc,
772772
bool stopAfterInnermostBraceStmt,
773773
ABIRole roleFilter,
774774
SmallVectorImpl<ValueDecl *> &);
775775

776-
static void lookupLocalDecls(SourceFile *sf, DeclName name, SourceLoc loc,
776+
static void lookupLocalDecls(SourceFile *sf, DeclNameRef name, SourceLoc loc,
777777
bool stopAfterInnermostBraceStmt,
778778
SmallVectorImpl<ValueDecl *> &results) {
779779
lookupLocalDecls(sf, name, loc, stopAfterInnermostBraceStmt,
780780
ABIRole::ProvidesAPI, results);
781781
}
782782

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

786786
/// Entry point to record the visible statement labels from the given
787787
/// point.

lib/AST/Identifier.cpp

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

208+
DeclNameRef DeclNameRef::createSelf(const ASTContext &ctx) {
209+
return DeclNameRef(ctx.Id_self);
210+
}
211+
208212
void DeclNameRef::dump() const {
209213
llvm::errs() << *this << "\n";
210214
}

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
}
@@ -907,14 +908,14 @@ namespace {
907908

908909
class ASTScopeDeclConsumerForLocalLookup
909910
: public AbstractASTScopeDeclConsumer {
910-
DeclName name;
911+
DeclNameRef name;
911912
bool stopAfterInnermostBraceStmt;
912913
ABIRole roleFilter;
913914
SmallVectorImpl<ValueDecl *> &results;
914915

915916
public:
916917
ASTScopeDeclConsumerForLocalLookup(
917-
DeclName name, bool stopAfterInnermostBraceStmt,
918+
DeclNameRef name, bool stopAfterInnermostBraceStmt,
918919
ABIRole roleFilter, SmallVectorImpl<ValueDecl *> &results)
919920
: name(name), stopAfterInnermostBraceStmt(stopAfterInnermostBraceStmt),
920921
roleFilter(roleFilter), results(results) {}
@@ -925,6 +926,8 @@ class ASTScopeDeclConsumerForLocalLookup
925926

926927
bool consume(ArrayRef<ValueDecl *> values,
927928
NullablePtr<DeclContext> baseDC) override {
929+
if (name.hasModuleSelector()) return false;
930+
928931
for (auto *value: values) {
929932
bool foundMatch = false;
930933
if (auto *varDecl = dyn_cast<VarDecl>(value)) {
@@ -938,7 +941,7 @@ class ASTScopeDeclConsumerForLocalLookup
938941
});
939942
}
940943

941-
if (!foundMatch && value->getName().matchesRef(name)
944+
if (!foundMatch && value->getName().matchesRef(name.getFullName())
942945
&& hasCorrectABIRole(value))
943946
results.push_back(value);
944947
}
@@ -965,7 +968,7 @@ class ASTScopeDeclConsumerForLocalLookup
965968

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

977-
ValueDecl *ASTScope::lookupSingleLocalDecl(SourceFile *sf, DeclName name,
980+
ValueDecl *ASTScope::lookupSingleLocalDecl(SourceFile *sf, DeclNameRef name,
978981
SourceLoc loc) {
979982
SmallVector<ValueDecl *, 1> result;
980983
ASTScope::lookupLocalDecls(sf, name, loc,

lib/Sema/CSDiagnostics.cpp

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

lib/Sema/CSGen.cpp

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

lib/Sema/PreCheckTarget.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,7 @@ Expr *TypeChecker::resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE,
558558
}
559559
}
560560

561-
DeclName lookupName(context, Name.getBaseName(), lookupLabels);
562-
LookupName = DeclNameRef(lookupName);
561+
LookupName = Name.withArgumentLabels(context, lookupLabels);
563562
}
564563

565564
// Perform standard value name lookup.
@@ -576,8 +575,7 @@ Expr *TypeChecker::resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE,
576575

577576
// First, look for a local binding in scope.
578577
if (Loc.isValid() && !Name.isOperator()) {
579-
ASTScope::lookupLocalDecls(DC->getParentSourceFile(),
580-
LookupName.getFullName(), Loc,
578+
ASTScope::lookupLocalDecls(DC->getParentSourceFile(), LookupName, Loc,
581579
/*stopAfterInnermostBraceStmt=*/false,
582580
ResultValues);
583581
for (auto *localDecl : ResultValues) {
@@ -2080,8 +2078,9 @@ VarDecl *PreCheckTarget::getImplicitSelfDeclForSuperContext(SourceLoc Loc) {
20802078

20812079
// Do an actual lookup for 'self' in case it shows up in a capture list.
20822080
auto *methodSelf = methodContext->getImplicitSelfDecl();
2083-
auto *lookupSelf = ASTScope::lookupSingleLocalDecl(DC->getParentSourceFile(),
2084-
Ctx.Id_self, Loc);
2081+
auto *lookupSelf = ASTScope::lookupSingleLocalDecl(
2082+
DC->getParentSourceFile(),
2083+
DeclNameRef::createSelf(Ctx), Loc);
20852084
if (lookupSelf && lookupSelf != methodSelf) {
20862085
// FIXME: This is the wrong diagnostic for if someone manually declares a
20872086
// variable named 'self' using backticks.

lib/Sema/TypeCheckDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,8 @@ BodyInitKindRequest::evaluate(Evaluator &evaluator,
574574
auto loc = declRef->getLoc();
575575
if (name.isSimpleName(ctx.Id_self)) {
576576
auto *otherSelfDecl =
577-
ASTScope::lookupSingleLocalDecl(Decl->getParentSourceFile(),
578-
name.getFullName(), loc);
577+
ASTScope::lookupSingleLocalDecl(Decl->getParentSourceFile(), name,
578+
loc);
579579
if (otherSelfDecl == Decl->getImplicitSelfDecl())
580580
myKind = BodyInitKind::Delegating;
581581
}

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,8 @@ CheckRedeclarationRequest::evaluate(Evaluator &eval, ValueDecl *current,
754754
else
755755
roleFilter = currentIsABIOnly ? ABIRole::ProvidesABI
756756
: ABIRole::ProvidesAPI;
757-
ASTScope::lookupLocalDecls(currentFile, current->getBaseName(),
757+
ASTScope::lookupLocalDecls(currentFile,
758+
DeclNameRef(current->getBaseName()),
758759
current->getLoc(),
759760
/*stopAfterInnermostBraceStmt=*/true,
760761
roleFilter, otherDefinitions);

test/NameLookup/module_selector.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ func main::decl1(
329329
// expected-error@-2 {{name in constant declaration cannot be qualified with a module selector}} expected-note@-2 {{remove module selector from this name}} {{28-34=}}
330330

331331
switch Optional(main::decl1g) {
332+
// expected-error@-1 {{cannot find 'main::decl1g' in scope}}
332333
case Optional.some(let main::decl1i):
333334
// expected-error@-1 {{name in constant declaration cannot be qualified with a module selector}} expected-note@-1 {{remove module selector from this name}} {{26-32=}}
334335
break
@@ -337,6 +338,7 @@ func main::decl1(
337338
}
338339

339340
switch Optional(main::decl1g) {
341+
// expected-error@-1 {{cannot find 'main::decl1g' in scope}}
340342
case let Optional.some(main::decl1j):
341343
// expected-error@-1 {{name in constant declaration cannot be qualified with a module selector}} expected-note@-1 {{remove module selector from this name}} {{26-32=}}
342344
break
@@ -345,6 +347,7 @@ func main::decl1(
345347
}
346348

347349
switch Optional(main::decl1g) {
350+
// expected-error@-1 {{cannot find 'main::decl1g' in scope}}
348351
case let main::decl1k?:
349352
// expected-error@-1 {{name in constant declaration cannot be qualified with a module selector}} expected-note@-1 {{remove module selector from this name}} {{11-17=}}
350353
break

0 commit comments

Comments
 (0)