[C++] Allow [[likely]] and [[unlikely]] in constexpr functions (PR c++/92343)
diff mbox series

Message ID 20191105073520.GN4650@tucnak
State New
Headers show
Series
  • [C++] Allow [[likely]] and [[unlikely]] in constexpr functions (PR c++/92343)
Related show

Commit Message

Jakub Jelinek Nov. 5, 2019, 7:35 a.m. UTC
Hi!

When Martin Liska added PREDICT_EXPR to potential_constant_expression_1,
it was with goto in mind and in that case goto isn't a potential
constant expression, but when the {,un}likely attributes are used on other
statements, they are valid.

Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for
trunk?

2019-11-05  Jakub Jelinek  <jakub@redhat.com>

	PR c++/92343
	* constexpr.c (potential_constant_expression_1): Return true rather
	than false for PREDICT_EXPR.

	* g++.dg/cpp2a/attr-likely6.C: New test.


	Jakub

Comments

Jason Merrill Nov. 5, 2019, 8:19 a.m. UTC | #1
OK.  I wonder why we're returning false for EMPTY_CLASS_EXPR?

On Tue, Nov 5, 2019 at 7:35 AM Jakub Jelinek <jakub@redhat.com> wrote:
>
> Hi!
>
> When Martin Liska added PREDICT_EXPR to potential_constant_expression_1,
> it was with goto in mind and in that case goto isn't a potential
> constant expression, but when the {,un}likely attributes are used on other
> statements, they are valid.
>
> Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for
> trunk?
>
> 2019-11-05  Jakub Jelinek  <jakub@redhat.com>
>
>         PR c++/92343
>         * constexpr.c (potential_constant_expression_1): Return true rather
>         than false for PREDICT_EXPR.
>
>         * g++.dg/cpp2a/attr-likely6.C: New test.
>
> --- gcc/cp/constexpr.c.jj       2019-11-02 00:26:48.965846855 +0100
> +++ gcc/cp/constexpr.c  2019-11-04 09:53:35.070621487 +0100
> @@ -6493,6 +6493,7 @@ potential_constant_expression_1 (tree t,
>      case LABEL_DECL:
>      case LABEL_EXPR:
>      case CASE_LABEL_EXPR:
> +    case PREDICT_EXPR:
>      case CONST_DECL:
>      case SIZEOF_EXPR:
>      case ALIGNOF_EXPR:
> @@ -7354,7 +7355,6 @@ potential_constant_expression_1 (tree t,
>        return true;
>
>      case EMPTY_CLASS_EXPR:
> -    case PREDICT_EXPR:
>        return false;
>
>      case GOTO_EXPR:
> --- gcc/testsuite/g++.dg/cpp2a/attr-likely6.C.jj        2019-11-04 09:54:50.126485303 +0100
> +++ gcc/testsuite/g++.dg/cpp2a/attr-likely6.C   2019-11-04 09:55:21.001017926 +0100
> @@ -0,0 +1,14 @@
> +// PR c++/92343
> +// { dg-do compile { target c++14 } }
> +
> +constexpr bool
> +foo (bool x)
> +{
> +  if (x)
> +    [[unlikely]] return true;
> +  else
> +    [[likely]] return false;
> +}
> +
> +static_assert (foo (true), "");
> +static_assert (!foo (false), "");
>
>         Jakub
Jakub Jelinek Nov. 5, 2019, 11:49 a.m. UTC | #2
On Tue, Nov 05, 2019 at 08:19:17AM +0000, Jason Merrill wrote:
> OK.

Thanks, committed.

>  I wonder why we're returning false for EMPTY_CLASS_EXPR?

No idea.  Seems it has been added in http://gcc.gnu.org/r227296 on
c++-delayed-folding branch by Kai, but there was no testsuite coverage nor
I can find any mail on gcc-patches for it.

	Jakub

Patch
diff mbox series

--- gcc/cp/constexpr.c.jj	2019-11-02 00:26:48.965846855 +0100
+++ gcc/cp/constexpr.c	2019-11-04 09:53:35.070621487 +0100
@@ -6493,6 +6493,7 @@  potential_constant_expression_1 (tree t,
     case LABEL_DECL:
     case LABEL_EXPR:
     case CASE_LABEL_EXPR:
+    case PREDICT_EXPR:
     case CONST_DECL:
     case SIZEOF_EXPR:
     case ALIGNOF_EXPR:
@@ -7354,7 +7355,6 @@  potential_constant_expression_1 (tree t,
       return true;
 
     case EMPTY_CLASS_EXPR:
-    case PREDICT_EXPR:
       return false;
 
     case GOTO_EXPR:
--- gcc/testsuite/g++.dg/cpp2a/attr-likely6.C.jj	2019-11-04 09:54:50.126485303 +0100
+++ gcc/testsuite/g++.dg/cpp2a/attr-likely6.C	2019-11-04 09:55:21.001017926 +0100
@@ -0,0 +1,14 @@ 
+// PR c++/92343
+// { dg-do compile { target c++14 } }
+
+constexpr bool
+foo (bool x)
+{
+  if (x)
+    [[unlikely]] return true;
+  else
+    [[likely]] return false;
+}
+
+static_assert (foo (true), "");
+static_assert (!foo (false), "");