From patchwork Tue Nov 15 12:28:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 20352 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2691390wru; Tue, 15 Nov 2022 04:28:54 -0800 (PST) X-Google-Smtp-Source: AA0mqf7M0nHlDfCqN6agXap5SbD7VuVfQrR8q7o7VQlwyVLnhHBrOwsnFroBqXYsvv/m+HP2tKE3 X-Received: by 2002:a50:d65a:0:b0:461:7378:3501 with SMTP id c26-20020a50d65a000000b0046173783501mr14341085edj.35.1668515333911; Tue, 15 Nov 2022 04:28:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668515333; cv=none; d=google.com; s=arc-20160816; b=rwIcDBzN8r4kOCSWx5eBkMZXd1npM4CwZrL7EsFX7bQmBSoEMv3r2aEkzyLD9Snh3K Lchj6a0AyTmK1hO+L1AaOKw0qU6QRdb02h0NsVgkL4spwrREpqBkp+AE83xmrhWKe2tJ eud7KaYI6mIp9FXkIk1nCNY5iKkNwBw0SYoQcGQZiMpfNPbfDgwO9h6MlaN/Ifqbp8LT uSadQJljRHyPN5lbvVyZhaMbtNTQF5Nynfk7kf2IDuaPRs5uWvE/y0NNEzBlwFs+9U12 OufjpKfINmBLvmJ/vPeEcDGr1T23JIO0cHZDKdsXaJIY7EBshlR9iEAa1Kq5m2U9tchb OkyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:in-reply-to:mime-version:references:message-id :subject:cc:to:date:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=muY4nh8WgBQ2h6jE6oXH68q3ahrF7DjXFIOxovzZWvo=; b=EOdfpQrp0B35zFrkUTTYFa5ArNcEoiqRG/M3JLubnSycL2MKHFeBg+NB+BzKtnn6tx fKIX2Zfm06KkXnLLoCQBADWVCgKg2PJaNvw2/+1wbD9SCncpst4fj4MiYlNrX9PgMFvn HtVKV2aLaEw8irzR7oKbwx2KoZmTlR0kv1OKuaFDpPx5PY1pOlA5juTfrcoIZhwfczq9 XfJWMMkDg/56/1hwObip1UwbeltVJu8X0B79M7ffAtZYE3bZGBht9O/sNk8TKAWoxmvN g7XtG/4AtEW1HLAwJhNtWULlqjlStFWAUimAzYdvWJHyq1b/hQ3c2QX6GZGQ6YpE5V2s qKPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=D7fGsnrc; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id gs34-20020a1709072d2200b0078d930212c0si11681703ejc.347.2022.11.15.04.28.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Nov 2022 04:28:53 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=D7fGsnrc; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B54E13894C17 for ; Tue, 15 Nov 2022 12:28:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B54E13894C17 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668515332; bh=muY4nh8WgBQ2h6jE6oXH68q3ahrF7DjXFIOxovzZWvo=; h=Date:To:Cc:Subject:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=D7fGsnrcFevpy3T2hUxkXDycavZGiEV2Idt8WlSBThfK8Sh20ovvQESUoAUvEGaZH FDH+TwPaBGCyhOzjWpY4mr5mRg42huhrjmPZ0rg34yRW5zdAaiZqxaflfZUTs6I3Pa iyGkKX2fP6J9ljLa1ouYRomyuGLxiE/g+rKtlHSc= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 0C53C3893676 for ; Tue, 15 Nov 2022 12:28:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0C53C3893676 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-596-3OC3qO-UOwmkPG9PeXJZgg-1; Tue, 15 Nov 2022 07:28:07 -0500 X-MC-Unique: 3OC3qO-UOwmkPG9PeXJZgg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5CF4938005C9 for ; Tue, 15 Nov 2022 12:28:07 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.38]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1A417C1908B; Tue, 15 Nov 2022 12:28:06 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 2AFCS21d560515 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 15 Nov 2022 13:28:02 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 2AFCS2AS560514; Tue, 15 Nov 2022 13:28:02 +0100 Date: Tue, 15 Nov 2022 13:28:01 +0100 To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Fix up calls to static operator() or operator[] [PR107624] Message-ID: References: <1ec099de-4247-29ec-0e06-602a90370388@redhat.com> MIME-Version: 1.0 In-Reply-To: <1ec099de-4247-29ec-0e06-602a90370388@redhat.com> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749565134897000282?= X-GMAIL-MSGID: =?utf-8?q?1749565134897000282?= Hi! On Mon, Nov 14, 2022 at 06:29:44PM -0500, Jason Merrill wrote: > Indeed. The code in build_new_method_call for this case has the comment > > /* In an expression of the form `a->f()' where `f' turns > out to be a static member function, `a' is > none-the-less evaluated. */ Had to tweak 3 spots for this. Furthermore, found that if in non-pedantic C++20 compilation static operator[] is accepted, we required that it has 2 arguments, I think it is better to require exactly one because that case is the only one that will actually work in C++20 and older. Lightly tested so far, ok for trunk if it passes bootstrap/regtest? Or do you want to outline the if (result != error_mark_node && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE && TREE_SIDE_EFFECTS (obj)) { /* But avoid the implicit lvalue-rvalue conversion when 'a' is volatile. */ tree a = obj; if (TREE_THIS_VOLATILE (a)) a = build_this (a); if (TREE_SIDE_EFFECTS (a)) result = build2 (COMPOUND_EXPR, TREE_TYPE (result), a, result); } part that is now repeated 4 times to some helper function? If yes, any suggestion on a good name? 2022-11-15 Jakub Jelinek PR c++/107624 * call.cc (build_op_call): If obj has side-effects and operator() is static member function, return COMPOUND_EXPR with the obj side-effects other than reading from volatile object. (build_op_subscript): Likewise. (build_new_op): Similarly for ARRAY_REF, just for arg1 rather than obj. * decl.cc (grok_op_properties): For C++20 and earlier, if operator[] is static member function, require exactly one parameter rather than exactly two parameters. * g++.dg/cpp23/static-operator-call4.C: New test. * g++.dg/cpp23/subscript10.C: New test. * g++.dg/cpp23/subscript11.C: New test. Jakub --- gcc/cp/call.cc.jj 2022-11-15 07:59:57.337231337 +0100 +++ gcc/cp/call.cc 2022-11-15 13:02:33.369531156 +0100 @@ -5137,7 +5137,24 @@ build_op_call (tree obj, vecfn) == FUNCTION_DECL && DECL_OVERLOADED_OPERATOR_P (cand->fn) && DECL_OVERLOADED_OPERATOR_IS (cand->fn, CALL_EXPR)) - result = build_over_call (cand, LOOKUP_NORMAL, complain); + { + result = build_over_call (cand, LOOKUP_NORMAL, complain); + /* In an expression of the form `a()' where cand->fn + which is operator() turns out to be a static member function, + `a' is none-the-less evaluated. */ + if (result != error_mark_node + && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE + && TREE_SIDE_EFFECTS (obj)) + { + /* But avoid the implicit lvalue-rvalue conversion when 'a' + is volatile. */ + tree a = obj; + if (TREE_THIS_VOLATILE (a)) + a = build_this (a); + if (TREE_SIDE_EFFECTS (a)) + result = build2 (COMPOUND_EXPR, TREE_TYPE (result), a, result); + } + } else { if (TREE_CODE (cand->fn) == FUNCTION_DECL) @@ -7046,6 +7063,24 @@ build_new_op (const op_location_t &loc, gcc_unreachable (); } } + + /* In an expression of the form `a[]' where cand->fn + which is operator[] turns out to be a static member function, + `a' is none-the-less evaluated. */ + if (code == ARRAY_REF + && result + && result != error_mark_node + && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE + && TREE_SIDE_EFFECTS (arg1)) + { + /* But avoid the implicit lvalue-rvalue conversion when 'a' + is volatile. */ + tree a = arg1; + if (TREE_THIS_VOLATILE (a)) + a = build_this (a); + if (TREE_SIDE_EFFECTS (a)) + result = build2 (COMPOUND_EXPR, TREE_TYPE (result), a, result); + } } else { @@ -7302,6 +7337,24 @@ build_op_subscript (const op_location_t /* Specify evaluation order as per P0145R2. */ CALL_EXPR_ORDERED_ARGS (call) = op_is_ordered (ARRAY_REF) == 1; } + + /* In an expression of the form `a[]' where cand->fn + which is operator[] turns out to be a static member function, + `a' is none-the-less evaluated. */ + if (result + && result != error_mark_node + && TREE_CODE (TREE_TYPE (cand->fn)) != METHOD_TYPE + && TREE_SIDE_EFFECTS (obj)) + { + /* But avoid the implicit lvalue-rvalue conversion when 'a' + is volatile. */ + tree a = obj; + if (TREE_THIS_VOLATILE (a)) + a = build_this (a); + if (TREE_SIDE_EFFECTS (a)) + result = build2 (COMPOUND_EXPR, TREE_TYPE (result), a, result); + } + } else gcc_unreachable (); --- gcc/cp/decl.cc.jj 2022-11-15 07:59:57.321231551 +0100 +++ gcc/cp/decl.cc 2022-11-15 13:13:51.814356965 +0100 @@ -15400,6 +15400,10 @@ grok_op_properties (tree decl, bool comp pedwarn (loc, OPT_Wc__23_extensions, "%qD may be a static member " "function only with %<-std=c++23%> or %<-std=gnu++23%>", decl); + if (operator_code == ARRAY_REF) + /* static operator[] should have exactly one argument + for C++20 and earlier, so that it isn't multidimensional. */ + op_flags = OVL_OP_FLAG_UNARY; } else if (DECL_STATIC_FUNCTION_P (decl)) { --- gcc/testsuite/g++.dg/cpp23/static-operator-call4.C.jj 2022-11-15 11:26:28.187511136 +0100 +++ gcc/testsuite/g++.dg/cpp23/static-operator-call4.C 2022-11-15 11:27:19.684814244 +0100 @@ -0,0 +1,37 @@ +// PR c++/107624 +// { dg-do run { target c++11 } } +// { dg-options "" } + +int n[3]; +struct S { + static void operator() (int x) { n[0] |= (1 << x); } // { dg-warning "may be a static member function only with" "" { target c++20_down } } + static void baz (int x) { n[1] |= (1 << x); } + int s; +}; +volatile S s[2]; + +S & +foo (int x) +{ + static S t; + n[2] |= (1 << x); + return t; +} + +int +main () +{ + int i = 0; + foo (0) (0); + if (n[0] != 1 || n[1] || n[2] != 1) + __builtin_abort (); + foo (1).baz (1); + if (n[0] != 1 || n[1] != 2 || n[2] != 3) + __builtin_abort (); + s[i++] (2); + if (i != 1 || n[0] != 5 || n[1] != 2 || n[2] != 3) + __builtin_abort (); + s[--i].baz (3); + if (i != 0 || n[0] != 5 || n[1] != 10 || n[2] != 3) + __builtin_abort (); +} --- gcc/testsuite/g++.dg/cpp23/subscript10.C.jj 2022-11-15 11:27:48.810420112 +0100 +++ gcc/testsuite/g++.dg/cpp23/subscript10.C 2022-11-15 13:07:02.974885454 +0100 @@ -0,0 +1,46 @@ +// PR c++/107624 +// { dg-do run { target c++11 } } +// { dg-options "" } + +int n[3]; +struct S { + static int &operator[] (int x) { n[0] |= (1 << x); return n[2]; } // { dg-warning "may be a static member function only with" "" { target c++20_down } } +#if __cpp_multidimensional_subscript >= 202211L + static int &operator[] () { n[0] |= (1 << 8); return n[2]; } + static int &operator[] (int y, int z, int w) { n[0] |= (1 << y) | (1 << z) | (1 << w); return n[2]; } +#endif + int s; +}; +volatile S s[2]; + +S & +foo (int x) +{ + static S t; + n[1] |= (1 << x); + return t; +} + +int +main () +{ + int i = 0; + foo (0) [0]++; + if (n[0] != 1 || n[1] != 1 || n[2] != 1) + __builtin_abort (); + s[i++][2]++; + if (i != 1 || n[0] != 5 || n[1] != 1 || n[2] != 2) + __builtin_abort (); +#if __cpp_multidimensional_subscript >= 202211L + foo (3) []++; + if (n[0] != 261 || n[1] != 9 || n[2] != 3) + __builtin_abort (); + int y = 10; + int z = 10; + int w = 13; + foo (4) [y++, ++z, w++]++; + if (n[0] != 11525 || n[1] != 25 || n[2] != 4 + || y != 11 || z != 11 || w != 14) + __builtin_abort (); +#endif +} --- gcc/testsuite/g++.dg/cpp23/subscript11.C.jj 2022-11-15 13:14:50.938557460 +0100 +++ gcc/testsuite/g++.dg/cpp23/subscript11.C 2022-11-15 13:19:20.681910074 +0100 @@ -0,0 +1,11 @@ +// { dg-do compile { target c++11 } } +// { dg-options "" } + +struct S { + static int &operator[] (int); // { dg-warning "may be a static member function only with" "" { target c++20_down } } + static int &operator[] (); // { dg-warning "may be a static member function only with" "" { target c++20_down } } + // { dg-error "'static int& S::operator\\\[\\\]\\\(\\\)' must have exactly one argument" "" { target c++20_down } .-1 } + static int &operator[] (int, int, int); // { dg-warning "may be a static member function only with" "" { target c++20_down } } + // { dg-error "'static int& S::operator\\\[\\\]\\\(int, int, int\\\)' must have exactly one argument" "" { target c++20_down } .-1 } + int s; +};