Skip to content

[move-keyword] Make sure we error correctly on f(_move y, &y) and f(&y, _move y) #60614

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
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,24 @@ struct ClosureOperandState {

} // namespace

/// Is this a reinit instruction that we know how to convert into its init form.
static bool isReinitToInitConvertibleInst(Operand *memUse) {
auto *memInst = memUse->getUser();
switch (memInst->getKind()) {
default:
return false;

case SILInstructionKind::CopyAddrInst: {
auto *cai = cast<CopyAddrInst>(memInst);
return !cai->isInitializationOfDest();
}
case SILInstructionKind::StoreInst: {
auto *si = cast<StoreInst>(memInst);
return si->getOwnershipQualifier() == StoreOwnershipQualifier::Assign;
}
}
}

static void convertMemoryReinitToInitForm(SILInstruction *memInst) {
switch (memInst->getKind()) {
default:
Expand Down Expand Up @@ -932,7 +950,7 @@ bool GatherClosureUseVisitor::visitUse(Operand *op, AccessUseType useTy) {
return true;
}

if (memInstMustReinitialize(op)) {
if (isReinitToInitConvertibleInst(op)) {
if (stripAccessMarkers(op->get()) != useState.address) {
LLVM_DEBUG(llvm::dbgs()
<< "!!! Error! Found reinit use not on base address: "
Expand Down Expand Up @@ -1308,7 +1326,7 @@ bool GatherLexicalLifetimeUseVisitor::visitUse(Operand *op,
return true;
}

if (memInstMustReinitialize(op)) {
if (isReinitToInitConvertibleInst(op)) {
if (stripAccessMarkers(op->get()) != useState.address) {
LLVM_DEBUG(llvm::dbgs()
<< "!!! Error! Found reinit use not on base address: "
Expand Down
39 changes: 39 additions & 0 deletions test/SILOptimizer/move_function_kills_copyable_addresses.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// RUN: %target-sil-opt -enable-sil-verify-all -o - -sil-move-kills-copyable-addresses-checker -verify %s

// This file is meant for specific SIL patterns that may be hard to produce with
// the current swift frontend but have reproduced before and we want to make
// sure keep on working.

sil_stage raw

class Klass {}

sil @useTwice : $@convention(thin) (@guaranteed Klass, @inout Klass) -> ()

sil hidden [ossa] @$s4test3fooyyAA5KlassCnF : $@convention(thin) (@owned Klass) -> () {
bb0(%0 : @owned $Klass):
%1 = begin_borrow [lexical] %0 : $Klass
debug_value %1 : $Klass, let, name "k", argno 1
%3 = alloc_stack [lexical] $Klass, var, name "k2" // expected-error {{'k2' used after being moved}}
%4 = copy_value %1 : $Klass
%5 = move_value [allows_diagnostics] %4 : $Klass
store %5 to [init] %3 : $*Klass
%7 = begin_access [modify] [static] %3 : $*Klass
%8 = alloc_stack $Klass
mark_unresolved_move_addr %7 to %8 : $*Klass // expected-note {{move here}}
%10 = load [copy] %8 : $*Klass
destroy_addr %8 : $*Klass
end_access %7 : $*Klass
%13 = begin_access [modify] [static] %3 : $*Klass
%14 = function_ref @useTwice : $@convention(thin) (@guaranteed Klass, @inout Klass) -> ()
%15 = apply %14(%10, %13) : $@convention(thin) (@guaranteed Klass, @inout Klass) -> () // expected-note {{use here}}
end_access %13 : $*Klass
destroy_value %10 : $Klass
dealloc_stack %8 : $*Klass
destroy_addr %3 : $*Klass
dealloc_stack %3 : $*Klass
end_borrow %1 : $Klass
destroy_value %0 : $Klass
%23 = tuple ()
return %23 : $()
}
Original file line number Diff line number Diff line change
Expand Up @@ -688,3 +688,19 @@ func reinitInPieces1<T : P>(_ k: ProtPair<T>) {
k2.lhs = selfType.getP()
k2.rhs = selfType.getP()
}

////////////////////////
// InOut and Use Test //
////////////////////////

func useValueAndInOut<T>(_ x: T, _ y: inout T) {}
func useValueAndInOut<T>(_ x: inout T, _ y: T) {}

func inoutAndUseTest<T>(_ x: T) {
var y = x // expected-error {{'y' used after being moved}}
// expected-error @-1 {{'y' used after being moved}}
useValueAndInOut(_move y, &y) // expected-note {{use here}}
// expected-note @-1 {{move here}}
useValueAndInOut(&y, _move y) // expected-note {{use here}}
// expected-note @-1 {{move here}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -729,3 +729,19 @@ func reinitInPieces1(_ k: KlassPair) {
k2.lhs = Klass()
k2.rhs = Klass()
}

////////////////////////
// InOut and Use Test //
////////////////////////

func useValueAndInOut(_ x: Klass, _ y: inout Klass) {}
func useValueAndInOut(_ x: inout Klass, _ y: Klass) {}

func inoutAndUseTest(_ x: Klass) {
var y = x // expected-error {{'y' used after being moved}}
// expected-error @-1 {{'y' used after being moved}}
useValueAndInOut(_move y, &y) // expected-note {{use here}}
// expected-note @-1 {{move here}}
useValueAndInOut(&y, _move y) // expected-note {{use here}}
// expected-note @-1 {{move here}}
}