checker: check printing of optional type

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

View File

@ -1017,6 +1017,10 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type {
if (fn_name == 'println' || fn_name == 'print') && call_expr.args.len > 0 {
c.expected_type = table.string_type
call_expr.args[0].typ = c.expr(call_expr.args[0].expr)
// check optional argument
if call_expr.args[0].typ.has_flag(.optional) {
c.error('cannot print optional type', call_expr.args[0].expr.position())
}
/*
// TODO: optimize `struct T{} fn (t &T) str() string {return 'abc'} mut a := []&T{} a << &T{} println(a[0])`
// It currently generates:

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/print_optional_type_err.v:3:13: error: cannot print optional type
1 | import os
2 | fn main() {
3 | println(os.ls('.'))
| ~~~~~~~
4 | }

View File

@ -0,0 +1,4 @@
import os
fn main() {
println(os.ls('.'))
}