Skip to content

Commit a97bbf9

Browse files
authored
Merge pull request #31949 from hborla/property-wrapper-default-init
[Property Wrappers] Fix handling of properties that are default initialized via property wrapper
2 parents defac25 + fcc4bef commit a97bbf9

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
@@ -6505,9 +6505,13 @@ ParamDecl::getDefaultValueStringRepresentation(
65056505
return getASTContext().SourceMgr.extractText(charRange);
65066506
}
65076507

6508-
// If there is no parent initializer, we used the default initializer.
6509-
auto parentInit = original->getParentInitializer();
6510-
if (!parentInit) {
6508+
// If there is no initial wrapped value, we used the default initializer.
6509+
Expr *wrappedValue = nullptr;
6510+
if (auto *parentInit = original->getParentInitializer())
6511+
if (auto *placeholder = findWrappedValuePlaceholder(parentInit))
6512+
wrappedValue = placeholder->getOriginalWrappedValue();
6513+
6514+
if (!wrappedValue) {
65116515
if (auto type = original->getPropertyWrapperBackingPropertyType()) {
65126516
if (auto nominal = type->getAnyNominal()) {
65136517
scratch.clear();
@@ -6522,9 +6526,8 @@ ParamDecl::getDefaultValueStringRepresentation(
65226526
return ".init()";
65236527
}
65246528

6525-
auto init =
6526-
findWrappedValuePlaceholder(parentInit)->getOriginalWrappedValue();
6527-
return extractInlinableText(getASTContext().SourceMgr, init, scratch);
6529+
auto &sourceMgr = getASTContext().SourceMgr;
6530+
return extractInlinableText(sourceMgr, wrappedValue, scratch);
65286531
}
65296532
}
65306533

lib/Sema/ConstraintSystem.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4284,13 +4284,16 @@ void SolutionApplicationTarget::maybeApplyPropertyWrapper() {
42844284
if (!outermostWrapperType)
42854285
return;
42864286

4287+
bool isImplicit = false;
4288+
42874289
// Retrieve the outermost wrapper argument. If there isn't one, we're
42884290
// performing default initialization.
42894291
auto outermostArg = outermostWrapperAttr->getArg();
42904292
if (!outermostArg) {
42914293
SourceLoc fakeParenLoc = outermostWrapperAttr->getRange().End;
42924294
outermostArg = TupleExpr::createEmpty(
42934295
ctx, fakeParenLoc, fakeParenLoc, /*Implicit=*/true);
4296+
isImplicit = true;
42944297
}
42954298

42964299
auto typeExpr = TypeExpr::createImplicitHack(
@@ -4301,7 +4304,7 @@ void SolutionApplicationTarget::maybeApplyPropertyWrapper() {
43014304
outermostWrapperAttr->getArgumentLabels(),
43024305
outermostWrapperAttr->getArgumentLabelLocs(),
43034306
/*hasTrailingClosure=*/false,
4304-
/*implicit=*/false);
4307+
/*implicit=*/isImplicit);
43054308
}
43064309
wrapperAttrs[0]->setSemanticInit(backingInitializer);
43074310

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)