Skip to content

[5.3][Property Wrappers] Fix handling of properties that are default initialized via property wrapper #31958

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
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
15 changes: 9 additions & 6 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6484,9 +6484,13 @@ ParamDecl::getDefaultValueStringRepresentation(
return getASTContext().SourceMgr.extractText(charRange);
}

// If there is no parent initializer, we used the default initializer.
auto parentInit = original->getParentInitializer();
if (!parentInit) {
// If there is no initial wrapped value, we used the default initializer.
Expr *wrappedValue = nullptr;
if (auto *parentInit = original->getParentInitializer())
if (auto *placeholder = findWrappedValuePlaceholder(parentInit))
wrappedValue = placeholder->getOriginalWrappedValue();

if (!wrappedValue) {
if (auto type = original->getPropertyWrapperBackingPropertyType()) {
if (auto nominal = type->getAnyNominal()) {
scratch.clear();
Expand All @@ -6501,9 +6505,8 @@ ParamDecl::getDefaultValueStringRepresentation(
return ".init()";
}

auto init =
findWrappedValuePlaceholder(parentInit)->getOriginalWrappedValue();
return extractInlinableText(getASTContext().SourceMgr, init, scratch);
auto &sourceMgr = getASTContext().SourceMgr;
return extractInlinableText(sourceMgr, wrappedValue, scratch);
}
}

Expand Down
5 changes: 4 additions & 1 deletion lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4247,13 +4247,16 @@ void SolutionApplicationTarget::maybeApplyPropertyWrapper() {
if (!outermostWrapperType)
return;

bool isImplicit = false;

// Retrieve the outermost wrapper argument. If there isn't one, we're
// performing default initialization.
auto outermostArg = outermostWrapperAttr->getArg();
if (!outermostArg) {
SourceLoc fakeParenLoc = outermostWrapperAttr->getRange().End;
outermostArg = TupleExpr::createEmpty(
ctx, fakeParenLoc, fakeParenLoc, /*Implicit=*/true);
isImplicit = true;
}

auto typeExpr = TypeExpr::createImplicitHack(
Expand All @@ -4264,7 +4267,7 @@ void SolutionApplicationTarget::maybeApplyPropertyWrapper() {
outermostWrapperAttr->getArgumentLabels(),
outermostWrapperAttr->getArgumentLabelLocs(),
/*hasTrailingClosure=*/false,
/*implicit=*/false);
/*implicit=*/isImplicit);
}
wrapperAttrs[0]->setSemanticInit(backingInitializer);

Expand Down
17 changes: 17 additions & 0 deletions test/decl/var/property_wrappers_default_init.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %target-swift-frontend -print-ast %s | %FileCheck %s

@propertyWrapper
struct Wrapper {
init() {}

var wrappedValue: Int = 0
}

// CHECK-LABEL: internal struct UseWrapperDefaultInit
struct UseWrapperDefaultInit {
@Wrapper var value
// CHECK: internal init(value: Wrapper = Wrapper())
}

let _ = UseWrapperDefaultInit()