Skip to content

Commit 1476f8f

Browse files
committed
[ownership-verifier] Allow owned enum arguments to be passed as guaranteed function arguments.
Enums require special handling in the ownership verifier since an enum is a sum type whose triviality or non-triviality is erased at function arguments. When the ownership verifier was changed to allow for owned values to be passed as guaranteed objects via instantaneous borrowing, this code was not updated. rdar://34222540
1 parent d3e0284 commit 1476f8f

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

lib/SIL/SILOwnershipVerifier.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,9 +1035,16 @@ OwnershipUseCheckerResult OwnershipCompatibilityUseChecker::visitEnumArgument(
10351035
// The operand has a non-trivial ownership kind. It must match the argument
10361036
// convention.
10371037
auto ownership = getOwnershipKind();
1038-
auto lifetimeConstraint = (ownership == ValueOwnershipKind::Owned)
1039-
? UseLifetimeConstraint::MustBeInvalidated
1040-
: UseLifetimeConstraint::MustBeLive;
1038+
UseLifetimeConstraint lifetimeConstraint;
1039+
if (ownership == ValueOwnershipKind::Owned) {
1040+
if (RequiredKind != ValueOwnershipKind::Owned) {
1041+
lifetimeConstraint = UseLifetimeConstraint::MustBeLive;
1042+
} else {
1043+
lifetimeConstraint = UseLifetimeConstraint::MustBeInvalidated;
1044+
}
1045+
} else {
1046+
lifetimeConstraint = UseLifetimeConstraint::MustBeLive;
1047+
}
10411048
return {compatibleOwnershipKinds(ownership, RequiredKind),
10421049
lifetimeConstraint};
10431050
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %target-sil-opt -enable-sil-ownership -enable-sil-verify-all=0 -module-name Swift -o /dev/null 2>&1 %s
2+
// REQUIRES: asserts
3+
4+
// This file is meant to contain tests that previously the verifier treated
5+
// incorrectly. This is important to ensure that the verifier does not
6+
// regress. It should only deal with dataflow over consuming failures.
7+
8+
sil_stage canonical
9+
10+
import Builtin
11+
12+
enum FakeOptional<T> {
13+
case some(T)
14+
case none
15+
}
16+
17+
class Klass {
18+
}
19+
20+
sil @klass_user : $@convention(thin) (@guaranteed FakeOptional<Klass>) -> ()
21+
22+
sil @guaranteed_is_not_owned_use : $@convention(thin) (@guaranteed Klass) -> () {
23+
bb0(%0 : @guaranteed $Klass):
24+
%1 = copy_value %0 : $Klass
25+
%2 = function_ref @klass_user : $@convention(thin) (@guaranteed FakeOptional<Klass>) -> ()
26+
%3 = enum $FakeOptional<Klass>, #FakeOptional.some!enumelt.1, %1 : $Klass
27+
%4 = apply %2(%3) : $@convention(thin) (@guaranteed FakeOptional<Klass>) -> ()
28+
destroy_value %3 : $FakeOptional<Klass>
29+
%9999 = tuple()
30+
return %9999 : $()
31+
}

0 commit comments

Comments
 (0)