Skip to content

Commit ff3d081

Browse files
Merge pull request #25403 from ravikandhadai/rdar51198592-DI-crash
[Definite Initialization] Fix a bug that made DI wrongly think that an unused access to "self" in a delegating initializer is a use of the form: `type(of:self)`
2 parents fdb6e08 + 8d1d863 commit ff3d081

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

lib/SILOptimizer/Mandatory/DIMemoryUseCollector.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,11 +1568,15 @@ collectDelegatingInitUses(const DIMemoryObjectInfo &TheMemory,
15681568
// be an end_borrow use in addition to the value_metatype.
15691569
if (isa<LoadBorrowInst>(User)) {
15701570
auto UserVal = cast<SingleValueInstruction>(User);
1571-
bool onlyUseIsValueMetatype = true;
1571+
bool onlyUseIsValueMetatype = false;
15721572
for (auto use : UserVal->getUses()) {
1573-
if (isa<EndBorrowInst>(use->getUser())
1574-
|| isa<ValueMetatypeInst>(use->getUser()))
1573+
auto *user = use->getUser();
1574+
if (isa<EndBorrowInst>(user))
15751575
continue;
1576+
if (isa<ValueMetatypeInst>(user)) {
1577+
onlyUseIsValueMetatype = true;
1578+
continue;
1579+
}
15761580
onlyUseIsValueMetatype = false;
15771581
break;
15781582
}

test/SILOptimizer/definite_init_diagnostics.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,3 +1579,12 @@ class WeakCycle {
15791579
self.d = 10
15801580
}
15811581
}
1582+
1583+
// <rdar://51198592> DI was crashing as it wrongly detected a `type(of: self)`
1584+
// use in a delegating initializer, when there was none.
1585+
class DelegatingInitTest {
1586+
convenience init(x: Int) {
1587+
self // expected-warning {{expression of type 'DelegatingInitTest' is unused}}
1588+
// expected-error@-1 {{'self' used before 'self.init' call or assignment to 'self'}}
1589+
} // expected-error {{'self.init' isn't called on all paths before returning from initializer}}
1590+
}

test/SILOptimizer/definite_init_markuninitialized_delegatingself.sil

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,20 @@ bb2:
275275
%7 = tuple ()
276276
return %7 : $()
277277
}
278+
279+
// <rdar://51198592> DI was crashing as it wrongly detected a `type(of: self)`
280+
// use in a delegating initializer, when there was none.
281+
class MyClass4 {
282+
}
283+
284+
sil hidden [ossa] @test_self_uninit_use_in_delegating_init : $@convention(method) (@thick MyClass4.Type) -> @owned MyClass4 {
285+
bb0(%1 : $@thick MyClass4.Type):
286+
%2 = alloc_stack $MyClass4, let, name "self"
287+
%3 = mark_uninitialized [delegatingself] %2 : $*MyClass4
288+
%5 = load_borrow %3 : $*MyClass4 // expected-error {{'self' used before 'self.init' call or assignment to 'self'}}
289+
end_borrow %5 : $MyClass4
290+
%7 = load [copy] %3 : $*MyClass4 // expected-error {{'self.init' isn't called on all paths before returning from initializer}}
291+
destroy_addr %3 : $*MyClass4
292+
dealloc_stack %2 : $*MyClass4
293+
return %7 : $MyClass4
294+
}

0 commit comments

Comments
 (0)