From patchwork Fri Apr 12 13:12:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1923103 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=QYPQYyAC; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VGH6l5K6Cz1yYB for ; Fri, 12 Apr 2024 23:12:45 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CF43B3858408 for ; Fri, 12 Apr 2024 13:12:42 +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 4B56D3858D38 for ; Fri, 12 Apr 2024 13:12:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4B56D3858D38 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4B56D3858D38 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=1712927540; cv=none; b=acXx4bljvGrdr8rOvZlGOSXZAjPKKqgRI/5oSv0h286ClrXGZVChDLJK8G3nRMphVxATnrj4aqSI5BihtRw7/qgjddKDLQNgnuxbT1dmooda4WH37JGQwF/hHGgPL8Unw2S2vqHN68pE55MXyMZFF7M6qDoJ02tNnM4uMYEjo0A= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712927540; c=relaxed/simple; bh=d6Asnk6EF2Ykrf4LRIRi0ueOlRDOgirNqDRrqkjU4H8=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=YX8oWOx1YgOwFxQq5Q1UR7GggsbUV22BFecTOsardkZoB5ZnwzplfVUGkH7PzIUijo5AdbNvMqckAf8NyugF6uynJlLMTDf0TT2JcrRnfmqM5nYb5QqwEi4L27WnTuvvbd//3Am5wlT/x12waB8Q9RrEV+OGBp5zZp8vEItYu0s= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712927537; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=50csb9ce1qT3YhtSsRHSxKb9LrGuVaShqjEoruDn27s=; b=QYPQYyACGzHl/B+1V5+KG8jrQEhSMeJJlaTArmBw2Q6Hghlp108HsM99FmfCEnrxz2ZSuy iY1+r8lf5Tbz0UnA0URdG07fsPVCp57wgA/N7HJIy8eqp3Ix77l9aXkv/MxZlhBcCF90EM eFa7TzDzS01xyeRU0ssdR7piS1LeJUM= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-439-ezWNJhvPPGSmPehvR1Vg9w-1; Fri, 12 Apr 2024 09:12:16 -0400 X-MC-Unique: ezWNJhvPPGSmPehvR1Vg9w-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3967A806603 for ; Fri, 12 Apr 2024 13:12:16 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.14]) by smtp.corp.redhat.com (Postfix) with ESMTPS id F34E02166B31; Fri, 12 Apr 2024 13:12:15 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 43CDCADM439930 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 12 Apr 2024 15:12:10 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 43CDCAeP439929; Fri, 12 Apr 2024 15:12:10 +0200 Date: Fri, 12 Apr 2024 15:12:09 +0200 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Diagnose or avoid constexpr dtors in classes with virtual bases [PR114426] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-4.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_SHORT, 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: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! I had another look at this P1 PR today. You said in the "c++: fix in-charge parm in constexpr" mail back in December (as well as in the r14-6507 commit message): "Since a class with vbases can't have constexpr 'tors there isn't actually a need for an in-charge parameter in a destructor" but the ICE is because the destructor is marked implicitly constexpr. https://eel.is/c++draft/dcl.constexpr#3.2 says that a destructor of a class with virtual bases is not constexpr-suitable, but we were actually implementing this just for constructors, so clearly my fault from the https://wg21.link/P0784R7 implementation. That paper clearly added that sentence in there and removed similar sentence just from the constructor case. So, the following patch makes sure the else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun))) { ret = false; if (complain) error ("%q#T has virtual base classes", DECL_CONTEXT (fun)); } hunk is done no just for DECL_CONSTRUCTOR_P (fun), but also DECL_DESTRUCTOR_P (fun) - in that case just for cxx_dialect >= cxx20, as for cxx_dialect < cxx20 we already set ret = false; and diagnose a different error, so no need to diagnose two. Bootstrapped/regtested on x86_64-linux and i686-linux, and checked it fixes the testcase in a cross to armv7hl-linux-gnueabi, ok for trunk? 2024-04-12 Jakub Jelinek PR c++/114426 * constexpr.cc (is_valid_constexpr_fn): Return false/diagnose with complain destructors in classes with virtual bases. * g++.dg/cpp2a/pr114426.C: New test. * g++.dg/cpp2a/constexpr-dtor16.C: New test. Jakub --- gcc/cp/constexpr.cc.jj 2024-04-09 09:29:04.708521907 +0200 +++ gcc/cp/constexpr.cc 2024-04-12 11:45:08.845476718 +0200 @@ -262,18 +262,15 @@ is_valid_constexpr_fn (tree fun, bool co inform (DECL_SOURCE_LOCATION (fun), "lambdas are implicitly % only in C++17 and later"); } - else if (DECL_DESTRUCTOR_P (fun)) + else if (DECL_DESTRUCTOR_P (fun) && cxx_dialect < cxx20) { - if (cxx_dialect < cxx20) - { - ret = false; - if (complain) - error_at (DECL_SOURCE_LOCATION (fun), - "% destructors only available" - " with %<-std=c++20%> or %<-std=gnu++20%>"); - } + ret = false; + if (complain) + error_at (DECL_SOURCE_LOCATION (fun), + "% destructors only available with " + "%<-std=c++20%> or %<-std=gnu++20%>"); } - else if (!DECL_CONSTRUCTOR_P (fun)) + else if (!DECL_CONSTRUCTOR_P (fun) && !DECL_DESTRUCTOR_P (fun)) { tree rettype = TREE_TYPE (TREE_TYPE (fun)); if (!literal_type_p (rettype)) --- gcc/testsuite/g++.dg/cpp2a/pr114426.C.jj 2024-04-12 12:05:07.443891700 +0200 +++ gcc/testsuite/g++.dg/cpp2a/pr114426.C 2024-04-12 12:05:07.443891700 +0200 @@ -0,0 +1,7 @@ +// PR c++/114426 +// { dg-do compile } +// { dg-additional-options "-O2" } + +struct A { virtual ~A (); }; +struct B : virtual A { virtual void foo () = 0; }; +struct C : B { C () {} }; --- gcc/testsuite/g++.dg/cpp2a/constexpr-dtor16.C.jj 2024-04-12 12:05:35.398505976 +0200 +++ gcc/testsuite/g++.dg/cpp2a/constexpr-dtor16.C 2024-04-12 12:08:31.771072322 +0200 @@ -0,0 +1,7 @@ +// PR c++/114426 +// { dg-do compile { target c++11 } } + +struct A { virtual ~A (); }; +struct B : virtual A { constexpr ~B () {} }; +// { dg-error "'struct B' has virtual base classes" "" { target c++20 } .-1 } +// { dg-error "'constexpr' destructors only available with" "" { target c++17_down } .-2 }