Skip to content

Commit a2ecb48

Browse files
committed
exclude private memberwise init
1 parent 8f7af45 commit a2ecb48

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

include/swift/Basic/Features.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,10 @@ EXPERIMENTAL_FEATURE(DefaultIsolationPerFile, false)
544544
/// Enable @_lifetime attribute
545545
SUPPRESSIBLE_EXPERIMENTAL_FEATURE(Lifetimes, true)
546546

547+
/// Excludes 'private' properties with initial values from the memberwise
548+
/// initializer.
549+
EXPERIMENTAL_FEATURE(ExcludePrivateFromMemberwiseInit, false)
550+
547551
/// Allow macro based aliases to be imported into Swift
548552
EXPERIMENTAL_FEATURE(ImportMacroAliases, true)
549553

lib/AST/Decl.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8412,10 +8412,17 @@ bool VarDecl::isMemberwiseInitialized(bool preferDeclaredProperties) const {
84128412
// If this is a stored property, and not a backing property in a case where
84138413
// we only want to see the declared properties, it can be memberwise
84148414
// initialized.
8415-
if (hasStorage() && preferDeclaredProperties &&
8416-
isBackingStorageForDeclaredProperty(this))
8417-
return false;
8418-
8415+
if (hasStorage()) {
8416+
if (isBackingStorageForDeclaredProperty(this)) {
8417+
if (preferDeclaredProperties)
8418+
return false;
8419+
} else if (getASTContext().LangOpts.hasFeature(Feature::ExcludePrivateFromMemberwiseInit)) {
8420+
// Private variables with initial values are never memberwise initialized.
8421+
if (getFormalAccess() == AccessLevel::Private)
8422+
if (hasInitialValue() || getParentPatternBinding()->isDefaultInitializable())
8423+
return false;
8424+
}
8425+
}
84198426
// If this is a computed property with `init` accessor, it's
84208427
// memberwise initializable when it could be used to initialize
84218428
// other stored properties.

lib/AST/FeatureSet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ UNINTERESTING_FEATURE(NonisolatedNonsendingByDefault)
127127
UNINTERESTING_FEATURE(KeyPathWithMethodMembers)
128128
UNINTERESTING_FEATURE(ImportMacroAliases)
129129
UNINTERESTING_FEATURE(NoExplicitNonIsolated)
130+
UNINTERESTING_FEATURE(ExcludePrivateFromMemberwiseInit)
130131

131132
// TODO: Return true for inlinable function bodies with module selectors in them
132133
UNINTERESTING_FEATURE(ModuleSelector)

0 commit comments

Comments
 (0)