From patchwork Tue Aug 3 20:04:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1513083 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=UBUDjiUn; dkim-atps=neutral Received: from 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GfQnl4hwqz9sPf for ; Wed, 4 Aug 2021 06:04:41 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EB7933851C0A for ; Tue, 3 Aug 2021 20:04:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EB7933851C0A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1628021079; bh=dQgEAHyrEcfY0YDQ5IpOvlpXI1G62HAGg0w1Ry/dt8Q=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=UBUDjiUnszp6Uj7ibbeEcSWivKFn0SN7zpq2q/JpoNvjrKCedr2V5KLQHxYHO/m0Z Y97sj03pdL8bhPnueJdE528Rc0NA+2DSasEtAs6GA+lwU/V7ossbLEsarQo24ttKMD HQtSzhb2ygwHo8E5mK32Xc/VHShpJzJulPK/c7yM= 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 4B5A0385843A for ; Tue, 3 Aug 2021 20:04:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4B5A0385843A 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-45-WbThiEoEMSWgpYipnHkqPg-1; Tue, 03 Aug 2021 16:04:10 -0400 X-MC-Unique: WbThiEoEMSWgpYipnHkqPg-1 Received: by mail-qt1-f198.google.com with SMTP id o7-20020ac87c470000b029025f8084df09so14064546qtv.6 for ; Tue, 03 Aug 2021 13:04:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=dQgEAHyrEcfY0YDQ5IpOvlpXI1G62HAGg0w1Ry/dt8Q=; b=FVJA34wpjC/35mggDP09KTbxFg8aRtAumjI7y5J7CaSkwD9RqCc8vhPEOT2tMxL/l2 +aYhPXG0tLDqrqPt3QDrRrE45CPNobGR4uplXJ/He3b1qsl+VKxKd6GkbX1jBxfngIYq 1Iu5BpuL4mvh6IB2d5Mmy0tNofdHvP2N1hvA8rFLLyBho4oLGcvrAxs4UsfUu+Nv1PL7 m2uR/XPIUUE1m8vjhdLtVAGfwPW0+wNrxefAgjFNk4KuBh4DzIfY2kzl7Ged9i9QaD9t sodjRI9Oh/OaxfKcAFozvPlLfqqT4Do48FTSjyXdk4bfoAADAkJzoVGA2v9oyd5VO9vo 1O5w== X-Gm-Message-State: AOAM533ND8iw/awi5DNjHaZJ7ePkxikjQCwkrLgUagL4rK6Sdk6xa7Ul O4eqq9YJiFCOe0nqVhmd43HnPa4NZAYzy2tBShIL7hy05aswoqZM+rVZKYg4d094bmnM9i+bxPz Oris9Ed6NUSx0Z0ui9L+4Uean3d6ZM0mcOqOzpq9Aw6/8i1y2M8JZ6MFKh9nqj8HvQ5w= X-Received: by 2002:ac8:430a:: with SMTP id z10mr4708970qtm.303.1628021049215; Tue, 03 Aug 2021 13:04:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyPsa6UnU9ogxM2wSmZALTXKzuGmVaOgJvNYuthQcuOCgOSgIjvyzaovXJ5rPh/vG6AhDntJA== X-Received: by 2002:ac8:430a:: with SMTP id z10mr4708951qtm.303.1628021048889; Tue, 03 Aug 2021 13:04:08 -0700 (PDT) Received: from localhost.localdomain (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id c1sm6872913qtj.36.2021.08.03.13.04.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Aug 2021 13:04:08 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: constexpr std::construct_at on empty field [PR101663] Date: Tue, 3 Aug 2021 16:04:01 -0400 Message-Id: <20210803200401.1170877-1-ppalka@redhat.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-16.1 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, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Here during constexpr evaluation of std::construct_at(&a._M_value) we find ourselves in cxx_eval_store_expression where the target object is 'a._M_value' and the initializer is {}. Since _M_value is an empty [[no_unique_address]] member we don't create a sub-CONSTRUCTOR for it, so we end up in the early exit code path for empty stores with mismatched types and we trip over the assert therein gcc_assert (is_empty_class (TREE_TYPE (init)) && !lval); because lval is true. The reason it's true is because the INIT_EXPR in question is the LHS of a COMPOUND_EXPR, and evaluation of the LHS is always performed with lval=true for some reason. This is the case ever since r5-5900, before which we used to do the evaluation with lval=false. I'm not sure why we evaluate the LHS of a COMPOUND_EXPR with lval=true (changing it to false survives bootstrap+regtest and is sufficient to fix the PR), but regardless it's also straightforward enough to make the relevant code path in cxx_eval_store_expression handle lval=true, which is the approach this patch takes. This patch also consolidates the duplicate implementations of std::construct_at/destroy_at from some of the C++20 constexpr tests into a common header file. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk/11? PR c++/101663 gcc/cp/ChangeLog: * constexpr.c (cxx_eval_store_expression): In the early exit code path for mismatched types, Pass false instead of true for lval when evaluating the LHS. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/construct_at.h: New convenience header that defines minimal implementations of std::construct_at/destroy_at, split out from ... * g++.dg/cpp2a/constexpr-new5.C: ... here. * g++.dg/cpp2a/constexpr-new6.C: Use the header. * g++.dg/cpp2a/constexpr-new14.C: Likewise. * g++.dg/cpp2a/constexpr-new20.C: New test. --- gcc/cp/constexpr.c | 4 +- gcc/testsuite/g++.dg/cpp2a/constexpr-new14.C | 60 +----------------- gcc/testsuite/g++.dg/cpp2a/constexpr-new20.C | 18 ++++++ gcc/testsuite/g++.dg/cpp2a/constexpr-new5.C | 60 +----------------- gcc/testsuite/g++.dg/cpp2a/constexpr-new6.C | 64 +------------------- gcc/testsuite/g++.dg/cpp2a/construct_at.h | 62 +++++++++++++++++++ 6 files changed, 85 insertions(+), 183 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/constexpr-new20.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/construct_at.h diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 1af365d47b9..25d84a377d8 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -5588,8 +5588,8 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, argument, which has the derived type rather than the base type. In this situation, just evaluate the initializer and return, since there's no actual data to store. */ - gcc_assert (is_empty_class (TREE_TYPE (init)) && !lval); - return init; + gcc_assert (is_empty_class (TREE_TYPE (init))); + return lval ? target : init; } CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init); TREE_CONSTANT (*valp) = TREE_CONSTANT (init); diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-new14.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-new14.C index fd6f6075ef0..26037397b1d 100644 --- a/gcc/testsuite/g++.dg/cpp2a/constexpr-new14.C +++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-new14.C @@ -1,65 +1,7 @@ // PR c++/97195 // { dg-do compile { target c++20 } } -namespace std -{ - typedef __SIZE_TYPE__ size_t; - - template - struct allocator - { - constexpr allocator () noexcept {} - - constexpr T *allocate (size_t n) - { return static_cast (::operator new (n * sizeof(T))); } - - constexpr void - deallocate (T *p, size_t n) - { ::operator delete (p); } - }; - - template - U __declval (int); - template - T __declval (long); - template - auto declval () noexcept -> decltype (__declval (0)); - - template - struct remove_reference - { typedef T type; }; - template - struct remove_reference - { typedef T type; }; - template - struct remove_reference - { typedef T type; }; - - template - constexpr T && - forward (typename std::remove_reference::type &t) noexcept - { return static_cast (t); } - - template - constexpr T && - forward (typename std::remove_reference::type &&t) noexcept - { return static_cast (t); } - - template - constexpr auto - construct_at (T *l, A &&... a) - noexcept (noexcept (::new ((void *) 0) T (std::declval ()...))) - -> decltype (::new ((void *) 0) T (std::declval ()...)) - { return ::new ((void *) l) T (std::forward (a)...); } - - template - constexpr inline void - destroy_at (T *l) - { l->~T (); } -} - -inline void *operator new (std::size_t, void *p) noexcept -{ return p; } +#include "construct_at.h" constexpr bool foo () diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-new20.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-new20.C new file mode 100644 index 00000000000..88bc4429a8a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-new20.C @@ -0,0 +1,18 @@ +// PR c++/101663 +// { dg-do compile { target c++20 } } + +#include "construct_at.h" + +template struct __box { + [[no_unique_address]] _Tp _M_value; +}; + +struct Empty {}; + +constexpr bool test() { + __box a; + std::construct_at(&a._M_value); + return true; +} + +static_assert(test()); diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-new5.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-new5.C index 2bb407a4b51..eeaee969266 100644 --- a/gcc/testsuite/g++.dg/cpp2a/constexpr-new5.C +++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-new5.C @@ -1,65 +1,7 @@ // P0784R7 // { dg-do compile { target c++20 } } -namespace std -{ - typedef __SIZE_TYPE__ size_t; - - template - struct allocator - { - constexpr allocator () noexcept {} - - constexpr T *allocate (size_t n) - { return static_cast (::operator new (n * sizeof(T))); } - - constexpr void - deallocate (T *p, size_t n) - { ::operator delete (p); } - }; - - template - U __declval (int); - template - T __declval (long); - template - auto declval () noexcept -> decltype (__declval (0)); - - template - struct remove_reference - { typedef T type; }; - template - struct remove_reference - { typedef T type; }; - template - struct remove_reference - { typedef T type; }; - - template - constexpr T && - forward (typename std::remove_reference::type &t) noexcept - { return static_cast (t); } - - template - constexpr T && - forward (typename std::remove_reference::type &&t) noexcept - { return static_cast (t); } - - template - constexpr auto - construct_at (T *l, A &&... a) - noexcept (noexcept (::new ((void *) 0) T (std::declval ()...))) - -> decltype (::new ((void *) 0) T (std::declval ()...)) - { return ::new ((void *) l) T (std::forward (a)...); } - - template - constexpr inline void - destroy_at (T *l) - { l->~T (); } -} - -inline void *operator new (std::size_t, void *p) noexcept -{ return p; } +#include "construct_at.h" constexpr bool foo () diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-new6.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-new6.C index d51bdbb8269..eeaee969266 100644 --- a/gcc/testsuite/g++.dg/cpp2a/constexpr-new6.C +++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-new6.C @@ -1,69 +1,7 @@ // P0784R7 // { dg-do compile { target c++20 } } -namespace std -{ - inline namespace _8 { } - namespace _8 { - - typedef __SIZE_TYPE__ size_t; - - template - struct allocator - { - constexpr allocator () noexcept {} - - constexpr T *allocate (size_t n) - { return static_cast (::operator new (n * sizeof(T))); } - - constexpr void - deallocate (T *p, size_t n) - { ::operator delete (p); } - }; - - template - U __declval (int); - template - T __declval (long); - template - auto declval () noexcept -> decltype (__declval (0)); - - template - struct remove_reference - { typedef T type; }; - template - struct remove_reference - { typedef T type; }; - template - struct remove_reference - { typedef T type; }; - - template - constexpr T && - forward (typename std::remove_reference::type &t) noexcept - { return static_cast (t); } - - template - constexpr T && - forward (typename std::remove_reference::type &&t) noexcept - { return static_cast (t); } - - template - constexpr auto - construct_at (T *l, A &&... a) - noexcept (noexcept (::new ((void *) 0) T (std::declval ()...))) - -> decltype (::new ((void *) 0) T (std::declval ()...)) - { return ::new ((void *) l) T (std::forward (a)...); } - - template - constexpr inline void - destroy_at (T *l) - { l->~T (); } - } -} - -inline void *operator new (std::size_t, void *p) noexcept -{ return p; } +#include "construct_at.h" constexpr bool foo () diff --git a/gcc/testsuite/g++.dg/cpp2a/construct_at.h b/gcc/testsuite/g++.dg/cpp2a/construct_at.h new file mode 100644 index 00000000000..dcc4f5b2532 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/construct_at.h @@ -0,0 +1,62 @@ +// A minimal conforming implementation of std::construct_at/destroy_at. +// some C++20 constexpr tests to avoid including all of . + +namespace std +{ + typedef __SIZE_TYPE__ size_t; + + template + struct allocator + { + constexpr allocator () noexcept {} + + constexpr T *allocate (size_t n) + { return static_cast (::operator new (n * sizeof(T))); } + + constexpr void + deallocate (T *p, size_t n) + { ::operator delete (p); } + }; + + template + U __declval (int); + template + T __declval (long); + template + auto declval () noexcept -> decltype (__declval (0)); + + template + struct remove_reference + { typedef T type; }; + template + struct remove_reference + { typedef T type; }; + template + struct remove_reference + { typedef T type; }; + + template + constexpr T && + forward (typename std::remove_reference::type &t) noexcept + { return static_cast (t); } + + template + constexpr T && + forward (typename std::remove_reference::type &&t) noexcept + { return static_cast (t); } + + template + constexpr auto + construct_at (T *l, A &&... a) + noexcept (noexcept (::new ((void *) 0) T (std::declval ()...))) + -> decltype (::new ((void *) 0) T (std::declval ()...)) + { return ::new ((void *) l) T (std::forward (a)...); } + + template + constexpr inline void + destroy_at (T *l) + { l->~T (); } +} + +inline void *operator new (std::size_t, void *p) noexcept +{ return p; }