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 5618E3858D35 for ; Thu, 16 Mar 2023 15:48:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5618E3858D35 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=1678981708; 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=AdWaqLI68zsM57GTDBal3k0dPOwVzWn9H3HElhGP41s=; b=ceFdKTv430VulEP31i3Mz/1T63D0cyXPRkTgTtgXbbce8Y/mVWANhh7EPTwl4cXvc2rfp+ hVZJo1yh3lfAt1Fs6F4Q/AN6/FzqcsBYt2gxukZQn6J9L7YJytNksRPU17kXMbhoH1eHw3 Xvy1oiWWn5+pxjxumAI8WKnQJpkfviE= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-157-4ClFoHjDOYaHs1GzQmYYcw-1; Thu, 16 Mar 2023 11:48:27 -0400 X-MC-Unique: 4ClFoHjDOYaHs1GzQmYYcw-1 Received: by mail-qt1-f198.google.com with SMTP id g11-20020ac8774b000000b003bfa92f56cbso1057557qtu.3 for ; Thu, 16 Mar 2023 08:48:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678981707; 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=AdWaqLI68zsM57GTDBal3k0dPOwVzWn9H3HElhGP41s=; b=kaJBAbrRL7uvxAIYzNZyWq209oFfuOCOWuDG+eSJEEa8rNzm4yQJ3VzwuX1uiES0Nm qOklcN8004EyLcEhwXD2sV/3Ce/smMcbhGj7qsRgGZuZEWaiKz/7Zjrp/6wLvBNZC6+s xd+QBSY1Gq8eUAGYf8twgak14o6MgOTM542qeCY5BQCNflFWUzC7UDnnZyay1M6X/Ngw +L9+ixiPRzhSa89hmlHjaW5cANz3W/IzGRtM5cUz5H5xmLfXuWqjae1QOiGoagoEwv23 F1obkDGKIJH8DTzbJt9FMt+NOWReXHwxFyLSHx5YSfVSrwDf4mepcy/3bkVEHYMwy9vs zkbg== X-Gm-Message-State: AO0yUKW05UinXPed5+zEK4PLfLQgIbkpWJbROEVhvS9wUpRV/y8KffM9 DspQiMbvbrm73A78c0V21poFmwfKPiFx6dEFxGUpSzJvYdkLa5h0bsmzA+9PrFLAzPk8cyal8cQ qqH6lFH9/o8ZDW3Gs8Q== X-Received: by 2002:a05:6214:c85:b0:5ac:7fb9:3c4 with SMTP id r5-20020a0562140c8500b005ac7fb903c4mr16559010qvr.47.1678981707198; Thu, 16 Mar 2023 08:48:27 -0700 (PDT) X-Google-Smtp-Source: AK7set+n59B+mDRFwC5QWzaxU8NI0sUp5rc8/A26WK2Ix9mQtxv9Z4oz8pqlSsJnjzg4/SmeZD0Hxg== X-Received: by 2002:a05:6214:c85:b0:5ac:7fb9:3c4 with SMTP id r5-20020a0562140c8500b005ac7fb903c4mr16558977qvr.47.1678981706849; Thu, 16 Mar 2023 08:48:26 -0700 (PDT) Received: from [192.168.1.130] (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id r8-20020a37a808000000b00741a8e96f25sm6196637qke.88.2023.03.16.08.48.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Mar 2023 08:48:26 -0700 (PDT) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Thu, 16 Mar 2023 11:48:25 -0400 (EDT) To: Jason Merrill cc: Patrick Palka , Marek Polacek , GCC Patches Subject: Re: [PATCH] c++: noexcept and copy elision [PR109030] In-Reply-To: Message-ID: <67e8023b-df78-f522-a643-c14b69719600@idea> References: <20230306235957.390533-1-polacek@redhat.com> <233db53c-67cb-37cf-92ef-620b3678d86f@idea> <5720dde1-3aae-0fc0-44b5-2d992951c55b@idea> 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,KAM_SHORT,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 Thu, 16 Mar 2023, Jason Merrill wrote: > On 3/16/23 10:09, Patrick Palka wrote: > > On Wed, 15 Mar 2023, Patrick Palka wrote: > > > > > On Thu, 9 Mar 2023, Jason Merrill wrote: > > > > > > > On 3/9/23 14:32, Patrick Palka wrote: > > > > > On Mon, 6 Mar 2023, Marek Polacek via Gcc-patches wrote: > > > > > > > > > > > When processing a noexcept, constructors aren't elided: > > > > > > build_over_call > > > > > > has > > > > > > /* It's unsafe to elide the constructor when handling > > > > > > a noexcept-expression, it may evaluate to the wrong > > > > > > value (c++/53025). */ > > > > > > && (force_elide || cp_noexcept_operand == 0)) > > > > > > so the assert I added recently needs to be relaxed a little bit. > > > > > > > > > > > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > > > > > > > > > > > PR c++/109030 > > > > > > > > > > > > gcc/cp/ChangeLog: > > > > > > > > > > > > * constexpr.cc (cxx_eval_call_expression): Relax assert. > > > > > > > > > > > > gcc/testsuite/ChangeLog: > > > > > > > > > > > > * g++.dg/cpp0x/noexcept77.C: New test. > > > > > > --- > > > > > > gcc/cp/constexpr.cc | 6 +++++- > > > > > > gcc/testsuite/g++.dg/cpp0x/noexcept77.C | 9 +++++++++ > > > > > > 2 files changed, 14 insertions(+), 1 deletion(-) > > > > > > create mode 100644 gcc/testsuite/g++.dg/cpp0x/noexcept77.C > > > > > > > > > > > > diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc > > > > > > index 364695b762c..5384d0e8e46 100644 > > > > > > --- a/gcc/cp/constexpr.cc > > > > > > +++ b/gcc/cp/constexpr.cc > > > > > > @@ -2869,7 +2869,11 @@ cxx_eval_call_expression (const constexpr_ctx > > > > > > *ctx, > > > > > > tree t, > > > > > > /* We used to shortcut trivial constructor/op= here, but > > > > > > nowadays > > > > > > we can only get a trivial function here with > > > > > > -fno-elide-constructors. */ > > > > > > - gcc_checking_assert (!trivial_fn_p (fun) || > > > > > > !flag_elide_constructors); > > > > > > + gcc_checking_assert (!trivial_fn_p (fun) > > > > > > + || !flag_elide_constructors > > > > > > + /* We don't elide constructors when processing > > > > > > + a noexcept-expression. */ > > > > > > + || cp_noexcept_operand); > > > > > > > > > > It seems weird that we're performing constant evaluation within an > > > > > unevaluated operand. Would it make sense to also fix this a second > > > > > way > > > > > by avoiding constant evaluation from maybe_constant_init when > > > > > cp_unevaluated_operand && !manifestly_const_eval, like in > > > > > maybe_constant_value? > > > > > > > > Sounds good. > > > > > > Hmm, while working on this I noticed we currently don't reject a version > > > of > > > g++.dg/cpp2a/constexpr-inst1.C that list initializes an aggregate instead > > > of > > > int (ever since r12-4425-g1595fe44e11a96): > > > > > > struct A { int m; }; > > > template constexpr int f() { return T::value; } > > > template void h(decltype(A{B ? f() : 0})); // > > > was int{...} > > > template void h(...); > > > void x() { > > > h(0); // OK? > > > } > > > > > > ISTM we should instantiate f here for the same reason we do in the > > > original version of the testcase, and for that to happen we need to > > > pass manifestly_const_eval=true in massage_init_elt. Does that seem > > > reasonable? > > > > > > > FWIW the reason this came up is because I tried contriving a testcase > > for the aforementioned maybe_constant_init change, and I came up with: > > > > struct __as_receiver { > > int empty_env; > > }; > > > > template > > constexpr int f(T t) { > > return t.fail; > > }; > > > > using type = decltype(__as_receiver{f(0)}); // OK, f no longer > > instantiated > > > > which we used to reject and afterwards accept. But since the elements > > of an initializer list are potentially constant evaluated, I wonder if > > that that means f should be instantiated here after all despite the > > unevaluated context? > > The relevant section of the standard would seem to be > https://eel.is/c++draft/expr.const#20 ; an immediate subexpression of a > braced-init-list is potentially constant-evaluated even though it isn't > potentially-evaluated or manifestly constant-evaluated. > > It seems like the call to fold_non_dependent_expr in check_narrowing ought to > cause instantiation in this case, why doesn't it? Looks like check_narrowing isn't called at all in this aggr init case. The call from e.g. convert_like_internal isn't reached because the conversion for the initializer element is ck_identity, and don't ever set conversion::check_narrowing for ck_identity conversions I think. Yet for using 'type = decltype(int{f(0)});' (similar to the example in [temp.inst]/8) we do call check_narrowing directly from finish_compound_literal, despite the conversion effectively being an identity conversion. > > > Here's the full patch for reference: > > > > -- >8 -- > > > > Subject: [PATCH] c++: maybe_constant_init and unevaluated operands > > [PR109030] > > > > This testcase in this PR (already fixed by r13-6526-ge4692319fd5fc7) > > illustrates that maybe_constant_init can be called on an unevaluated > > operand (from massage_init_elt), so this entry point should limit > > constant evaluation in that case, like maybe_constant_value does. > > > > PR c++/109030 > > > > gcc/cp/ChangeLog: > > > > * constexpr.cc (maybe_constant_init_1): For an unevaluated > > non-manifestly-constant operand, don't constant evaluate > > and instead call fold_to_constant. > > > > gcc/testsuite/ChangeLog: > > > > * g++.dg/cpp0x/decltype83.C: New test. > > --- > > gcc/cp/constexpr.cc | 2 ++ > > gcc/testsuite/g++.dg/cpp0x/decltype83.C | 14 ++++++++++++++ > > 2 files changed, 16 insertions(+) > > create mode 100644 gcc/testsuite/g++.dg/cpp0x/decltype83.C > > > > diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc > > index 8683c00596a..f325af375c8 100644 > > --- a/gcc/cp/constexpr.cc > > +++ b/gcc/cp/constexpr.cc > > @@ -8795,6 +8795,8 @@ maybe_constant_init_1 (tree t, tree decl, bool > > allow_non_constant, > > && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))); > > if (is_static) > > manifestly_const_eval = true; > > + if (cp_unevaluated_operand && !manifestly_const_eval) > > + return fold_to_constant (t); > > t = cxx_eval_outermost_constant_expr (t, allow_non_constant, > > !is_static, > > mce_value (manifestly_const_eval), > > false, decl); > > diff --git a/gcc/testsuite/g++.dg/cpp0x/decltype83.C > > b/gcc/testsuite/g++.dg/cpp0x/decltype83.C > > new file mode 100644 > > index 00000000000..17005a92eb5 > > --- /dev/null > > +++ b/gcc/testsuite/g++.dg/cpp0x/decltype83.C > > @@ -0,0 +1,14 @@ > > +// { dg-do compile { target c++11 } } > > + > > +struct __as_receiver { > > + int empty_env; > > +}; > > + > > +template > > +constexpr int f(T t) { > > + return t.fail; > > +}; > > + > > +int main() { > > + using type = decltype(__as_receiver{f(0)}); // OK, f not > > instantiated > > +} > >