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.133.124]) by sourceware.org (Postfix) with ESMTPS id E19643858D28 for ; Wed, 1 Nov 2023 15:07:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E19643858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E19643858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698851272; cv=none; b=Lboji05CgLiNx///fHX0Ih/XT/MG7yn+FEK0hGK7fP+Cfdwri82BjUCoW4lgTyb3ymcvy/ebeSx7pMeXoMvXWTVqIc7hkpPIEdPBRfosd5hGtQ8QWHSIib7CPAgB62IbioxDak8T6YIiIVtmID1nfi0CoRARuEB72oqxYJfq+Ok= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698851272; c=relaxed/simple; bh=RcJDFzi7RC+Xq9jtYidWOTYbtOl8a6ncJxCkNnjMtGo=; h=DKIM-Signature:From:Date:To:Subject:Message-ID:MIME-Version; b=MPV0sWd+/+X6AknPRvZwpSrsTnDKURIGEGAj6CRtKZmwAhMGz4SkxLKl4o+nEamCkDJxhRLHUMoSzFKD6wprVKJx1eUgZNs7zAGidHoRG5Ok+41/t+9zHBDBSVC5pq1t9bC6E0PFxlz0ebw4xfJ3v/xYUcdURLPS5N5uw5C87H4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698851261; 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=7moRpbNezBZpJmI3LKGTuY1/sUVf295IPRy7DLr2J48=; b=UG8z+VGIZmtgSEpCSfeIIhNHgDZmIKAu3mlhuyaE//jI9Y6v2qx3qCUDU17aJ/gu+fpNXH 1l6s2if9RyQlP4S8q9mcXusifyKdg7U9uxeTo3pXff5W6BGK0xdfdAwOoOUJkiIitxqF5X +3tDdxjEOQwUXKvrbIHfAyRXXYO8poc= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-110-He0UQ467PBKSuymaY-n9sg-1; Wed, 01 Nov 2023 11:07:31 -0400 X-MC-Unique: He0UQ467PBKSuymaY-n9sg-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-7788fb069abso157968485a.0 for ; Wed, 01 Nov 2023 08:07:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698851250; x=1699456050; 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=7moRpbNezBZpJmI3LKGTuY1/sUVf295IPRy7DLr2J48=; b=mmsh8m12Nycfrk9fxbR3kAZXthKcz25Nf4bcZRisicnkH/NmKC8IfqwWcdws5EgdqU pn6LdT33VQXi42ZSrgHBgxQE+GjLcB8j0++PlxEU0+GgOwGsaXNxB3yiBEHL3U2qoFm2 v7bWPCTTmbnZDyZSgxgKufBdEVScW3Q8uP7EpuQReLyC+ihjr2pVjxQy9kYg2bHIbNDA AjRSU8+eXAHGZbBNnwtdDZl9vupqUUmIz9qi+NKYjcvgAdoMhMXCEMvLiwv6Ku7fzQzI +KoUHCPjz4i/2nlKzoVb3L73HhEaoM3mrwsEpIZVj2wGBW16CsyWPyZli5pKw1Riom7+ CyTA== X-Gm-Message-State: AOJu0YzAihU3gJ9hx5SJptkfIwc78l2bVkjOBUxEQwoNiFP04HoyVdLB R4DtebY/NgCFpN4sC39KGfS68tmSLQ+F1trXFi7qsMUTu7ujvpDwHfBUb/KYeFhJ7PsRkCRzf2Y /A6pnEeHcQPC5sa2/Gg== X-Received: by 2002:a05:620a:1488:b0:76e:f73d:65ae with SMTP id w8-20020a05620a148800b0076ef73d65aemr3587352qkj.6.1698851250598; Wed, 01 Nov 2023 08:07:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHvRgbVfVB1tdCDQ3/HP7dDFcjmwVWWwXgwhIqM42NwnEVHw1P0V17fbESL40ZstQWC4KycoQ== X-Received: by 2002:a05:620a:1488:b0:76e:f73d:65ae with SMTP id w8-20020a05620a148800b0076ef73d65aemr3587330qkj.6.1698851250273; Wed, 01 Nov 2023 08:07:30 -0700 (PDT) Received: from [192.168.1.130] (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id o26-20020a05620a111a00b0076d25b11b62sm1510374qkk.38.2023.11.01.08.07.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 08:07:29 -0700 (PDT) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Wed, 1 Nov 2023 11:07:29 -0400 (EDT) To: Patrick Palka cc: gcc-patches@gcc.gnu.org, jason@redhat.com Subject: Re: [PATCH] c++: constantness of local var in constexpr fn [PR111703, PR112269] In-Reply-To: <20231031181726.3944801-1-ppalka@redhat.com> Message-ID: <805a8e2e-7944-c8f3-164f-a47dfe4de9bc@idea> References: <20231031181726.3944801-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.2 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_H3,RCVD_IN_MSPIKE_WL,RCVD_IN_SORBS_WEB,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 List-Id: On Tue, 31 Oct 2023, Patrick Palka wrote: > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for > trunk? Does it look OK for release branches as well for sake of PR111703? > > -- >8 -- > > potential_constant_expression was incorrectly treating most local > variables from a constexpr function as (potentially) constant because it > wasn't considering the 'now' parameter. This patch fixes this by > relaxing some var_in_maybe_constexpr_fn checks accordingly, which turns > out to partially fix two recently reported regressions: > > PR111703 is a regression caused by r11-550-gf65a3299a521a4 for > restricting constexpr evaluation during warning-dependent folding. > The mechanism is intended to restrict only constant evaluation of the > instantiated non-dependent expression, but it also ends up restricting > constant evaluation (as part of satisfaction) during instantiation of > the expression, in particular when resolving the ck_rvalue conversion of > the 'x' argument into a copy constructor call. Oops, this analysis is inaccurate for this specific testcase (although the general idea is the same)... We don't call fold_for_warn on 'f(x)' but rather on its 'x' argument that has been processed by convert_arguments into an IMPLICIT_CONV_EXPR. And it's the instantiation of this IMPLICIT_CONV_EXPR that turns it into a copy constructor call. There is no ck_rvalue conversion at all here since 'f' is a function pointer, not an actual function, and so ICSes don't get computed (IIUC). If 'f' is changed to be an actual function then there's no issue since build_over_call doesn't perform argument conversions when in a template context and therefore doesn't call check_function_arguments on the converted arguments (from which the problematic fold_for_warn call occurs). > This seems like a bug in > the mechanism[1], though I don't know if we want to refine the mechanism > or get rid of it completely since the original testcases which motivated > the mechanism are fixed more simply by r13-1225-gb00b95198e6720. In any > case, this patch partially fixes this by making us correctly treat 'x' > and therefore 'f(x)' in the below testcase as non-constant, which > prevents the problematic warning-dependent folding from occurring at > all. If this bug crops up again then I figure we could decide what to > do with the mechanism then. > > PR112269 is caused by r14-4796-g3e3d73ed5e85e7 for merging tsubst_copy > into tsubst_copy_and_build. tsubst_copy used to exit early when 'args' > was empty, behavior which that commit deliberately didn't preserve. > This early exit masked the fact that COMPLEX_EXPR wasn't handled by > tsubst at all, and is a tree code that apparently we could see during > warning-dependent folding on some targets. A complete fix is to add > handling for this tree code in tsubst_expr, but this patch should fix > the reported testsuite failures since the situations where COMPLEX_EXPR > crops up in turn out to not be constant expressions in the > first place after this patch. > > [1]: The mechanism incorrectly assumes that instantiation of the > non-dependent expression shouldn't induce any template instantiation > since ahead of time checking of the expression should've already induced > whatever template instantiation was needed, but in this case although > overload resolution was performed ahead of time, a ck_rvalue conversion > gets resolved to a copy constructor call only at instantiation time. > > PR c++/111703 > > gcc/cp/ChangeLog: > > * constexpr.cc (potential_constant_expression_1) : > Only consider var_in_maybe_constexpr_fn if 'now' is false. > : Likewise. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp2a/concepts-fn8.C: New test. > --- > gcc/cp/constexpr.cc | 4 ++-- > gcc/testsuite/g++.dg/cpp2a/concepts-fn8.C | 24 +++++++++++++++++++++++ > 2 files changed, 26 insertions(+), 2 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-fn8.C > > diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc > index c05760e6789..8a6b210144a 100644 > --- a/gcc/cp/constexpr.cc > +++ b/gcc/cp/constexpr.cc > @@ -9623,7 +9623,7 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, > return RECUR (DECL_VALUE_EXPR (t), rval); > } > if (want_rval > - && !var_in_maybe_constexpr_fn (t) > + && (now || !var_in_maybe_constexpr_fn (t)) > && !type_dependent_expression_p (t) > && !decl_maybe_constant_var_p (t) > && (strict > @@ -9737,7 +9737,7 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, > STRIP_NOPS (x); > if (is_this_parameter (x) && !is_capture_proxy (x)) > { > - if (!var_in_maybe_constexpr_fn (x)) > + if (now || !var_in_maybe_constexpr_fn (x)) > { > if (flags & tf_error) > constexpr_error (loc, fundef_p, "use of % in a " > diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-fn8.C b/gcc/testsuite/g++.dg/cpp2a/concepts-fn8.C > new file mode 100644 > index 00000000000..3f63a5b28d7 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-fn8.C > @@ -0,0 +1,24 @@ > +// PR c++/111703 > +// { dg-do compile { target c++20 } } > + > +template > +constexpr bool always_true() { return true; } > + > +struct P { > + P() = default; > + > + template > + requires (always_true()) // { dg-bogus "used before its definition" } > + constexpr P(const T&) { } > + > + int n, m; > +}; > + > +void (*f)(P); > + > +template > +constexpr bool g() { > + P x; > + f(x); // { dg-bogus "from here" } > + return true; > +} > -- > 2.42.0.526.g3130c155df > >