Skip to content

[Property Wrappers] Only re-write assign_by_wrapper to assignment if all fields have been initialized. #31248

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
merged 1 commit into from
Apr 24, 2020
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
10 changes: 9 additions & 1 deletion lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,15 @@ void LifetimeChecker::handleStoreUse(unsigned UseID) {
if (isFullyUninitialized) {
Use.Kind = DIUseKind::Initialization;
} else if (isFullyInitialized) {
Use.Kind = DIUseKind::Assign;
// Only re-write assign_by_wrapper to assignment if all fields have been
// initialized.
if (isa<AssignByWrapperInst>(Use.Inst) &&
getAnyUninitializedMemberAtInst(Use.Inst, 0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you still need the isFullyUninitialized check above? Unless I'm missing something, it seems like "property wrapper is uninitialized" would be a special case of "any field uninitialized".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That case is still needed for non-property-wrapped properties

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like we need to use assign_by_wrapper for everything then, but maybe not in this PR :-)

TheMemory.getNumElements()) != -1) {
Use.Kind = DIUseKind::Initialization;
} else {
Use.Kind = DIUseKind::Assign;
}
} else {
// If it is initialized on some paths, but not others, then we have an
// inconsistent initialization, which needs dynamic control logic in the
Expand Down
32 changes: 32 additions & 0 deletions test/SILOptimizer/di_property_wrappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,37 @@ public final class Synchronized<Value> {
}
}

struct SR_12341 {
@Wrapper var wrapped: Int = 10
var str: String

init() {
wrapped = 42
str = ""
wrapped = 27
}

init(condition: Bool) {
wrapped = 42
wrapped = 27
str = ""
}
}

func testSR_12341() {
// CHECK: ## SR_12341
print("\n## SR_12341")

// CHECK-NEXT: .. init 10
// CHECK-NEXT: .. init 42
// CHECK-NEXT: .. set 27
_ = SR_12341()

// CHECK-NEXT: .. init 10
// CHECK-NEXT: .. init 42
// CHECK-NEXT: .. init 27
_ = SR_12341(condition: true)
}

testIntStruct()
testIntClass()
Expand All @@ -511,3 +542,4 @@ testOptIntStruct()
testDefaultNilOptIntStruct()
testComposed()
testWrapperInitWithDefaultArg()
testSR_12341()