Skip to content

Commit 9b2cd5e

Browse files
committed
[NameLookup] Teach unqualified lookup to resolve backing property wrapper
and projected value references
1 parent 3a47087 commit 9b2cd5e

File tree

5 files changed

+33
-2
lines changed

5 files changed

+33
-2
lines changed

include/swift/AST/NameLookup.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ enum class UnqualifiedLookupFlags {
225225
/// This lookup should include results from outside the innermost scope with
226226
/// results.
227227
IncludeOuterResults = 1 << 4,
228+
/// Includes property wrapper name lookup results
229+
IncludePropertyWrapperResults = 1 << 5,
228230
};
229231

230232
using UnqualifiedLookupOptions = OptionSet<UnqualifiedLookupFlags>;

lib/AST/UnqualifiedLookup.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/AST/ModuleNameLookup.h"
2323
#include "swift/AST/NameLookup.h"
2424
#include "swift/AST/NameLookupRequests.h"
25+
#include "swift/AST/PropertyWrappers.h"
2526
#include "swift/Basic/Debug.h"
2627
#include "swift/Basic/STLExtras.h"
2728
#include "swift/Basic/SourceManager.h"
@@ -602,8 +603,28 @@ bool ASTScopeDeclConsumerForUnqualifiedLookup::consume(
602603
}
603604
}
604605

605-
if (!value->getName().matchesRef(factory.Name.getFullName()))
606-
continue;
606+
auto fullName = factory.Name.getFullName();
607+
if (!value->getName().matchesRef(fullName)) {
608+
if (!factory.options.contains(UnqualifiedLookupFlags::IncludePropertyWrapperResults))
609+
continue;
610+
611+
auto *varDecl = dyn_cast<VarDecl>(value);
612+
if (!varDecl || !varDecl->hasAttachedPropertyWrapper())
613+
continue;
614+
615+
auto wrapperInfo = varDecl->getPropertyWrapperBackingPropertyInfo();
616+
if (!wrapperInfo)
617+
continue;
618+
619+
if (wrapperInfo.backingVar->ValueDecl::getName().matchesRef(fullName)) {
620+
value = wrapperInfo.backingVar;
621+
} else if (wrapperInfo.projectionVar &&
622+
wrapperInfo.projectionVar->ValueDecl::getName().matchesRef(fullName)) {
623+
value = wrapperInfo.projectionVar;
624+
} else {
625+
continue;
626+
}
627+
}
607628

608629
// In order to preserve the behavior of the existing context-based lookup,
609630
// which finds all results for non-local variables at the top level instead

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,10 @@ Expr *TypeChecker::resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE,
538538
// name/module qualifier to access top-level name.
539539
lookupOptions |= NameLookupFlags::IncludeOuterResults;
540540

541+
// Include property wrapper results in case we have a reference to a backing
542+
// property wrapper or projected value
543+
lookupOptions |= NameLookupFlags::IncludePropertyWrapperResults;
544+
541545
if (Loc.isInvalid())
542546
DC = DC->getModuleScopeContext();
543547

lib/Sema/TypeCheckNameLookup.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ convertToUnqualifiedLookupOptions(NameLookupOptions options) {
211211
newOptions |= UnqualifiedLookupFlags::IgnoreAccessControl;
212212
if (options.contains(NameLookupFlags::IncludeOuterResults))
213213
newOptions |= UnqualifiedLookupFlags::IncludeOuterResults;
214+
if (options.contains(NameLookupFlags::IncludePropertyWrapperResults))
215+
newOptions |= UnqualifiedLookupFlags::IncludePropertyWrapperResults;
214216

215217
return newOptions;
216218
}

lib/Sema/TypeChecker.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ enum class NameLookupFlags {
192192
/// Whether to include results from outside the innermost scope that has a
193193
/// result.
194194
IncludeOuterResults = 1 << 1,
195+
/// Whether to consider property wrapper names
196+
IncludePropertyWrapperResults = 1 << 2,
195197
};
196198

197199
/// A set of options that control name lookup.

0 commit comments

Comments
 (0)