https://gcc.gnu.org/g:6e10ec0cd82b7611b792e80eee2bd1f24b603d00

commit r16-9013-g6e10ec0cd82b7611b792e80eee2bd1f24b603d00
Author: Marek Polacek <[email protected]>
Date:   Thu May 28 13:43:58 2026 -0400

    c++: fix infinite looping with arr[arr] [PR125454]
    
    Here r16-3466 moved the canonicalization step that transforms
    idx[array] to array[idx] to the beginning of cp_build_array_ref.
    When we have array[array], we'll be swapping till we blow the stack.
    
    Previously, we'd give the !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
    error so there was no problem.
    
            PR c++/125454
    
    gcc/cp/ChangeLog:
    
            * typeck.cc (cp_build_array_ref): Don't recurse for array[array].
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/other/array8.C: New test.
    
    Reviewed-by: Jason Merrill <[email protected]>
    (cherry picked from commit a33b41d895f55fd95f8c9f486f6ef23c71a7a1e5)

Diff:
---
 gcc/cp/typeck.cc                    |  3 ++-
 gcc/testsuite/g++.dg/other/array8.C | 15 +++++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc
index dcc35ae28a24..f6b8b26c542c 100644
--- a/gcc/cp/typeck.cc
+++ b/gcc/cp/typeck.cc
@@ -4162,7 +4162,8 @@ cp_build_array_ref (location_t loc, tree array, tree idx,
     return error_mark_node;
 
   /* 0[array] */
-  if (TREE_CODE (TREE_TYPE (idx)) == ARRAY_TYPE)
+  if (TREE_CODE (TREE_TYPE (idx)) == ARRAY_TYPE
+      && TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE)
     {
       std::swap (array, idx);
 
diff --git a/gcc/testsuite/g++.dg/other/array8.C 
b/gcc/testsuite/g++.dg/other/array8.C
new file mode 100644
index 000000000000..d238e3ba5898
--- /dev/null
+++ b/gcc/testsuite/g++.dg/other/array8.C
@@ -0,0 +1,15 @@
+// PR c++/125454
+// { dg-do compile { target c++11 } }
+
+template <typename T>
+void
+foo()
+{
+  T a = a[a]; // { dg-error "array subscript is not an integer" }
+}
+
+void
+bar ()
+{
+  foo<int[]>;
+}

Reply via email to