parser: check the receiver error of method call (#13203)

pull/13204/head
yuyi 2022-01-18 18:47:06 +08:00 committed by GitHub
parent 1c4c430642
commit f0b7e5049b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 0 deletions

View File

@ -2317,6 +2317,10 @@ pub fn (mut p Parser) name_expr() ast.Expr {
scope: p.scope
}
}
if p.peek_token(2).kind == .name && p.peek_token(3).kind == .lpar && !known_var {
p.error_with_pos('the receiver of the method call must be an instantiated object, e.g. `foo.bar()`',
p.tok.position())
}
// `Color.green`
mut enum_name := p.check_name()
enum_name_pos := p.prev_tok.position()

View File

@ -0,0 +1,7 @@
vlib/v/parser/tests/method_call_receiver_err.vv:9:11: error: the receiver of the method call must be an instantiated object, e.g. `foo.bar()`
7 |
8 | $for method in S1.methods {
9 | println(S1.method_hello('yo'))
| ~~
10 | }
11 | }

View File

@ -0,0 +1,15 @@
module main
struct S1{}
fn main() {
s1 := S1{}
$for method in S1.methods {
println(S1.method_hello('yo'))
}
}
fn (t S1) method_hello() string {
return 'Hello'
}