Skip to content

Commit 6933382

Browse files
committed
[Property delegates] Fix default argument printing with default initialization
1 parent f34aa79 commit 6933382

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

lib/AST/Decl.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5334,7 +5334,9 @@ bool VarDecl::isMemberwiseInitialized(bool preferDeclaredProperties) const {
53345334
origVar = origDelegate;
53355335
if (origVar->getFormalAccess() < AccessLevel::Internal &&
53365336
origVar->getAttachedPropertyDelegate() &&
5337-
origVar->isParentInitialized())
5337+
(origVar->isParentInitialized() ||
5338+
(origVar->getParentPatternBinding() &&
5339+
origVar->getParentPatternBinding()->isDefaultInitializable())))
53385340
return false;
53395341

53405342
return true;
@@ -5745,8 +5747,25 @@ ParamDecl::getDefaultValueStringRepresentation(
57455747
return getASTContext().SourceMgr.extractText(charRange);
57465748
}
57475749

5748-
auto init = findOriginalPropertyDelegateInitialValue(
5749-
original, original->getParentInitializer());
5750+
// If there is no parent initializer, we used the default initializer.
5751+
auto parentInit = original->getParentInitializer();
5752+
if (!parentInit) {
5753+
if (auto type = original->getPropertyDelegateBackingPropertyType()) {
5754+
if (auto nominal = type->getAnyNominal()) {
5755+
scratch.clear();
5756+
auto typeName = nominal->getName().str();
5757+
scratch.append(typeName.begin(), typeName.end());
5758+
scratch.push_back('(');
5759+
scratch.push_back(')');
5760+
return {scratch.data(), scratch.size()};
5761+
}
5762+
}
5763+
5764+
return ".init()";
5765+
}
5766+
5767+
auto init =
5768+
findOriginalPropertyDelegateInitialValue(original, parentInit);
57505769
return extractInlinableText(getASTContext().SourceMgr, init, scratch);
57515770
}
57525771
}

test/IDE/print_property_delegates.swift

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,28 @@
55

66
@_propertyDelegate
77
struct Delegate<Value> {
8-
var value: Value
8+
var _stored: Value?
9+
10+
var value: Value {
11+
get {
12+
return _stored!
13+
}
14+
15+
set {
16+
_stored = newValue
17+
}
18+
}
19+
20+
init() {
21+
self._stored = nil
22+
}
923

1024
init(initialValue: Value) {
11-
self.value = initialValue
25+
self._stored = initialValue
1226
}
1327

1428
init(closure: () -> Value) {
15-
self.value = closure()
29+
self._stored = closure()
1630
}
1731
}
1832

@@ -29,8 +43,11 @@ struct HasDelegates {
2943
@Delegate
3044
var y = true
3145

46+
@Delegate
47+
var z: String
48+
3249
// Memberwise initializer.
33-
// CHECK: init(x: Delegate<Int> = Delegate(closure: foo), y: Bool = true)
50+
// CHECK: init(x: Delegate<Int> = Delegate(closure: foo), y: Bool = true, z: String = Delegate())
3451
}
3552

3653
func trigger() {

0 commit comments

Comments
 (0)