1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: disallow declaring static functions as method receivers (#19007)

This commit is contained in:
Swastik Baranwal 2023-07-30 08:41:11 +05:30 committed by GitHub
parent 77049600e6
commit 8735694d13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 1 deletions

View File

@ -292,7 +292,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
} }
mut name := '' mut name := ''
mut type_sym := p.table.sym(rec.typ) mut type_sym := p.table.sym(rec.typ)
name_pos := p.tok.pos() mut name_pos := p.tok.pos()
if p.tok.kind == .name { if p.tok.kind == .name {
mut check_name := '' mut check_name := ''
// TODO high order fn // TODO high order fn
@ -304,6 +304,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
p.check(.dot) p.check(.dot)
check_name = p.check_name() check_name = p.check_name()
name = type_name + '__static__' + check_name // "foo__bar" name = type_name + '__static__' + check_name // "foo__bar"
name_pos = name_pos.extend(p.prev_tok.pos())
} else { } else {
check_name = if language == .js { p.check_js_name() } else { p.check_name() } check_name = if language == .js { p.check_js_name() } else { p.check_name() }
name = check_name name = check_name
@ -452,6 +453,9 @@ run them via `v file.v` instead',
} }
} }
} }
if is_method && is_static_type_method {
p.error_with_pos('cannot declare a static function as a receiver method', name_pos)
}
// Register // Register
if is_method { if is_method {
// Do not allow to modify / add methods to types from other modules // Do not allow to modify / add methods to types from other modules

View File

@ -0,0 +1,7 @@
vlib/v/parser/tests/declare_static_fn_as_receiver_method_err.vv:4:12: error: cannot declare a static function as a receiver method
2 | }
3 |
4 | fn (y Foo) Foo.new() Foo {
| ~~~~~~~
5 | return Foo{}
6 | }

View File

@ -0,0 +1,11 @@
struct Foo {
}
fn (y Foo) Foo.new() Foo {
return Foo{}
}
fn main() {
x := Foo{}.new()
println(x)
}