Skip to content

Commit 6de9c97

Browse files
committed
Make it explicit that MemoryLifetimeVerifier will not handle reborrows
1 parent 95215d4 commit 6de9c97

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

lib/SIL/Utils/MemoryLocations.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ bool MemoryLocations::analyzeLocationUsesRecursively(SILValue V, unsigned locIdx
357357
if (!handleNonTrivialProjections && hasInoutArgument(ApplySite(user)))
358358
return false;
359359
break;
360+
case SILInstructionKind::LoadBorrowInst:
361+
// Reborrows are not handled
362+
if (!cast<LoadBorrowInst>(user)->getUsersOfType<BranchInst>().empty())
363+
return false;
364+
break;
360365
case SILInstructionKind::InjectEnumAddrInst:
361366
case SILInstructionKind::SelectEnumAddrInst:
362367
case SILInstructionKind::ExistentialMetatypeInst:
@@ -367,7 +372,6 @@ bool MemoryLocations::analyzeLocationUsesRecursively(SILValue V, unsigned locIdx
367372
case SILInstructionKind::StoreInst:
368373
case SILInstructionKind::StoreBorrowInst:
369374
case SILInstructionKind::EndAccessInst:
370-
case SILInstructionKind::LoadBorrowInst:
371375
case SILInstructionKind::DestroyAddrInst:
372376
case SILInstructionKind::CheckedCastAddrBranchInst:
373377
case SILInstructionKind::UncheckedRefCastAddrInst:

test/SIL/memory_lifetime_failures.sil

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,3 +538,32 @@ bb0(%0 : $Bool):
538538
%10 = tuple ()
539539
return %10 : $()
540540
}
541+
542+
// MemoryLifetimeVerifier does not detect an error here due to reborrows
543+
sil [ossa] @test_load_borrow1 : $@convention(thin) (@in Optional<T>) -> () {
544+
bb0(%0 : $*Optional<T>):
545+
destroy_addr %0 : $*Optional<T>
546+
%1 = load_borrow %0 : $*Optional<T>
547+
br bb1(%1 : $Optional<T>)
548+
549+
bb1(%3 : @guaranteed $Optional<T>):
550+
end_borrow %3 : $Optional<T>
551+
br bb2
552+
553+
bb2:
554+
%r = tuple ()
555+
return %r : $()
556+
}
557+
558+
// CHECK: SIL memory lifetime failure in @test_load_borrow2: memory is not initialized, but should
559+
sil [ossa] @test_load_borrow2 : $@convention(thin) (@in Optional<T>) -> () {
560+
bb0(%0 : $*Optional<T>):
561+
destroy_addr %0 : $*Optional<T>
562+
%1 = load_borrow %0 : $*Optional<T>
563+
end_borrow %1 : $Optional<T>
564+
br bb1
565+
566+
bb1:
567+
%r = tuple ()
568+
return %r : $()
569+
}

0 commit comments

Comments
 (0)