Hi! No idea how this slipped in, I'm terribly sorry. Strangely nothing in the testsuite has caught this, so I've added a new test for that.
Bootstrapped/regtested on x86_64-linux and i686-linux, committed to trunk and for 15.2, 14.4, 13.4 and 12.5. 2025-07-03 Jakub Jelinek <ja...@redhat.com> PR c++/120940 * typeck.cc (cp_build_array_ref): Fix a pasto. * g++.dg/parse/pr120940.C: New test. * g++.dg/warn/Wduplicated-branches9.C: New test. --- gcc/cp/typeck.cc.jj 2025-07-03 12:44:48.361162801 +0200 +++ gcc/cp/typeck.cc 2025-07-03 19:32:04.155912353 +0200 @@ -4004,7 +4004,7 @@ cp_build_array_ref (location_t loc, tree tree op0, op1, op2; op0 = TREE_OPERAND (array, 0); op1 = TREE_OPERAND (array, 1); - op2 = TREE_OPERAND (array, 1); + op2 = TREE_OPERAND (array, 2); if (TREE_SIDE_EFFECTS (idx) || !tree_invariant_p (idx)) { /* If idx could possibly have some SAVE_EXPRs, turning --- gcc/testsuite/g++.dg/parse/pr120940.C.jj 2025-07-03 19:39:26.808149189 +0200 +++ gcc/testsuite/g++.dg/parse/pr120940.C 2025-07-03 19:42:27.499903370 +0200 @@ -0,0 +1,18 @@ +// PR c++/120940 +// { dg-do run } + +int a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; +int b[8] = { 9, 10, 11, 12, 13, 14, 15, 16 }; + +__attribute__((noipa)) int +foo (int x, int y) +{ + return (x ? a : b)[y]; +} + +int +main () +{ + if (foo (1, 4) != 5 || foo (0, 6) != 15) + __builtin_abort (); +} --- gcc/testsuite/g++.dg/warn/Wduplicated-branches9.C.jj 2025-07-03 19:35:53.383915748 +0200 +++ gcc/testsuite/g++.dg/warn/Wduplicated-branches9.C 2025-07-03 19:35:47.132997460 +0200 @@ -0,0 +1,11 @@ +// PR c++/120940 +// { dg-do compile } +// { dg-options "-Wduplicated-branches" } + +static char a[16][8], b[16][8]; + +char * +foo (int x, int y) +{ + return (x ? a : b)[y]; +} Jakub