Skip to content

Mutable sum types passed to a function can't be modified #23982

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
jorgeluismireles opened this issue Mar 19, 2025 · 3 comments · Fixed by #23988
Open

Mutable sum types passed to a function can't be modified #23982

jorgeluismireles opened this issue Mar 19, 2025 · 3 comments · Fixed by #23988
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@jorgeluismireles
Copy link

jorgeluismireles commented Mar 19, 2025

Describe the bug

We pass mutable sum types to a function to update the types fields and cannot be modified.

Reproduction Steps

struct Int { mut: i int }

struct String { mut: s string }

type Sum = Int | String

fn init(mut s Sum) {
	match mut s {
		Int    { s.i = 0 }
		String { s.s = '' }
	}
}

fn init_int(mut i Int) {
	i.i = 0
}

fn main() {
	mut i := Int{ i: 333 }
	mut s := String{ s: 'string' }
	init(mut i)
	init(mut s)
	println('${i.i}') // prints 333, should be 0
	println('${s.s}') // prints 'string', should be ''
	init_int(mut i)
	println('${i.i}') // prints 0 as expected
}

Function init above tries to initialize every sum type received but can't do it. Function init_int works.

Expected Behavior

Mutable fields passed to init function should be set to zero for integer sum type and empty for string sum type.

Current Behavior

Mutable fields passed to init function are not set to 0 or empty string.

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.9 86bb2d7

Environment details (OS name and version, etc.)

https://play.vlang.io/p/cfbb7907ac

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.

@jorgeluismireles jorgeluismireles added the Bug This tag is applied to issues which reports bugs. label Mar 19, 2025
Copy link

Connected to Huly®: V_0.6-22377

@felipensp felipensp added Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. labels Mar 20, 2025
@felipensp
Copy link
Member

Good catch. 👍🏻

@felipensp felipensp self-assigned this Mar 20, 2025
@spytheman
Copy link
Member

@jorgeluismireles I'll merge the PR with the fix, but I'll leave the issue opened, since we need to bootstrap the compiler first.
After a few more PRs, I will close the issue

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. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants