Bug 96182 - GCC accepts constexpr function with no return-statement
Summary: GCC accepts constexpr function with no return-statement
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 11.0
: P3 enhancement
Target Milestone: 11.0
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2020-07-13 11:52 UTC by Haoxin Tu
Modified: 2021-08-28 00:03 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-07-13 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Haoxin Tu 2020-07-13 11:52:14 UTC
Hi,all.

This code, test.cc, is an invalid code I guess, but GCC accepts it.

$cat test.cc
constexpr int
foo()
{}

$g++ test.cc
test.cc: In function ‘constexpr int foo()’:
test.cc:3:2: warning: no return statement in function returning non-void [-Wreturn-type]
    3 | {}
      |  ^

GCC only emits normal -Wreturn-type warning and then accepts it.

Weirdly, this code is rejected under -std=c++11.

$g++ -std=c++11 test.cc
test.cc: In function ‘constexpr int foo()’:
test.cc:3:2: error: body of ‘constexpr’ function ‘constexpr int foo()’ not a return-statement
    3 | {}
      |  ^
test.cc:3:2: warning: no return statement in function returning non-void [-type-Wreturn-type]

I think constexpr function is not a deprecated feature in c++14. I also test this in Clang, it is rejected by both standards.

Every GCC versions from 5.1 to trunk behave the same.

Thanks,
Haoxin
Comment 1 Jakub Jelinek 2020-07-13 11:59:01 UTC
The difference is that in C++11 the standard requires that the body of a constexpr function is return expression, that is not the case of C++14 anymore.
And, you'd get an error if you tried constexpr int a = foo (); i.e. when it is evaluated in constant expression, but when it is only evaluated e.g. in int b = foo (); it is a problem only at runtime.
Comment 2 Haoxin Tu 2020-07-13 12:09:15 UTC
(In reply to Jakub Jelinek from comment #1)
> The difference is that in C++11 the standard requires that the body of a
> constexpr function is return expression, that is not the case of C++14
> anymore.
> And, you'd get an error if you tried constexpr int a = foo (); i.e. when it
> is evaluated in constant expression, but when it is only evaluated e.g. in
> int b = foo (); it is a problem only at runtime.

Thank you, Jakub.

At runtime this must be a error. But I guess should it be rejected at compile time?

Maybe reject this in compile time will help users to fix this issue early, like other mainstream compilers do. Just for a suggestion, please understand if anything I stated is unsuitable.

Thanks.
Comment 3 Jakub Jelinek 2020-07-13 12:18:51 UTC
E.g.
constexpr int foo (int x) { if (x == 5) return 3; } constexpr int a = foo (5);
is accepted just with warning by both compilers and I think that is right, it is only an error if one does constexpr int b = foo (4); or similar.
But for some reason clang treats your case differently (when there are no return statements at all), but only in constexpr functions.  Not sure how is it backed up, unless the standard says that a constexpr function must have a return statement or something similar, not sure if that is ok.  It is true that in these cases all invocations of such function in constant expression contexts will result in an error, but if you don't invoke them...
Comment 4 Jakub Jelinek 2020-07-13 12:21:19 UTC
Or another possible wording would be that a constexpr function which is not a valid constant expression for all possible parameter values is invalid.
I believe such wording is there for templates and instead of parameter values all template arguments.
Comment 5 Jonathan Wakely 2020-07-13 12:28:44 UTC
[decl.constexpr] p6 in the C++ standard says:

"if no argument values exist such that an invocation of the function or constructor could be an evaluated subexpression of a core constant expression [...] the program is ill-formed, no diagnostic required."

So the foo() function is ill-formed, but implementations are not required to diagnose it unless the function is actually evaluated.

So this is not accepts-invalid, but I'll confirm it as a request for a diagnostic enhancement.
Comment 6 Jakub Jelinek 2020-07-13 12:36:13 UTC
So we could do something like:
--- gcc/cp/decl.c.jj	2020-07-09 11:27:51.088908783 +0200
+++ gcc/cp/decl.c	2020-07-13 14:34:59.887259561 +0200
@@ -17164,7 +17164,9 @@ finish_function (bool inline_p)
   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
 
   /* Complain if there's just no return statement.  */
-  if (warn_return_type
+  if ((warn_return_type
+       || (cxx_dialect >= cxx14
+	   && DECL_DECLARED_CONSTEXPR_P (fndecl)))
       && !VOID_TYPE_P (TREE_TYPE (fntype))
       && !dependent_type_p (TREE_TYPE (fntype))
       && !current_function_returns_value && !current_function_returns_null
@@ -17196,8 +17198,12 @@ finish_function (bool inline_p)
 					    global_dc->option_state))
 	    add_return_star_this_fixit (&richloc, fndecl);
 	}
-      if (warning_at (&richloc, OPT_Wreturn_type,
-	  "no return statement in function returning non-void"))
+      if (cxx_dialect >= cxx14 && DECL_DECLARED_CONSTEXPR_P (fndecl))
+	error_at (&richloc, "no return statement in %<constexpr%> function "
+			    "returning non-void");
+      else if (warning_at (&richloc, OPT_Wreturn_type,
+			   "no return statement in function returning "
+			   "non-void"))
 	TREE_NO_WARNING (fndecl) = 1;
     }
 
and look for what breaks.
Comment 7 GCC Commits 2020-07-31 21:08:52 UTC
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:5f9669d9e23a1116e040c80e0f3d4f43639bda52

commit r11-2473-g5f9669d9e23a1116e040c80e0f3d4f43639bda52
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri Jul 31 23:08:00 2020 +0200

    c++: Use error_at rather than warning_at for missing return in constexpr functions [PR96182]
    
    For C++11 we already emit an error if a constexpr function doesn't contain
    a return statement, because in C++11 that is the only thing it needs to
    contain, but for C++14 we would normally issue a -Wreturn-type warning.
    
    As mentioned by Jonathan, such constexpr functions are invalid, no
    diagnostics required, because there doesn't exist any arguments for
    which it would result in valid constant expression.
    
    This raises it to an error in such cases.  The !LAMBDA_TYPE_P case
    is to avoid error on g++.dg/pr81194.C where the user didn't write
    constexpr anywhere and the operator() is compiler generated.
    
    2020-07-31  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/96182
            * decl.c (finish_function): In constexpr functions use for C++14 and
            later error instead of warning if no return statement is present and
            diagnose it regardless of warn_return_type.  Move the warn_return_type
            diagnostics earlier in the function.
    
            * g++.dg/cpp1y/constexpr-96182.C: New test.
            * g++.dg/other/error35.C (S<T>::g()): Add return statement.
            * g++.dg/cpp1y/pr63996.C (foo): Likewise.
            * g++.dg/cpp1y/constexpr-return2.C (f): Likewise.
            * g++.dg/cpp1y/var-templ44.C (make_array): Add throw 1.
Comment 8 Drea Pinski 2021-08-28 00:03:13 UTC
Fixed so closing.