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 257B83858D1E for ; Wed, 20 Dec 2023 17:31:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 257B83858D1E 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 257B83858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703093473; cv=none; b=HJFLr/6r6iM2Wh9ryGuuwcG940BHXJLRo8WdfVvMZZ0aXxEDu6OrEQ7Nzp10d4+X1M6rbFIe7RvrUF0zhQiOblAXuXZmRyt/o/GYAIiZfMhVS5VYjBMxehieEC75o9Djvnyt8e2ZMLSzWzFImqEzCoZp930azMQq10GXYWt+OzA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703093473; c=relaxed/simple; bh=KoXErVP4nEyaStnK7YOHP6aenAMgdGLiFU+eExeZi8Y=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=J4kgdQ7D0uxHaHpcnB0KIbYHYXzHhY/MWtHghIxfzkZ14pMuhnZZ120rKafv6zqHAgLtJjhwGWMRnHKeU/Mn/pgIyLUx4+X9KIsKjsq+gW+lQoleFs7nadNMDoEQNdrFTRMPCec6eyYr2d2kYvFA0GB5PpQt9vC+kjaVR5HQIfY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1703093469; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=wU1Hh0OfYUMWA3RXDad7yc1ti1M6hvAbbRV8/c7ShK4=; b=BvFU4sUD4u8rionpXmEpm/P0lVH+2HZXjRyd6ILHu7Travb9qP94Rv3OLeWns6ZcyuK+zb NE8EyzQBFigefWS7HeEdoCBkuC7VAKU95RFBy6jlBO+c6fnK/N1D+zNremdTwUSN+6A050 L4BzqUmOJw7auHBEW0TZUT1jsOcoGoA= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-107-eDeCp6QYOKSwhy9UXtjI-Q-1; Wed, 20 Dec 2023 12:31:08 -0500 X-MC-Unique: eDeCp6QYOKSwhy9UXtjI-Q-1 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-67f3d90abfdso54758256d6.2 for ; Wed, 20 Dec 2023 09:31:07 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703093467; x=1703698267; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=wU1Hh0OfYUMWA3RXDad7yc1ti1M6hvAbbRV8/c7ShK4=; b=feBA+d4iD99vzlNhQQOaLFb5wTfE0BADZ365LoJfY6jtY1AUVJ9r9Et9Z8KnZk8Ff2 uCVeZrEKHNQ8Rou8dpX8tvQ92Rjmlf2Yq4jGj4cR7s9Ds0Q7ajsj81nJ5u6vPi1QKpS8 yDplasSUssl2Y3JUtu+sRVKou0kIExjomr1oZH2I/CFAs5WaKqWJ5o5OLR53/Ds3ERmX eKPNLiF9qcXCl0t1QnRi77olSH1yRAe8P1nH80jXyobdmWzqz03jPJMgVtN5BM9X4w3o lzJwsrnZ6zAifmT1iE5AHLhFRBPtnwwX8U1rt/ejQlMRnyeTwmCmDYbMuPn3TPJZoU44 z/1g== X-Gm-Message-State: AOJu0YymuKo3wf+2Ce+C68J5C54U1dR8gVRx3xuXcVrpbjREJMv95Skt fChTQ+RD64IlYrU1Ei/lTjLka/PxG3u6lkzxM75BM1hgtIlDttA/Fc4tHe30nlrnyz4YLs3foaB NH0ZIELvOG/IZvKXSZw02hKomZODUP2y7cxgkGLoeMRyWSIR9U/ijrtR0wOQEZgBBSTIc7L5X2w == X-Received: by 2002:a05:6214:da2:b0:67f:30d4:44cb with SMTP id h2-20020a0562140da200b0067f30d444cbmr10243238qvh.8.1703093467091; Wed, 20 Dec 2023 09:31:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IEL6qcfdBn70QJHfHFrun8GihPNvHZKWllcGfy5zbbjg0/2GypqZOwuwhAb52acNmlms59z+Q== X-Received: by 2002:a05:6214:da2:b0:67f:30d4:44cb with SMTP id h2-20020a0562140da200b0067f30d444cbmr10243218qvh.8.1703093466674; Wed, 20 Dec 2023 09:31:06 -0800 (PST) Received: from jason.com (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id bu6-20020ad455e6000000b0067f2a72f68bsm39072qvb.146.2023.12.20.09.31.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 09:31:06 -0800 (PST) From: Jason Merrill To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: xvalue array subscript [PR103185] Date: Wed, 20 Dec 2023 12:31:04 -0500 Message-Id: <20231220173104.3105138-1-jason@redhat.com> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-11.5 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_H4,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: Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- Normally we handle xvalue array subscripting with ARRAY_REF, but in this case we weren't doing that because the operands were reversed. Handle that case better. PR c++/103185 gcc/cp/ChangeLog: * typeck.cc (cp_build_array_ref): Handle swapped operands. gcc/testsuite/ChangeLog: * g++.dg/cpp1z/array-prvalue2.C: New test. * g++.dg/cpp1z/eval-order3.C: Test swapped operands. --- gcc/cp/typeck.cc | 16 +++++++++++++--- gcc/testsuite/g++.dg/cpp1z/array-prvalue2.C | 5 +++++ gcc/testsuite/g++.dg/cpp1z/eval-order3.C | 5 +++++ 3 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/array-prvalue2.C diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index a6e2f4ee7da..1fe30fc7c48 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -3932,13 +3932,14 @@ cp_build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring, If INDEX is of some user-defined type, it must be converted to integer type. Otherwise, to make a compatible PLUS_EXPR, it will inherit the type of the array, which will be some pointer type. - + LOC is the location to use in building the array reference. */ tree cp_build_array_ref (location_t loc, tree array, tree idx, tsubst_flags_t complain) { + tree first = NULL_TREE; tree ret; if (idx == 0) @@ -3983,6 +3984,14 @@ cp_build_array_ref (location_t loc, tree array, tree idx, bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx); + /* 0[array] */ + if (TREE_CODE (TREE_TYPE (idx)) == ARRAY_TYPE) + { + std::swap (array, idx); + if (flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (array)) + idx = first = save_expr (idx); + } + if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE) { tree rval, type; @@ -4058,15 +4067,16 @@ cp_build_array_ref (location_t loc, tree array, tree idx, protected_set_expr_location (ret, loc); if (non_lvalue) ret = non_lvalue_loc (loc, ret); + if (first) + ret = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (ret), first, ret); return ret; } { tree ar = cp_default_conversion (array, complain); tree ind = cp_default_conversion (idx, complain); - tree first = NULL_TREE; - if (flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (ind)) + if (!first && flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (ind)) ar = first = save_expr (ar); /* Put the integer in IND to simplify error checking. */ diff --git a/gcc/testsuite/g++.dg/cpp1z/array-prvalue2.C b/gcc/testsuite/g++.dg/cpp1z/array-prvalue2.C new file mode 100644 index 00000000000..60a038cfd16 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/array-prvalue2.C @@ -0,0 +1,5 @@ +// PR c++/103185 +// { dg-do compile { target c++11 } } + +using intarr = int[]; +static_assert(__is_same(decltype(0[intarr{0}]), int&&), ""); diff --git a/gcc/testsuite/g++.dg/cpp1z/eval-order3.C b/gcc/testsuite/g++.dg/cpp1z/eval-order3.C index 5773591d49f..897cdef7483 100644 --- a/gcc/testsuite/g++.dg/cpp1z/eval-order3.C +++ b/gcc/testsuite/g++.dg/cpp1z/eval-order3.C @@ -166,6 +166,11 @@ void g() ip(24)[f(25)-25] = 0; last=0; + // even with swapped operands + (f(20)-20)[afn(21)].memfn(f(22),23); + (f(24)-24)[ip(25)] = 0; + last=0; + // a << b aref(24) << f(25); iref(26) << f(27); base-commit: a3ff76278efe006dc0b50249c8e5baf565bff56b -- 2.39.3