From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2122) id B21983858C60; Wed, 24 Jan 2024 19:45:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B21983858C60 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1706125506; bh=W7QWRHmtjbiN5f5duffH5F3otr+sHsx2k6mYONFrx2Y=; h=From:To:Subject:Date:From; b=hXCRqjBighQ2tloDS2l0xKHxIT9JTuskv8eKW/5Pa3NGYRBEfHuJViKEa8jJiRTnG gd1qBrhH15EDRMFIWzqtuYmevXajGfPVtsrkdNMZ3ArJx9ZyUFgHOG4e4iFeQzbIG1 tuFY89cSibaodJHvzo11WEiZDZS4mDVaxHCkFKVw= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jason Merrill To: gcc-cvs@gcc.gnu.org Subject: [gcc r11-11208] c++: xvalue array subscript [PR103185] X-Act-Checkin: gcc X-Git-Author: Jason Merrill X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: 8fbd570006f5514a5bbdc0bf7d67afde16258322 X-Git-Newrev: 89e4f3304ad45d2251bfaf66d2a355cebf87310b Message-Id: <20240124194506.B21983858C60@sourceware.org> Date: Wed, 24 Jan 2024 19:45:06 +0000 (GMT) List-Id: https://gcc.gnu.org/g:89e4f3304ad45d2251bfaf66d2a355cebf87310b commit r11-11208-g89e4f3304ad45d2251bfaf66d2a355cebf87310b Author: Jason Merrill Date: Tue Dec 19 16:12:02 2023 -0500 c++: xvalue array subscript [PR103185] 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.c (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. (cherry picked from commit 8dfc52a75d4d6c8be1c61b4aa831b1812b14a10e) Diff: --- gcc/cp/typeck.c | 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(-) diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 61e3222c6c6..26c65f72eb5 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -3563,13 +3563,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) @@ -3614,6 +3615,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; @@ -3689,15 +3698,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);