public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/67371] New: Never executed "throw" in constexpr function fails to compile
@ 2015-08-27 13:26 ldionne.2 at gmail dot com
  2015-08-27 22:52 ` [Bug c++/67371] " ldionne.2 at gmail dot com
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: ldionne.2 at gmail dot com @ 2015-08-27 13:26 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 67371
           Summary: Never executed "throw" in constexpr function fails to
                    compile
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ldionne.2 at gmail dot com
  Target Milestone: ---

The following code fails to compile on GCC trunk:

    constexpr void f() {
        if (false)
            throw;
    }


The error is:

    [snip]: In function 'constexpr void f()':
    [snip]: error: expression '<throw-expression>' is not a constant-expression
     }
     ^


The code should compile because the throw expression is never executed inside a
constexpr context. Clang indeed compiles this just fine.

Live example: http://melpon.org/wandbox/permlink/V0g96xpWdO2eWGNx


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

* [Bug c++/67371] Never executed "throw" in constexpr function fails to compile
  2015-08-27 13:26 [Bug c++/67371] New: Never executed "throw" in constexpr function fails to compile ldionne.2 at gmail dot com
@ 2015-08-27 22:52 ` ldionne.2 at gmail dot com
  2015-08-28  5:30 ` trippels at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: ldionne.2 at gmail dot com @ 2015-08-27 22:52 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Louis Dionne <ldionne.2 at gmail dot com> ---
This is almost certainly a duplicate of #66026, yet it is still unconfirmed.


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

* [Bug c++/67371] Never executed "throw" in constexpr function fails to compile
  2015-08-27 13:26 [Bug c++/67371] New: Never executed "throw" in constexpr function fails to compile ldionne.2 at gmail dot com
  2015-08-27 22:52 ` [Bug c++/67371] " ldionne.2 at gmail dot com
@ 2015-08-28  5:30 ` trippels at gcc dot gnu.org
  2015-08-28  5:32 ` trippels at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: trippels at gcc dot gnu.org @ 2015-08-28  5:30 UTC (permalink / raw)
  To: gcc-bugs

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

Markus Trippelsdorf <trippels at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rhalbersma at gmail dot com

--- Comment #2 from Markus Trippelsdorf <trippels at gcc dot gnu.org> ---
*** Bug 66026 has been marked as a duplicate of this bug. ***


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

* [Bug c++/67371] Never executed "throw" in constexpr function fails to compile
  2015-08-27 13:26 [Bug c++/67371] New: Never executed "throw" in constexpr function fails to compile ldionne.2 at gmail dot com
  2015-08-27 22:52 ` [Bug c++/67371] " ldionne.2 at gmail dot com
  2015-08-28  5:30 ` trippels at gcc dot gnu.org
@ 2015-08-28  5:32 ` trippels at gcc dot gnu.org
  2015-08-28  8:43 ` trippels at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: trippels at gcc dot gnu.org @ 2015-08-28  5:32 UTC (permalink / raw)
  To: gcc-bugs

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

Markus Trippelsdorf <trippels at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |rejects-valid
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2015-08-28
                 CC|                            |trippels at gcc dot gnu.org
     Ever confirmed|0                           |1
      Known to fail|                            |5.2.1, 6.0

--- Comment #3 from Markus Trippelsdorf <trippels at gcc dot gnu.org> ---
Switch statements are rejected, too:

constexpr int fun(int n) {
  switch (n) {
  case 0:
    return 1;
  default:
    throw;
  }
}
static_assert(fun(0), "");

Only the ternary operator works fine.


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

* [Bug c++/67371] Never executed "throw" in constexpr function fails to compile
  2015-08-27 13:26 [Bug c++/67371] New: Never executed "throw" in constexpr function fails to compile ldionne.2 at gmail dot com
                   ` (2 preceding siblings ...)
  2015-08-28  5:32 ` trippels at gcc dot gnu.org
@ 2015-08-28  8:43 ` trippels at gcc dot gnu.org
  2015-08-28  8:57 ` miyuki at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: trippels at gcc dot gnu.org @ 2015-08-28  8:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Markus Trippelsdorf <trippels at gcc dot gnu.org> ---
The fix might be as simple as:

diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index 1eacb8be9a44..7016b347a79c 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -4324,7 +4324,6 @@ potential_constant_expression_1 (tree t, bool want_rval,
bool strict,
     case VEC_NEW_EXPR:
     case DELETE_EXPR:
     case VEC_DELETE_EXPR:
-    case THROW_EXPR:
     case OMP_ATOMIC:
     case OMP_ATOMIC_READ:
     case OMP_ATOMIC_CAPTURE_OLD:
@@ -4629,6 +4628,9 @@ potential_constant_expression_1 (tree t, bool want_rval,
bool strict,
       /* We can see these in statement-expressions.  */
       return true;

+    case THROW_EXPR:
+      return true;
+
     default:
       if (objc_is_property_ref (t))
        return false;


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

* [Bug c++/67371] Never executed "throw" in constexpr function fails to compile
  2015-08-27 13:26 [Bug c++/67371] New: Never executed "throw" in constexpr function fails to compile ldionne.2 at gmail dot com
                   ` (3 preceding siblings ...)
  2015-08-28  8:43 ` trippels at gcc dot gnu.org
@ 2015-08-28  8:57 ` miyuki at gcc dot gnu.org
  2015-08-28  9:37 ` trippels at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: miyuki at gcc dot gnu.org @ 2015-08-28  8:57 UTC (permalink / raw)
  To: gcc-bugs

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

Mikhail Maltsev <miyuki at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |miyuki at gcc dot gnu.org

--- Comment #5 from Mikhail Maltsev <miyuki at gcc dot gnu.org> ---
(In reply to Markus Trippelsdorf from comment #4)
> The fix might be as simple as:

I tried something similar but got:

./constexpr_throw.cc:4:15: error: call to non-constexpr function 'void*
__cxa_allocate_exception(long unsigned int)'
         throw 1;
or:
./constexpr_throw.cc:4:9: error: call to non-constexpr function 'void
__cxa_rethrow()'
         throw;


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

* [Bug c++/67371] Never executed "throw" in constexpr function fails to compile
  2015-08-27 13:26 [Bug c++/67371] New: Never executed "throw" in constexpr function fails to compile ldionne.2 at gmail dot com
                   ` (4 preceding siblings ...)
  2015-08-28  8:57 ` miyuki at gcc dot gnu.org
@ 2015-08-28  9:37 ` trippels at gcc dot gnu.org
  2015-08-28 10:06 ` Casey at Carter dot net
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: trippels at gcc dot gnu.org @ 2015-08-28  9:37 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Markus Trippelsdorf <trippels at gcc dot gnu.org> ---
Well, my patch survives regression testing and fixes the issue.

But unfortunately it leads to accept invalid cases like:

constexpr int f1() {
  throw;
  return 0;
}

or 

constexpr void f2() {
  throw;
}

(Even clang accepts f2.)


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

* [Bug c++/67371] Never executed "throw" in constexpr function fails to compile
  2015-08-27 13:26 [Bug c++/67371] New: Never executed "throw" in constexpr function fails to compile ldionne.2 at gmail dot com
                   ` (5 preceding siblings ...)
  2015-08-28  9:37 ` trippels at gcc dot gnu.org
@ 2015-08-28 10:06 ` Casey at Carter dot net
  2015-08-29 18:52 ` trippels at gcc dot gnu.org
  2015-08-29 18:55 ` trippels at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: Casey at Carter dot net @ 2015-08-28 10:06 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Casey Carter <Casey at Carter dot net> ---
(In reply to Markus Trippelsdorf from comment #6)
> Well, my patch survives regression testing and fixes the issue.
> 
> But unfortunately it leads to accept invalid cases like:
> 
> constexpr int f1() {
>   throw;
>   return 0;
> }
> 
> or 
> 
> constexpr void f2() {
>   throw;
> }
> 
> (Even clang accepts f2.)

I think it would be better for the compiler to accept constexpr functions that
are ill-formed NDR - like f1 and f2 - than to reject constexpr functions that
are conforming like:

constexpr int f3() {
  return 0;
  throw;
}

constexpr int* f4(bool b) {
  if (b) { return nullptr; }
  else { return new int{42}; }
}


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

* [Bug c++/67371] Never executed "throw" in constexpr function fails to compile
  2015-08-27 13:26 [Bug c++/67371] New: Never executed "throw" in constexpr function fails to compile ldionne.2 at gmail dot com
                   ` (6 preceding siblings ...)
  2015-08-28 10:06 ` Casey at Carter dot net
@ 2015-08-29 18:52 ` trippels at gcc dot gnu.org
  2015-08-29 18:55 ` trippels at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: trippels at gcc dot gnu.org @ 2015-08-29 18:52 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Markus Trippelsdorf <trippels at gcc dot gnu.org> ---
Author: trippels
Date: Sat Aug 29 18:51:26 2015
New Revision: 227323

URL: https://gcc.gnu.org/viewcvs?rev=227323&root=gcc&view=rev
Log:
Fix c++/67371 (issues with throw in constexpr)

As PR67371 shows gcc currently rejects all throw statements in
constant-expressions, even when they are never executed.

        PR c++/67371
        * constexpr.c (potential_constant_expression_1): Remove IF_STMT
        case. Move label to COND_EXPR case. Remove checking of
        SWITCH_STMT_BODY.

Added:
    trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-new.C
    trunk/gcc/testsuite/g++.dg/cpp1y/constexpr-throw.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/constexpr.c


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

* [Bug c++/67371] Never executed "throw" in constexpr function fails to compile
  2015-08-27 13:26 [Bug c++/67371] New: Never executed "throw" in constexpr function fails to compile ldionne.2 at gmail dot com
                   ` (7 preceding siblings ...)
  2015-08-29 18:52 ` trippels at gcc dot gnu.org
@ 2015-08-29 18:55 ` trippels at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: trippels at gcc dot gnu.org @ 2015-08-29 18:55 UTC (permalink / raw)
  To: gcc-bugs

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

Markus Trippelsdorf <trippels at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #9 from Markus Trippelsdorf <trippels at gcc dot gnu.org> ---
Fixed.


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

end of thread, other threads:[~2015-08-29 18:55 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-27 13:26 [Bug c++/67371] New: Never executed "throw" in constexpr function fails to compile ldionne.2 at gmail dot com
2015-08-27 22:52 ` [Bug c++/67371] " ldionne.2 at gmail dot com
2015-08-28  5:30 ` trippels at gcc dot gnu.org
2015-08-28  5:32 ` trippels at gcc dot gnu.org
2015-08-28  8:43 ` trippels at gcc dot gnu.org
2015-08-28  8:57 ` miyuki at gcc dot gnu.org
2015-08-28  9:37 ` trippels at gcc dot gnu.org
2015-08-28 10:06 ` Casey at Carter dot net
2015-08-29 18:52 ` trippels at gcc dot gnu.org
2015-08-29 18:55 ` trippels 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).