Skip to content

Commit 14f530e

Browse files
authored
Merge pull request #23239 from gottesmm/pr-e583ec89d59c41a88ee3d6cc7ed0d1a1acdb0b91
2 parents 9802875 + abe5fa9 commit 14f530e

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

include/swift/SIL/SILInstruction.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5959,7 +5959,7 @@ class InitExistentialValueInst final
59595959
class InitExistentialRefInst final
59605960
: public UnaryInstructionWithTypeDependentOperandsBase<
59615961
SILInstructionKind::InitExistentialRefInst, InitExistentialRefInst,
5962-
OwnershipForwardingSingleValueInst> {
5962+
SingleValueInstruction> {
59635963
friend SILBuilder;
59645964

59655965
CanType ConcreteType;
@@ -5970,8 +5970,7 @@ class InitExistentialRefInst final
59705970
ArrayRef<SILValue> TypeDependentOperands,
59715971
ArrayRef<ProtocolConformanceRef> Conformances)
59725972
: UnaryInstructionWithTypeDependentOperandsBase(
5973-
DebugLoc, Instance, TypeDependentOperands, ExistentialType,
5974-
Instance.getOwnershipKind()),
5973+
DebugLoc, Instance, TypeDependentOperands, ExistentialType),
59755974
ConcreteType(FormalConcreteType), Conformances(Conformances) {}
59765975

59775976
static InitExistentialRefInst *

lib/SIL/OperandOwnership.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ CONSTANT_OWNERSHIP_INST(Owned, MustBeInvalidated, DestroyValue)
161161
CONSTANT_OWNERSHIP_INST(Owned, MustBeInvalidated, ReleaseValue)
162162
CONSTANT_OWNERSHIP_INST(Owned, MustBeInvalidated, ReleaseValueAddr)
163163
CONSTANT_OWNERSHIP_INST(Owned, MustBeInvalidated, StrongRelease)
164-
CONSTANT_OWNERSHIP_INST(Owned, MustBeInvalidated, InitExistentialRef)
165164
CONSTANT_OWNERSHIP_INST(Owned, MustBeInvalidated, EndLifetime)
165+
CONSTANT_OWNERSHIP_INST(Owned, MustBeInvalidated, InitExistentialRef)
166166
CONSTANT_OWNERSHIP_INST(Any, MustBeLive, AbortApply)
167167
CONSTANT_OWNERSHIP_INST(Any, MustBeLive, AddressToPointer)
168168
CONSTANT_OWNERSHIP_INST(Any, MustBeLive, BeginAccess)

lib/SIL/ValueOwnership.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ CONSTANT_OWNERSHIP_INST(Owned, KeyPath)
7777
CONSTANT_OWNERSHIP_INST(Owned, InitExistentialValue)
7878
CONSTANT_OWNERSHIP_INST(Owned, GlobalValue) // TODO: is this correct?
7979

80+
// NOTE: Even though init_existential_ref from a reference counting perspective
81+
// is not considered to be "owned" since it doesn't affect reference counts,
82+
// conceptually we want to treat it as an owned value that produces owned
83+
// things, rather than a forwarding thing since initialization is generally a
84+
// consuming operation.
85+
CONSTANT_OWNERSHIP_INST(Owned, InitExistentialRef)
86+
8087
// One would think that these /should/ be unowned. In truth they are owned since
8188
// objc metatypes do not go through the retain/release fast path. In their
8289
// implementations of retain/release nothing happens, so this is safe.
@@ -232,7 +239,6 @@ ValueOwnershipKindClassifier::visitForwardingInst(SILInstruction *i,
232239
}
233240
FORWARDING_OWNERSHIP_INST(BridgeObjectToRef)
234241
FORWARDING_OWNERSHIP_INST(ConvertFunction)
235-
FORWARDING_OWNERSHIP_INST(InitExistentialRef)
236242
FORWARDING_OWNERSHIP_INST(OpenExistentialRef)
237243
FORWARDING_OWNERSHIP_INST(RefToBridgeObject)
238244
FORWARDING_OWNERSHIP_INST(SelectValue)

test/SIL/ownership-verifier/over_consume.sil

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ class SuperKlass {
2929
class Klass : SuperKlass {
3030
}
3131

32+
typealias AnyObject = Builtin.AnyObject
33+
34+
class ClassProtConformingRef {}
35+
protocol ClassProt : AnyObject {}
36+
extension ClassProtConformingRef : ClassProt {}
37+
3238
///////////
3339
// Tests //
3440
///////////
@@ -452,4 +458,30 @@ bb0(%0 : @owned $Klass):
452458
dealloc_stack %1 : $*SuperKlass
453459
%9999 = tuple()
454460
return %9999 : $()
455-
}
461+
}
462+
463+
// CHECK-LABEL: Function: 'init_existential_ref_not_forwarding'
464+
// CHECK: Have operand with incompatible ownership?!
465+
// CHECK: Value: %0 = argument of bb0 : $ClassProtConformingRef
466+
// CHECK: User: %2 = init_existential_ref %0 : $ClassProtConformingRef : $ClassProtConformingRef, $ClassProt
467+
// CHECK: Operand Number: 0
468+
// CHECK: Conv: guaranteed
469+
// CHECK: OwnershipMap:
470+
// CHECK: -- OperandOwnershipKindMap --
471+
// CHECK: unowned: No.
472+
// CHECK: owned: Yes. Liveness: MustBeInvalidated
473+
// CHECK: guaranteed: No.
474+
// CHECK: any: Yes. Liveness: MustBeLive
475+
// CHECK-NOT: init_existential_ref %1
476+
// CHECK: Function: 'init_existential_ref_not_forwarding'
477+
// CHECK: Error! Found a leaked owned value that was never consumed.
478+
// CHECK: Value: %2 = init_existential_ref %0 : $ClassProtConformingRef : $ClassProtConformingRef, $ClassProt
479+
// CHECK-NOT: init_existential_ref %1
480+
sil [ossa] @init_existential_ref_not_forwarding : $@convention(thin) (@guaranteed ClassProtConformingRef, @owned ClassProtConformingRef) -> @owned (ClassProt, ClassProt) {
481+
bb0(%0 : @guaranteed $ClassProtConformingRef, %1 : @owned $ClassProtConformingRef):
482+
%2 = init_existential_ref %0 : $ClassProtConformingRef : $ClassProtConformingRef, $ClassProt
483+
%3 = copy_value %2 : $ClassProt
484+
%4 = init_existential_ref %1 : $ClassProtConformingRef : $ClassProtConformingRef, $ClassProt
485+
%5 = tuple(%3 : $ClassProt, %4 : $ClassProt)
486+
return %5 : $(ClassProt, ClassProt)
487+
}

0 commit comments

Comments
 (0)