public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/43943]  New: "warning: no return statement in function returning non-void" should be an error
@ 2010-04-29 20:13 david at rothlis dot net
  2010-04-29 20:32 ` [Bug c++/43943] " pinskia at gcc dot gnu dot org
                   ` (5 more replies)
  0 siblings, 6 replies; 10+ messages in thread
From: david at rothlis dot net @ 2010-04-29 20:13 UTC (permalink / raw)
  To: gcc-bugs

I know that dozens of bugs have been opened for this over the years, but please
read this through.

If you have a C++ program missing a return statement from a function that is
supposed to return a value, g++ will compile it happily with no errors (or even
a warning, unless -Wreturn-type or -Wall is used). Trying to use the return
value from the function will most likely cause a segmentation fault. Dozens of
bugs have been opened for this over the years (e.g. bug 11474), all rejected as
invalid based on the following statement from section 6.6.3 of the C++
standard:

  Flowing off the end of a function is equivalent to a return with no value;
  this results in undefined behavior in a value-returning function.

However, the very same paragraph begins with:

  A return statement without an expression can be used only in functions 
  that do not return a value, that is, a function with the return type void,
  a constructor (12.1), or a destructor (12.4).

In my opinion, the standard contradicts itself. Either flowing off the end of a
function is "equivalent to a return with no value" (i.e. invalid according to
the beginning of the paragraph) or it is "undefined behaviour". It can't be
both.

Appendix C is informative rather than normative, but can help us understand the
intent of the standard. Section C.1.4 says:

  Change: It is now invalid to return (explicitly or implicitly) from a
  function which is declared to return a value without actually returning a
  value.
  Rationale: The caller and callee may assume fairly elaborate return-value
  mechanisms for the return of class objects. If some flow paths execute a
  return without specifying any value, the implementation must embody many more
  complications. Besides, promising to return a value of a given type, and then
  not returning such a value, has always been recognized to be a questionable
  practice, tolerated only because very-old C had no distinction between void
  functions and int functions.

The number of bugs opened for this issue over the years indicates how
surprising the current behaviour is. Since it is not always practical to enable
-Werror on large codebases, I think this should be an error by default.


-- 
           Summary: "warning: no return statement in function returning non-
                    void" should be an error
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: david at rothlis dot net


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43943


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

* [Bug c++/43943] "warning: no return statement in function returning non-void" should be an error
  2010-04-29 20:13 [Bug c++/43943] New: "warning: no return statement in function returning non-void" should be an error david at rothlis dot net
@ 2010-04-29 20:32 ` pinskia at gcc dot gnu dot org
  2010-04-29 20:36 ` redi at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2010-04-29 20:32 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2010-04-29 20:31 -------
-Werror=Wreturn-type will turn just that warning into an error message in
recent GCC's.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43943


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

* [Bug c++/43943] "warning: no return statement in function returning non-void" should be an error
  2010-04-29 20:13 [Bug c++/43943] New: "warning: no return statement in function returning non-void" should be an error david at rothlis dot net
  2010-04-29 20:32 ` [Bug c++/43943] " pinskia at gcc dot gnu dot org
@ 2010-04-29 20:36 ` redi at gcc dot gnu dot org
  2010-04-30  6:05 ` david at rothlis dot net
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu dot org @ 2010-04-29 20:36 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from redi at gcc dot gnu dot org  2010-04-29 20:36 -------
that should be -Werror=return-type 

but spelling aside, this is not a bug now

the compiler MUST NOT reject it by default, since a function with a missing
return is valid as long as the caller does not use the (missing) return value. 
making this an unconditional error would be non-conforming

if you want that behaviour, use -Werror=return-type


-- 

redi at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |WORKSFORME


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43943


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

* [Bug c++/43943] "warning: no return statement in function returning non-void" should be an error
  2010-04-29 20:13 [Bug c++/43943] New: "warning: no return statement in function returning non-void" should be an error david at rothlis dot net
  2010-04-29 20:32 ` [Bug c++/43943] " pinskia at gcc dot gnu dot org
  2010-04-29 20:36 ` redi at gcc dot gnu dot org
@ 2010-04-30  6:05 ` david at rothlis dot net
  2010-04-30 11:22 ` redi at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 10+ messages in thread
From: david at rothlis dot net @ 2010-04-30  6:05 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from david at rothlis dot net  2010-04-30 06:05 -------
> a function with a missing return is valid

I just can't reconcile that with the following line from the C++ standard: "It
is now invalid to return (explicitly or implicitly) from a function which is
declared to return a value without actually returning a value."  Any insights?

Thanks for the "-Werror=return-type" tip.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43943


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

* [Bug c++/43943] "warning: no return statement in function returning non-void" should be an error
  2010-04-29 20:13 [Bug c++/43943] New: "warning: no return statement in function returning non-void" should be an error david at rothlis dot net
                   ` (2 preceding siblings ...)
  2010-04-30  6:05 ` david at rothlis dot net
@ 2010-04-30 11:22 ` redi at gcc dot gnu dot org
  2010-04-30 11:26 ` redi at gcc dot gnu dot org
  2010-04-30 14:01 ` david at rothlis dot net
  5 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu dot org @ 2010-04-30 11:22 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from redi at gcc dot gnu dot org  2010-04-30 11:22 -------
