Skip to content

Commit

Permalink
parser: check enum method duplicated
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Sep 24, 2024
1 parent 03e0b9e commit 511bc63
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ pub:
file string
file_mode Language
pos token.Pos
name_pos token.Pos
return_type_pos token.Pos
pub mut:
return_type Type
Expand Down
12 changes: 11 additions & 1 deletion vlib/v/parser/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,15 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
}
}
if is_duplicate {
p.error_with_pos('duplicate method `${name}`', name_pos)
if type_sym.kind == .enum_ {
if enum_fn := type_sym.find_method(name) {
name_pos = enum_fn.name_pos
}
p.error_with_pos('duplicate method `${name}`, `${name}` is an enum type built-in method',
name_pos)
} else {
p.error_with_pos('duplicate method `${name}`', name_pos)
}
return ast.FnDecl{
scope: unsafe { nil }
}
Expand Down Expand Up @@ -539,6 +547,7 @@ run them via `v file.v` instead',
mod: p.mod
file: p.file_path
pos: start_pos
name_pos: name_pos
language: language
})
} else {
Expand Down Expand Up @@ -591,6 +600,7 @@ run them via `v file.v` instead',
mod: p.mod
file: p.file_path
pos: start_pos
name_pos: name_pos
language: language
})
}
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/parser/tests/enum_method_duplicated_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
vlib/v/parser/tests/enum_method_duplicated_err.vv: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 | }
17 changes: 17 additions & 0 deletions vlib/v/parser/tests/enum_method_duplicated_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@[flag]
enum Flags {
bit0
bit1
bit2
bit3
bit4
bit5
bit6
bit7
}

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

fn main() {}

0 comments on commit 511bc63

Please sign in to comment.