Skip to content

[Sema] Always install property wrappers during qualified lookup #29937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions lib/AST/NameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1534,6 +1534,34 @@ bool DeclContext::lookupQualified(Type type,
return lookupQualified(nominalTypesToLookInto, member, options, decls);
}

static void installPropertyWrapperMembersIfNeeded(NominalTypeDecl *target,
DeclNameRef member) {
auto &Context = target->getASTContext();
auto baseName = member.getBaseName();
if (!member.isSimpleName() || baseName.isSpecial())
return;

if ((!baseName.getIdentifier().str().startswith("$") &&
!baseName.getIdentifier().str().startswith("_")) ||
baseName.getIdentifier().str().size() <= 1) {
return;
}

// $- and _-prefixed variables can be generated by properties that have
// attached property wrappers.
auto originalPropertyName =
Context.getIdentifier(baseName.getIdentifier().str().substr(1));
for (auto member : target->lookupDirect(originalPropertyName)) {
if (auto var = dyn_cast<VarDecl>(member)) {
if (var->hasAttachedPropertyWrapper()) {
auto sourceFile = var->getDeclContext()->getParentSourceFile();
if (sourceFile && sourceFile->Kind != SourceFileKind::Interface)
(void)var->getPropertyWrapperBackingProperty();
}
}
}
}

bool DeclContext::lookupQualified(ArrayRef<NominalTypeDecl *> typeDecls,
DeclNameRef member,
NLOptions options,
Expand Down Expand Up @@ -1584,6 +1612,7 @@ QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,

// Visit all of the nominal types we know about, discovering any others
// we need along the way.
auto &ctx = DC->getASTContext();
bool wantProtocolMembers = (options & NL_ProtocolMembers);
while (!stack.empty()) {
auto current = stack.back();
Expand All @@ -1592,6 +1621,11 @@ QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
if (tracker)
tracker->addUsedMember({current, member.getBaseName()},isLookupCascading);

// Make sure we've resolved property wrappers, if we need them.
if (ctx.areSemanticQueriesEnabled()) {
installPropertyWrapperMembersIfNeeded(current, member);
}

// Look for results within the current nominal type and its extensions.
bool currentIsProtocol = isa<ProtocolDecl>(current);
auto flags = OptionSet<NominalTypeDecl::LookupDirectFlags>();
Expand Down
40 changes: 0 additions & 40 deletions lib/Sema/TypeCheckNameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "swift/AST/NameLookup.h"
#include "swift/AST/NameLookupRequests.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/AST/SourceFile.h"
#include "swift/Basic/TopCollection.h"
#include <algorithm>

Expand Down Expand Up @@ -230,47 +229,10 @@ convertToUnqualifiedLookupOptions(NameLookupOptions options) {
return newOptions;
}

static void installPropertyWrapperMembersIfNeeded(NominalTypeDecl *target,
DeclName member) {
if (!target) return;

auto &Context = target->getASTContext();
auto baseName = member.getBaseName();
if (!member.isSimpleName() || baseName.isSpecial())
return;

if ((!baseName.getIdentifier().str().startswith("$") &&
!baseName.getIdentifier().str().startswith("_")) ||
baseName.getIdentifier().str().size() <= 1) {
return;
}

// $- and _-prefixed variables can be generated by properties that have
// attached property wrappers.
auto originalPropertyName =
Context.getIdentifier(baseName.getIdentifier().str().substr(1));
for (auto member : target->lookupDirect(originalPropertyName)) {
if (auto var = dyn_cast<VarDecl>(member)) {
if (var->hasAttachedPropertyWrapper()) {
auto sourceFile = var->getDeclContext()->getParentSourceFile();
if (sourceFile && sourceFile->Kind != SourceFileKind::Interface)
(void)var->getPropertyWrapperBackingProperty();
}
}
}
}

LookupResult TypeChecker::lookupUnqualified(DeclContext *dc, DeclNameRef name,
SourceLoc loc,
NameLookupOptions options) {
auto ulOptions = convertToUnqualifiedLookupOptions(options);

// Make sure we've resolved implicit members, if we need them.
if (auto *current = dc->getInnermostTypeContext()) {
installPropertyWrapperMembersIfNeeded(current->getSelfNominalTypeDecl(),
name.getFullName());
}

auto &ctx = dc->getASTContext();
auto descriptor = UnqualifiedLookupDescriptor(name, dc, loc, ulOptions);
auto lookup = evaluateOrDefault(ctx.evaluator,
Expand Down Expand Up @@ -365,7 +327,6 @@ LookupResult TypeChecker::lookupMember(DeclContext *dc,
// Make sure we've resolved implicit members, if we need them.
if (auto *current = type->getAnyNominal()) {
current->synthesizeSemanticMembersIfNeeded(name.getFullName());
installPropertyWrapperMembersIfNeeded(current, name.getFullName());
}

LookupResultBuilder builder(result, dc, options);
Expand Down Expand Up @@ -445,7 +406,6 @@ LookupTypeResult TypeChecker::lookupMemberType(DeclContext *dc,
// Make sure we've resolved implicit members, if we need them.
if (auto *current = type->getAnyNominal()) {
current->synthesizeSemanticMembersIfNeeded(name.getFullName());
installPropertyWrapperMembersIfNeeded(current, name.getFullName());
}

if (!dc->lookupQualified(type, name, subOptions, decls))
Expand Down
23 changes: 23 additions & 0 deletions test/decl/var/Inputs/property_wrappers_multi_file_2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@propertyWrapper
public struct WrapGod<T> {
private var value: T

public init(wrappedValue: T) {
value = wrappedValue
}

public var wrappedValue: T {
get { value }
set { value = newValue }
}

public var projectedValue: T {
get { value }
set { value = newValue }
}
}

class BaseClass {
@WrapGod final var value: Int = 42
init() {}
}
10 changes: 10 additions & 0 deletions test/decl/var/property_wrappers_multi_file.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -verify -O -primary-file %s %S/Inputs/property_wrappers_multi_file_2.swift -c -o %t/use.o
// RUN: %target-swift-frontend %s -verify -O -primary-file %S/Inputs/property_wrappers_multi_file_2.swift %s -c -o %t/def.o

final class Subclass: BaseClass {
override init() {
super.init()
$value = 42
}
}