public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/105794] New: Spurious "control reaches end of non-void function" warning with a combination of destructor, try/catch and if (true)
@ 2022-05-31 15:42 kamil.sliwak at codepoets dot it
  2022-05-31 20:14 ` [Bug c++/105794] " mpolacek at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: kamil.sliwak at codepoets dot it @ 2022-05-31 15:42 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794

            Bug ID: 105794
           Summary: Spurious "control reaches end of non-void function"
                    warning with a combination of destructor, try/catch
                    and if (true)
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kamil.sliwak at codepoets dot it
  Target Milestone: ---

After switching to GCC 12 I noticed that in several places in the C++ project
I'm working on I'm now getting the `control reaches end of non-void function`
warning. This seems like a regression since it's not happening with earlier GCC
versions or with latest Clang.

I managed to distill the problematic code into a small repro (see below) and
it's reproducible via https://godbolt.org so it does not seem dependent on my
specific environment.

The original code is more complicated but in the end a combination of three
things seems to be causing this:
1. A variable of a custom class type that has an explicitly defined destructor
(which can be empty though).
2. A try/catch block with a return.
3. A `throw` wrapped in a condition that's a compile-time constant.

Removing any of the above makes the warning go away.

### Environment
- GCC version: 12.1.0
- System: Arch Linux

### Repro
#### Command
```
g++ test.cpp
```

#### `test.cpp`
```
#include <exception>

class C {
public:
        ~C() {}
};

int foo()
{
        C c;

        try
        {
                return 1;
        }
        catch (std::exception const&)
        {
        }

        if (true)
                throw std::exception();
}

int main() {
    return 0;
}
```

#### Output
```
test.cpp: In function ‘int foo()’:
test.cpp:22:1: warning: control reaches end of non-void function
[-Wreturn-type]
   22 | }
      | ^
```

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/105794] Spurious "control reaches end of non-void function" warning with a combination of destructor, try/catch and if (true)
  2022-05-31 15:42 [Bug c++/105794] New: Spurious "control reaches end of non-void function" warning with a combination of destructor, try/catch and if (true) kamil.sliwak at codepoets dot it
@ 2022-05-31 20:14 ` mpolacek at gcc dot gnu.org
  2022-05-31 20:33 ` mpolacek at gcc dot gnu.org
  2022-06-01 19:41 ` [Bug c++/105794] [12/13 Regression] " jason at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2022-05-31 20:14 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2022-05-31
                 CC|                            |mpolacek at gcc dot gnu.org
           Keywords|                            |diagnostic
             Status|UNCONFIRMED                 |NEW
   Target Milestone|---                         |12.2
           Priority|P3                          |P2

--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Confirmed, started with r12-5638-ga3e75c1491cd2d

commit a3e75c1491cd2d501081210925a89a65b1c1e5e5
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Nov 25 10:50:59 2021 -0500

    c++: don't fold away 'if' with constant condition

It changed:

--- 105794.ii.005t.original     2022-05-31 16:12:51.932479674 -0400
+++ 105794.ii.005t.original.1   2022-05-31 16:10:21.158400398 -0400
@@ -502,13 +502,16 @@ if (<<cleanup_point std::__exception_ptr
         __cxa_end_catch ();
       }
   } >>> >>>;
-      <<cleanup_point <<< Unknown tree: expr_stmt
+      if (1)
+        {
+          <<cleanup_point <<< Unknown tree: expr_stmt
   <<< Unknown tree: throw_expr
   <<cleanup_point TARGET_EXPR <D.7348, __cxa_allocate_exception (8)>;,
*(struct exception *) D.7348 = TARGET_EXPR <D.7345, <<< Unknown tree:
aggr_init_expr
   4
   __ct_comp 
   D.7345
   (struct exception *) <<< Unknown tree: void_cst >>> >>>>;>>;, __cxa_throw
(D.7348, (void *) &_ZTISt9exception, __dt_comp ); >>> >>>>>;
+        }
     }
   finally
     {

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/105794] Spurious "control reaches end of non-void function" warning with a combination of destructor, try/catch and if (true)
  2022-05-31 15:42 [Bug c++/105794] New: Spurious "control reaches end of non-void function" warning with a combination of destructor, try/catch and if (true) kamil.sliwak at codepoets dot it
  2022-05-31 20:14 ` [Bug c++/105794] " mpolacek at gcc dot gnu.org
@ 2022-05-31 20:33 ` mpolacek at gcc dot gnu.org
  2022-06-01 19:41 ` [Bug c++/105794] [12/13 Regression] " jason at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2022-05-31 20:33 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794

--- Comment #2 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Probably the same issue:

struct C {
  ~C();
};
int foo() {
  C c;
  return 1;
  if (true)
    ;
}

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/105794] [12/13 Regression] Spurious "control reaches end of non-void function" warning with a combination of destructor, try/catch and if (true)
  2022-05-31 15:42 [Bug c++/105794] New: Spurious "control reaches end of non-void function" warning with a combination of destructor, try/catch and if (true) kamil.sliwak at codepoets dot it
  2022-05-31 20:14 ` [Bug c++/105794] " mpolacek at gcc dot gnu.org
  2022-05-31 20:33 ` mpolacek at gcc dot gnu.org
@ 2022-06-01 19:41 ` jason at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: jason at gcc dot gnu.org @ 2022-06-01 19:41 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |DUPLICATE
                 CC|                            |jason at gcc dot gnu.org

--- Comment #3 from Jason Merrill <jason at gcc dot gnu.org> ---
Dup.

*** This bug has been marked as a duplicate of bug 104787 ***

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-06-01 19:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-31 15:42 [Bug c++/105794] New: Spurious "control reaches end of non-void function" warning with a combination of destructor, try/catch and if (true) kamil.sliwak at codepoets dot it
2022-05-31 20:14 ` [Bug c++/105794] " mpolacek at gcc dot gnu.org
2022-05-31 20:33 ` mpolacek at gcc dot gnu.org
2022-06-01 19:41 ` [Bug c++/105794] [12/13 Regression] " jason at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).