Skip to content

Variables that captured by closure is dirtied on different context #16635

@ChAoSUnItY

Description

@ChAoSUnItY

Describe the bug

Closure data should not be dirtied by other process, functions, or closures, but currently, it would be dirtied based on context.

Expected Behavior

as described.

Current Behavior

as described.

Reproduction Steps

Consider the code:

import arrays { flat_map, flat_map_indexed, map_indexed, max, fold }

fn main() {
	data := [[3, 0, 3, 7, 3], [2, 5, 5, 1, 2], [6, 5, 3, 3, 2], [3, 3, 5, 4, 9], [3, 5, 3, 9, 0]]

	println(part1(data))
	println(part2(data)!)
}

fn part1(data [][]int) int {
	return flat_map_indexed[[]int, bool](data, fn [data] (y int, row []int) []bool {
		return map_indexed(row, fn [data, y] (x int, height int) bool {
			return map_indexed(grid_cross_walk(data.len, x, y), fn [data, x, y, height] (i int, grid_walk []int) bool {
				return grid_walk.map(fn [data, x, y, i] (pos int) int {
					dump(i)
					return if i < 2 {
						data[y][pos]
					} else {
						data[pos][x]
					}
				}).all(it < height)
			}).any(it)
		}).filter(it)
	}).len
}

fn part2(data [][]int) !int {
	return max(flat_map_indexed[[]int, int](data, fn [data] (y int, row []int) []int {
		return map_indexed(row, fn [data, y] (x int, height int) int {
			return fold(map_indexed(grid_cross_walk(data.len, x, y), fn [data, x, y, height] (i int, grid_walk []int) int {
				mut count := 0
				heights := grid_walk.map(fn [data, x, y, i] (pos int) int {
					dump(i)
					return if i < 2 {
						data[y][pos]
					} else {
						data[pos][x]
					}
				})

				for h in heights {
					count++
					if h >= height {
						break
					}
				}

				return count
			}), 1, fn (acc int, elem int) int { 
				return acc * elem
			})
		})
	}))!
}

fn range(size int, start int) [][]int {
	return [[]int{len: start, init: start - it - 1}, []int{len: size - start - 1, init: start + 1 + it}]
}

fn grid_cross_walk(size int, start_x int, start_y int) [][]int {
	return flat_map[int, []int]([start_x, start_y], fn [size] (elem int) [][]int {
		return range(size, elem)
	})
}

We only care about captured variable i in both functions part1 and part2.

After running it, you would see captured variable i being dumped. The issue can be seen clearly which is the variable i somehow exceeded actual possible values for it (variable i should always in [0,4)).

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.3.2 42a9eaa

Environment details (OS name and version, etc.)

OS: windows, Microsoft Windows 11 Home v22000 64-bit Processor: 20 cpus, 64bit, little endian, 12th Gen Intel(R) Core(TM) i7-12700 CC version: Error: exec failed (CreateProcess) with code 2: 系統找不到指定的檔案。 cmd: cc --version getwd: C:\Users\chaos\projects\advent-of-code-2022-rs\2022 vmodules: C:\Users\chaos.vmodules vroot: C:\Users\chaos\projects\v vexe: C:\Users\chaos\projects\v\v.exe vexe mtime: 2022-12-10 12:22:33 is vroot writable: true is vmodules writable: true V full version: V 0.3.2 73675dc.42a9eaa Git version: git version 2.35.1.windows.2 Git vroot status: weekly.2022.40-427-g42a9eaac .git/config present: true thirdparty/tcc status: thirdparty-windows-amd64 cb89a4fe

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugThis tag is applied to issues which reports bugs.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions