From a6026fd505b300447f7e9273fb5ccbc9365710b6 Mon Sep 17 00:00:00 2001 From: StunxFS <56417208+StunxFS@users.noreply.github.com> Date: Mon, 8 Aug 2022 10:35:24 -0400 Subject: [PATCH] checker: disallow method calls with invalid expressions (#15337) --- vlib/v/checker/fn.v | 4 ++++ vlib/v/checker/tests/void_method_call.out | 6 ++++++ vlib/v/checker/tests/void_method_call.vv | 6 ++++++ 3 files changed, 16 insertions(+) create mode 100644 vlib/v/checker/tests/void_method_call.out create mode 100644 vlib/v/checker/tests/void_method_call.vv diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 50f91be479..03d59b627f 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -1113,6 +1113,10 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) pub fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type { left_type := c.expr(node.left) + if left_type == ast.void_type { + c.error('cannot call a method using an invalid expression', node.pos) + return ast.void_type + } c.expected_type = left_type mut is_generic := left_type.has_flag(.generic) node.left_type = left_type diff --git a/vlib/v/checker/tests/void_method_call.out b/vlib/v/checker/tests/void_method_call.out new file mode 100644 index 0000000000..1df2dae421 --- /dev/null +++ b/vlib/v/checker/tests/void_method_call.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/void_method_call.vv:5:17: error: cannot call a method using an invalid expression + 3 | + 4 | fn main() { + 5 | simple_fn().method() + | ~~~~~~~~ + 6 | } diff --git a/vlib/v/checker/tests/void_method_call.vv b/vlib/v/checker/tests/void_method_call.vv new file mode 100644 index 0000000000..87e5962ae9 --- /dev/null +++ b/vlib/v/checker/tests/void_method_call.vv @@ -0,0 +1,6 @@ +fn simple_fn() { +} + +fn main() { + simple_fn().method() +}