From patchwork Fri Feb 17 20:32:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 58771 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp55355wrn; Fri, 17 Feb 2023 12:33:02 -0800 (PST) X-Google-Smtp-Source: AK7set+Se+JwEu5/5ZL78r9cJfQYOxe0cRrLRZBjsYGByp9AQiWWMmXZLTsIXRLXfR2I/Dj4z2bT X-Received: by 2002:a17:907:2149:b0:8aa:6e27:103b with SMTP id rk9-20020a170907214900b008aa6e27103bmr1780298ejb.67.1676665982409; Fri, 17 Feb 2023 12:33:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676665982; cv=none; d=google.com; s=arc-20160816; b=alyDa5lF6jaGuUWBKn3iJPosBGJ3l+7jRhv4b+P4JSh3LnfPsfb4Ps3dqE8V2uwvdI U9q/HinoCf//ji9M2L7TpzMNGpkIaSiDyydNnlAbQuhAq4rI9pWdwqJ83KfYOo4d4xP6 Qx7P3vT4QctAOqvTVrtcDIF4eeXdhP0dEOgdQj8Jyy89Q9USlRciEzLbDzs9BMtrvmuU IX4owwBqy2c+cPOHAWAof+jowPp2TViHPxEaI7iFKueATPyth1S7eUirLLLc2V7hOwQp DRTRwahkA0G9sNZDS80jWNt5GySBOtzD3X8iICoXhhgVHpGnBYs6tMC3OfcyXcgIgzvr rYDQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=SKZ4OFTZrIk6Bfzu1kJrmS5uVbbIPWa3S78sOlbLI4M=; b=M4yGv9/qWzaLd+ZtB+5PSQqwSYJN73ssgJRjJMsZsCB+YPMi1jdbJl7cweDxgf0CIg US6jcA0Akagydu/B3/Rni0DT/KNxl9zdpyjpncU+bvfdg6nutgDX1XEbN2iOAIe6hzD1 JS21ufvqk8HEIHDK254nQ26dyFuKuA53wCR1Uabv/wqLhzGn3mGxN/lSzmzDPg4cKWjl Z8nDzXdd6bb8HQ/IhMPnFukjZzjQ9gGEeUGBgjtkxrEPQSR9LiVMoUBtejsafIqsvqQM u44Q3yxN/L1cMYPhXqnAHBZLS+G8vYqPwJ1lax3eMr8pPnZ3FtcBpxRx8XqBmtQVIufy mGzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=S1RqZS2g; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id vj23-20020a170907131700b008b17f97aa47si3309470ejb.710.2023.02.17.12.33.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Feb 2023 12:33:02 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=S1RqZS2g; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 384E7384F031 for ; Fri, 17 Feb 2023 20:33:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 384E7384F031 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1676665981; bh=SKZ4OFTZrIk6Bfzu1kJrmS5uVbbIPWa3S78sOlbLI4M=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=S1RqZS2gjHbPj9wUz0sMTCY7ipM7a0JEaQLuyHNm050lO6aqOkO0bijv6EawISB/q ktQfxagFcjicW0xFd85L7VZydFRmsX9aNteAzlHhzwSddj+RoiwKFARAfkBT+UZ1yN pHbuLB0HWAbbu3xd51opwAELCalsDSetCseRD6Z4= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id F1B2A385828E for ; Fri, 17 Feb 2023 20:32:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F1B2A385828E Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-351-1f2NFbe-O9K7a7aulFAURA-1; Fri, 17 Feb 2023 15:32:16 -0500 X-MC-Unique: 1f2NFbe-O9K7a7aulFAURA-1 Received: by mail-qk1-f199.google.com with SMTP id x28-20020a05620a099c00b006e08208eb31so727308qkx.3 for ; Fri, 17 Feb 2023 12:32:16 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=SKZ4OFTZrIk6Bfzu1kJrmS5uVbbIPWa3S78sOlbLI4M=; b=laWLRQpNAXpqbqJXSGAvpn2pJTW0eqRPrX/LNPDv0s/wI55Qo5kCFTtR4fh5eEbMvx 4vQ4mxevDV/JZ3qULwJEdRSZ2F3Y0IsxOERgu8Jdx4ulSToPeUy7Rtu1lCR5D3JlmQPC PqmZ+GLlxfc78ErYEOWFErZrXGAwBF0GqwDg36HNv1iCkBHkwNk06Kt0ukuvSMHs0/6Z z3XQ/PY6Qp7tBvxCsM8dAWEb+Xvg4kAUwvtg6qeS3lWBzXM/bicvvw9gGuiYvCPk1elB BcnCaJqNZ8jLAXtVX6V0T2PISa6apuPnkIBK0EpfYUOAbWiHtdqQN0a7NFCoL1mMch92 cSpQ== X-Gm-Message-State: AO0yUKUUYSFwR8cI5h+iwk/NsAskc9R2laaioM1dMHR9D7c/cH5H4EIw MOb46emg3qePePCRR6CboSlIG79MmuNvLR+M7s15aL4f5HBr1GeHeTRMvtaalYYk+rDVqCOMeX+ 1udgu0/xpizvlx/GETkWNtxgary41JaXCscYzR2Gm2fA5qCiIATf4PZG3o8VCN6WYf/4x0g0= X-Received: by 2002:ac8:7d46:0:b0:3b8:2e36:9d24 with SMTP id h6-20020ac87d46000000b003b82e369d24mr2696957qtb.50.1676665935617; Fri, 17 Feb 2023 12:32:15 -0800 (PST) X-Received: by 2002:ac8:7d46:0:b0:3b8:2e36:9d24 with SMTP id h6-20020ac87d46000000b003b82e369d24mr2696893qtb.50.1676665934977; Fri, 17 Feb 2023 12:32:14 -0800 (PST) Received: from jason.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id b20-20020ac844d4000000b003b84b92052asm3850005qto.57.2023.02.17.12.32.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Feb 2023 12:32:14 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: [PATCH RFC] c++: static_assert (false) in template [DR2518] Date: Fri, 17 Feb 2023 15:32:09 -0500 Message-Id: <20230217203209.2141339-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.5 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_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758111708935137994?= X-GMAIL-MSGID: =?utf-8?q?1758111708935137994?= Tested x86_64-pc-linux-gnu. This isn't really a regression fix, but it's very safe, and fixes a longstanding annoyance, so I'm leaning toward putting it into GCC 13. Thoughts? -- 8< -- For a long time, people have expected to be able to write static_assert (false) in a template and only have it diagnosed if the template is instantiated, but we (and other implementations) gave an error about the uninstantiated template because the standard says that if no valid instantiation of the template is possible, the program is ill-formed, no diagnostic required, and we try to diagnose IFNDR things when feasible. At the meeting last week we were looking at CWG2518, which wanted to specify that an implementation must not accept a program containing a failing #error or static_assert. We also looked at P2593, which proposed allowing static_assert in an uninstantiated template. We ended up combining these two in order to avoid requiring implementations to reject programs with static_assert (false) in uninstantiated templates. The committee accepted this as a DR, so I'm making the change to all standard modes. This behavior was also conformant previously, since no diagnostic was required in this case. We continue to diagnose non-constant or otherwise ill-formed conditions, so no changes to existing tests were needed. DR 2518 PR c++/52809 PR c++/53638 PR c++/87389 PR c++/89741 PR c++/92099 PR c++/104041 PR c++/104691 gcc/cp/ChangeLog: * semantics.cc (finish_static_assert): Don't diagnose in template context. gcc/testsuite/ChangeLog: * g++.dg/DRs/dr2518.C: New test. --- gcc/cp/semantics.cc | 17 ++++++++++------- gcc/testsuite/g++.dg/DRs/dr2518.C | 7 +++++++ 2 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/g++.dg/DRs/dr2518.C base-commit: 07f497c2da3600cc99cd7d1b5c6726972fb2b5a1 diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index c2df0b69b30..79b7cc72f21 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -11232,14 +11232,16 @@ finish_static_assert (tree condition, tree message, location_t location, if (check_for_bare_parameter_packs (condition)) condition = error_mark_node; + /* Save the condition in case it was a concept check. */ + tree orig_condition = condition; + if (instantiation_dependent_expression_p (condition)) { /* We're in a template; build a STATIC_ASSERT and put it in the right place. */ - tree assertion; - - assertion = make_node (STATIC_ASSERT); - STATIC_ASSERT_CONDITION (assertion) = condition; + defer: + tree assertion = make_node (STATIC_ASSERT); + STATIC_ASSERT_CONDITION (assertion) = orig_condition; STATIC_ASSERT_MESSAGE (assertion) = message; STATIC_ASSERT_SOURCE_LOCATION (assertion) = location; @@ -11253,9 +11255,6 @@ finish_static_assert (tree condition, tree message, location_t location, return; } - /* Save the condition in case it was a concept check. */ - tree orig_condition = condition; - /* Fold the expression and convert it to a boolean value. */ condition = contextual_conv_bool (condition, complain); condition = fold_non_dependent_expr (condition, complain, @@ -11270,6 +11269,10 @@ finish_static_assert (tree condition, tree message, location_t location, if (integer_zerop (condition)) { + /* CWG2518: static_assert failure in a template is not IFNDR. */ + if (processing_template_decl) + goto defer; + int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (message)))); int len = TREE_STRING_LENGTH (message) / sz - 1; diff --git a/gcc/testsuite/g++.dg/DRs/dr2518.C b/gcc/testsuite/g++.dg/DRs/dr2518.C new file mode 100644 index 00000000000..240186211e6 --- /dev/null +++ b/gcc/testsuite/g++.dg/DRs/dr2518.C @@ -0,0 +1,7 @@ +// CWG 2518 +// { dg-do compile { target c++11 } } + +template void f() +{ + static_assert (false, ""); +}