Skip to content

JS Backend: sum type doesn't work well with array types #24237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
gchumillas opened this issue Apr 15, 2025 · 1 comment · Fixed by #24259
Open

JS Backend: sum type doesn't work well with array types #24237

gchumillas opened this issue Apr 15, 2025 · 1 comment · Fixed by #24259
Labels
Bug This tag is applied to issues which reports bugs. Unit: JS Bugs/feature requests, that are related to the JavaScript backend.

Comments

@gchumillas
Copy link
Contributor

gchumillas commented Apr 15, 2025

Describe the bug

When compiling this code using v -b js_browser .:

type SumType = string | []string

fn main() {
	x := SumType('aaa')
	match x {
		string { println('string') }
		[]string { println('[]string') }
	}
}

throws the following error:

Uncaught SyntaxError: Unexpected identifier 'string' (at main.js:7185:27)

/** @type {main__SumType} */
const x = new string("aaa");
if (x instanceof string) {
	builtin__println (new string("string"));
}
else if (x instanceof []string) { // <!-- HERE!
	builtin__println (new string("[]string"));
}

Reproduction Steps

As above.

Expected Behavior

The sum types should work well with array types.

Current Behavior

It throws a JavaScript error.

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.10 d559a62

Environment details (OS name and version, etc.)

V full version V 0.4.10 30ececc.d559a62
OS macos, macOS, 15.3.2, 24D81
Processor 8 cpus, 64bit, little endian, Apple M1 Pro
Memory 2.16GB/16GB
V executable /Users/gonzalo/v/v
V last modified time 2025-04-13 10:51:40
V home dir OK, value: /Users/gonzalo/v
VMODULES OK, value: /Users/gonzalo/.vmodules
VTMP OK, value: /tmp/v_501
Current working dir OK, value: /Users/gonzalo/Projects/Personal/v/rx
Git version git version 2.39.1
V git status weekly.2025.14-39-gd559a62c (12 commit(s) behind V master)
.git/config present true
cc version Apple clang version 17.0.0 (clang-1700.0.13.3)
gcc version Apple clang version 17.0.0 (clang-1700.0.13.3)
clang version Apple clang version 17.0.0 (clang-1700.0.13.3)
tcc version tcc version 0.9.28rc 2024-02-05 HEAD@105d70f7 (AArch64 Darwin)
tcc git status thirdparty-macos-arm64 713692d4
emcc version N/A
glibc version N/A

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@gchumillas gchumillas added the Bug This tag is applied to issues which reports bugs. label Apr 15, 2025
Copy link

Connected to Huly®: V_0.6-22619

gchumillas added a commit to gchumillas/v that referenced this issue Apr 17, 2025
…24237 )

This commit fixes a bug in the JavaScript code generator
where array types in sum type match expressions were not
being properly checked. The issue was that the generated
code was checking if array elements were instances of the
array type itself, rather than the element type of the array.
gchumillas added a commit to gchumillas/v that referenced this issue Apr 17, 2025
…24237 )

This commit fixes a bug in the JavaScript code generator where array types in sum type match expressions were not being properly checked. The issue was that the generated code was checking if array elements were instances of the array type itself, rather than the element type of the array.
@huly-for-github huly-for-github bot reopened this Apr 18, 2025
@felipensp felipensp added the Unit: JS Bugs/feature requests, that are related to the JavaScript backend. label Apr 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Unit: JS Bugs/feature requests, that are related to the JavaScript backend.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants