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

checker: check result type method call (#15794)

This commit is contained in:
yuyi
2022-09-17 16:45:13 +08:00
committed by GitHub
parent 26443cf9fa
commit de636dbb2b
3 changed files with 22 additions and 0 deletions

View File

@@ -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' {

View File

@@ -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 | }

View File

@@ -0,0 +1,13 @@
struct Foo {}
fn (f Foo) foo() int {
return 1
}
fn new_foo() !Foo {
return Foo{}
}
fn main() {
new_foo().foo()
}