parser: check interface methods name (fix #14217) (#14218)

yuyi 2022-04-29 20:48:03 +08:00 committed by Jef Roosens
parent d1a0ac26fd
commit 2109a6a85c
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 14 additions and 5 deletions

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/interface_method_name_err.vv:2:2: error: method name `Fizz` cannot contain uppercase letters, use snake_case instead
1 | interface Foo {
2 | Fizz()
| ~~~~~~
3 | }
4 |

View File

@ -0,0 +1,6 @@
interface Foo {
Fizz()
}
fn main() {
}

View File

@ -516,7 +516,8 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
mut mut_pos := -1
mut ifaces := []ast.InterfaceEmbedding{}
for p.tok.kind != .rcbr && p.tok.kind != .eof {
if p.tok.kind == .name && p.tok.lit.len > 0 && p.tok.lit[0].is_capital() {
if p.tok.kind == .name && p.tok.lit.len > 0 && p.tok.lit[0].is_capital()
&& p.peek_tok.kind != .lpar {
iface_pos := p.tok.pos()
mut iface_name := p.tok.lit
iface_type := p.parse_type()
@ -584,10 +585,6 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
p.error_with_pos('duplicate method `$name`', method_start_pos)
return ast.InterfaceDecl{}
}
if language == .v && util.contains_capital(name) {
p.error('interface methods cannot contain uppercase letters, use snake_case instead')
return ast.InterfaceDecl{}
}
// field_names << name
args2, _, is_variadic := p.fn_args() // TODO merge ast.Param and ast.Arg to avoid this
mut args := [