Skip to content

Commit 9e77c9a

Browse files
authored
Merge pull request #31958 from hborla/5.3-property-wrapper-default-init
[5.3][Property Wrappers] Fix handling of properties that are default initialized via property wrapper
2 parents 508b217 + f8cf96e commit 9e77c9a

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

lib/AST/Decl.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6484,9 +6484,13 @@ ParamDecl::getDefaultValueStringRepresentation(
64846484
return getASTContext().SourceMgr.extractText(charRange);
64856485
}
64866486

6487-
// If there is no parent initializer, we used the default initializer.
6488-
auto parentInit = original->getParentInitializer();
6489-
if (!parentInit) {
6487+
// If there is no initial wrapped value, we used the default initializer.
6488+
Expr *wrappedValue = nullptr;
6489+
if (auto *parentInit = original->getParentInitializer())
6490+
if (auto *placeholder = findWrappedValuePlaceholder(parentInit))
6491+
wrappedValue = placeholder->getOriginalWrappedValue();
6492+
6493+
if (!wrappedValue) {
64906494
if (auto type = original->getPropertyWrapperBackingPropertyType()) {
64916495
if (auto nominal = type->getAnyNominal()) {
64926496
scratch.clear();
@@ -6501,9 +6505,8 @@ ParamDecl::getDefaultValueStringRepresentation(
65016505
return ".init()";
65026506
}
65036507

6504-
auto init =
6505-
findWrappedValuePlaceholder(parentInit)->getOriginalWrappedValue();
6506-
return extractInlinableText(getASTContext().SourceMgr, init, scratch);
6508+
auto &sourceMgr = getASTContext().SourceMgr;
6509+
return extractInlinableText(sourceMgr, wrappedValue, scratch);
65076510
}
65086511
}
65096512

lib/Sema/ConstraintSystem.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4247,13 +4247,16 @@ void SolutionApplicationTarget::maybeApplyPropertyWrapper() {
42474247
if (!outermostWrapperType)
42484248
return;
42494249

4250+
bool isImplicit = false;
4251+
42504252
// Retrieve the outermost wrapper argument. If there isn't one, we're
42514253
// performing default initialization.
42524254
auto outermostArg = outermostWrapperAttr->getArg();
42534255
if (!outermostArg) {
42544256
SourceLoc fakeParenLoc = outermostWrapperAttr->getRange().End;
42554257
outermostArg = TupleExpr::createEmpty(
42564258
ctx, fakeParenLoc, fakeParenLoc, /*Implicit=*/true);
4259+
isImplicit = true;
42574260
}
42584261

42594262
auto typeExpr = TypeExpr::createImplicitHack(
@@ -4264,7 +4267,7 @@ void SolutionApplicationTarget::maybeApplyPropertyWrapper() {
42644267
outermostWrapperAttr->getArgumentLabels(),
42654268
outermostWrapperAttr->getArgumentLabelLocs(),
42664269
/*hasTrailingClosure=*/false,
4267-
/*implicit=*/false);
4270+
/*implicit=*/isImplicit);
42684271
}
42694272
wrapperAttrs[0]->setSemanticInit(backingInitializer);
42704273

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %target-swift-frontend -print-ast %s | %FileCheck %s
2+
3+
@propertyWrapper
4+
struct Wrapper {
5+
init() {}
6+
7+
var wrappedValue: Int = 0
8+
}
9+
10+
// CHECK-LABEL: internal struct UseWrapperDefaultInit
11+
struct UseWrapperDefaultInit {
12+
@Wrapper var value
13+
// CHECK: internal init(value: Wrapper = Wrapper())
14+
}
15+
16+
let _ = UseWrapperDefaultInit()
17+

0 commit comments

Comments
 (0)