Skip to content

Commit d2528bc

Browse files
committed
Handle module selectors with local vars right
1 parent 4be229e commit d2528bc

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
@@ -780,6 +780,7 @@ class DeclNameRef {
780780
public:
781781
static DeclNameRef createSubscript();
782782
static DeclNameRef createConstructor();
783+
static DeclNameRef createSelf(const ASTContext &ctx);
783784

784785
DeclNameRef() : storage(DeclName()) { }
785786

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
}
@@ -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
@@ -1776,7 +1776,7 @@ class VarDeclMultipleReferencesChecker : public ASTWalker {
17761776
if (name.isSimpleName(varDecl->getName()) && loc.isValid()) {
17771777
auto *otherDecl =
17781778
ASTScope::lookupSingleLocalDecl(DC->getParentSourceFile(),
1779-
name.getFullName(), loc);
1779+
name, loc);
17801780
if (otherDecl == varDecl)
17811781
++count;
17821782
}

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
@@ -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) {
@@ -2081,8 +2079,9 @@ VarDecl *PreCheckTarget::getImplicitSelfDeclForSuperContext(SourceLoc Loc) {
20812079

20822080
// Do an actual lookup for 'self' in case it shows up in a capture list.
20832081
auto *methodSelf = methodContext->getImplicitSelfDecl();
2084-
auto *lookupSelf = ASTScope::lookupSingleLocalDecl(DC->getParentSourceFile(),
2085-
Ctx.Id_self, Loc);
2082+
auto *lookupSelf = ASTScope::lookupSingleLocalDecl(
2083+
DC->getParentSourceFile(),
2084+
DeclNameRef::createSelf(Ctx), Loc);
20862085
if (lookupSelf && lookupSelf != methodSelf) {
20872086
// FIXME: This is the wrong diagnostic for if someone manually declares a
20882087
// 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
@@ -731,7 +731,8 @@ CheckRedeclarationRequest::evaluate(Evaluator &eval, ValueDecl *current,
731731
else
732732
roleFilter = currentIsABIOnly ? ABIRole::ProvidesABI
733733
: ABIRole::ProvidesAPI;
734-
ASTScope::lookupLocalDecls(currentFile, current->getBaseName(),
734+
ASTScope::lookupLocalDecls(currentFile,
735+
DeclNameRef(current->getBaseName()),
735736
current->getLoc(),
736737
/*stopAfterInnermostBraceStmt=*/true,
737738
roleFilter, otherDefinitions);

test/NameLookup/module_selector.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,20 +275,23 @@ func decl1(
275275
label p3: @escaping () -> A
276276
) {
277277
switch Optional(main::p2) {
278+
// expected-error@-1 {{cannot find 'main::p2' in scope}}
278279
case Optional.some(let decl1i):
279280
break
280281
case .none:
281282
break
282283
}
283284

284285
switch Optional(main::p2) {
286+
// expected-error@-1 {{cannot find 'main::p2' in scope}}
285287
case let Optional.some(decl1j):
286288
break
287289
case .none:
288290
break
289291
}
290292

291293
switch Optional(main::p2) {
294+
// expected-error@-1 {{cannot find 'main::p2' in scope}}
292295
case let decl1k?:
293296
break
294297
case .none:

0 commit comments

Comments
 (0)