diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 7bd0c7de29..66b18e4bc5 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -1199,6 +1199,9 @@ pub fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type { if left_type.has_flag(.optional) { c.error('optional type cannot be called directly', node.left.pos()) return ast.void_type + } else if left_type.has_flag(.result) { + c.error('result type cannot be called directly', node.left.pos()) + return ast.void_type } if left_sym.kind in [.sum_type, .interface_] { if method_name == 'type_name' { diff --git a/vlib/v/checker/tests/result_type_call_err.out b/vlib/v/checker/tests/result_type_call_err.out new file mode 100644 index 0000000000..8b6e5700b3 --- /dev/null +++ b/vlib/v/checker/tests/result_type_call_err.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/result_type_call_err.vv:12:2: error: result type cannot be called directly + 10 | + 11 | fn main() { + 12 | new_foo().foo() + | ~~~~~~~~~ + 13 | } diff --git a/vlib/v/checker/tests/result_type_call_err.vv b/vlib/v/checker/tests/result_type_call_err.vv new file mode 100644 index 0000000000..6c6cd48df7 --- /dev/null +++ b/vlib/v/checker/tests/result_type_call_err.vv @@ -0,0 +1,13 @@ +struct Foo {} + +fn (f Foo) foo() int { + return 1 +} + +fn new_foo() !Foo { + return Foo{} +} + +fn main() { + new_foo().foo() +}