From patchwork Sun Jan 23 03:18:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 50367 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 6DFCA3858C60 for ; Sun, 23 Jan 2022 03:19:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6DFCA3858C60 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1642907966; bh=qRYdMsUbfrB3iBwIcxS8q34yQisyjLRFAy7f/LWMSAE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=jaJqb+wjNri3WmOQwnHTs/d22IdKd170w/nYv1hBOAaqhTP19wIEojKRLixnGiDyJ nBVQC6/HAra3hV+1HFGSAhPiM7aHW419IljuaNtegRjmD4Tf+y9guar/ZiurYE3LNi LuW6AN1oPQiOK4tUlkpIsM6oVhGqgoYSNLUewHMI= 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 929203858C60 for ; Sun, 23 Jan 2022 03:18:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 929203858C60 Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-628-3pBhSLcoO5G1-X8j9745Bw-1; Sat, 22 Jan 2022 22:18:42 -0500 X-MC-Unique: 3pBhSLcoO5G1-X8j9745Bw-1 Received: by mail-qt1-f199.google.com with SMTP id e14-20020ac84b4e000000b002cfbbdf8206so192600qts.10 for ; Sat, 22 Jan 2022 19:18:41 -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=qRYdMsUbfrB3iBwIcxS8q34yQisyjLRFAy7f/LWMSAE=; b=J9u801P1+cy4RJQyd1yJtCROi8z+H3SrL6ZBm791vyiVlnEasmxOSddxK6FnYTVwjI HPv4rUNd8QT9JErUQqgNsGIgStke128ncyia3vjnDFNrx9ClgYHxR0E8v1lxyXStMOmR lfYIbzNd7Eg7ClSyNtRypgcEh8KfdGmNdfqpc6FHFLde1wst5Ku9wkSCJXZSy7PKrdM9 XBf0m6IyG2Xs6kw5UNZuvM5eH21PIb810r9obQ3tFLpG11yaSycTZV4IFT0uGfmeEZn7 qFMoSPaoJnuaNleTPgFlYmIQe2cc+oajcVzrcmlIk/OtFBXWbxa9JV9MAoDWgROSvwk3 TYXg== X-Gm-Message-State: AOAM533uc4dY63lTqMBCpMTqXLUuHVUiUFrxXphm74mEfmeDUHW3IRH5 bADX+dGLBaWyLGbViQe6WO4xqNK3wpxmRAELdK2XC/VbFCeIADRR+ZpRnMSHMrer7gEgivVINty WA72R2AvCEUOvvoRzLGDXLBsnNuCNkOl/uW6nVyq7fEk7jYacqo3JF3/AU4QZA1rQRg== X-Received: by 2002:a05:622a:1044:: with SMTP id f4mr3035874qte.635.1642907921048; Sat, 22 Jan 2022 19:18:41 -0800 (PST) X-Google-Smtp-Source: ABdhPJydWJLDRRXXqLyZ3ZgHxesu4cVzrAxCAZcLvwTXQ0+F0Ee9N0i/XkOHU/DULqEqOky8qDocbg== X-Received: by 2002:a05:622a:1044:: with SMTP id f4mr3035858qte.635.1642907920547; Sat, 22 Jan 2022 19:18:40 -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 br11sm5357894qkb.118.2022.01.22.19.18.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Jan 2022 19:18:39 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: constexpr and -fno-elide-constructors [PR101072] Date: Sat, 22 Jan 2022 22:18:37 -0500 Message-Id: <20220123031837.685619-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_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: 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" We've been trying for a while to avoid TARGET_EXPRs in template code, but there were still a few that snuck through, and the one in this case broke the code that tried to handle it. Fixed by using IMPLICIT_CONV_EXPR, as we have done elsewhere. I also noticed that finish_compound_literal was assuming that all T{init} were for aggregate T, and we got a few more TARGET_EXPRs from that. Fixed by only messing with TARGET_EXPR if we actually have an aggregate init. Tested x86_64-pc-linux-gnu, applying to trunk. PR c++/101072 gcc/cp/ChangeLog: * cp-tree.h (build_implicit_conv_flags): Declare. * call.cc (build_implicit_conv_flags): Split out from... (perform_implicit_conversion_flags): ...here. * decl.cc (check_initializer): Use it. * pt.cc (tsubst_copy_and_build): Remove TARGET_EXPR handling. * semantics.cc (finish_compound_literal): Don't treat scalar values like CONSTRUCTORs. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/constexpr-empty14a.C: New test. --- gcc/cp/cp-tree.h | 1 + gcc/cp/call.cc | 32 ++++++++++++------- gcc/cp/decl.cc | 7 +++- gcc/cp/pt.cc | 9 ------ gcc/cp/semantics.cc | 9 ++++-- .../g++.dg/cpp0x/constexpr-empty14a.C | 11 +++++++ 6 files changed, 45 insertions(+), 24 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-empty14a.C base-commit: d43be9dcc1bef95d70fb411d98b4de0d83c6b85d diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 186c54424a6..b9eb71fbc3a 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -6599,6 +6599,7 @@ extern tree strip_top_quals (tree); extern bool reference_related_p (tree, tree); extern bool reference_compatible_p (tree, tree); extern int remaining_arguments (tree); +extern tree build_implicit_conv_flags (tree, tree, int); extern tree perform_implicit_conversion (tree, tree, tsubst_flags_t); extern tree perform_implicit_conversion_flags (tree, tree, tsubst_flags_t, int); extern tree build_converted_constant_expr (tree, tree, tsubst_flags_t); diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index d4a07a7a9b3..f7f861cd16e 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -12638,6 +12638,25 @@ can_convert_arg_bad (tree to, tree from, tree arg, int flags, return t != NULL; } +/* Return an IMPLICIT_CONV_EXPR from EXPR to TYPE with bits set from overload + resolution FLAGS. */ + +tree +build_implicit_conv_flags (tree type, tree expr, int flags) +{ + /* In a template, we are only concerned about determining the + type of non-dependent expressions, so we do not have to + perform the actual conversion. But for initializers, we + need to be able to perform it at instantiation + (or instantiate_non_dependent_expr) time. */ + expr = build1 (IMPLICIT_CONV_EXPR, type, expr); + if (!(flags & LOOKUP_ONLYCONVERTING)) + IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true; + if (flags & LOOKUP_NO_NARROWING) + IMPLICIT_CONV_EXPR_BRACED_INIT (expr) = true; + return expr; +} + /* Convert EXPR to TYPE. Return the converted expression. Note that we allow bad conversions here because by the time we get to @@ -12674,18 +12693,7 @@ perform_implicit_conversion_flags (tree type, tree expr, expr = error_mark_node; } else if (processing_template_decl && conv->kind != ck_identity) - { - /* In a template, we are only concerned about determining the - type of non-dependent expressions, so we do not have to - perform the actual conversion. But for initializers, we - need to be able to perform it at instantiation - (or instantiate_non_dependent_expr) time. */ - expr = build1 (IMPLICIT_CONV_EXPR, type, expr); - if (!(flags & LOOKUP_ONLYCONVERTING)) - IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true; - if (flags & LOOKUP_NO_NARROWING) - IMPLICIT_CONV_EXPR_BRACED_INIT (expr) = true; - } + expr = build_implicit_conv_flags (type, expr, flags); else { /* Give a conversion call the same location as expr. */ diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 8e5421848d1..c5d4f2f5369 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -7235,7 +7235,12 @@ check_initializer (tree decl, tree init, int flags, vec **cleanups) /* In C++20, the call to build_aggr_init could have created an INIT_EXPR with a CONSTRUCTOR as the RHS to handle A(1, 2). */ - init = TREE_OPERAND (init_code, 1); + tree rhs = TREE_OPERAND (init_code, 1); + if (processing_template_decl && TREE_CODE (rhs) == TARGET_EXPR) + /* Avoid leaking TARGET_EXPR into template trees. */ + rhs = build_implicit_conv_flags (type, init, flags); + init = rhs; + init_code = NULL_TREE; /* Don't call digest_init; it's unnecessary and will complain about aggregate initialization of non-aggregate classes. */ diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 5afcb41eccd..bba62a5800a 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -21151,15 +21151,6 @@ tsubst_copy_and_build (tree t, RETURN (build_lambda_object (r)); } - case TARGET_EXPR: - /* We can get here for a constant initializer of non-dependent type. - FIXME stop folding in cp_parser_initializer_clause. */ - { - tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)), - complain); - RETURN (r); - } - case TRANSACTION_EXPR: RETURN (tsubst_expr(t, args, complain, in_decl, integral_constant_expression_p)); diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 640b2d75471..07c2b3393be 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -3272,12 +3272,17 @@ finish_compound_literal (tree type, tree compound_literal, /* Represent other compound literals with TARGET_EXPR so we produce a prvalue, and can elide copies. */ - if (!VECTOR_TYPE_P (type)) + if (TREE_CODE (compound_literal) == CONSTRUCTOR + || TREE_CODE (compound_literal) == VEC_INIT_EXPR) { /* The CONSTRUCTOR is now an initializer, not a compound literal. */ - TREE_HAS_CONSTRUCTOR (compound_literal) = false; + if (TREE_CODE (compound_literal) == CONSTRUCTOR) + TREE_HAS_CONSTRUCTOR (compound_literal) = false; compound_literal = get_target_expr_sfinae (compound_literal, complain); } + else + /* For e.g. int{42} just make sure it's a prvalue. */ + compound_literal = rvalue (compound_literal); return compound_literal; } diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-empty14a.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-empty14a.C new file mode 100644 index 00000000000..df34cfcb8e7 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-empty14a.C @@ -0,0 +1,11 @@ +// PR c++/101072 +// { dg-do compile { target c++11 } } +// { dg-additional-options -fno-elide-constructors } + +struct S {}; + +template void +foo (S s) +{ + constexpr S x = s; +}