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 3DF2E3858D39 for ; Mon, 6 Mar 2023 22:01:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3DF2E3858D39 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=1678140073; 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=M7o6cL3omF5yu7GC3ga1tsdgtGRaKwAHsU3hIUMtzt4=; b=L/5GZWAWpQmAy14xBK4/1SibXHRcllbe4LjYJ2rSn15dwCCiXPFAaONoiWVsHz/hotrlnb EsyOe85EqfO1tjnxDDNyIfXKP1ns0xOqcA2qFLPrIfqmigVS9aafDRaHcqrY2jQ/qOE4UP pkFbACuSoAXq1vRQIVwJS4h8V6eISRI= 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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-643-RJYZZ3QDN0ak3AGe0XWMUA-1; Mon, 06 Mar 2023 17:01:03 -0500 X-MC-Unique: RJYZZ3QDN0ak3AGe0XWMUA-1 Received: by mail-qv1-f72.google.com with SMTP id pv11-20020ad4548b000000b0056e96f4fd64so6361583qvb.15 for ; Mon, 06 Mar 2023 14:01:03 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678140063; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=M7o6cL3omF5yu7GC3ga1tsdgtGRaKwAHsU3hIUMtzt4=; b=2mmYN3YqrAwn34pXRhvcPXGm3xq5/d3iT6xR0I80HkKRNQzKpTngwBHB5gdgx/xz0Z zp/fsMKGpw6/ybhZfEJ2VvBZIqEQK7+Q41BMZ8FdZje98zsZYvBk1XvMXVxAg5b9kv5N xhUcCdNCpS52IGvl0vky890fJc91kjQzAjGGnlsM4AWrYyHllY8wZXZyrf+508j8FO3r UP+GOU3ZhsY8U/vR973/JCO9AGp4qQaN7Xe0ToT6qpGc8fiJeNQhPtOhXgg8X7X2Z3ej EUuPzWg2fPangKIJjdA6A6945F4sXmp3wD33xHE1kn5hvAQ13Foag1pNFOG8rZ9x78ZB jaOg== X-Gm-Message-State: AO0yUKXaYVFNyVORHO/h3aN624iMVTbK7LJf8e7kvXmjrZaWp6x5GE82 bLGwonW9H4Q7ujTW+bY2U20Rg18XQ7lHWPxmc1sXeeMsGg/dbc8aNJ8MRZNBWf1chJA1JRcmvkg 81WgQ18qdEkRqxRbU50M8fCpOdA== X-Received: by 2002:a05:6214:19ed:b0:57e:56f5:5410 with SMTP id q13-20020a05621419ed00b0057e56f55410mr20677790qvc.39.1678140063026; Mon, 06 Mar 2023 14:01:03 -0800 (PST) X-Google-Smtp-Source: AK7set9PaUI21iErijf4Exj8vzKftSCS8z3ZqPv4uGwNmi1s+7vGmVTqXQu6BN29aen+tL8WxrVH6Q== X-Received: by 2002:a05:6214:19ed:b0:57e:56f5:5410 with SMTP id q13-20020a05621419ed00b0057e56f55410mr20677744qvc.39.1678140062660; Mon, 06 Mar 2023 14:01:02 -0800 (PST) Received: from redhat.com (2603-7000-9500-34a5-0000-0000-0000-1db4.res6.spectrum.com. [2603:7000:9500:34a5::1db4]) by smtp.gmail.com with ESMTPSA id r62-20020a374441000000b00706c1f7a608sm8201401qka.89.2023.03.06.14.01.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Mar 2023 14:01:02 -0800 (PST) Date: Mon, 6 Mar 2023 17:01:00 -0500 From: Marek Polacek To: Jason Merrill Cc: GCC Patches Subject: [PATCH v2] c++: error with constexpr operator() [PR107939] Message-ID: References: <20230303175121.705791-1-polacek@redhat.com> MIME-Version: 1.0 In-Reply-To: User-Agent: Mutt/2.2.9 (2022-11-12) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-12.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_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 Mon, Mar 06, 2023 at 11:12:56AM -0500, Jason Merrill wrote: > On 3/3/23 12:51, Marek Polacek wrote: > > Similarly to PR107938, this also started with r11-557, whereby cp_finish_decl > > can call check_initializer even in a template for a constexpr initializer. > > > > Here we are rejecting > > > > extern const Q q; > > > > template > > constexpr auto p = q(0); > > > > even though q has a constexpr operator(). It's deemed non-const by > > decl_maybe_constant_var_p because even though 'q' is const it is not > > of integral/enum type. I think the fix is for p_c_e to treat q(0) as > > potentially-constant, as below. > > > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/12? > > > > PR c++/107939 > > > > gcc/cp/ChangeLog: > > > > * constexpr.cc (is_constexpr_function_object): New. > > (potential_constant_expression_1): Treat an object with constexpr > > operator() as potentially-constant. > > > > gcc/testsuite/ChangeLog: > > > > * g++.dg/cpp1y/var-templ74.C: Remove dg-error. > > * g++.dg/cpp1y/var-templ77.C: New test. > > --- > > gcc/cp/constexpr.cc | 23 ++++++++++++++++++++++- > > gcc/testsuite/g++.dg/cpp1y/var-templ74.C | 2 +- > > gcc/testsuite/g++.dg/cpp1y/var-templ77.C | 14 ++++++++++++++ > > 3 files changed, 37 insertions(+), 2 deletions(-) > > create mode 100644 gcc/testsuite/g++.dg/cpp1y/var-templ77.C > > > > diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc > > index acf9847a4d1..7d786f332b4 100644 > > --- a/gcc/cp/constexpr.cc > > +++ b/gcc/cp/constexpr.cc > > @@ -8929,6 +8929,24 @@ check_for_return_continue (tree *tp, int *walk_subtrees, void *data) > > return NULL_TREE; > > } > > +/* Return true iff TYPE is a class with constexpr operator(). */ > > + > > +static bool > > +is_constexpr_function_object (tree type) > > +{ > > + if (!CLASS_TYPE_P (type)) > > + return false; > > + > > + for (tree f = TYPE_FIELDS (type); f; f = DECL_CHAIN (f)) > > + if (TREE_CODE (f) == FUNCTION_DECL > > + && DECL_OVERLOADED_OPERATOR_P (f) > > + && DECL_OVERLOADED_OPERATOR_IS (f, CALL_EXPR) > > + && DECL_DECLARED_CONSTEXPR_P (f)) > > + return true; > > + > > + return false; > > +} > > + > > /* Return true if T denotes a potentially constant expression. Issue > > diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true, > > an lvalue-rvalue conversion is implied. If NOW is true, we want to > > @@ -9160,7 +9178,10 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, > > } > > else if (fun) > > { > > - if (RECUR (fun, rval)) > > + if (VAR_P (fun) > > + && is_constexpr_function_object (TREE_TYPE (fun))) > > + /* Could be an object with constexpr operator(). */; > > I guess if fun is not a function pointer, we don't know if we're using it as > an lvalue or rvalue Presumably the operator function could return this, making it an lvalue? I'm not sure I'm really clear on this. > , so we want to pass 'any' for want_rval, which should > make this work; Yes, want_rval==false means that p_c_e/VAR_DECL will not issue the hard error. > I don't think we need to be specific about constexpr op(), > as a constexpr conversion operator to fn* could also do the trick. Ah, those surrogate classes. I couldn't reproduce the problem with them, though I'm adding a test for it anyway. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- Similarly to PR107938, this also started with r11-557, whereby cp_finish_decl can call check_initializer even in a template for a constexpr initializer. Here we are rejecting extern const Q q; template constexpr auto p = q(0); even though q has a constexpr operator(). It's deemed non-const by decl_maybe_constant_var_p because even though 'q' is const it is not of integral/enum type. If fun is not a function pointer, we don't know if we're using it as an lvalue or rvalue, so with this patch we pass 'any' for want_rval. With that, p_c_e/VAR_DECL doesn't flat out reject the underlying VAR_DECL. PR c++/107939 gcc/cp/ChangeLog: * constexpr.cc (potential_constant_expression_1) : Pass 'any' when recursing on a VAR_DECL and not a pointer to function. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/var-templ74.C: Remove dg-error. * g++.dg/cpp1y/var-templ77.C: New test. --- gcc/cp/constexpr.cc | 8 ++++-- gcc/testsuite/g++.dg/cpp1y/var-templ74.C | 2 +- gcc/testsuite/g++.dg/cpp1y/var-templ77.C | 32 ++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/var-templ77.C diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 364695b762c..3079561f2e8 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -9179,8 +9179,12 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, } else if (fun) { - if (RECUR (fun, rval)) - /* Might end up being a constant function pointer. */; + if (RECUR (fun, FUNCTION_POINTER_TYPE_P (fun) ? rval : any)) + /* Might end up being a constant function pointer. But it + could also be a function object with constexpr op(), so + we pass 'any' so that the underlying VAR_DECL is deemed + as potentially-constant even though it wasn't declared + constexpr. */; else return false; } diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ74.C b/gcc/testsuite/g++.dg/cpp1y/var-templ74.C index 4e2e800a6eb..c76a7d949ac 100644 --- a/gcc/testsuite/g++.dg/cpp1y/var-templ74.C +++ b/gcc/testsuite/g++.dg/cpp1y/var-templ74.C @@ -9,7 +9,7 @@ struct Q { extern const Q q; template -constexpr const Q* p = q(0); // { dg-bogus "not usable" "PR107939" { xfail *-*-* } } +constexpr const Q* p = q(0); void g () diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ77.C b/gcc/testsuite/g++.dg/cpp1y/var-templ77.C new file mode 100644 index 00000000000..0c56d70a034 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/var-templ77.C @@ -0,0 +1,32 @@ +// PR c++/107939 +// { dg-do compile { target c++14 } } + +struct Q { + struct P { + const Q* p; + }; + int n; + constexpr P operator()(int) const { return {this}; } +}; + +extern const Q q; +template +constexpr auto p = q(0); +static_assert(p<0>.p == &q, ""); + +constexpr int +fn (int) +{ + return 42; +} + +struct Sur { + using FN = int(int); + constexpr operator FN*() const { return &fn; } +}; + +extern const Sur sur; +template +constexpr int aja = sur (0); +static_assert(aja<0> == 42, ""); +static_assert(sur(1) == 42, ""); base-commit: 553ff2524f412be4e02e2ffb1a0a3dc3e2280742 -- 2.39.2