From 68cf7aba36c2a926caf58d29d1e544ca1a8a3b14 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 25 Mar 2026 16:42:16 +0300 Subject: [PATCH] checker: fix inline array index assignment with literals (fixes #9691) --- .../assign/inline_array_index_assign_test.v | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 vlib/v/tests/assign/inline_array_index_assign_test.v diff --git a/vlib/v/tests/assign/inline_array_index_assign_test.v b/vlib/v/tests/assign/inline_array_index_assign_test.v new file mode 100644 index 000000000..8af1cd1e2 --- /dev/null +++ b/vlib/v/tests/assign/inline_array_index_assign_test.v @@ -0,0 +1,70 @@ +fn test_inline_array_index_assign_uses_full_expression() { + mut ram := []u8{len: 0x10000} + rom := [u8(0x00), 0x12, 0x00, 0x00] + pc := 0 + value := u8(0x0f) + + address := u16(rom[pc + 2]) << 8 | u16(rom[pc + 1]) + ram[address] = value + assert ram[0x0012] == value + assert ram[0] == 0 + + mut ram2 := []u8{len: 0x10000} + ram2[u16(rom[pc + 2]) << 8 | u16(rom[pc + 1])] = value + assert ram2[0x0012] == value + assert ram2[0] == 0 +} + +fn test_inline_fixed_array_index_assign_uses_full_expression() { + mut ram := [0x10000]u8{} + rom := [u8(0x00), 0x12, 0x00, 0x00]! + pc := 0 + value := u8(0x0f) + + address := u16(rom[pc + 2]) << 8 | u16(rom[pc + 1]) + ram[address] = value + assert ram[0x0012] == value + assert ram[0] == 0 + + mut ram2 := [0x10000]u8{} + ram2[u16(rom[pc + 2]) << 8 | u16(rom[pc + 1])] = value + assert ram2[0x0012] == value + assert ram2[0] == 0 +} + +struct MemoryState { +mut: + a u8 + pc int + ram [0x10000]u8 + rom [4]u8 +} + +fn (mut state MemoryState) write_with_temp() { + address := u16(state.rom[state.pc + 2]) << 8 | u16(state.rom[state.pc + 1]) + state.ram[address] = state.a +} + +fn (mut state MemoryState) write_inline() { + state.ram[u16(state.rom[state.pc + 2]) << 8 | u16(state.rom[state.pc + 1])] = state.a +} + +fn test_inline_struct_fixed_array_index_assign_uses_full_expression() { + mut temp_state := MemoryState{ + a: 0x0f + pc: 0 + rom: [u8(0x00), 0x12, 0x00, 0x00]! + } + temp_state.write_with_temp() + assert temp_state.ram[0x0012] == temp_state.a + assert temp_state.ram[0] == 0 + + mut inline_state := MemoryState{ + a: 0x0f + pc: 0 + rom: [u8(0x00), 0x12, 0x00, 0x00]! + } + inline_state.write_inline() + assert inline_state.ram[0x0012] == inline_state.a + assert inline_state.ram[0] == 0 +} -- 2.39.5