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 480BF3858C36 for ; Fri, 10 Nov 2023 14:49:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 480BF3858C36 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 480BF3858C36 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=1699627789; cv=none; b=UcOHf5V5Ehky309SSjLJQE3QC1PQJuU65+f2/zKRtPPzoGrvr/yUqOXxkyV2Xda/GICbdhd0VrSPQGA0Y79B0ZxfgjO5w9DTEEBO22F78xyj4R02FSDynvWH0xfW+Za/rU6swH0Aj7lmifijl9yGA9G7StwJ5yKoJJGN90126dA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699627789; c=relaxed/simple; bh=gd9n/7w/oNv9qqUXpXQUG4cc0TOHcD3LWbLKcILHumM=; h=DKIM-Signature:From:Date:To:Subject:Message-ID:MIME-Version; b=qummb/feSZn692FoVh4Yrk3lCWuGaAqm6kxdTzzUEkLB5CI9ozVXcKv7+Hztqx0J1YvhmGogH+LvbgreKGW9VQx3xNLdtuKVKEtxORFeT2KUCEy6MHuG0EXouGPbOXqanQNXCD00RZLV77XBl7dUNnwWJyi7c6Z56EoT3uu9+q4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699627778; 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=6/qpEbbiFdi9kcjij5Gi+NaK4YVZ4IEK9S0RiXo3i/E=; b=beBRcUuAXQEdiLMvN8Pbhd6nZlcQ5rUQumyDp+VGmCAULBINNjwrYEDBJ3BYjbF6Ce3NLo 6BMMx6JTVFrehB1gGt6JGrVqeYnKDwPevebcNtj2dsyvpsXxitQhzUow+ZLdhBjfFM5j1+ gjdlof/Yu7fDUtmsUkr70az0XB5waAw= 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-88-eirExmfmP6eCioGIotLotQ-1; Fri, 10 Nov 2023 09:49:28 -0500 X-MC-Unique: eirExmfmP6eCioGIotLotQ-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-77891f36133so227113585a.0 for ; Fri, 10 Nov 2023 06:49:28 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699627768; x=1700232568; 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=6/qpEbbiFdi9kcjij5Gi+NaK4YVZ4IEK9S0RiXo3i/E=; b=QK97e9SMRDUBatwOfgm/wP8Wphb4ZntaR9cAuZ8A4fqDHfGu1VT94huMKqQhJaoOLt x23FXsAa1DPS0p/H26hhCYQKxmRrnkKA6QkrujJEtakB5nJVgKAuJedN0lu0LmSpn0g0 X0yVtyr2R5trTaL90esNJusHVlrh0mp++xVln2XgnJQkbImZCA3OQdMY1BhOOpSTYjlH jTI+4CpWTAJlF1FNWWBt/VCNZZYGXTCz0K0tQDXeolhO3vXfLGWy85h2oIokTeZBKKGQ GJ6Www59M1eeKvfB00huxu9m5wDDtpwZ0YIGkhVHFHE6UNY+koXdU03qq53XFxRyNYLT OaBg== X-Gm-Message-State: AOJu0YwRFolpVrju5v5tJzZHtTJ6RDd2D+JGVAjtvSleXbdBTOL8WNfq gPHbiY0cMuNrSG7FuOue3ZWj3vkc5ZBebXgqUIXisNmVE3Jo5A6pTaahWAVv72/42SiNWYAq3su culGUoG8kgsoAYBtOfg== X-Received: by 2002:a05:620a:400b:b0:775:9041:73dc with SMTP id h11-20020a05620a400b00b00775904173dcmr9784506qko.16.1699627768182; Fri, 10 Nov 2023 06:49:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IHpOG/kM03eHXUsd3ukSIzc1shx6MCmSCw6bHAjUosSQTJC/vBZgjPwiKSo6goMOhhsrZ86Wg== X-Received: by 2002:a05:620a:400b:b0:775:9041:73dc with SMTP id h11-20020a05620a400b00b00775904173dcmr9784494qko.16.1699627767909; Fri, 10 Nov 2023 06:49:27 -0800 (PST) Received: from [192.168.1.130] (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id t3-20020a05620a034300b0075ca4cd03d4sm734118qkm.64.2023.11.10.06.49.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Nov 2023 06:49:27 -0800 (PST) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Fri, 10 Nov 2023 09:49:26 -0500 (EST) 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: <805a8e2e-7944-c8f3-164f-a47dfe4de9bc@idea> Message-ID: <9bf57f95-1810-b16c-5ec9-d56552ea4232@idea> References: <20231031181726.3944801-1-ppalka@redhat.com> <805a8e2e-7944-c8f3-164f-a47dfe4de9bc@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.0 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 Wed, 1 Nov 2023, Patrick Palka wrote: > 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? Ping. > > > > -- >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. N.B. adding COMPLEX_EXPR handling to tsubst_expr is complicated by the fact that these COMPLEX_EXRRs are created by convert_to_complex (a middle-end routine) which occasionally creates SAVE_EXPR sub trees which we don't expect to see inside templated trees... > > > > [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 > > > > >