Skip to content

Commit 9cb7ab1

Browse files
authored
Merge pull request #25007 from gottesmm/pr-d77fff3d54f2b70d825402ebfd766d3891da9b0b
[diagnose-unreachable] Ignore/eliminate end_borrows after noreturn fu…
2 parents 00cb21a + 372046c commit 9cb7ab1

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

lib/SILOptimizer/Mandatory/DiagnoseUnreachable.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -529,15 +529,20 @@ static bool simplifyBlocksWithCallsToNoReturn(SILBasicBlock &BB,
529529
// Diagnose the unreachable code within the same block as the call to
530530
// noreturn.
531531
if (isUserCode(CurrentInst) && !DiagnosedUnreachableCode) {
532-
if (NoReturnCall->getLoc().is<RegularLocation>()) {
533-
if (!NoReturnCall->getLoc().isASTNode<ExplicitCastExpr>()) {
534-
diagnose(BB.getModule().getASTContext(),
535-
CurrentInst->getLoc().getSourceLoc(),
536-
diag::unreachable_code);
537-
diagnose(BB.getModule().getASTContext(),
538-
NoReturnCall->getLoc().getSourceLoc(),
539-
diag::call_to_noreturn_note);
540-
DiagnosedUnreachableCode = true;
532+
// If we have an instruction that is an end_borrow, ignore it. This
533+
// happens when passing a guaranteed argument through generic code paths
534+
// to no return functions.
535+
if (!isa<EndBorrowInst>(CurrentInst)) {
536+
if (NoReturnCall->getLoc().is<RegularLocation>()) {
537+
if (!NoReturnCall->getLoc().isASTNode<ExplicitCastExpr>()) {
538+
diagnose(BB.getModule().getASTContext(),
539+
CurrentInst->getLoc().getSourceLoc(),
540+
diag::unreachable_code);
541+
diagnose(BB.getModule().getASTContext(),
542+
NoReturnCall->getLoc().getSourceLoc(),
543+
diag::call_to_noreturn_note);
544+
DiagnosedUnreachableCode = true;
545+
}
541546
}
542547
}
543548
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %target-swift-frontend %s -enable-ownership-stripping-after-serialization -emit-sil -verify
2+
3+
class TuringMachine {
4+
func halt() -> Never {
5+
repeat { } while true
6+
}
7+
}
8+
9+
func diagnose_missing_return_no_error_after_noreturn_method() -> Int {
10+
TuringMachine().halt()
11+
} // no error
12+
13+
func testUnreachableAfterNoReturnMethod() -> Int {
14+
TuringMachine().halt(); // expected-note{{a call to a never-returning function}}
15+
return 0; // expected-warning {{will never be executed}}
16+
}
17+

0 commit comments

Comments
 (0)