Skip to content

Commit d824fd3

Browse files
committed
DiagnoseInfiniteRecursion: handle invariant conditions and improve the warning message
The main change is to detect infinite recursive calls under invariant conditions. For example: func f() { if #available(macOS 10.4.4, *) { f() } } or invariant conditions due to forwarded arguments: func f(_ x: Int) { if x > 0 { f(x) } } Also, improve the warning message. Instead of giving a warning at the function location warning: all paths through this function will call itself give a warning at the call location: warning: function call causes an infinite recursion Especially in case of multiple recursive calls, it makes it easier to locate the problem. https://bugs.swift.org/browse/SR-11842 rdar://57460599
1 parent 5e374b6 commit d824fd3

File tree

6 files changed

+644
-148
lines changed

6 files changed

+644
-148
lines changed

include/swift/AST/DiagnosticsSIL.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ WARNING(unreachable_case,none,
277277
WARNING(switch_on_a_constant,none,
278278
"switch condition evaluates to a constant", ())
279279
NOTE(unreachable_code_note,none, "will never be executed", ())
280-
WARNING(warn_infinite_recursive_function,none,
281-
"all paths through this function will call itself", ())
280+
WARNING(warn_infinite_recursive_call,none,
281+
"function call causes an infinite recursion", ())
282282

283283
// 'transparent' diagnostics
284284
ERROR(circular_transparent,none,

include/swift/SIL/SILBasicBlock.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,10 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
314314
const_succblock_iterator succblock_end() const {
315315
return getTerminator()->succblock_end();
316316
}
317+
318+
unsigned getNumSuccessors() const {
319+
return getTerminator()->getNumSuccessors();
320+
}
317321

318322
SILBasicBlock *getSingleSuccessorBlock() {
319323
return getTerminator()->getSingleSuccessorBlock();

0 commit comments

Comments
 (0)