Skip to content

Commit

Permalink
parser: check non-generic interface defining generic method
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Jan 15, 2024
1 parent 9268241 commit c52ae3b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion vlib/v/checker/tests/generic_interface_method_decl_err.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
vlib/v/checker/tests/generic_interface_method_decl_err.vv:2:8: error: no need to add generic type names in generic interface's method
1 | interface Expr {
1 | interface Expr<R> {
2 | accept<R>(v Visitor<R>) R
| ^
3 | }
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/checker/tests/generic_interface_method_decl_err.vv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
interface Expr {
interface Expr<R> {
accept<R>(v Visitor<R>) R
}

Expand Down
9 changes: 7 additions & 2 deletions vlib/v/parser/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -632,8 +632,13 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
mut_pos = fields.len
}
if p.peek_tok.kind in [.lt, .lsbr] && p.peek_tok.is_next_to(p.tok) {
p.error_with_pos("no need to add generic type names in generic interface's method",
p.peek_tok.pos())
if generic_types.len == 0 {
p.error_with_pos('non-generic interface `${interface_name}` cannot define a generic method',
p.peek_tok.pos())
} else {
p.error_with_pos("no need to add generic type names in generic interface's method",
p.peek_tok.pos())
}
return ast.InterfaceDecl{}
}
mut comments := p.eat_comments()
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/parser/tests/generic_interface_decl_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
vlib/v/parser/tests/generic_interface_decl_err.vv:4:6: error: non-generic interface `Abc` cannot define a generic method
2 |
3 | interface Abc {
4 | test[T]()
| ^
5 | }
6 |
13 changes: 13 additions & 0 deletions vlib/v/parser/tests/generic_interface_decl_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module main

interface Abc {
test[T]()
}

struct Xyz {}

fn (xyz Xyz) test[T]() {}

fn main() {
_ := Abc(Xyz{})
}

0 comments on commit c52ae3b

Please sign in to comment.