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.129.124]) by sourceware.org (Postfix) with ESMTPS id 46CA93858436 for ; Sun, 31 Jul 2022 06:03:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 46CA93858436 Received: from mail-oo1-f69.google.com (mail-oo1-f69.google.com [209.85.161.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-35-XR5nBN79P6ysNkEHXmR3qg-1; Sun, 31 Jul 2022 02:03:06 -0400 X-MC-Unique: XR5nBN79P6ysNkEHXmR3qg-1 Received: by mail-oo1-f69.google.com with SMTP id v15-20020a056820004f00b00435ab0e6f8eso4104561oob.1 for ; Sat, 30 Jul 2022 23:03:05 -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:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=5ag1PsRgd8uZDldCIorAE3l3+ufjwgsJw8DW39srrjA=; b=u1/0BLrkRs6E0X5J7ZtGhHSqMgvxUa4NoZ73NaugLLPMAeHmKxNvhu3Z0cK73vJX2h svtVhJbHdnakotOZnAAp8zLBvvLKd8yiKs/7OnAvoVPCuQz+4S6690w41DkjQht7IjTq G0kzuTNodPXNw50ANhhKoO1IHuSFmeFKB+Ijkw9Ojo6O7+/TU0uPlD87M+YRKQascxDF inH38w1CyGKsLYwtGsQRhuVSUO92I4CVA1qz5H3wCKlu3knVRJevL7A0foeOLOF5Bmc+ wDysVCwKRRvnouul6ybc2jBNHpse681LTlaeL53+tnPkwVp3rMhteRTcpRLUSuXYuRS1 DT1g== X-Gm-Message-State: ACgBeo2T6UIOEuYp/FpqTLcpmDcqBl+KfGOrwQCrsYv0bjiYeWMo9ht8 56TGc9ffOUzdmL2IaONlr3FmAfwgAkQjCyLE4BTMX3DDdIxn+IFv6dHkDfQck8U2Nba8JOUNU6A 0AXchP2cc/3ZFICKjbobY82IgFMU5myNeSk7rmKPzKaqrwzYX4ua/1KBaHJXbaLDvVA== X-Received: by 2002:a05:6870:f710:b0:10e:9958:fd55 with SMTP id ej16-20020a056870f71000b0010e9958fd55mr4387619oab.77.1659247385097; Sat, 30 Jul 2022 23:03:05 -0700 (PDT) X-Google-Smtp-Source: AA6agR4J2hY9+FMyStZ7HX+k1gVGaa/NkaDMvK5VsJog7vw2q4pMeNQi5StDzQSq0E7Tytqeof4hAQ== X-Received: by 2002:a05:6870:f710:b0:10e:9958:fd55 with SMTP id ej16-20020a056870f71000b0010e9958fd55mr4387611oab.77.1659247384668; Sat, 30 Jul 2022 23:03:04 -0700 (PDT) Received: from barrymore.redhat.com (97-113-32-239.tukw.qwest.net. [97.113.32.239]) by smtp.gmail.com with ESMTPSA id q42-20020a05687082aa00b0010173875df2sm2308900oae.58.2022.07.30.23.03.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Jul 2022 23:03:03 -0700 (PDT) From: Jason Merrill To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: constexpr, empty base after non-empty [PR106369] Date: Sat, 30 Jul 2022 23:02:55 -0700 Message-Id: <20220731060255.1449004-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-13.9 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_LOW, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 31 Jul 2022 06:03:29 -0000 Here the CONSTRUCTOR we were providing for D{} had an entry for the B base subobject at offset 0 following the entry for the C base, causing output_constructor_regular_field to ICE due to going backwards. It might be nice for that function to be more tolerant of empty fields, but it also seems reasonable for the front end to prune the useless entry. Tested x86_64-pc-linux-gnu, applying to trunk. PR c++/106369 gcc/cp/ChangeLog: * constexpr.cc (reduced_constant_expression_p): Return false if a CONSTRUCTOR initializes an empty field. gcc/testsuite/ChangeLog: * g++.dg/cpp1z/constexpr-lambda27.C: New test. --- gcc/cp/constexpr.cc | 8 +++++- .../g++.dg/cpp1z/constexpr-lambda27.C | 26 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-lambda27.C diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 5f7fc6f8f0c..5e0d3399172 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -3081,7 +3081,13 @@ reduced_constant_expression_p (tree t) element. */ if (!reduced_constant_expression_p (e.value)) return false; - /* Empty class field may or may not have an initializer. */ + /* We want to remove initializers for empty fields in a struct to + avoid confusing output_constructor. */ + if (is_empty_field (e.index) + && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE) + return false; + /* Check for non-empty fields between initialized fields when + CONSTRUCTOR_NO_CLEARING. */ for (; field && e.index != field; field = next_subobject_field (DECL_CHAIN (field))) if (!is_really_empty_class (TREE_TYPE (field), diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-lambda27.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-lambda27.C new file mode 100644 index 00000000000..24e2e9b6c98 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-lambda27.C @@ -0,0 +1,26 @@ +// PR c++/106369 +// { dg-do compile { target c++17 } } + +struct A { + int a[256]; + constexpr int &operator[] (int n) noexcept { return a[n]; } + constexpr const int &operator[] (int n) const noexcept { return a[n]; } +}; +struct B {}; +template +struct C { + constexpr T &foo (const char x) noexcept { c = T::d[x]; return static_cast(*this); } + int c; +}; +struct D : public C, public B +{ + D () noexcept = default; + static constexpr char e[9] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I' }; + static constexpr A d = [] () constexpr { + A f {}; + for (int i = 0; i < 9; ++i) + f[e[i]] = 1; + return f; + } (); +}; +constexpr auto g = D{}.foo ('E'); base-commit: 9ef2c9aa5b351efa9b751de4f10180427cd0fe70 -- 2.31.1