From patchwork Fri Oct 13 22:15:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 152789 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp2182665vqb; Fri, 13 Oct 2023 15:16:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFzXBhyEKuAtGKapmRkcMMqbNCaG2iotcKjYPUs3oH9PahlUL2RR8axARX3j/bHjtc7n3sg X-Received: by 2002:a05:6808:bca:b0:3a7:6b1c:8142 with SMTP id o10-20020a0568080bca00b003a76b1c8142mr38105930oik.25.1697235414595; Fri, 13 Oct 2023 15:16:54 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697235414; cv=pass; d=google.com; s=arc-20160816; b=AsA2hrpgCN8GhWGrBs6F0qs9LsVgJ0kABIxGdV40EMEkjzKns3pTBZ7uxaK9jn0KBA tV0XrofCWEWKLOowTiFjQknmJHK0nylJ4xu+618m31ComZaDSlZ7TFn0Tp2BP6BPnnl4 fZnfwNRGGtWUMmniTi1U6na2ZKs0tsgWE8hjH9kVRvaQ5pftc8lcjryL0X4dQ+IiQoMi PupBxZ4kI9hj+eNMGDgka++5j3FOeTs61tMTftT4/RWbpiuEX1IKkt3MVVmVpxiY2i6w SVpMXaLY1y3yRA7bJD/Uo6wbGlv17rhWhPAophFzFPupWJhefvKgn5+WNqEhLd/rxxJm 8gmQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :dmarc-filter:arc-filter:delivered-to; bh=6SBsn4PypUT7buqIDh9x5q/ux46UQDnAKxU+YMt6PmQ=; fh=2BFo3VQCimnyWTJsWe6sNdepvNrW+J/2ctoM6N7MZ0Y=; b=ZW2zFJWMgfHuiLOl0GI8lHssxsO8+Ru9eQSUFn5BosZEX3v6IywuQCq40RKAfKwZCS m6UzGRGGvbnfyBy9Oq3ISbSMhW5cqRueqSgQXazJmM4CLyjtYnLUotOpTtU6ILKdRYuQ rIiHMYheF3PU3WclXllmwDrO3QlBVv+HMkemYrEC7eJNSAiK32URB+SJ9uE1s9Vxd2fY p9gUp0S+FaojiWeAJf8ut7wxZws434u6utgiEn5CY6BBaWJJBSxjvsDmJfoWcsSxDw6u 0yJKPUVZB63ppLkmsBS8ODQv0t6UMxuRHJKvH0SExTDKPaGf5LUWDOs2Y378S8AhKXkN HHFg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Y/xmfoxd"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id a7-20020a0cca87000000b0064f81c25e79si1892109qvk.486.2023.10.13.15.16.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Oct 2023 15:16:54 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Y/xmfoxd"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 577F638582A4 for ; Fri, 13 Oct 2023 22:16:54 +0000 (GMT) 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 C23B63858D28 for ; Fri, 13 Oct 2023 22:16:19 +0000 (GMT) ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C23B63858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697235390; cv=none; b=DHZMwwft5J5DRjJqUXt6LXG4BZMArJWIeJdXSK+MMP0m3BLe+sMYybcz/01UsImr6LNgDXdsBqxhK5SeBVKkc7M3YPIZo7bYQBqOdUqbezdJn2uQxS2bdntoXxq5cxn8HMsjaqvLRV512kAj1DV78M4mFsR+8oqhtsC5g6ZfOMc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697235390; c=relaxed/simple; bh=4J6ciNaYiczwxQsLJLeAIDvyW8H5Jh54+t6e4Y3deq0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=e8PmgfGhSoHYRvCnKM0747Dk4kx1QPPJVlgLtz1u0f7qtcVBq8UknNCgf73MHUmF5P8Rc4+pd8++zpUHKAviJwhqzn5F5RUuhv9/3czxqbVlqF//lexeAI9gA/XbQxXfg5XaDnPl4IhD6LUnmm+Z6js2aNoVLdGkpYxuhL5XRTc= ARC-Authentication-Results: i=1; server2.sourceware.org DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C23B63858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1697235379; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=6SBsn4PypUT7buqIDh9x5q/ux46UQDnAKxU+YMt6PmQ=; b=Y/xmfoxdsC4dbjvguyY1HXzybbc/2CJ1+sGdr35mp0DoDo69VSMt4L2MqqJKXEBC+UOWio 4h11nmbKCjzPYrHMMwpHUx1UJCYXatWiSqs4MiUkzTOiuiPS31xdMpDPl/tLK9bnRUQlWT 5YRr/SdTvzzJN/1jC9InA0bVgOx0qdI= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-677-6E_RgUjhPF6YcUR9RCTDCw-1; Fri, 13 Oct 2023 18:15:59 -0400 X-MC-Unique: 6E_RgUjhPF6YcUR9RCTDCw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id ACE8E858F1C for ; Fri, 13 Oct 2023 22:15:58 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.9.83]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8D975C15BBC; Fri, 13 Oct 2023 22:15:58 +0000 (UTC) From: Marek Polacek To: GCC Patches , Jason Merrill Subject: [PATCH] c++: fix truncated diagnostic in C++23 [PR111272] Date: Fri, 13 Oct 2023 18:15:52 -0400 Message-ID: <20231013221552.518072-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 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_H4, RCVD_IN_MSPIKE_WL, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779680322038251149 X-GMAIL-MSGID: 1779680322038251149 Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- In C++23, since P2448, a constexpr function F that calls a non-constexpr function N is OK as long as we don't actually call F in a constexpr context. So instead of giving an error in maybe_save_constexpr_fundef, we only give an error when evaluating the call. Unfortunately, as shown in this PR, the diagnostic can be truncated: z.C:10:13: note: 'constexpr Jam::Jam()' is not usable as a 'constexpr' function because: 10 | constexpr Jam() { ft(); } | ^~~ ...because what? With this patch, we say: z.C:10:13: note: 'constexpr Jam::Jam()' is not usable as a 'constexpr' function because: 10 | constexpr Jam() { ft(); } | ^~~ z.C:10:23: error: call to non-'constexpr' function 'int Jam::ft()' 10 | constexpr Jam() { ft(); } | ~~^~ z.C:8:7: note: 'int Jam::ft()' declared here 8 | int ft() { return 42; } | ^~ Like maybe_save_constexpr_fundef, explain_invalid_constexpr_fn should also check the body of a constructor, not just the mem-initializer. PR c++/111272 gcc/cp/ChangeLog: * constexpr.cc (explain_invalid_constexpr_fn): Also check the body of a constructor in C++14 and up. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/constexpr-diag1.C: New test. --- gcc/cp/constexpr.cc | 10 +++++++++- gcc/testsuite/g++.dg/cpp1y/constexpr-diag1.C | 21 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-diag1.C base-commit: d78fef5371759849944966dec65d9e987efba509 diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 0f948db7c2d..dde4fec4a44 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -1098,7 +1098,15 @@ explain_invalid_constexpr_fn (tree fun) body = massage_constexpr_body (fun, body); require_potential_rvalue_constant_expression (body); if (DECL_CONSTRUCTOR_P (fun)) - cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true); + { + cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true); + if (cxx_dialect > cxx11) + { + /* Also check the body, not just the ctor-initializer. */ + body = DECL_SAVED_TREE (fun); + require_potential_rvalue_constant_expression (body); + } + } } } } diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-diag1.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-diag1.C new file mode 100644 index 00000000000..0e2909e83ef --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-diag1.C @@ -0,0 +1,21 @@ +// PR c++/111272 +// { dg-do compile { target c++14 } } +// { dg-options "-Werror=invalid-constexpr" } +// { dg-prune-output "some warnings being treated as errors" } + +struct Jam +{ + // constexpr // n.b. + int ft() { return 42; } // { dg-message "declared here" } + + constexpr Jam() { ft(); } // { dg-error "call to non-.constexpr. function" } +// { dg-message "declared here" "" { target c++20_down } .-1 } +}; + +constexpr bool test() +{ + Jam j; // { dg-error "called in a constant expression" } + return true; +} + +static_assert(test(), ""); // { dg-error "non-constant condition" }