From patchwork Thu Mar 24 21:53:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1609249 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=yYPqgEbQ; dkim-atps=neutral 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=) Received: from sourceware.org (ip-8-43-85-97.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 bilbo.ozlabs.org (Postfix) with ESMTPS id 4KPfCD40xTz9s09 for ; Fri, 25 Mar 2022 08:54:47 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2EDA0385741A for ; Thu, 24 Mar 2022 21:54:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2EDA0385741A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1648158883; bh=vaU3CcrinKhzpXE4Gr3wLKux/RT5FpawJtGWu0dtwqY=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=yYPqgEbQHDSsADLL+nhlIihrHfjgJXkjHKHpLFtnDdyBLseVbaVlNnFlmNO1skbZ1 W5zcPDLQiv4FBSBtWPnDB2tlGdx9ROh4uVcrc55Wc64wPQvbGTC8za/57lvJGcqUHt th4MyhOKfS8J38P2pmW3cW7eyIK1C6Nm3FOu4JNc= 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 7584F3858C2C for ; Thu, 24 Mar 2022 21:53:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7584F3858C2C Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-107-bEf4o3L3OJuE0LV_g8wspQ-1; Thu, 24 Mar 2022 17:53:57 -0400 X-MC-Unique: bEf4o3L3OJuE0LV_g8wspQ-1 Received: by mail-qv1-f72.google.com with SMTP id h18-20020a05621402f200b00440cedaa9a2so4602892qvu.17 for ; Thu, 24 Mar 2022 14:53:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=vaU3CcrinKhzpXE4Gr3wLKux/RT5FpawJtGWu0dtwqY=; b=LFAGAh6xgvruPcl5zFh4yjGwgLKEcl41aC7AszqYbQcq8Pu4jAIciCLOroA4SYZ5Zm idY/p1/wX8Ty49Hv6kHNSfrK6ExncvlZja6F10ttGMVaJc8ZroxS69w7SRsGsAUcFv1M z1TiykLlXUpeN5FhJODTYmSLyA8GMvEtyz7cA/fWy03MbrOmLbXgVYEzX/7JNtSJTQtP t+kF8geqbBw0UXAMT/O392JzYUrc2iJFoxZj0RHPWJDczgdPzwHc1MSOIMsDur2k2rCk hy4+LeUC7zvH1yHEPkNiU0gJqoKpvMvL8sd0HELbPsIb6JQ4ILdh9QRa46L1xIH594ax l+Ew== X-Gm-Message-State: AOAM532kwppeQkoJEcReVhkdUijxEbLFj1qnZb43woEql+UN6c3roT7n bo1Xbv6bSxKrvRT0srhE3Ho58RYFc+YTf2Lmt2hwitR0pVcfjrqPGJtfjL7CAK2A44bOSnQ7KQj hpQoo8PavPP1XuaXCtA== X-Received: by 2002:a05:620a:1279:b0:67e:e60:1bc4 with SMTP id b25-20020a05620a127900b0067e0e601bc4mr4874720qkl.374.1648158836232; Thu, 24 Mar 2022 14:53:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxrUzjn5fxelso8QRShfPVNaDLUzsCdSNerLx5fE96v38WFEr5ezcUXAUpJw2CUgURDeNYoXA== X-Received: by 2002:a05:620a:1279:b0:67e:e60:1bc4 with SMTP id b25-20020a05620a127900b0067e0e601bc4mr4874706qkl.374.1648158835889; Thu, 24 Mar 2022 14:53:55 -0700 (PDT) Received: from redhat.com ([2601:184:4780:4310::5f2c]) by smtp.gmail.com with ESMTPSA id x6-20020ac86b46000000b002e02be9c0easm2854057qts.69.2022.03.24.14.53.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Mar 2022 14:53:55 -0700 (PDT) Date: Thu, 24 Mar 2022 17:53:53 -0400 To: Jason Merrill Subject: [PATCH v3] c++: ICE with template code in constexpr [PR104284] Message-ID: References: <20220310220459.91301-1-polacek@redhat.com> <4bc3f576-c238-2559-197e-3a51b3722505@redhat.com> MIME-Version: 1.0 In-Reply-To: <4bc3f576-c238-2559-197e-3a51b3722505@redhat.com> User-Agent: Mutt/2.1.5 (2021-12-30) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-13.6 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, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Cc: GCC Patches Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" On Thu, Mar 24, 2022 at 11:40:11AM -0400, Jason Merrill wrote: > On 3/18/22 17:55, Marek Polacek wrote: > > On Fri, Mar 11, 2022 at 06:46:42PM -0500, Jason Merrill wrote: > > > On 3/10/22 18:04, Marek Polacek wrote: > > > > Since r9-6073 cxx_eval_store_expression preevaluates the value to > > > > be stored, and that revealed a crash where a template code (here, > > > > code=IMPLICIT_CONV_EXPR) leaks into cxx_eval*. > > > > > > > > It happens because we're performing build_vec_init while processing > > > > a template > > > > > > Hmm, that seems like the bug. Where's that call coming from? > > > > From build_aggr_init. So we're handling e.g. > > > > template > > constexpr void g () { > > constexpr S s2[]{{'a'}}; > > } > > > > cp_finish_decl (decl=s2, init={{'a'}}) sees we're in processing_template_decl, > > but also that we have a constexpr var which is not dependent, nor is its > > initializer: > > > > else if (init > > && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl)) > > && !TYPE_REF_P (type) > > && decl_maybe_constant_var_p (decl) > > && !(dep_init = value_dependent_init_p (init))) > > { > > /* This variable seems to be a non-dependent constant, so process > > its initializer. If check_initializer returns non-null the > > initialization wasn't constant after all. */ > > tree init_code; > > cleanups = make_tree_vector (); > > init_code = check_initializer (decl, init, flags, &cleanups); > > > > so we call check_initializer, where we go down this path: > > > > init_code = build_aggr_init_full_exprs (decl, init, flags); > > > > build_aggr_init sees that the type of 's2' is ARRAY_TYPE, so it calls > > build_vec_init. > > > > I now recall that we've discussed build_vec_init in a template in the > > past, for example in the context of c++/93676. So I agree we ought to > > make an effort to avoid calling build_vec_init in a template. Perhaps > > like this: use an INIT_EXPR. With that, we should call build_vec_init > > if needed while instantiating. Does that make any sense? > > Hmm. If we do that, then we get back to > > > if (TREE_CODE (init_code) == INIT_EXPR) > > in check_initializer, and pull out the same init again, and set > LOOKUP_ALREADY_DIGESTED. But I think that's wrong, we haven't digested it > yet. Yeah, that's probably no good :( > Maybe we could avoid entering the below block of check_initializer at all in > this situation? > > > if (((type_build_ctor_call (type) || CLASS_TYPE_P (type)) > > && !(flags & LOOKUP_ALREADY_DIGESTED) > > && !(init && BRACE_ENCLOSED_INITIALIZER_P (init) > > && CP_AGGREGATE_TYPE_P (type) > > && (CLASS_TYPE_P (type) > > Maybe by adding || processing_template_decl here? That seems to work! Thanks. I've checked that we call build_vec_init when instantiating, so we shouldn't be losing any of its effects. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- Since r9-6073 cxx_eval_store_expression preevaluates the value to be stored, and that revealed a crash where a template code (here, code=IMPLICIT_CONV_EXPR) leaks into cxx_eval*. It happens because we're performing build_vec_init while processing a template, which calls get_temp_regvar which creates an INIT_EXPR. This INIT_EXPR's RHS contains an rvalue conversion so we create an IMPLICIT_CONV_EXPR. Its operand is not type-dependent and the whole INIT_EXPR is not type-dependent. So we call build_non_dependent_expr which, with -fchecking=2, calls fold_non_dependent_expr. At this point the expression still has an IMPLICIT_CONV_EXPR, which ought to be handled in instantiate_non_dependent_expr_internal. However, tsubst_copy_and_build doesn't handle INIT_EXPR; it will just call tsubst_copy which does nothing when args is null. So we fail to replace the IMPLICIT_CONV_EXPR and ICE. The problem is that we call build_vec_init in a template in the first place. We can avoid doing so by checking p_t_d before calling build_aggr_init in check_initializer. PR c++/104284 gcc/cp/ChangeLog: * decl.cc (check_initializer): Don't call build_aggr_init in a template. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/constexpr-104284-1.C: New test. * g++.dg/cpp1y/constexpr-104284-2.C: New test. * g++.dg/cpp1y/constexpr-104284-3.C: New test. * g++.dg/cpp1y/constexpr-104284-4.C: New test. --- gcc/cp/decl.cc | 4 +++ .../g++.dg/cpp1y/constexpr-104284-1.C | 34 ++++++++++++++++++ .../g++.dg/cpp1y/constexpr-104284-2.C | 33 +++++++++++++++++ .../g++.dg/cpp1y/constexpr-104284-3.C | 33 +++++++++++++++++ .../g++.dg/cpp1y/constexpr-104284-4.C | 35 +++++++++++++++++++ 5 files changed, 139 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-104284-1.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-104284-2.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-104284-3.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-104284-4.C base-commit: 346ab5a54a831ad9c78afcbd8dfe98e0e07e3070 diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 68741bbf5d2..69f60a6dc0f 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -7332,6 +7332,10 @@ check_initializer (tree decl, tree init, int flags, vec **cleanups) && !(init && BRACE_ENCLOSED_INITIALIZER_P (init) && CP_AGGREGATE_TYPE_P (type) && (CLASS_TYPE_P (type) + /* The call to build_aggr_init below could end up + calling build_vec_init, which may break when we + are processing a template. */ + || processing_template_decl || !TYPE_NEEDS_CONSTRUCTING (type) || type_has_extended_temps (type)))) || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE)) diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-104284-1.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-104284-1.C new file mode 100644 index 00000000000..809c26a6161 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-104284-1.C @@ -0,0 +1,34 @@ +// PR c++/104284 +// { dg-do compile { target c++14 } } +// { dg-additional-options "-fchecking=2" } + +struct S { + char c{}; +}; + +auto x1 = [](auto) { constexpr S s[]{{}}; }; +auto x2 = [](auto) { constexpr S s[]{{'a'}}; }; +#if __cpp_designated_initializers >= 201707L +auto x3 = [](auto) { constexpr S s[]{{.c = 'a'}}; }; +#endif +auto x4 = [](auto) { constexpr S s[]{'a'}; }; +auto x5 = [](auto) { constexpr S s[]{{{}}}; }; + +template +constexpr void g () +{ + constexpr S s1[]{{}}; + static_assert(s1[0].c == '\0', ""); + constexpr S s2[]{{'a'}}; + static_assert(s2[0].c == 'a', ""); +#if __cpp_designated_initializers >= 201707L + constexpr S s3[]{{.c = 'a'}}; + static_assert(s3[0].c == 'a', ""); +#endif + constexpr S s4[]{'a'}; + static_assert(s4[0].c == 'a', ""); + constexpr S s5[]{{{}}}; + static_assert(s5[0].c == '\0', ""); +} + +static_assert ((g(), true), ""); diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-104284-2.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-104284-2.C new file mode 100644 index 00000000000..704d37de129 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-104284-2.C @@ -0,0 +1,33 @@ +// PR c++/104284 +// { dg-do compile { target c++14 } } +// { dg-additional-options "-fchecking=2" } + +struct S { + char a; + constexpr S() : a{'a'} { } + constexpr S(char a_) : a{a_} { } +}; + +auto x1 = [](auto) { constexpr S s[]{{}}; }; +auto x2 = [](auto) { constexpr S s[]{{'a'}}; }; +auto x3 = [](auto) { constexpr S s[]{'a'}; }; +auto x4 = [](auto) { constexpr S s[]{{{}}}; }; + +template +constexpr void g() +{ + constexpr S s1[]{{}}; + static_assert(s1[0].a == 'a', ""); + constexpr S s2[]{{'a'}}; + static_assert(s2[0].a == 'a', ""); + constexpr S s3[]{'a'}; + static_assert(s3[0].a == 'a', ""); + constexpr S s4[]{{{}}}; + static_assert(s4[0].a == '\0', ""); +} + +void +f () +{ + g(); +} diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-104284-3.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-104284-3.C new file mode 100644 index 00000000000..6f23b255f9c --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-104284-3.C @@ -0,0 +1,33 @@ +// PR c++/104284 +// { dg-do compile { target c++14 } } +// { dg-additional-options "-fchecking=2" } +// Like constexpr-104284.C, but the function template is not +// constexpr. In that case, we were still calling build_vec_init +// in a template, just not crashing. + +struct S { + char c{}; +}; + +template +void g () +{ + constexpr S s1[]{{}}; + static_assert(s1[0].c == '\0', ""); + constexpr S s2[]{{'a'}}; + static_assert(s2[0].c == 'a', ""); +#if __cpp_designated_initializers >= 201707L + constexpr S s3[]{{.c = 'a'}}; + static_assert(s3[0].c == 'a', ""); +#endif + constexpr S s4[]{'a'}; + static_assert(s4[0].c == 'a', ""); + constexpr S s5[]{{{}}}; + static_assert(s5[0].c == '\0', ""); +} + +void +f () +{ + g(); +} diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-104284-4.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-104284-4.C new file mode 100644 index 00000000000..a99d3255a47 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-104284-4.C @@ -0,0 +1,35 @@ +// PR c++/104284 +// { dg-do run { target c++14 } } +// { dg-additional-options "-fchecking=2" } + +struct S { + char c{}; +}; + +template +constexpr void g () +{ + S s1[]{{}}; + if (s1[0].c != '\0') + __builtin_abort (); + S s2[]{{'a'}}; + if (s2[0].c != 'a') + __builtin_abort (); +#if __cpp_designated_initializers >= 201707L + S s3[]{{.c = 'a'}}; + if (s3[0].c != 'a') + __builtin_abort (); +#endif + S s4[]{'a'}; + if (s4[0].c != 'a') + __builtin_abort (); + S s5[]{{{}}}; + if (s5[0].c != '\0') + __builtin_abort (); +} + +int +main () +{ + g(); +}