Skip to content

Commit 5cb66a0

Browse files
authored
Merge pull request #31248 from hborla/property-wrapper-di
[Property Wrappers] Only re-write assign_by_wrapper to assignment if all fields have been initialized.
2 parents de2a8b7 + 3aabdb5 commit 5cb66a0

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,15 @@ void LifetimeChecker::handleStoreUse(unsigned UseID) {
10471047
if (isFullyUninitialized) {
10481048
Use.Kind = DIUseKind::Initialization;
10491049
} else if (isFullyInitialized) {
1050-
Use.Kind = DIUseKind::Assign;
1050+
// Only re-write assign_by_wrapper to assignment if all fields have been
1051+
// initialized.
1052+
if (isa<AssignByWrapperInst>(Use.Inst) &&
1053+
getAnyUninitializedMemberAtInst(Use.Inst, 0,
1054+
TheMemory.getNumElements()) != -1) {
1055+
Use.Kind = DIUseKind::Initialization;
1056+
} else {
1057+
Use.Kind = DIUseKind::Assign;
1058+
}
10511059
} else {
10521060
// If it is initialized on some paths, but not others, then we have an
10531061
// inconsistent initialization, which needs dynamic control logic in the

test/SILOptimizer/di_property_wrappers.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,37 @@ public final class Synchronized<Value> {
501501
}
502502
}
503503

504+
struct SR_12341 {
505+
@Wrapper var wrapped: Int = 10
506+
var str: String
507+
508+
init() {
509+
wrapped = 42
510+
str = ""
511+
wrapped = 27
512+
}
513+
514+
init(condition: Bool) {
515+
wrapped = 42
516+
wrapped = 27
517+
str = ""
518+
}
519+
}
520+
521+
func testSR_12341() {
522+
// CHECK: ## SR_12341
523+
print("\n## SR_12341")
524+
525+
// CHECK-NEXT: .. init 10
526+
// CHECK-NEXT: .. init 42
527+
// CHECK-NEXT: .. set 27
528+
_ = SR_12341()
529+
530+
// CHECK-NEXT: .. init 10
531+
// CHECK-NEXT: .. init 42
532+
// CHECK-NEXT: .. init 27
533+
_ = SR_12341(condition: true)
534+
}
504535

505536
testIntStruct()
506537
testIntClass()
@@ -511,3 +542,4 @@ testOptIntStruct()
511542
testDefaultNilOptIntStruct()
512543
testComposed()
513544
testWrapperInitWithDefaultArg()
545+
testSR_12341()

0 commit comments

Comments
 (0)