v icon indicating copy to clipboard operation
v copied to clipboard

Typecasting causes? Invalid memory access when trying to directly `match` or `if <> is` check.

Open ttytm opened this issue 2 years ago • 0 comments

*Smartcasting causes?

Describe the bug

Direct match arms will throw invalid memory access in some scenarios. Calling the same behavior in else works on the other hand. I assumed there is an issue with smart casting, though I'm not certain if it's the root of this issue.

module main

import term

type Color = MultiColor | OneColor

type OneColor = fn (msg string) string

struct MultiColor {
	primary   OneColor
	secondary OneColor
}

fn colorize(color Color) {
	match color {
		MultiColor {}
		OneColor { paint_uni(color) } // --> Invalid memory access
		// else { paint_uni(color as OneColor) } // --> Works: matching over `else` instead of `OneColor`.
	}

	// Same for if <> is
	// Matching doesn't work.
	// if color is OneColor {
	// 	paint_uni(color)
	// }

	// Works when negating others.
	// if color !is MultiColor {
	// 	paint_uni(color as OneColor)
	// }
}

fn paint_uni(color OneColor) {
	// If not using `else` it gets until here, but then fails with Invalid memory access.
	println(term.colorize(color, 'Message'))
}

fn main() {
	color := term.red
	colorize(color)
}

Sorry, that this is not the optimal code for a test case in a potential fix. But this was what I had at hand in the given time to create an isolated and reproducible scenario. I hope this is at least a start.

Expected Behavior

Direct match wroks.

Current Behavior

Invalid memory access.

Reproduction Steps

See example above.

Possible Solution

No response

Additional Information/Context

No response

V version

0.3.3

Environment details (OS name and version, etc.)

linux, arch

ttytm avatar Mar 27 '23 18:03 ttytm