Skip to content

[Sema] TypeWrappers: Disable memberwise synthesis if type has designa… #60944

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
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
12 changes: 8 additions & 4 deletions lib/Sema/CodeSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1255,10 +1255,14 @@ void TypeChecker::addImplicitConstructors(NominalTypeDecl *decl) {
return;

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

decl->setAddedImplicitInitializers();
return;
Expand Down
10 changes: 10 additions & 0 deletions test/Interpreter/Inputs/type_wrapper_defs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,13 @@ public struct PersonWithUnmanagedTest {

@PropWrapper public var favoredColor: String = "red"
}

@Wrapper
public class ClassWithDesignatedInit {
public var a: Int
@PropWrapperWithoutInit public var b: [Int]

public init(a: Int, b: [Int] = [1, 2, 3]) {
$_storage = .init(memberwise: $Storage(a: 42, _b: PropWrapperWithoutInit(value: b)))
}
}
27 changes: 27 additions & 0 deletions test/Interpreter/type_wrappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,30 @@ do {
// CHECK: in getter
// CHECK-NEXT: yellow
}

do {
var test = ClassWithDesignatedInit(a: 42)

print(test.a)
// CHECK: in getter
// CHECK-NEXT: 42

print(test.b)
// CHECK: in getter
// CHECK-NEXT: [1, 2, 3]

test.a = 0
// CHECK: in setter => 0

test.b = [42]
// CHECK: in getter
// CHECK-NEXT: in setter => PropWrapperWithoutInit<Array<Int>>(value: [42])

print(test.a)
// CHECK: in getter
// CHECK-NEXT: 0

print(test.b)
// CHECK: in getter
// CHECK-NEXT: [42]
}