checker: check optional type call

pull/5462/head
yuyi 2020-06-23 17:39:58 +08:00 committed by GitHub
parent ed393896f5
commit e2b5debc6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 0 deletions

View File

@ -735,6 +735,11 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type {
call_expr.left_type = left_type
left_type_sym := c.table.get_type_symbol(c.unwrap_generic(left_type))
method_name := call_expr.name
if left_type.has_flag(.optional) {
c.error('optional type cannot be called directly', call_expr.left.position())
return table.void_type
}
// TODO: remove this for actual methods, use only for compiler magic
// FIXME: Argument count != 1 will break these
if left_type_sym.kind == .array && method_name in ['filter', 'clone', 'repeat', 'reverse',

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/optional_type_call_err.v:4:5: error: optional type cannot be called directly
2 |
3 | fn main() {
4 | os.ls('.').filter(it.ends_with('.v')) or { return }
| ~~~~~~~
5 | }

View File

@ -0,0 +1,5 @@
import os
fn main() {
os.ls('.').filter(it.ends_with('.v')) or { return }
}