Skip to content

[4.1] DefiniteInitialization: Storing back to the 'self' box in a class init is OK. #14395

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
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
22 changes: 17 additions & 5 deletions lib/SILOptimizer/Mandatory/DIMemoryUseCollectorOwnership.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1128,8 +1128,14 @@ void ElementUseCollector::collectClassSelfUses() {
}

// A store of a load from the box is ignored.
// FIXME: SILGen should not emit these.
if (auto *LI = dyn_cast<LoadInst>(SI->getSrc()))
// SILGen emits these if delegation to another initializer was
// interrupted before the initializer was called.
SILValue src = SI->getSrc();
// Look through conversions.
while (auto conversion = dyn_cast<ConversionInst>(src))
src = conversion->getConverted();

if (auto *LI = dyn_cast<LoadInst>(src))
if (LI->getOperand() == MUI)
continue;

Expand Down Expand Up @@ -1503,7 +1509,7 @@ void DelegatingInitElementUseCollector::collectClassInitSelfUses() {
// Stores to self.
if (auto *SI = dyn_cast<StoreInst>(User)) {
if (Op->getOperandNumber() == 1) {
// The initial store of 'self' into the box at the start of the
// A store of 'self' into the box at the start of the
// function. Ignore it.
if (auto *Arg = dyn_cast<SILArgument>(SI->getSrc())) {
if (Arg->getParent() == MUI->getParent()) {
Expand All @@ -1513,8 +1519,14 @@ void DelegatingInitElementUseCollector::collectClassInitSelfUses() {
}

// A store of a load from the box is ignored.
// FIXME: SILGen should not emit these.
if (auto *LI = dyn_cast<LoadInst>(SI->getSrc()))
// SILGen emits these if delegation to another initializer was
// interrupted before the initializer was called.
SILValue src = SI->getSrc();
// Look through conversions.
while (auto conversion = dyn_cast<ConversionInst>(src))
src = conversion->getConverted();

if (auto *LI = dyn_cast<LoadInst>(src))
if (LI->getOperand() == MUI)
continue;

Expand Down
9 changes: 8 additions & 1 deletion lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1636,7 +1636,14 @@ void LifetimeChecker::handleLoadUseFailure(const DIMemoryUse &Use,
bool SuperInitDone,
bool FailedSelfUse) {
SILInstruction *Inst = Use.Inst;


// Stores back to the 'self' box are OK.
if (auto store = dyn_cast<StoreInst>(Inst)) {
if (store->getDest() == TheMemory.MemoryInst
&& TheMemory.isClassInitSelf())
return;
}

if (FailedSelfUse) {
emitSelfConsumedDiagnostic(Inst);
return;
Expand Down
12 changes: 12 additions & 0 deletions test/SILOptimizer/definite-init-try-in-self-init-argument.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %target-swift-frontend -emit-sil -verify %s

class Y: X {
required init(_: Z) throws {
try super.init(Z())
}
}
class Z { init() throws {} }

class X {
required init(_: Z) throws {}
}