From patchwork Tue Sep 28 20:34:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 45536 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 73364385800C for ; Tue, 28 Sep 2021 20:35:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 73364385800C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1632861328; bh=mLrqWQg5RiGEs2pu57mKo5+bMN0Pcuk+WjKUcVqZRS8=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=YY9hOGZ1ehyRU+WI3KxRrAYpmp3YTcNdL7CYJ/qL4RTDf8LWo6ytCfzxwq4p+d0ZK 9Ec83vttos5i2uD4qsy0brzucqZ6RE/afGC6xmidK5bf+zXENXnjxsHXBa9+A1WEMi rMNg08J/cvHlqJgOngOd7HD5n4c+Y5tXxjS1yo6c= 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 196E53858406 for ; Tue, 28 Sep 2021 20:34:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 196E53858406 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-404-FpbmB3jVPpCujZI5Y7bI3w-1; Tue, 28 Sep 2021 16:34:57 -0400 X-MC-Unique: FpbmB3jVPpCujZI5Y7bI3w-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7D739835DEE for ; Tue, 28 Sep 2021 20:34:56 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.193.109]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 46D4560C81; Tue, 28 Sep 2021 20:34:42 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 18SKYeFJ3221565 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 28 Sep 2021 22:34:40 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 18SKYd8Q3221564; Tue, 28 Sep 2021 22:34:39 +0200 Date: Tue, 28 Sep 2021 22:34:39 +0200 To: Jason Merrill Subject: [PATCH, v2] c++: Fix up synthetization of defaulted comparison operators on classes with bitfields [PR102490] Message-ID: <20210928203439.GG304296@tucnak> References: <20210928092455.GU304296@tucnak> <742e6e4c-f921-e12-ccc0-edc6ca19f99@idea> <28ed21ef-fac2-8e93-44f4-f2b5ab287621@idea> <38a8c792-f599-5802-ffe3-a43eaee81479@redhat.com> MIME-Version: 1.0 In-Reply-To: <38a8c792-f599-5802-ffe3-a43eaee81479@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-5.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, 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: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" On Tue, Sep 28, 2021 at 03:33:35PM -0400, Jason Merrill wrote: > > > According to the function comment for defaulted_late_check, won't > > > COMPLETE_TYPE_P (ctx) always be false here? > > Not for a function defaulted outside the class. > > > If so, I wonder if we could get away with moving this entire fragment > > from defaulted_late_check to finish_struct_1 instead of calling > > defaulted_late_check from finish_struct_1. > > The comment in check_bases_and_members says that we call it there so that > it's before we clone [cd]tors. Probably better to leave the call there for > other functions, just skip it for comparisons. So like this instead then? Just tested with dg.exp=*spaceship* so far. 2021-09-28 Jakub Jelinek PR c++/102490 * method.c (defaulted_late_check): Don't synthetize constexpr defaulted comparisons. (finish_struct_1): Synthetize constexpr defaulted comparisons here after layout_class_type. * g++.dg/cpp2a/spaceship-eq11.C: New test. * g++.dg/cpp2a/spaceship-eq12.C: New test. Jakub --- gcc/cp/method.c.jj 2021-09-28 11:34:10.165412477 +0200 +++ gcc/cp/method.c 2021-09-28 22:28:23.637981709 +0200 @@ -3158,18 +3158,7 @@ defaulted_late_check (tree fn) special_function_kind kind = special_function_p (fn); if (kind == sfk_comparison) - { - /* If the function was declared constexpr, check that the definition - qualifies. Otherwise we can define the function lazily. */ - if (DECL_DECLARED_CONSTEXPR_P (fn) && !DECL_INITIAL (fn)) - { - /* Prevent GC. */ - function_depth++; - synthesize_method (fn); - function_depth--; - } - return; - } + return; bool fn_const_p = (copy_fn_p (fn) == 2); tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p, --- gcc/cp/class.c.jj 2021-09-28 11:34:10.096413431 +0200 +++ gcc/cp/class.c 2021-09-28 22:29:59.072669058 +0200 @@ -7467,7 +7467,21 @@ finish_struct_1 (tree t) for any static member objects of the type we're working on. */ for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x)) if (DECL_DECLARES_FUNCTION_P (x)) - DECL_IN_AGGR_P (x) = false; + { + /* Synthetize constexpr defaulted comparisons. */ + if (!DECL_ARTIFICIAL (x) + && DECL_DEFAULTED_IN_CLASS_P (x) + && special_function_p (x) == sfk_comparison + && DECL_DECLARED_CONSTEXPR_P (x) + && !DECL_INITIAL (x)) + { + /* Prevent GC. */ + function_depth++; + synthesize_method (x); + function_depth--; + } + DECL_IN_AGGR_P (x) = false; + } else if (VAR_P (x) && TREE_STATIC (x) && TREE_TYPE (x) != error_mark_node && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t)) --- gcc/testsuite/g++.dg/cpp2a/spaceship-eq11.C.jj 2021-09-28 22:27:40.524574708 +0200 +++ gcc/testsuite/g++.dg/cpp2a/spaceship-eq11.C 2021-09-28 22:27:40.524574708 +0200 @@ -0,0 +1,43 @@ +// PR c++/102490 +// { dg-do run { target c++20 } } + +struct A +{ + unsigned char a : 1; + unsigned char b : 1; + constexpr bool operator== (const A &) const = default; +}; + +struct B +{ + unsigned char a : 8; + int : 0; + unsigned char b : 7; + constexpr bool operator== (const B &) const = default; +}; + +struct C +{ + unsigned char a : 3; + unsigned char b : 1; + constexpr bool operator== (const C &) const = default; +}; + +void +foo (C &x, int y) +{ + x.b = y; +} + +int +main () +{ + A a{}, b{}; + B c{}, d{}; + C e{}, f{}; + a.b = 1; + d.b = 1; + foo (e, 0); + foo (f, 1); + return a == b || c == d || e == f; +} --- gcc/testsuite/g++.dg/cpp2a/spaceship-eq12.C.jj 2021-09-28 22:27:40.524574708 +0200 +++ gcc/testsuite/g++.dg/cpp2a/spaceship-eq12.C 2021-09-28 22:27:40.524574708 +0200 @@ -0,0 +1,5 @@ +// PR c++/102490 +// { dg-do run { target c++20 } } +// { dg-options "-O2" } + +#include "spaceship-eq11.C"