From patchwork Tue Jun 6 18:29:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 70671 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 914DC385802F for ; Tue, 6 Jun 2023 18:30:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 914DC385802F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1686076258; bh=vDwM1Xw6drlyyv3v4tbS24YBLitW84AgMtZyZVlYunk=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=q3IkpWKCRmrJuX5k5WVo2eInHccXokyrZ2OY5w97YgitMJTQHw2WifGCSR0AdiMmd d3tLThtUwb8tIwz7jOvht3IpvCbh5eA+sUEVd6veiC8iyAfK/cnI+LbRU+sdCv1BcS f8Zqm/x2wSBdTXSguC71Tdxd9nGMQhoUAa6vRrv0= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 0840C3858D38 for ; Tue, 6 Jun 2023 18:30:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0840C3858D38 Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-519-tvlHT4_fNDaSsQW8QHG53Q-1; Tue, 06 Jun 2023 14:30:03 -0400 X-MC-Unique: tvlHT4_fNDaSsQW8QHG53Q-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-75d44483fdeso345179485a.0 for ; Tue, 06 Jun 2023 11:30:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686076198; x=1688668198; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=vDwM1Xw6drlyyv3v4tbS24YBLitW84AgMtZyZVlYunk=; b=GlkjLcOg5/1WbJUg50WL7hBOeKlIntLlSEsHnC7CVcphmU6xd2vLGPbxuym7eH+R6y +YXwn/uVx2NgN/7U8frzl0tDQk4N5agaZBQwf+8+x+FuOcKyk6ich3gIyvMDcTxXkqdC c//SK055oJJYw7UJuVmR6BP31XNwV5qJq7Tzf3iYAk1AoMDLvrv+dLAnkH5wvogpTsLU dpbWMvafCeyuhHAVYHK2UDLDC6Twel4dFKmh/B7+RTriCU6itBJfZzPkfMNrV3XxXWbi suMLt+g9LO3DRA+zOrrJ73JSdjH5e1XNF7qC5Z74hZVLwbWvxkN9SVq40eVf5c6g03QX 0v+g== X-Gm-Message-State: AC+VfDzOb8Eb/cJlWf6Edg8peDWrQMhkwYsN+JBfPrjBLJ/aUjSvvliW Cfx52I+ZD/OmlxocpewFWgtHQj18rllP6NyuiTz7C/KGCfKlb0k0WWiJD4/xj78j3BWYSBzCs0d osy0SXHtURu9/lB+ZKsXnAKg7IXEH3QfhqMjDU8PAwuHs11P5uhClnAeUYsamyvBIFJZlAC8zz0 c= X-Received: by 2002:a05:620a:8f0f:b0:75b:23a1:8e46 with SMTP id rh15-20020a05620a8f0f00b0075b23a18e46mr579958qkn.23.1686076198194; Tue, 06 Jun 2023 11:29:58 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5yDlSYq0kxGr5uzKFGI0Ny5xd6jvfOYmBXJTYhVD2+u0rrWQziX6cPgj89iMYh5OP6NAkSgw== X-Received: by 2002:a05:620a:8f0f:b0:75b:23a1:8e46 with SMTP id rh15-20020a05620a8f0f00b0075b23a18e46mr579944qkn.23.1686076197908; Tue, 06 Jun 2023 11:29:57 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id s9-20020a05620a16a900b0075914b01c10sm3448899qkj.85.2023.06.06.11.29.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 11:29:57 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: unsynthesized defaulted constexpr fn [PR110122] Date: Tue, 6 Jun 2023 14:29:53 -0400 Message-ID: <20230606182953.815966-1-ppalka@redhat.com> X-Mailer: git-send-email 2.41.0.rc1.10.g9e49351c30 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.7 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_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? -- >8 -- In the second testcase of PR110122, during regeneration of the generic lambda with V=Bar{}, substitution followed by coerce_template_parms for A's template argument naturally yields a copy of V in terms of Bar's (implicitly) defaulted copy constructor. This however happens inside a template context so although we introduced a use of the copy constructor, mark_used didn't actually synthesize it, which causes subsequent constant evaluation of the template argument to fail with: nontype-class58.C: In instantiation of ‘void f() [with Bar V = Bar{Foo()}]’: nontype-class58.C:22:11: required from here nontype-class58.C:18:18: error: ‘constexpr Bar::Bar(const Bar&)’ used before its definition Conveniently we already make sure to instantiate eligible constexpr functions before such (manifestly) constant evaluation, as per P0859R0. So this patch fixes this by making sure to synthesize eligible defaulted constexpr functions beforehand as well. PR c++/110122 gcc/cp/ChangeLog: * constexpr.cc (instantiate_cx_fn_r): Also synthesize defaulted functions. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/nontype-class58.C: New test. --- gcc/cp/constexpr.cc | 7 ++++-- gcc/testsuite/g++.dg/cpp2a/nontype-class58.C | 23 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/nontype-class58.C diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 8f7f0b7d325..a7efebcded1 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -8110,11 +8110,14 @@ instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/) && DECL_DECLARED_CONSTEXPR_P (*tp) && !DECL_INITIAL (*tp) && !trivial_fn_p (*tp) - && DECL_TEMPLOID_INSTANTIATION (*tp) + && (DECL_TEMPLOID_INSTANTIATION (*tp) || DECL_DEFAULTED_FN (*tp)) && !uid_sensitive_constexpr_evaluation_p ()) { ++function_depth; - instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false); + if (DECL_TEMPLOID_INSTANTIATION (*tp)) + instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false); + else + synthesize_method (*tp); --function_depth; } else if (TREE_CODE (*tp) == CALL_EXPR diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class58.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class58.C new file mode 100644 index 00000000000..6e40698da2f --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class58.C @@ -0,0 +1,23 @@ +// PR c++/110122 +// { dg-do compile { target c++20 } } + +struct Foo { + Foo() = default; + constexpr Foo(const Foo&) { } +}; + +struct Bar { + Foo _; +}; + +template +struct A { }; + +template +void f() { + [](auto){ A d; }(0); // { dg-bogus "used before its definition" } +}; + +int main() { + f(); +}