Skip to content

[ownership-verifier] Allow owned enum arguments to be passed as guara… #14550

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
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
13 changes: 10 additions & 3 deletions lib/SIL/SILOwnershipVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1035,9 +1035,16 @@ OwnershipUseCheckerResult OwnershipCompatibilityUseChecker::visitEnumArgument(
// The operand has a non-trivial ownership kind. It must match the argument
// convention.
auto ownership = getOwnershipKind();
auto lifetimeConstraint = (ownership == ValueOwnershipKind::Owned)
? UseLifetimeConstraint::MustBeInvalidated
: UseLifetimeConstraint::MustBeLive;
UseLifetimeConstraint lifetimeConstraint;
if (ownership == ValueOwnershipKind::Owned) {
if (RequiredKind != ValueOwnershipKind::Owned) {
lifetimeConstraint = UseLifetimeConstraint::MustBeLive;
} else {
lifetimeConstraint = UseLifetimeConstraint::MustBeInvalidated;
}
} else {
lifetimeConstraint = UseLifetimeConstraint::MustBeLive;
}
return {compatibleOwnershipKinds(ownership, RequiredKind),
lifetimeConstraint};
}
Expand Down
31 changes: 31 additions & 0 deletions test/SIL/ownership-verifier/over_consume_positive.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// RUN: %target-sil-opt -enable-sil-ownership -enable-sil-verify-all=0 -module-name Swift -o /dev/null 2>&1 %s
// REQUIRES: asserts

// This file is meant to contain tests that previously the verifier treated
// incorrectly. This is important to ensure that the verifier does not
// regress. It should only deal with dataflow over consuming failures.

sil_stage canonical

import Builtin

enum FakeOptional<T> {
case some(T)
case none
}

class Klass {
}

sil @klass_user : $@convention(thin) (@guaranteed FakeOptional<Klass>) -> ()

sil @guaranteed_is_not_owned_use : $@convention(thin) (@guaranteed Klass) -> () {
bb0(%0 : @guaranteed $Klass):
%1 = copy_value %0 : $Klass
%2 = function_ref @klass_user : $@convention(thin) (@guaranteed FakeOptional<Klass>) -> ()
%3 = enum $FakeOptional<Klass>, #FakeOptional.some!enumelt.1, %1 : $Klass
%4 = apply %2(%3) : $@convention(thin) (@guaranteed FakeOptional<Klass>) -> ()
destroy_value %3 : $FakeOptional<Klass>
%9999 = tuple()
return %9999 : $()
}