From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 946E43858408 for ; Tue, 28 Sep 2021 13:53:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 946E43858408 Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-578-zHgCVWKyMh6kpdp4G1JWNw-1; Tue, 28 Sep 2021 09:53:09 -0400 X-MC-Unique: zHgCVWKyMh6kpdp4G1JWNw-1 Received: by mail-qt1-f198.google.com with SMTP id a18-20020aed2792000000b002a6d480aa95so71566250qtd.14 for ; Tue, 28 Sep 2021 06:53:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:date:to:cc:subject:in-reply-to:message-id :references:mime-version; bh=GDIWhdR7Ah5NrUh151zNsIfdCPPGOO2egUqWNx81PEY=; b=oyPbbBoVdIDlmg47RQrHnqASMZdWzVDq24Q31LJ5fSiG7xV1vmU8K/rWWSfowehbo6 qG2woTC0c8mX4CydlxYAO6X4NXL9PRDpTCnWK+N8kBUZqNg5aNMzuvCbHuS14k1VQ8Lr G0wdhXKhia1U61DjEIdm9YVR4o+Ho8Vuwkn99OnkDnbF8bzzv5idGIPXwKawMuzpccsN S/Hz/fWED6m3uhJ7aZQoPuvgWAR6nY2jtCLh1jBxKjYMfoG+EuyYUGirGdPc//4GYuZc iWhynM7KCN5Bh+PEYEZoE9st2DGZXgtHcZYo+eDncaBKpzgJmkVSQVojniDRlWqnIPpD q03w== X-Gm-Message-State: AOAM532VrlTCcC/Xux8fOMeQMuYvEWcVBySFA3ObjhAwjyPadcgJwiUx E/nhsXvbeN3y3jK+cIjIA0SuVgqBe4dv9zUXf+aVtbEAYgvnT4C0HzHd14AL5uYY5Yr1SsqucLv foUoLE4bEnRmxRCEntA== X-Received: by 2002:ac8:738f:: with SMTP id t15mr5774511qtp.113.1632837189267; Tue, 28 Sep 2021 06:53:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzxlsxUtgRTHIMi3EYnFuZdRs8rrhQysq+Ke33w1v6fzhQLMAofUZxhANJKNorsS1JWdzzbrw== X-Received: by 2002:ac8:738f:: with SMTP id t15mr5774483qtp.113.1632837189039; Tue, 28 Sep 2021 06:53:09 -0700 (PDT) Received: from [192.168.1.130] (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id 13sm14594961qka.56.2021.09.28.06.53.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Sep 2021 06:53:08 -0700 (PDT) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Tue, 28 Sep 2021 09:53:07 -0400 (EDT) To: Patrick Palka cc: Jakub Jelinek , Jason Merrill , gcc-patches@gcc.gnu.org Subject: Re: [PATCH] c++: Fix up synthetization of defaulted comparison operators on classes with bitfields [PR102490] In-Reply-To: <742e6e4c-f921-e12-ccc0-edc6ca19f99@idea> Message-ID: <28ed21ef-fac2-8e93-44f4-f2b5ab287621@idea> References: <20210928092455.GU304296@tucnak> <742e6e4c-f921-e12-ccc0-edc6ca19f99@idea> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-10.0 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-List-Received-Date: Tue, 28 Sep 2021 13:53:13 -0000 On Tue, 28 Sep 2021, Patrick Palka wrote: > On Tue, 28 Sep 2021, Jakub Jelinek via Gcc-patches wrote: > > > 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. > > Nice, this might also fix PR98712. > > > > > 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. > > > > --- 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)) > > { > > According to the function comment for defaulted_late_check, won't > COMPLETE_TYPE_P (ctx) always be false here? 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. > > > /* 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" > > > > Jakub > > > > >