From 52e2161ea73bf96664ebaf7aa9734225eed44eb0 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Tue, 7 Jul 2026 08:51:53 +0200 Subject: [PATCH] Add regression test for duplicate array-element assignment A second store to an already-written array element must prevent HandleSimpleArrayInitializer from folding the stores into a collection initializer: the earlier store would be dropped (a visible change when its value has side effects), and without the nextMinimumIndex guard the transform crashes on the colliding index. The nextMinimumIndex check already enforces this; pin it with a Pretty fixture so the boundary between a foldable initializer and a duplicate-write sequence stays covered. Assisted-by: Claude:claude-fable-5:Claude Code --- .../TestCases/Pretty/InitializerTests.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.cs index 1a6709851..c59c83add 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.cs @@ -515,6 +515,19 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty.InitializerTests X(Y(), new int[5] { a, 0, b, 0, c }); } + public static int[] DuplicateElementAssignment() + { + // A second store to an element that was already written must stop the array-initializer + // transform from folding these stores into a 'new int[] { ... }'. Doing so would drop the + // earlier store, which is a visible change when its value has side effects. + int[] array = new int[3]; + array[0] = 1; + array[0] = 2; + array[1] = 3; + array[2] = 4; + return array; + } + public static void NestedArray(int a, int b, int c) { X(Y(), new int[3][] {