From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 D72FD3858D1E for ; Thu, 22 Dec 2022 16:31:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D72FD3858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671726673; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=qsk06jUF4rYyBjSbq1tWlALImcqkl3EmiZieAJ4D2Oo=; b=WJPjdBdGZW6PaRQsUvIbIh4xBilQBrMvRT9jExfu7NaEivhAdf6EJfkfZjyUaxgkZPyE9u V0k/4xAN7t6Lzhke5ilDptr17302ZGl+gAC44bfXekgQ4iVPUg+g+JVQKgN+Q9W425d7ho 8IlyrHfGABc23iZAjpP8QSZJySEe1pQ= Received: from mail-vk1-f200.google.com (mail-vk1-f200.google.com [209.85.221.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-201-1QSZQAiWPWCzHzUL52TNEg-1; Thu, 22 Dec 2022 11:31:12 -0500 X-MC-Unique: 1QSZQAiWPWCzHzUL52TNEg-1 Received: by mail-vk1-f200.google.com with SMTP id t125-20020a1faa83000000b003d0e23c1210so766020vke.10 for ; Thu, 22 Dec 2022 08:31:12 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:references:message-id:in-reply-to:subject:cc:to:date :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=qsk06jUF4rYyBjSbq1tWlALImcqkl3EmiZieAJ4D2Oo=; b=Gas7pBV8e12pLw2vSqR8RuWdSEVd9J89Tm5niJztxbQjOfG8AqdSGtJ5DNVJ0BqeGW 7bA8f8W/7SVAi9b+m+bHade949CwdjPKkE9Hb1XBBbCxo+retDvhx6Tet6A26SoO/uyf z8R4x6s9LX0FtS7cIrrSncRHBjmAfYrMUL/op5ttNcwaxz2QJF6UoJCASIq+WeI4dcRa mx8xd2WSI4QyOen6EE3ElUNRlD19TtFv6JWDPX0/NydGo7vmZvyvroXCMfEdDyeCL1/C sxOL+uQUVuitAY9/Owu0+dcWGzCafyupsPPE89uDwF5LjuiQh9GvvCNF4eyKh8Hm7jpG +w1g== X-Gm-Message-State: AFqh2kq61PVJDuSc4HFVzlpwg8dTgs4lnbSjOU6rBftuvlNp9a4yTFZS F+lTao5cv73mncBL3b3/1g5VJisiN3hRJfMX0oGMdRFJon2DB6qxavasD/PDtwwnU9dt+JzH/Ms 1QWIHRN75NsIhfeR2nA== X-Received: by 2002:a67:ec15:0:b0:3b3:7657:43a3 with SMTP id d21-20020a67ec15000000b003b3765743a3mr2514017vso.8.1671726671276; Thu, 22 Dec 2022 08:31:11 -0800 (PST) X-Google-Smtp-Source: AMrXdXutTEXYj9UjrkFBKBo0wpOjgOqQvARD8T2ymfO4WAcpAA+FAmNUD2R1/w6NZJfv1Ja2kBoD6A== X-Received: by 2002:a67:ec15:0:b0:3b3:7657:43a3 with SMTP id d21-20020a67ec15000000b003b3765743a3mr2513991vso.8.1671726670931; Thu, 22 Dec 2022 08:31:10 -0800 (PST) Received: from [192.168.1.130] (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id m10-20020a05620a220a00b006eeca296c00sm511238qkh.104.2022.12.22.08.31.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Dec 2022 08:31:10 -0800 (PST) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Thu, 22 Dec 2022 11:31:09 -0500 (EST) To: Jason Merrill cc: Patrick Palka , gcc-patches@gcc.gnu.org Subject: Re: [PATCH] c++: get_nsdmi in template context [PR108116] In-Reply-To: Message-ID: <9f0509b3-abd3-2d26-2264-183cc1cf4b11@idea> References: <20221221145254.389983-1-ppalka@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-13.9 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_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Wed, 21 Dec 2022, Jason Merrill wrote: > On 12/21/22 09:52, Patrick Palka wrote: > > Here during ahead of time checking of C{}, we indirectly call get_nsdmi > > for C::m from finish_compound_literal, which in turn calls > > break_out_target_exprs for C::m's (non-templated) initializer, during > > which we end up building a call to A::~A and checking expr_noexcept_p > > for it (from build_vec_delete_1). But this is all done with > > processing_template_decl set, so the built A::~A call is templated > > (whose form r12-6897-gdec8d0e5fa00ceb2 recently changed) which > > expr_noexcept_p doesn't expect and we crash. > > > > In r10-6183-g20afdcd3698275 we fixed a similar issue by guarding a > > expr_noexcept_p call with !processing_template_decl, which works here > > too. But it seems to me since the initializer we obtain in get_nsdmi is > > always non-templated, it should be calling break_out_target_exprs with > > processing_template_decl cleared since otherwise the function might end > > up mixing templated and non-templated trees. > > > > I'm not sure about this though, perhaps this is not the best fix here. > > Alternatively, when processing_template_decl we could make get_nsdmi > > avoid calling break_out_target_exprs at all or something. Additionally, > > perhaps break_out_target_exprs should be a no-op more generally when > > processing_template_decl since we shouldn't see any TARGET_EXPRs inside > > a template? > > Hmm. > > Any time we would call break_out_target_exprs we're dealing with non-dependent > expressions; if we're in a template, we're building up an initializer or a > call that we'll soon throw away, just for the purpose of checking or type > computation. > > Furthermore, as you say, the argument is always a non-template tree, whether > in get_nsdmi or convert_default_arg. So having processing_template_decl > cleared would be correct. > > I don't think we can get away with not calling break_out_target_exprs at all > in a template; if nothing else, we would lose immediate invocation expansion. > However, we could probably skip the bot_manip tree walk, which should avoid > the problem. > > Either way we end up returning non-template trees, as we do now, and callers > have to deal with transient CONSTRUCTORs containing such (as we do in > massage_init_elt). Ah I see, makes sense. > > Does convert_default_arg not run into the same problem, e.g. when calling > > void g(B = {0}); In practice it seems not, because we don't call convert_default_arg when processing_template_decl is set (verified with an assert to that effect). In build_over_call for example we exit early when processing_template_decl is set, and return a templated CALL_EXPR that doesn't include default arguments at all. A consequence of this is that we don't reject ahead of time a call that would use an ill-formed dependent default argument, e.g. template void g(B = T{0}); template void f() { g(); } since the default argument instantiation would be the responsibility of convert_default_arg. Thinking hypothetically here, if we do in the future want to include default arguments in the templated form of a CALL_EXPR, we'd probably have to instantiate them with processing_template_decl set so that the result is templated. And we'd subsequently want to call break_out_target_exprs on the result also with processing_template_decl set IIUC, to perform immediate invocation expansion. This seems to be a potential use case for being able to call break_out_target_exprs on templated trees, and so unconditionally clearing p_t_d from break_out_target_exprs might not be future proof. In light of this, shall we go with the original approach to clear processing_template_decl directly from get_nsdmi? > > ? > > > Bootstrapped and regtested on x86_64-pc-linux-gnu. > > > > PR c++/108116 > > > > gcc/cp/ChangeLog: > > > > * init.cc (get_nsdmi): Clear processing_template_decl before > > processing the non-templated initializer. > > > > gcc/testsuite/ChangeLog: > > > > * g++.dg/cpp0x/nsdmi-template24.C: New test. > > --- > > gcc/cp/init.cc | 8 ++++++- > > gcc/testsuite/g++.dg/cpp0x/nsdmi-template24.C | 22 +++++++++++++++++++ > > 2 files changed, 29 insertions(+), 1 deletion(-) > > create mode 100644 gcc/testsuite/g++.dg/cpp0x/nsdmi-template24.C > > > > diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc > > index 73e6547c076..c4345ebdaea 100644 > > --- a/gcc/cp/init.cc > > +++ b/gcc/cp/init.cc > > @@ -561,7 +561,8 @@ perform_target_ctor (tree init) > > return init; > > } > > -/* Return the non-static data initializer for FIELD_DECL MEMBER. */ > > +/* Return the non-static data initializer for FIELD_DECL MEMBER. > > + The initializer returned is always non-templated. */ > > static GTY((cache)) decl_tree_cache_map *nsdmi_inst; > > @@ -670,6 +671,11 @@ get_nsdmi (tree member, bool in_ctor, tsubst_flags_t > > complain) > > current_class_ptr = build_address (current_class_ref); > > } > > + /* Since INIT is always non-templated clear processing_template_decl > > + before processing it so that we don't interleave templated and > > + non-templated trees. */ > > + processing_template_decl_sentinel ptds; > > + > > /* Strip redundant TARGET_EXPR so we don't need to remap it, and > > so the aggregate init code below will see a CONSTRUCTOR. */ > > bool simple_target = (init && SIMPLE_TARGET_EXPR_P (init)); > > diff --git a/gcc/testsuite/g++.dg/cpp0x/nsdmi-template24.C > > b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template24.C > > new file mode 100644 > > index 00000000000..202c67d7321 > > --- /dev/null > > +++ b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template24.C > > @@ -0,0 +1,22 @@ > > +// PR c++/108116 > > +// { dg-do compile { target c++11 } } > > + > > +#include > > + > > +struct A { > > + A(int); > > + ~A(); > > +}; > > + > > +struct B { > > + B(std::initializer_list); > > +}; > > + > > +struct C { > > + B m{0}; > > +}; > > + > > +template > > +void f() { > > + C c = C{}; > > +}; > >