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 9688A3854811 for ; Tue, 7 Mar 2023 15:11:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9688A3854811 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=1678201901; 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=oy+3djN916BMocYmVScU9AJoDAwG60ypOawbQDiK6kg=; b=AzfATqP1zIYtoE0rMyYk2HxxZlMBTi1+H+/78vOp7cNKNOfEwJGGMhKdVtmzmUkVBnK6wi dSY+v3kCeCDZg8YXWJYcV1LHyUh6uw+6lfDGoXSjb7F6Oclo8bG4eSSfX4Nz7ErtPBG9rP 4Ibiu563edX5tMaTuJ70m4KFlIlXriI= 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-1-TJspV7-LNTCmV4v0fsKRNQ-1; Tue, 07 Mar 2023 10:11:24 -0500 X-MC-Unique: TJspV7-LNTCmV4v0fsKRNQ-1 Received: by mail-qt1-f198.google.com with SMTP id x4-20020ac85384000000b003bfbb485e2dso7273917qtp.22 for ; Tue, 07 Mar 2023 07:11:15 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678201875; 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=oy+3djN916BMocYmVScU9AJoDAwG60ypOawbQDiK6kg=; b=StGIN0yRMNwln8tKnMaEgMWeJNifdNVXm6qZCEv/w8RKTrknAVyIyEadGnnIcpXgQp 8dh4ACtPZ80Qgi++NJC4JrdudRHeZziFEdS3QcVPLFv/nSYikQBYhq2LkjuNRCo7KPQR JRH0aXJd5FTacmraLCI0R8qAW2/sl+yOpJVpHaXsTAvpUjuzBPCvpc2NDYIhR66deYbe 7/z+U6CE5y4yiqxPnwX0i7IAFdtCQsS5XdRfqKdtiiim1wPkAerqLSoWX253/if0+TEr M5u+LmRJQVdHSpXbhAdYgV6GiBdpvhi3uGsKgYMN71BTVCPVRCATzDzIofiiVNVux+RA ikog== X-Gm-Message-State: AO0yUKVOnrSZQQueK8tmUu1PYqlmKxxfGG7IFTfthZVpB6Z7/Njh61HI 8ZXLgNDQ4iULshLJOdQmYtaI77w+hH/TgEfvqz7WRr/TEdc1PIxSYpiiSpXm3cgT9DqhBNInDet KLBgn0SH+wDmUgqr/qk/3DGqumg== X-Received: by 2002:a05:622a:15c6:b0:3bf:e39f:a9aa with SMTP id d6-20020a05622a15c600b003bfe39fa9aamr27099201qty.27.1678201874732; Tue, 07 Mar 2023 07:11:14 -0800 (PST) X-Google-Smtp-Source: AK7set9vi2sUJjhFfKsIe451b3lRPkJXlpUmC4MNtjtWaRS2Xswf8E0PbSm7VbPRcP9tP5HHTNetDA== X-Received: by 2002:a05:622a:15c6:b0:3bf:e39f:a9aa with SMTP id d6-20020a05622a15c600b003bfe39fa9aamr27099149qty.27.1678201874296; Tue, 07 Mar 2023 07:11:14 -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 l6-20020ac84a86000000b003ba11bfe4fcsm9642021qtq.28.2023.03.07.07.11.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Mar 2023 07:11:13 -0800 (PST) Date: Tue, 7 Mar 2023 10:11:12 -0500 From: Marek Polacek To: Jason Merrill Cc: GCC Patches Subject: Re: [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 Tue, Mar 07, 2023 at 09:53:28AM -0500, Jason Merrill wrote: > On 3/6/23 17:01, Marek Polacek wrote: > > 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. > > I mean just calling the operator uses the variable as an lvalue, by passing > its address as 'this'. Ah yeah, right. Unless there's the && ref-qual etc. > > > , 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? > > OK, thanks. Thanks. Marek