(In reply to comment #3)
> > a function with a missing return is valid
> 
> I just can't reconcile that with the following line from the C++ standard: "It
> is now invalid to return (explicitly or implicitly) from a function which is
> declared to return a value without actually returning a value."  Any insights?

Apologies, you're right, in C++ it's not valid and is always undefined
behaviour.

But there have been long discussions on the standard committee reflector about
why it cannot be a "diagnostic required" error, rather than undefined
behaviour.  There are cases which are either not diagnosable or cannot be
written, such as this example from Doug Gregor:

in generic code, there might not be a way to create a value with the
appropriate type. For example:

       template<typename T>
       T maybe_call(std::function<T(void)> f) {
               if (f)
                       return f();
               else
                       abort_program();

               // Cannot write a return here, because we have no way to create
a value of type 'T'
       }


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43943


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

* [Bug c++/43943] "warning: no return statement in function returning non-void" should be an error
  2010-04-29 20:13 [Bug c++/43943] New: "warning: no return statement in function returning non-void" should be an error david at rothlis dot net
                   ` (3 preceding siblings ...)
  2010-04-30 11:22 ` redi at gcc dot gnu dot org
@ 2010-04-30 11:26 ` redi at gcc dot gnu dot org
  2010-04-30 14:01 ` david at rothlis dot net
  5 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu dot org @ 2010-04-30 11:26 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from redi at gcc dot gnu dot org  2010-04-30 11:26 -------
The point being that it's undefined behaviour to /return/ from such a function,
not to /write/ such a function.

If the programmer guarantees the "missing return" will never happen then
there's no error.

I was wrong to say it's OK as long as the caller doesn't use the return value -
but it is OK as long as control never reaches the "missing return"


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43943


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

* [Bug c++/43943] "warning: no return statement in function returning non-void" should be an error
  2010-04-29 20:13 [Bug c++/43943] New: "warning: no return statement in function returning non-void" should be an error david at rothlis dot net
                   ` (4 preceding siblings ...)
  2010-04-30 11:26 ` redi at gcc dot gnu dot org
@ 2010-04-30 14:01 ` david at rothlis dot net
  5 siblings, 0 replies; 10+ messages in thread
From: david at rothlis dot net @ 2010-04-30 14:01 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from david at rothlis dot net  2010-04-30 14:01 -------
OK, thanks for the explanation!


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43943


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

* [Bug c++/43943] "warning: no return statement in function returning non-void" should be an error
       [not found] <bug-43943-4@http.gcc.gnu.org/bugzilla/>
  2020-04-26  9:44 ` redi at gcc dot gnu.org
  2022-04-19  9:55 ` kdevel at vogtner dot de
@ 2023-03-31 18:38 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-03-31 18:38 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |contact@thunderperfectwitch
                   |                            |craft.org

--- Comment #9 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 109364 has been marked as a duplicate of this bug. ***

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

* [Bug c++/43943] "warning: no return statement in function returning non-void" should be an error
       [not found] <bug-43943-4@http.gcc.gnu.org/bugzilla/>
  2020-04-26  9:44 ` redi at gcc dot gnu.org
@ 2022-04-19  9:55 ` kdevel at vogtner dot de
  2023-03-31 18:38 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 10+ messages in thread
From: kdevel at vogtner dot de @ 2022-04-19  9:55 UTC (permalink / raw)
  To: gcc-bugs

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

Stefan <kdevel at vogtner dot de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kdevel at vogtner dot de

--- Comment #8 from Stefan <kdevel at vogtner dot de> ---
(In reply to Jonathan Wakely from comment #4)
> There are cases which are either not diagnosable or cannot be
> written, such as this example from Doug Gregor:
> 
> in generic code, there might not be a way to create a value with the
> appropriate type. For example:
> 
>        template<typename T>
>        T maybe_call(std::function<T(void)> f) {
>                if (f)
>                        return f();
>                else
>                        abort_program();
> 
>                // Cannot write a return here, because we have no way to
> create a value of type 'T'
>        }

One can of course write a return there:

   template<typename T>
   T maybe_call(std::function<T(void)> f)
   {
      if (f)
         return f();
      else
         abort_program();

      // Cannot write a return here, because we have no way to create a value
of type 'T'
      return f();
   }

which refactors nicely into

   template<typename T>
   T maybe_call(std::function<T(void)> f)
   {
      if (! f)
         abort_program();
      return f();
   }

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

* [Bug c++/43943] "warning: no return statement in function returning non-void" should be an error
       [not found] <bug-43943-4@http.gcc.gnu.org/bugzilla/>
@ 2020-04-26  9:44 ` redi at gcc dot gnu.org
  2022-04-19  9:55 ` kdevel at vogtner dot de
  2023-03-31 18:38 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2020-04-26  9:44 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dcb314 at hotmail dot com

--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> ---
*** Bug 94768 has been marked as a duplicate of this bug. ***

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

end of thread, other threads:[~2023-03-31 18:38 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-04-29 20:13 [Bug c++/43943] New: "warning: no return statement in function returning non-void" should be an error david at rothlis dot net
2010-04-29 20:32 ` [Bug c++/43943] " pinskia at gcc dot gnu dot org
2010-04-29 20:36 ` redi at gcc dot gnu dot org
2010-04-30  6:05 ` david at rothlis dot net
2010-04-30 11:22 ` redi at gcc dot gnu dot org
2010-04-30 11:26 ` redi at gcc dot gnu dot org
2010-04-30 14:01 ` david at rothlis dot net
     [not found] <bug-43943-4@http.gcc.gnu.org/bugzilla/>
2020-04-26  9:44 ` redi at gcc dot gnu.org
2022-04-19  9:55 ` kdevel at vogtner dot de
2023-03-31 18:38 ` pinskia 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).