Skip to content

[SE-0400] All properties with init accessors become part of the memberwise init #66978

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
2 changes: 1 addition & 1 deletion lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7170,7 +7170,7 @@ bool VarDecl::isMemberwiseInitialized(bool preferDeclaredProperties) const {
// other stored properties.
if (hasInitAccessor()) {
if (auto *init = getAccessor(AccessorKind::Init))
return init->getAttrs().hasAttribute<InitializesAttr>();
return true;
}

// If this is a computed property, it's not memberwise initialized unless
Expand Down
5 changes: 3 additions & 2 deletions lib/SILGen/SILGenConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ static void emitImplicitValueConstructor(SILGenFunction &SGF,
decl->collectPropertiesInitializableByInitAccessors(initializedViaAccessor);

// Emit the indirect return argument, if any.
bool hasInitAccessors = !decl->getInitAccessorProperties().empty();
SILValue resultSlot;
if (selfTy.isAddress()) {
auto &AC = SGF.getASTContext();
Expand All @@ -371,7 +372,7 @@ static void emitImplicitValueConstructor(SILGenFunction &SGF,
VD->setSpecifier(ParamSpecifier::InOut);
VD->setInterfaceType(selfIfaceTy);
resultSlot = SGF.F.begin()->createFunctionArgument(selfTy, VD);
} else if (!initializedViaAccessor.empty()) {
} else if (hasInitAccessors) {
// Allocate "self" on stack which we are going to use to
// reference/init fields and then load to return.
resultSlot = SGF.emitTemporaryAllocation(Loc, selfTy);
Expand Down Expand Up @@ -504,7 +505,7 @@ static void emitImplicitValueConstructor(SILGenFunction &SGF,
}

// Load as "take" from our stack allocation and return.
if (!selfTy.isAddress() && !initializedViaAccessor.empty()) {
if (!selfTy.isAddress() && hasInitAccessors) {
auto resultValue = SGF.B.emitLoadValueOperation(
Loc, resultSlot, LoadOwnershipQualifier::Take);

Expand Down
4 changes: 0 additions & 4 deletions lib/Sema/CodeSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1300,10 +1300,6 @@ HasMemberwiseInitRequest::evaluate(Evaluator &evaluator,
if (!var->isMemberwiseInitialized(/*preferDeclaredProperties=*/true))
continue;

// If init accessors are not involved, we are done.
if (initializedViaAccessor.empty())
return true;

// Check whether use of init accessors results in access to uninitialized
// properties.

Expand Down
2 changes: 1 addition & 1 deletion test/Interpreter/init_accessors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ func test_memberwise_ordering() {
var _c: Int
}

let test3 = Test3(_a: 1, _b: 2, _c: 3)
let test3 = Test3(_a: 1, _b: 2, pair: (1, 2), _c: 3)
print("test-memberwise-ordering-3: \(test3)")
}

Expand Down
2 changes: 1 addition & 1 deletion test/decl/var/init_accessors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ func test_memberwise_ordering() {
var _c: Int
}

_ = Test4(_a: 0, _b: 1, _c: 2) // Ok
_ = Test4(_a: 0, _b: 1, pair: (1, 2), _c: 2) // Ok

struct Test5 {
var _a: Int
Expand Down