Skip to content

Commit f29d636

Browse files
committed
Do not minimize memberwise initializer access level with nominal access level.
It is important for private declarations to have internal memberwise initializers: https://forums.swift.org/t/undiagnosed-private-structs-classes-with-public-members/31940/5
1 parent 6f706e9 commit f29d636

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

lib/Sema/CodeSynthesis.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,11 @@ ConstructorDecl *swift::createMemberwiseImplicitConstructor(
282282
ConstructorDecl *swift::getOrCreateEffectiveMemberwiseInitializer(
283283
ASTContext &ctx, NominalTypeDecl *nominal) {
284284
// Compute the access level for the memberwise initializer: the minimum of:
285-
// - The access level of the nominal type declaration itself.
285+
// - Public, by default. This enables public nominal types to have public
286+
// memberwise initializers.
286287
// - The access level of each memberwise-initialized property in the nominal
287288
// type declaration.
288-
auto accessLevel = std::min(AccessLevel::Public, nominal->getFormalAccess());
289+
auto accessLevel = AccessLevel::Public;
289290
for (auto *member : nominal->getMembers()) {
290291
auto var = dyn_cast<VarDecl>(member);
291292
if (!var ||

lib/Sema/CodeSynthesis.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ ConstructorDecl *createMemberwiseImplicitConstructor(ASTContext &ctx,
7070
// Get the effective memberwise initializer of the given nominal type, or create
7171
// it if it does not exist.
7272
// Sets the access level of the memberwise initializer to the minimum of:
73-
// - The access level of the nominal type declaration itself.
73+
// - Public, by default. This enables public nominal types to have public
74+
// memberwise initializers.
75+
// - NOTE(TF-1077): The `public` default is important for `TangentVector`
76+
// structs synthesized during `Differentiable` derived conformances.
77+
// Manually extending `TangentVector` structs to define a public
78+
// memberwise initializer causes a redeclaration error.
7479
// - The access level of each memberwise-initialized property in the nominal
7580
// type declaration.
7681
ConstructorDecl *getOrCreateEffectiveMemberwiseInitializer(

test/Sema/differentiable_access_level.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ private struct PrivateStruct: Differentiable {}
1919
// CHECK-LABEL: internal struct InternalStruct : Differentiable {
2020
// CHECK: internal init()
2121
// CHECK: internal struct TangentVector : Differentiable, AdditiveArithmetic, PointwiseMultiplicative, ElementaryFunctions {
22-
// CHECK: internal init()
22+
// CHECK: public init()
2323
// CHECK: }
2424
// CHECK: }
2525

2626
// CHECK-LABEL: private struct PrivateStruct : Differentiable {
2727
// CHECK: internal init()
2828
// CHECK: fileprivate struct TangentVector : Differentiable, AdditiveArithmetic, PointwiseMultiplicative, ElementaryFunctions {
29-
// CHECK: fileprivate init()
29+
// CHECK: public init()
3030
// CHECK: }
3131
// CHECK: }
3232

@@ -44,13 +44,13 @@ private class PrivateClass: Differentiable {}
4444
// CHECK-LABEL: internal class InternalClass : Differentiable {
4545
// CHECK: internal init()
4646
// CHECK: internal struct TangentVector : Differentiable, AdditiveArithmetic, PointwiseMultiplicative, ElementaryFunctions {
47-
// CHECK: internal init()
47+
// CHECK: public init()
4848
// CHECK: }
4949
// CHECK: }
5050

5151
// CHECK-LABEL: private class PrivateClass : Differentiable {
5252
// CHECK: internal init()
5353
// CHECK: fileprivate struct TangentVector : Differentiable, AdditiveArithmetic, PointwiseMultiplicative, ElementaryFunctions {
54-
// CHECK: fileprivate init()
54+
// CHECK: public init()
5555
// CHECK: }
5656
// CHECK: }

0 commit comments

Comments
 (0)