From patchwork Tue Sep 28 09:24:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 45495 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 8D0B43858426 for ; Tue, 28 Sep 2021 09:25:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8D0B43858426 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1632821134; bh=c8Rhfhoe0RyH7T35RkA1LD74VHhJmtWFShxsRCvtBao=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=eI7NhAytXYWvAbon5ZFKh6adXxKRqVWElK3EVasWzPyJXioL6tmDhs5NE775Wh9QI ZVVJwqV4zA02yOCgxL8gRyNeKU/aQuZBgxicOs7insZgL21b6Z2m+3F4KEw3vkfmQ9 HKu9TSw83SmPLXszvl+Rrzh39N5WiJRYIJ7euKAk= 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 [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 5DE493858C2C for ; Tue, 28 Sep 2021 09:25:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5DE493858C2C 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-341-As4NMWPLN428ndomFPgrVQ-1; Tue, 28 Sep 2021 05:25:01 -0400 X-MC-Unique: As4NMWPLN428ndomFPgrVQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BFC4F1006AA2 for ; Tue, 28 Sep 2021 09:25:00 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.34]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5B9DB60871; Tue, 28 Sep 2021 09:24:58 +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 18S9Otws3197964 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 28 Sep 2021 11:24:55 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 18S9Otte3197963; Tue, 28 Sep 2021 11:24:55 +0200 Date: Tue, 28 Sep 2021 11:24:55 +0200 To: Jason Merrill Subject: [PATCH] c++: Fix up synthetization of defaulted comparison operators on classes with bitfields [PR102490] Message-ID: <20210928092455.GU304296@tucnak> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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" Hi! The testcases in the patch are either miscompiled or ICE with checking, because the defaulted operator== is synthetized too early (but only if constexpr), when the corresponding class type is still incomplete type. The problem is that at that point the bitfield FIELD_DECLs still have as TREE_TYPE their underlying type rather than integral type with their precision and when layout_class_type is called for the class soon after that, it changes those types but the COMPONENT_REFs type stay the way that they were during the operator== synthetize_method type and the middle-end is then upset by the mismatch of types. As what exact type will be given isn't just a one liner but quite long code especially for over-sized bitfields, I think it is best to just not synthetize the comparison operators so early (the defaulted_late_check change) and call defaulted_late_check for them once again as soon as the class is complete. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2021-09-28 Jakub Jelinek PR c++/102490 * method.c (defaulted_late_check): Don't synthetize constexpr defaulted comparisons if context is still incomplete type. (finish_struct_1): Call defaulted_late_check again for defaulted comparisons. * g++.dg/cpp2a/spaceship-eq11.C: New test. * g++.dg/cpp2a/spaceship-eq12.C: New test. Jakub --- gcc/cp/method.c.jj 2021-09-15 08:55:37.563497558 +0200 +++ gcc/cp/method.c 2021-09-27 13:48:12.139271830 +0200 @@ -3160,8 +3160,11 @@ defaulted_late_check (tree 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)) + qualifies. Otherwise we can define the function lazily. + Don't do this if the class type is still incomplete. */ + if (DECL_DECLARED_CONSTEXPR_P (fn) + && !DECL_INITIAL (fn) + && COMPLETE_TYPE_P (ctx)) { /* Prevent GC. */ function_depth++; --- gcc/cp/class.c.jj 2021-09-03 09:46:28.801428380 +0200 +++ gcc/cp/class.c 2021-09-27 14:07:03.465562255 +0200 @@ -7467,7 +7467,14 @@ 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) + defaulted_late_check (x); + 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-27 14:20:04.723713371 +0200 +++ gcc/testsuite/g++.dg/cpp2a/spaceship-eq11.C 2021-09-27 14:20:20.387495858 +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-27 14:20:12.050611625 +0200 +++ gcc/testsuite/g++.dg/cpp2a/spaceship-eq12.C 2021-09-27 14:20:39.633228602 +0200 @@ -0,0 +1,5 @@ +// PR c++/102490 +// { dg-do run { target c++20 } } +// { dg-options "-O2" } + +#include "spaceship-eq11.C"