From patchwork Thu Jan 19 08:52:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 45647 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp216308wrn; Thu, 19 Jan 2023 00:53:25 -0800 (PST) X-Google-Smtp-Source: AMrXdXubFN9JsnXOfP13aUOFiQut0dx6txm+t5KL30RX3TKahgiAYVScbBL1t9eZLAm96kQaxR8b X-Received: by 2002:a05:6402:32b:b0:49e:ef4:51c3 with SMTP id q11-20020a056402032b00b0049e0ef451c3mr636808edw.16.1674118405086; Thu, 19 Jan 2023 00:53:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674118405; cv=none; d=google.com; s=arc-20160816; b=r8vUnRG1dMmGqIms4EUrA93qO1Xc4N0q6I+lfQaeA3ntBwXYywUdrPgze4XcmRc6rA a+sXrwQnPEAYn/qQn9tWGDVfAV3IFguStuiMC6ExbX6yoySH+qUdF0aVSK4TAEgqXoyH RZFTqrssQUswmb3h+r6vuT5B7Xjx+eu12vR8tungZ52oGrnnVB2ht/kWXp/PtHaRC5+Q qeC/KLAAD+tKB0/cyJsOB6VKdIf1QlLr2p273lgDSyesCgfphk64yPF1EglbaAUow/tN fXCLjHu3Zn64UxWh54+BiGTgwJHKx+KcebjNKA9GE0Vwznl7oZRUJRm4c6dyh6MGxjjs lh9Q== 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:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=vzYtDjXaAetKKqP2wlevwJyXkIlaFkAzfxSW3Lxfvlo=; b=CzmmpA/9nVcAU0LNz09euakJJRk3GwPhy5asebgQnObftlkcvTgbTL6dKpkMbt5Z2N j11awMOg6AC2HAq33SkFYNvqPAjGMfQM6C8EDFRO+P6YsDfFybvmmizx+L/D4+e1pxPe yD6eGUHaft/0RXzXyoiPNtR4sZq7h8F2o8GawV3R9Hr2VvOLKEhcvRgv82H0MoZrRcmr h0L8ZW5EeW/+0DFcj0Sw4WjXnaN42NVcIp3yRx3tiHUy0SudHHzB0WZoAqlxxWnEBYst 1n69obPP33F47Fe0ngX9krNO2wM0OAn0vkYK6YDrpnIsGqy9KCfeEAvISTCKZR2fcqMF 2rlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=yqmtzhy0; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id rh16-20020a17090720f000b00877571a4f49si3899001ejb.794.2023.01.19.00.53.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Jan 2023 00:53:25 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=yqmtzhy0; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 244323858002 for ; Thu, 19 Jan 2023 08:53:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 244323858002 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674118404; bh=vzYtDjXaAetKKqP2wlevwJyXkIlaFkAzfxSW3Lxfvlo=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=yqmtzhy0j/ud4RNzN7DPDEptBg30snZ2qkgdr2kVvUKqEMl3wPdrQz6KrKFACFQ8C VHlShC+TJVElryrty+mOOS8f0leU/C+Bud23x9T/uORwob9aRtry5iO2+3ETtcYWiE ANDKyE6tY6QnVfmOatAvDKt/trnaigtv/VMB8VGg= 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 C8C1D3858D28 for ; Thu, 19 Jan 2023 08:52:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C8C1D3858D28 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-48-v6SIA51eOvKfblu9Fsjj0w-1; Thu, 19 Jan 2023 03:52:32 -0500 X-MC-Unique: v6SIA51eOvKfblu9Fsjj0w-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B757A800B30 for ; Thu, 19 Jan 2023 08:52:31 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.223]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7365E51EF; Thu, 19 Jan 2023 08:52:31 +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 30J8qSMR1232138 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 19 Jan 2023 09:52:29 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 30J8qS3N1232137; Thu, 19 Jan 2023 09:52:28 +0100 Date: Thu, 19 Jan 2023 09:52:27 +0100 To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Fix up handling of non-dependent subscript with static operator[] [PR108437] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.5 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?1755440380685351257?= X-GMAIL-MSGID: =?utf-8?q?1755440380685351257?= Hi! As the following testcases shows, when adding static operator[] support I've missed that the 2 build_min_non_dep_op_overload functions need to be adjusted. The first one we only use for the single index case, but as cp_tree_code_length (ARRAY_REF) is 2, we were running into an assertion there which compared nargs and expected_nargs. For ARRAY_REF, the operator[] is either a non-static member or newly static member, never out of class and for the static member case if user uses single index the operator[] needs to have a single argument as well, but the function is called with 2 - the object it is invoked on and the index. We need to evaluate side-effects of the object and use just a single argument in the call - the index. The other build_min_non_dep_op_overload overload has been added solely for ARRAY_REF - CALL_EXPR is the other operator that accepts variable number of operands but that one goes through different routines. There we asserted it is a METHOD_TYPE, so again we shouldn't assert that but handle the case when it is not one by making sure object's side-effects are evaluated if needed and passing all the index arguments to the static operator[]. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-01-19 Jakub Jelinek PR c++/108437 * cp-tree.h (keep_unused_object_arg): Declare. * call.cc (keep_unused_object_arg): No longer static. * tree.cc (build_min_non_dep_op_overload): Handle ARRAY_REF with overload being static member function. * g++.dg/cpp23/subscript12.C: New test. * g++.dg/cpp23/subscript13.C: New test. Jakub --- gcc/cp/cp-tree.h.jj 2023-01-16 11:52:16.067734300 +0100 +++ gcc/cp/cp-tree.h 2023-01-18 16:47:55.258697839 +0100 @@ -6599,6 +6599,7 @@ inline tree build_new_op (const op_locat return build_new_op (loc, code, flags, arg1, arg2, NULL_TREE, NULL_TREE, NULL, complain); } +extern tree keep_unused_object_arg (tree, tree, tree); extern tree build_op_call (tree, vec **, tsubst_flags_t); extern tree build_op_subscript (const op_location_t &, tree, --- gcc/cp/call.cc.jj 2023-01-16 11:52:16.059734418 +0100 +++ gcc/cp/call.cc 2023-01-18 16:48:02.966586328 +0100 @@ -5187,7 +5187,7 @@ build_operator_new_call (tree fnname, ve or static operator(), in which cases the source expression would be `obj[...]' or `obj(...)'. */ -static tree +tree keep_unused_object_arg (tree result, tree obj, tree fn) { if (result == NULL_TREE --- gcc/cp/tree.cc.jj 2023-01-16 11:52:16.093733917 +0100 +++ gcc/cp/tree.cc 2023-01-18 17:01:08.937242864 +0100 @@ -3693,14 +3693,14 @@ build_min_non_dep_op_overload (enum tree { va_list p; int nargs, expected_nargs; - tree fn, call; + tree fn, call, obj = NULL_TREE; non_dep = extract_call_expr (non_dep); nargs = call_expr_nargs (non_dep); expected_nargs = cp_tree_code_length (op); - if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE) + if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE || op == ARRAY_REF) expected_nargs -= 1; if ((op == POSTINCREMENT_EXPR || op == POSTDECREMENT_EXPR) @@ -3715,6 +3715,8 @@ build_min_non_dep_op_overload (enum tree if (TREE_CODE (TREE_TYPE (overload)) == FUNCTION_TYPE) { fn = overload; + if (op == ARRAY_REF) + obj = va_arg (p, tree); for (int i = 0; i < nargs; i++) { tree arg = va_arg (p, tree); @@ -3746,6 +3748,8 @@ build_min_non_dep_op_overload (enum tree CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep); CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep); + if (obj) + return keep_unused_object_arg (call, obj, overload); return call; } @@ -3759,11 +3763,15 @@ build_min_non_dep_op_overload (tree non_ non_dep = extract_call_expr (non_dep); unsigned int nargs = call_expr_nargs (non_dep); - gcc_assert (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE); - tree binfo = TYPE_BINFO (TREE_TYPE (object)); - tree method = build_baselink (binfo, binfo, overload, NULL_TREE); - tree fn = build_min (COMPONENT_REF, TREE_TYPE (overload), - object, method, NULL_TREE); + tree fn = overload; + if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE) + { + tree binfo = TYPE_BINFO (TREE_TYPE (object)); + tree method = build_baselink (binfo, binfo, overload, NULL_TREE); + fn = build_min (COMPONENT_REF, TREE_TYPE (overload), + object, method, NULL_TREE); + object = NULL_TREE; + } gcc_assert (vec_safe_length (args) == nargs); tree call = build_min_non_dep_call_vec (non_dep, fn, args); @@ -3774,6 +3782,8 @@ build_min_non_dep_op_overload (tree non_ CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep); CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep); + if (object) + return keep_unused_object_arg (call, object, overload); return call; } --- gcc/testsuite/g++.dg/cpp23/subscript12.C.jj 2023-01-18 17:15:04.963153521 +0100 +++ gcc/testsuite/g++.dg/cpp23/subscript12.C 2023-01-18 17:14:40.816503156 +0100 @@ -0,0 +1,34 @@ +// PR c++/108437 +// { dg-do run { target c++23 } } + +struct S { static int &operator[] (int x) { static int a[2]; return a[x]; } }; +struct U { static int &operator[] (int x, int y, int z) { static int a[2]; return a[x + y + z]; } }; +struct V { static int &operator[] () { static int a; return a; } }; + +template void +foo () +{ + S s; + s[0]++; + T t; + t[0]++; + U u; + u[0, 0, 0]++; + V v; + v[]++; + W w; + w[0, 0, 0]++; + X x; + x[]++; +} + +int +main () +{ + S::operator[] (0) = 1; + U::operator[] (0, 0, 0) = 2; + V::operator[] () = 3; + foo (); + if (S::operator[] (0) != 3 || U::operator[] (0, 0, 0) != 4 || V::operator[] () != 5) + __builtin_abort (); +} --- gcc/testsuite/g++.dg/cpp23/subscript13.C.jj 2023-01-18 17:15:08.110107955 +0100 +++ gcc/testsuite/g++.dg/cpp23/subscript13.C 2023-01-18 17:14:55.833285718 +0100 @@ -0,0 +1,43 @@ +// PR c++/108437 +// { dg-do run { target c++23 } } + +struct S { static int &operator[] (int x) { static int a[2]; return a[x]; } }; +struct U { static int &operator[] (int x, int y, int z) { static int a[2]; return a[x + y + z]; } }; +struct V { static int &operator[] () { static int a; return a; } }; +int cnt; + +template +T & +bar (T &x) +{ + ++cnt; + return x; +} + +template void +foo () +{ + S s; + bar (s)[0]++; + T t; + bar (t)[0]++; + U u; + bar (u)[0, 0, 0]++; + V v; + bar (v)[]++; + W w; + bar (w)[0, 0, 0]++; + X x; + bar (x)[]++; +} + +int +main () +{ + S::operator[] (0) = 1; + U::operator[] (0, 0, 0) = 2; + V::operator[] () = 3; + foo (); + if (S::operator[] (0) != 3 || U::operator[] (0, 0, 0) != 4 || V::operator[] () != 5 || cnt != 6) + __builtin_abort (); +}