Bug 88232 - Please implement -Winfinite-recursion
Summary: Please implement -Winfinite-recursion
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 8.2.1
: P3 normal
Target Milestone: 12.0
Assignee: Martin Sebor
URL:
Keywords: diagnostic, patch
Depends on:
Blocks: new-warning, new_warning
  Show dependency treegraph
 
Reported: 2018-11-27 22:08 UTC by Rafael Avila de Espindola
Modified: 2022-11-10 20:38 UTC (History)
6 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-11-27 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Rafael Avila de Espindola 2018-11-27 22:08:01 UTC
This may sound like a silly warning, but it actually very useful in finding missing member functions in CRTP. Given the testcase

template <typename D> struct C {
  void foo() { static_cast<D *>(this)->foo(); }
};
struct D : C<D> {
    // this is missing:
    // void foo() {}
};
void f(D *d) { d->foo(); }

gcc is silent, but clang prints:

test.cpp:2:14: warning: all paths through this function will call itself [-Winfinite-recursion]
  void foo() { static_cast<D *>(this)->foo(); }
Comment 1 Marek Polacek 2018-11-27 22:09:58 UTC
I'd like this too -- I've run into the same thing many times.
Comment 2 Jakub Jelinek 2018-11-28 09:25:21 UTC
Another nice example is PR87742 where it would be nice to tell the user that the operator * has infinite recursion.
Comment 3 Thomas Schwinge 2021-11-09 12:30:43 UTC
Such a '-Winfinite-recursion' diagnostic would've been helpful for PR101204/PR103157 (meaning: prevented/flagged that issue early, via a GCC build-time '-Werror' diagnostic).
Comment 5 GCC Commits 2021-11-23 22:37:14 UTC
The master branch has been updated by Martin Sebor <msebor@gcc.gnu.org>:

https://gcc.gnu.org/g:30ba058f77eedfaf7a0582f5d42aff949710bce4

commit r12-5483-g30ba058f77eedfaf7a0582f5d42aff949710bce4
Author: Martin Sebor <msebor@redhat.com>
Date:   Tue Nov 23 15:30:29 2021 -0700

    Implement -Winfinite-recursion [PR88232].
    
    Resolves:
    PR middle-end/88232 - Please implement -Winfinite-recursion
    
    gcc/ChangeLog:
    
            PR middle-end/88232
            * Makefile.in (OBJS): Add gimple-warn-recursion.o.
            * common.opt: Add -Winfinite-recursion.
            * doc/invoke.texi (-Winfinite-recursion): Document.
            * passes.def (pass_warn_recursion): Schedule a new pass.
            * tree-pass.h (make_pass_warn_recursion): Declare.
            * gimple-warn-recursion.c: New file.
    
    gcc/c-family/ChangeLog:
    
            PR middle-end/88232
            * c.opt: Add -Winfinite-recursion.
    
    gcc/testsuite/ChangeLog:
    
            PR middle-end/88232
            * c-c++-common/attr-used-5.c: Suppress valid warning.
            * c-c++-common/attr-used-6.c: Same.
            * c-c++-common/attr-used-9.c: Same.
            * g++.dg/warn/Winfinite-recursion-2.C: New test.
            * g++.dg/warn/Winfinite-recursion-3.C: New test.
            * g++.dg/warn/Winfinite-recursion.C: New test.
            * gcc.dg/Winfinite-recursion-2.c: New test.
            * gcc.dg/Winfinite-recursion.c: New test.
Comment 6 Martin Sebor 2021-11-23 22:43:12 UTC
Done for GCC 12.