From patchwork Thu Jul 8 01:40:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1502095 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=V3pJpgB2; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GKzYJ5wtFz9sWl for ; Thu, 8 Jul 2021 11:41:55 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 51D52396C81E for ; Thu, 8 Jul 2021 01:41:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 51D52396C81E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1625708511; bh=KQLW80Ha01NdlYZK9chzGQPT3WV+FbT4CRsJSfbQMmE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=V3pJpgB2vK2kjCe3w5MlvL38sQO7bZH//i82oUKWPDs4vPkH6PSbJ7kNxjwJS53AZ iNlYZgOtcv8pKpG0a/GbpiDu6MnqYHt4Ul2C+1lWeGiJWFhjbjGTM5nCxjcF7wohf4 gFYh+TsM4DCZI3GvYmEN/X7ofI+8XR8gT+Kxp1SI= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 3A8F23839806 for ; Thu, 8 Jul 2021 01:41:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3A8F23839806 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-458-_ff7sY-NMk--alofjO1dQQ-1; Wed, 07 Jul 2021 21:41:02 -0400 X-MC-Unique: _ff7sY-NMk--alofjO1dQQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 040FB5074F for ; Thu, 8 Jul 2021 01:41:02 +0000 (UTC) Received: from pdp-11.hsd1.ma.comcast.net (ovpn-114-138.rdu2.redhat.com [10.10.114.138]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8FF915D6A8; Thu, 8 Jul 2021 01:41:01 +0000 (UTC) To: Jason Merrill , GCC Patches Subject: [PATCH] c++: Fix noexcept with unevaluated operand [PR101087] Date: Wed, 7 Jul 2021 21:40:58 -0400 Message-Id: <20210708014058.854624-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 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. Only checking cp_unevaluated_operand therein does not work, because expr_noexcept_p can be called in an unevaluated context, so I resorted to the following cp_evaluated hack. Does that seem acceptable? Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? PR c++/101087 gcc/cp/ChangeLog: * except.c (check_noexcept_r): Don't walk into unevaluated operands. (expr_noexcept_p): Use cp_evaluated. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/noexcept70.C: New test. --- gcc/cp/except.c | 14 +++++++++++--- gcc/testsuite/g++.dg/cpp0x/noexcept70.C | 5 +++++ 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/noexcept70.C base-commit: a110855667782dac7b674d3e328b253b3b3c919b diff --git a/gcc/cp/except.c b/gcc/cp/except.c index a8cea53cf91..6f97ac40b4b 100644 --- a/gcc/cp/except.c +++ b/gcc/cp/except.c @@ -1033,12 +1033,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 (cp_unevaluated_operand) + *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 @@ -1155,6 +1158,11 @@ expr_noexcept_p (tree expr, tsubst_flags_t complain) if (expr == error_mark_node) return false; + /* Even though the operand of noexcept is an _unevaluated_ operand, + temporarily clearing cp_unevaluated_operand allows us to check it + in check_noexcept_r, to handle noexcept(sizeof(f())). It could be + set when we are called in the context of synthesized_method_walk. */ + cp_evaluated ev; fn = cp_walk_tree_without_duplicates (&expr, check_noexcept_r, 0); if (fn) { 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())), "");