Skip to content
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

[5.1] [SE-0258] Trigger synthesis of _foo/$foo from name lookup. #25821

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/Sema/CodeSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,9 @@ static VarDecl *synthesizePropertyWrapperStorageWrapperProperty(
Identifier name = ctx.getIdentifier(nameBuf);

// Determine the type of the property.
if (!wrapperVar->hasInterfaceType()) {
static_cast<TypeChecker &>(*ctx.getLazyResolver()).validateDecl(wrapperVar);
}
Type propertyType = wrapperType->getTypeOfMember(
var->getModuleContext(), wrapperVar,
wrapperVar->getValueInterfaceType());
Expand Down
19 changes: 19 additions & 0 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5506,6 +5506,25 @@ void TypeChecker::synthesizeMemberForLookup(NominalTypeDecl *target,
if (!evaluateTargetConformanceTo(decodableProto))
(void)evaluateTargetConformanceTo(encodableProto);
}

if ((baseName.getIdentifier().str().startswith("$") ||
baseName.getIdentifier().str().startswith("_")) &&
baseName.getIdentifier().str().size() > 1) {
// $- 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->getPropertyWrapperBackingPropertyInfo();
}
}
}
}

} else {
auto argumentNames = member.getArgumentNames();
if (member.isCompoundName() && argumentNames.size() != 1)
Expand Down
23 changes: 23 additions & 0 deletions test/multifile/Inputs/rdar51725203.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@propertyWrapper
struct Wrapper<Value> {
var wrappedValue: Value

var projectedValue: Wrapper<Value> {
get { self }
set { self = newValue }
}

init(initialValue: Value) {
wrappedValue = initialValue
}
}

struct StructModel {
@Wrapper var foo: Int
@Wrapper var bar: Int // expected-note{{'_bar' declared here}}
}

class ClassModel {
@Wrapper var foo = 17
@Wrapper var bar = 17 // expected-note{{'_bar' declared here}}
}
9 changes: 9 additions & 0 deletions test/multifile/property-wrappers-rdar51725203.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %target-swift-frontend -typecheck -verify -primary-file %s %S/Inputs/rdar51725203.swift

func test(sm: StructModel, cm: ClassModel) {
_ = sm.$foo
_ = cm.$foo

_ = sm._bar // expected-error{{'_bar' is inaccessible due to 'private' protection level}}
_ = cm._bar // expected-error{{'_bar' is inaccessible due to 'private' protection level}}
}