From patchwork Mon Nov 15 07:51:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 47651 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 3C6A33858403 for ; Mon, 15 Nov 2021 07:53:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3C6A33858403 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1636962791; bh=106kxypvSE5xHzKCAYZvV2r+J79My3GeKkvT8A/To0E=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=fKk0xYn1ZQJZueWnPd9gVrY3MJbzc86NZAZtS/SehIY1QcLERouRO9YcXk0YQlv8c QyCc1bTCCd+8BuAiuLkdOnrglZJWjfRtriogIFJmejxiowMjlXsB+JEWGBUJy/22nZ HU0EhBmzMg1lEPt6JHKMR9183nuO9S/vWU/4P+jM= 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 ESMTPS id 2939E3857C62 for ; Mon, 15 Nov 2021 07:51:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2939E3857C62 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-262-TTvAMunBP-KnbUsb41iG-A-1; Mon, 15 Nov 2021 02:51:28 -0500 X-MC-Unique: TTvAMunBP-KnbUsb41iG-A-1 Received: by mail-qv1-f69.google.com with SMTP id ke1-20020a056214300100b003b5a227e98dso15240210qvb.14 for ; Sun, 14 Nov 2021 23:51:28 -0800 (PST) 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=106kxypvSE5xHzKCAYZvV2r+J79My3GeKkvT8A/To0E=; b=FWySI8kCtCaMk2hs8lz1cb6XRPr4z4OGLTBufbpBJ14dd2NCwMJDrIR1T43pf+p1TC K+a88/tS6Y6zCHsr046smPr9qE5AT0RL1cxUSSwYeaCURzI+qt+0NMK8u5yB8xpQNSnc Vfw8gHK3hdmRGaEZaiuonj+S6H2ujxMWEMUEKVF9mFS1siDIARmHwf2AuRs9K7c/cZI3 Kq5yoT33LLj6Y/xk5J1ZEUmERiPnanLp8pOwOWhjCxneiaCT2PZH8KkhHPq2q42+mImv n9mKUYs/A2pjEjv4iBW7P3Js1fC8ufIKP7VuYEMXxD7lymzH7m8Ao5FYexVGfI3lFc8P gtLg== X-Gm-Message-State: AOAM530WNs15stxz839IkdUbS1sW9YOk/vTKrEfKC3duxWnyOygbr2rq 2H7MW6NyW3uqo0SRzp2GtqzVvlNUzU3mFnxGg9P5AFB6gPSKKXaMmBG48LSgPo3miJlrWsm2bae usr3aVL/aXNE8YWEkCi9+qxWn9EXt2dQGhQ6g1hEhE+P4maR9zxLCYwTt1xIvIPFqjA== X-Received: by 2002:ac8:5a01:: with SMTP id n1mr4172766qta.13.1636962687932; Sun, 14 Nov 2021 23:51:27 -0800 (PST) X-Google-Smtp-Source: ABdhPJxeVrY9L2Hkz4wcmXxRIou9Jjq+n7IQ5W7YeFgqHbz1yEhjghBVhURgXO6O/T5b3GggIawFLQ== X-Received: by 2002:ac8:5a01:: with SMTP id n1mr4172750qta.13.1636962687603; Sun, 14 Nov 2021 23:51:27 -0800 (PST) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id h16sm7738660qtx.20.2021.11.14.23.51.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Nov 2021 23:51:27 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: check constexpr constructor body Date: Mon, 15 Nov 2021 02:51:24 -0500 Message-Id: <20211115075124.3355269-1-jason@redhat.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.4 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_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: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" The implicit constexpr patch revealed that our checks for constexpr constructors that could possibly produce a constant value (which otherwise are IFNDR) was failing to look at most of the function body. Fixing that required some library tweaks. Tested x86_64-pc-linux-gnu, applying to trunk. gcc/cp/ChangeLog: * constexpr.c (maybe_save_constexpr_fundef): Also check whether the body of a constructor is potentially constant. libstdc++-v3/ChangeLog: * src/c++17/memory_resource.cc: Add missing constexpr. * include/experimental/internet: Only mark copy constructor as constexpr with __cpp_constexpr_dynamic_alloc. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/constexpr-89285-2.C: Expect error. * g++.dg/cpp1y/constexpr-89285.C: Adjust error. --- gcc/cp/constexpr.c | 27 ++++++++++++++----- .../g++.dg/cpp1y/constexpr-89285-2.C | 2 +- gcc/testsuite/g++.dg/cpp1y/constexpr-89285.C | 2 +- libstdc++-v3/src/c++17/memory_resource.cc | 2 +- libstdc++-v3/include/experimental/internet | 2 ++ 5 files changed, 25 insertions(+), 10 deletions(-) base-commit: bd95d75f3412e1a7debab7c6c602ba409f274eb5 prerequisite-patch-id: 27a5ed4ace5d1a1b46b77a7be24baec6a2d6bdf3 diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 82a597d7bad..c92db5d413c 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -870,7 +870,9 @@ maybe_save_constexpr_fundef (tree fun) || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))) return; - if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun))) + bool complain = !DECL_GENERATED_P (fun); + + if (!is_valid_constexpr_fn (fun, complain)) return; tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun)); @@ -883,15 +885,26 @@ maybe_save_constexpr_fundef (tree fun) } bool potential = potential_rvalue_constant_expression (massaged); - if (!potential && !DECL_GENERATED_P (fun)) + if (!potential && complain) require_potential_rvalue_constant_expression (massaged); - if (DECL_CONSTRUCTOR_P (fun) - && cx_check_missing_mem_inits (DECL_CONTEXT (fun), - massaged, !DECL_GENERATED_P (fun))) - potential = false; + if (DECL_CONSTRUCTOR_P (fun) && potential) + { + if (cx_check_missing_mem_inits (DECL_CONTEXT (fun), + massaged, complain)) + potential = false; + else if (cxx_dialect > cxx11) + { + /* What we got from massage_constexpr_body is pretty much just the + ctor-initializer, also check the body. */ + massaged = DECL_SAVED_TREE (fun); + potential = potential_rvalue_constant_expression (massaged); + if (!potential && complain) + require_potential_rvalue_constant_expression (massaged); + } + } - if (!potential && !DECL_GENERATED_P (fun)) + if (!potential && complain) return; constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE}; diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-89285-2.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-89285-2.C index 656bc9cb7f1..ea44daa849e 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-89285-2.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-89285-2.C @@ -10,7 +10,7 @@ struct B { int *c = &x->a; while (*c) c = reinterpret_cast((reinterpret_cast(c) + *c)); - *c = reinterpret_cast(this) - reinterpret_cast(c); + *c = reinterpret_cast(this) - reinterpret_cast(c); // { dg-error "reinterpret_cast" } } }; struct C : A { diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-89285.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-89285.C index 3809e1f7a9f..26aab9b6a50 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-89285.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-89285.C @@ -17,4 +17,4 @@ struct C : A { B bar {this}; }; -constexpr C foo {}; // { dg-message "expansion of" } +constexpr C foo {}; // { dg-message "" } diff --git a/libstdc++-v3/src/c++17/memory_resource.cc b/libstdc++-v3/src/c++17/memory_resource.cc index 1ba79903f87..9fc3bb754c1 100644 --- a/libstdc++-v3/src/c++17/memory_resource.cc +++ b/libstdc++-v3/src/c++17/memory_resource.cc @@ -603,7 +603,7 @@ namespace pmr void* pointer = nullptr; aligned_size _M_size; - size_t size() const noexcept + constexpr size_t size() const noexcept { if (_M_size.value == size_t(-1)) [[unlikely]] return size_t(-1); diff --git a/libstdc++-v3/include/experimental/internet b/libstdc++-v3/include/experimental/internet index 65c97de07d9..95b8cdc9963 100644 --- a/libstdc++-v3/include/experimental/internet +++ b/libstdc++-v3/include/experimental/internet @@ -460,7 +460,9 @@ namespace ip // constructors: constexpr address() noexcept : _M_v4(), _M_is_v4(true) { } +#if __cpp_constexpr_dynamic_alloc constexpr +#endif address(const address& __a) noexcept : _M_uninit(), _M_is_v4(__a._M_is_v4) { if (_M_is_v4)