Skip to content

Commit 65ef6ea

Browse files
authored
Merge pull request #60944 from xedin/suppress-type-wrapper-memberwise-init-when-user-has-one
[Sema] TypeWrappers: Disable memberwise synthesis if type has designa…
2 parents e7d55b2 + 6d2edc2 commit 65ef6ea

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

lib/Sema/CodeSynthesis.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,10 +1255,14 @@ void TypeChecker::addImplicitConstructors(NominalTypeDecl *decl) {
12551255
return;
12561256

12571257
if (!shouldAttemptInitializerSynthesis(decl)) {
1258-
// If declaration is type wrapped, synthesize a
1259-
// special initializer that would instantiate storage.
1260-
if (decl->hasTypeWrapper())
1261-
(void)decl->getTypeWrapperInitializer();
1258+
if (decl->hasTypeWrapper()) {
1259+
auto &ctx = decl->getASTContext();
1260+
// If declaration is type wrapped and there are no
1261+
// designated initializers, synthesize a special
1262+
// memberwise initializer that would instantiate `$_storage`.
1263+
if (!hasUserDefinedDesignatedInit(ctx.evaluator, decl))
1264+
(void)decl->getTypeWrapperInitializer();
1265+
}
12621266

12631267
decl->setAddedImplicitInitializers();
12641268
return;

test/Interpreter/Inputs/type_wrapper_defs.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,13 @@ public struct PersonWithUnmanagedTest {
136136

137137
@PropWrapper public var favoredColor: String = "red"
138138
}
139+
140+
@Wrapper
141+
public class ClassWithDesignatedInit {
142+
public var a: Int
143+
@PropWrapperWithoutInit public var b: [Int]
144+
145+
public init(a: Int, b: [Int] = [1, 2, 3]) {
146+
$_storage = .init(memberwise: $Storage(a: 42, _b: PropWrapperWithoutInit(value: b)))
147+
}
148+
}

test/Interpreter/type_wrappers.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,3 +381,30 @@ do {
381381
// CHECK: in getter
382382
// CHECK-NEXT: yellow
383383
}
384+
385+
do {
386+
var test = ClassWithDesignatedInit(a: 42)
387+
388+
print(test.a)
389+
// CHECK: in getter
390+
// CHECK-NEXT: 42
391+
392+
print(test.b)
393+
// CHECK: in getter
394+
// CHECK-NEXT: [1, 2, 3]
395+
396+
test.a = 0
397+
// CHECK: in setter => 0
398+
399+
test.b = [42]
400+
// CHECK: in getter
401+
// CHECK-NEXT: in setter => PropWrapperWithoutInit<Array<Int>>(value: [42])
402+
403+
print(test.a)
404+
// CHECK: in getter
405+
// CHECK-NEXT: 0
406+
407+
print(test.b)
408+
// CHECK: in getter
409+
// CHECK-NEXT: [42]
410+
}

0 commit comments

Comments
 (0)