From f0b7e5049bd5d9f62681d5751d4c41a469aa86c4 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 18 Jan 2022 18:47:06 +0800 Subject: [PATCH] parser: check the receiver error of method call (#13203) --- vlib/v/parser/parser.v | 4 ++++ vlib/v/parser/tests/method_call_receiver_err.out | 7 +++++++ vlib/v/parser/tests/method_call_receiver_err.vv | 15 +++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 vlib/v/parser/tests/method_call_receiver_err.out create mode 100644 vlib/v/parser/tests/method_call_receiver_err.vv diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index cc09e34394..1a4f113687 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -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() diff --git a/vlib/v/parser/tests/method_call_receiver_err.out b/vlib/v/parser/tests/method_call_receiver_err.out new file mode 100644 index 0000000000..9b4eb8651b --- /dev/null +++ b/vlib/v/parser/tests/method_call_receiver_err.out @@ -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 | } diff --git a/vlib/v/parser/tests/method_call_receiver_err.vv b/vlib/v/parser/tests/method_call_receiver_err.vv new file mode 100644 index 0000000000..d63520ee90 --- /dev/null +++ b/vlib/v/parser/tests/method_call_receiver_err.vv @@ -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' +}