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

checker: check enum fields with duplicate value (fix #19309) #19310

Merged
merged 3 commits into from
Sep 10, 2023

Conversation

yuyi98
Copy link
Member

@yuyi98 yuyi98 commented Sep 9, 2023

This PR check enum fields with duplicate value (fix #19309).

  • Check enum fields with duplicate value.
  • Add test.
pub enum HardwareAccelerationDevice as i64 {
	@none = 0
	cpu = (1 << 0)
	gpu = (1 << 1)
	accelerator = (1 << 2)
	default = (1 << 0)
	all = 0xFFFFFFFF
}

fn main() {
	device := HardwareAccelerationDevice.@none
	println(device)
}

PS D:\Test\v\tt1> v run .    
tt1.v:6:12: error: enum value `1` already exists
    4 |     gpu = (1 << 1)
    5 |     accelerator = (1 << 2)
    6 |     default = (1 << 0)
      |               ~~~~~~~~
    7 |     all = 0xFFFFFFFF
    8 | }

@spytheman
Copy link
Member

spytheman commented Sep 9, 2023

The new code is very repetitive, and it does not handle the overflow cases (see the ast.IntegerLiteral branch from above). Imho extract a common method, that takes the expression and the low/high values, and call it in all branches.

@yuyi98
Copy link
Member Author

yuyi98 commented Sep 9, 2023

OK, I see.

Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work.

@spytheman spytheman merged commit 1218d88 into vlang:master Sep 10, 2023
47 checks passed
@yuyi98 yuyi98 deleted the fix_enum_field_value_init branch September 10, 2023 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enum.str() generates C error when enum fields have the same value.
2 participants