public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Marek Polacek <mpolacek@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r11-8709] c++: Fix noexcept with unevaluated operand [PR101087]
Date: Thu,  8 Jul 2021 21:48:12 +0000 (GMT)	[thread overview]
Message-ID: <20210708214812.B3FF7385780B@sourceware.org> (raw)

https://gcc.gnu.org/g:cbef732522568f8adce46c472b16391c864d0fd0

commit r11-8709-gcbef732522568f8adce46c472b16391c864d0fd0
Author: Marek Polacek <polacek@redhat.com>
Date:   Wed Jul 7 20:02:18 2021 -0400

    c++: Fix noexcept with unevaluated operand [PR101087]
    
    It sounds plausible that this assert
    
      int f();
      static_assert(noexcept(sizeof(f())));
    
    should pass: sizeof produces a std::size_t and its operand is not
    evaluated, so it can't throw.  noexcept should only evaluate to
    false for potentially evaluated operands.  Therefore I think that
    check_noexcept_r shouldn't walk into operands of sizeof/decltype/
    alignof/typeof.
    
            PR c++/101087
    
    gcc/cp/ChangeLog:
    
            * cp-tree.h (unevaluated_p): New.
            * except.c (check_noexcept_r): Use it.  Don't walk into
            unevaluated operands.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp0x/noexcept70.C: New test.
    
    (cherry picked from commit dee00bf6894be0cabb8f263c993357a6f8444f8b)

Diff:
---
 gcc/cp/cp-tree.h                        | 13 +++++++++++++
 gcc/cp/except.c                         |  9 ++++++---
 gcc/testsuite/g++.dg/cpp0x/noexcept70.C |  5 +++++
 3 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 877ed1c1c65..bdc33a79241 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -8429,6 +8429,19 @@ is_constrained_auto (const_tree t)
   return is_auto (t) && PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t);
 }
 
+/* True if CODE, a tree code, denotes a tree whose operand is not evaluated
+   as per [expr.context], i.e., an operand to sizeof, typeof, decltype, or
+   alignof.  */
+
+inline bool
+unevaluated_p (tree_code code)
+{
+  return (code == DECLTYPE_TYPE
+	  || code == ALIGNOF_EXPR
+	  || code == SIZEOF_EXPR
+	  || code == NOEXCEPT_EXPR);
+}
+
 /* RAII class to push/pop class scope T; if T is not a class, do nothing.  */
 
 struct push_nested_class_guard
diff --git a/gcc/cp/except.c b/gcc/cp/except.c
index cbafc09629b..bc260d145d1 100644
--- a/gcc/cp/except.c
+++ b/gcc/cp/except.c
@@ -1032,12 +1032,15 @@ check_handlers (tree handlers)
      expression whose type is a polymorphic class type (10.3).  */
 
 static tree
-check_noexcept_r (tree *tp, int * /*walk_subtrees*/, void * /*data*/)
+check_noexcept_r (tree *tp, int *walk_subtrees, void *)
 {
   tree t = *tp;
   enum tree_code code = TREE_CODE (t);
-  if ((code == CALL_EXPR && CALL_EXPR_FN (t))
-      || code == AGGR_INIT_EXPR)
+
+  if (unevaluated_p (code))
+    *walk_subtrees = false;
+  else if ((code == CALL_EXPR && CALL_EXPR_FN (t))
+	   || code == AGGR_INIT_EXPR)
     {
       /* We can only use the exception specification of the called function
 	 for determining the value of a noexcept expression; we can't use
diff --git a/gcc/testsuite/g++.dg/cpp0x/noexcept70.C b/gcc/testsuite/g++.dg/cpp0x/noexcept70.C
new file mode 100644
index 00000000000..45a6137dd6f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/noexcept70.C
@@ -0,0 +1,5 @@
+// PR c++/101087
+// { dg-do compile { target c++11 } }
+
+int f();
+static_assert(noexcept(sizeof(f())), "");


                 reply	other threads:[~2021-07-08 21:48 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210708214812.B3FF7385780B@sourceware.org \
    --to=mpolacek@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).