Skip to content
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

Enum values set with constants calculated with $if/$else can result in duplicated values #23348

Open
jorgeluismireles opened this issue Jan 2, 2025 · 1 comment
Labels
Bug This tag is applied to issues which reports bugs. Unit: Checker Bugs/feature requests, that are related to the type checker.

Comments

@jorgeluismireles
Copy link

jorgeluismireles commented Jan 2, 2025

Describe the bug

Two constants are calculated with $if/$else. The constants are used to set the values of two enum elements. After $if/$else the values can be equal and the enum consistency is not checked for repetition.

Reproduction Steps

const x = $if macos { 1 } $else { 2 }
const y = $if macos { 3 } $else { 2 }

enum Foo {
	foo = x
	bar = y
}

fn main() {
	println('${Foo.foo:d}') // prints 2 in linux
	println('${Foo.bar:d}') // prints 2 in linux
}

Expected Behavior

Prevent duplicated values in the enum.

Current Behavior

Enum can get duplicated values which by definition is invalid.

Possible Solution

No response

Additional Information/Context

Previously mentioned in: #23336

V version

V 0.4.9 c50d4ee

Environment details (OS name and version, etc.)

https://play.vlang.io/p/028f55a768

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.

Huly®: V_0.6-21779

@jorgeluismireles jorgeluismireles added the Bug This tag is applied to issues which reports bugs. label Jan 2, 2025
@felipensp felipensp added the Unit: Checker Bugs/feature requests, that are related to the type checker. label Jan 3, 2025
@jorgeluismireles
Copy link
Author

More simpler case where seems pre-calculations are done later than enum checking for duplicates:

enum Num {
	x = 2*3
	y = 1*6
}
println('${Num.x:d} ${Num.y:d}') // prints 6 6

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: Checker Bugs/feature requests, that are related to the type checker.
Projects
None yet
Development

No branches or pull requests

2 participants