Skip to content

[TypeChecker] PropertyWrappers: Re-parent any captures from wrapped t… #61870

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
Nov 2, 2022
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
23 changes: 21 additions & 2 deletions lib/Sema/TypeCheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,13 @@ namespace {
return Action::SkipChildren(E);
}

// Capture lists need to be reparented to enclosing autoclosures.
if (auto CapE = dyn_cast<CaptureListExpr>(E)) {
if (isa<AutoClosureExpr>(ParentDC)) {
// Capture lists need to be reparented to enclosing autoclosures
// and/or initializers of property wrapper backing properties
// (because they subsume initializers associated with wrapped
// properties).
if (isa<AutoClosureExpr>(ParentDC) ||
isPropertyWrapperBackingPropertyInitContext(ParentDC)) {
for (auto &Cap : CapE->getCaptureList()) {
Cap.PBD->setDeclContext(ParentDC);
Cap.getVar()->setDeclContext(ParentDC);
Expand Down Expand Up @@ -136,6 +140,21 @@ namespace {
// variables.
return Action::VisitChildrenIf(isa<PatternBindingDecl>(D));
}

private:
static bool isPropertyWrapperBackingPropertyInitContext(DeclContext *DC) {
auto *init = dyn_cast<PatternBindingInitializer>(DC);
if (!init)
return false;

if (auto *PB = init->getBinding()) {
auto *var = PB->getSingleVar();
return var && var->getOriginalWrappedProperty(
PropertyWrapperSynthesizedPropertyKind::Backing);
}

return false;
}
};

static DeclName getDescriptiveName(AbstractFunctionDecl *AFD) {
Expand Down
37 changes: 37 additions & 0 deletions test/Sema/property_wrappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,40 @@ struct S2_58201 {
// CHECK: autoclosure_expr implicit type='() -> Bool' location={{.*}}.swift:[[@LINE+1]]:26 range=[{{.+}}] discriminator=1 escaping
@W_58201 var b: Bool = false
}

// https://github.com/apple/swift/issues/61570
// rdar://problem/101813792 - Check that captured variables are re-parented to backing var initializer
do {
@propertyWrapper
struct Wrapper {
init(_: String) {}

var wrappedValue: Int { return 0 }
}

struct TestExplicitCapture {
@Wrapper("\([1,2,3].map({[x = 42] in "\($0 + x)" }).reduce("", +))")
var wrapped: Int // Ok, becomes var _wrapped: Wrapper<Int> = Wrapper(wrappedValue: "\([1,2,3].map({[x = 42] in "\($0 + x)" }).reduce("", +)))
}

@propertyWrapper
struct Option {
let help: String
var value: Double = 0.0

var wrappedValue: Double {
get { value }
set { value = newValue }
}
}

enum TestEnum: String, CaseIterable {
case hello = "hello"
case world = "world"
}

struct TestImplicitCapture {
@Option(help: "Values: \(TestEnum.allCases.map(\.rawValue))")
var value // Ok - $kp$ of key path is captured implicitly by backing variable init
}
}