Skip to content

parser: check enum method duplicated (fix #20924) - #22294

Merged
spytheman merged 2 commits into
vlang:masterfrom
yuyi98:fix_enum_flag_has
Sep 24, 2024
Merged

spytheman merged 2 commits into
vlang:masterfrom
yuyi98:fix_enum_flag_has

Conversation

@yuyi98

@yuyi98 yuyi98 commented Sep 24, 2024

Copy link
Copy Markdown
Member

This PR check enum method duplicated (fix #20924).

  • Check enum method duplicated.
  • Add test.
@[flag]
enum Flags {
	bit0
	bit1
	bit2
	bit3
	bit4
	bit5
	bit6
	bit7
}

fn (f Flags) has(f2 Flags) bool {
	return f & f2 > 0
}

fn main() {}

PS D:\Test\v\tt1> v run .
tt1.v:13:14: error: duplicate method `has`, `has` is an enum type built-in method
   11 | }
   12 | 
   13 | fn (f Flags) has(f2 Flags) bool {
      |              ~~~
   14 |     return f & f2 > 0
   15 | }

@Delta456 Delta456 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good work

@spytheman

spytheman commented Sep 24, 2024 •

Copy link
Copy Markdown
Contributor

Good work, but please also check if the name is in ['is_empty', 'has', 'all', 'set', 'set_all', 'clear', 'clear_all', 'toggle', 'zero', 'from'], otherwise the message can become a bit misleading for a case like this:

@[flag]
enum Flags {
	bit0
	bit1
}

fn (f Flags) zzz(f2 Flags) bool {
	return f & f2 > 0
}

fn (f Flags) zzz(f2 Flags) bool {
	return f & f2 > 0
}

Currently it will show that the method name zzz is a builtin method:

#0 11:34:51 ^ fix_enum_flag_has /v/oo>v ee.v
ee.v:13:14: error: duplicate method `zzz`, `zzz` is an enum type built-in method
   11 | }
   12 |
   13 | fn (f Flags) zzz(f2 Flags) bool {
      |              ~~~
   14 |     return f & f2 > 0

@spytheman
spytheman merged commit 4ee948f into vlang:master Sep 24, 2024
@yuyi98
yuyi98 deleted the fix_enum_flag_has branch September 25, 2024 00:47
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.

Strange error message with @[flag] enum

3 participants