checker: prohibit `char.str()`, add tests (#10615)

pull/9796/head
shadowninja55 2021-06-30 01:16:31 -04:00 committed by GitHub
parent 00333806e0
commit f029f7e897
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 19 additions and 3 deletions

View File

@ -1839,6 +1839,10 @@ pub fn (mut c Checker) method_call(mut call_expr ast.CallExpr) ast.Type {
}
call_expr.return_type = info.return_type
return info.return_type
} else if left_type_sym.kind == .char && left_type.nr_muls() == 0 && method_name == 'str' {
c.error('calling `.str()` on type `char` is not allowed, use its address or cast it to an integer instead',
call_expr.left.position().extend(call_expr.pos))
return ast.void_type
}
mut method := ast.Fn{}
mut has_method := false
@ -2492,11 +2496,15 @@ pub fn (mut c Checker) fn_call(mut call_expr ast.CallExpr) ast.Type {
c.inside_println_arg = true
c.expected_type = ast.string_type
call_expr.args[0].typ = c.expr(call_expr.args[0].expr)
_ := c.check_expr_opt_call(call_expr.args[0].expr, call_expr.args[0].typ)
if call_expr.args[0].typ.is_void() {
arg := call_expr.args[0]
_ := c.check_expr_opt_call(arg.expr, arg.typ)
if arg.typ.is_void() {
c.error('`$fn_name` can not print void expressions', call_expr.pos)
} else if arg.typ == ast.char_type && arg.typ.nr_muls() == 0 {
c.error('`$fn_name` cannot print type `char` directly, print its address or cast it to an integer instead',
call_expr.pos)
}
c.fail_if_unreadable(call_expr.args[0].expr, call_expr.args[0].typ, 'argument to print')
c.fail_if_unreadable(arg.expr, arg.typ, 'argument to print')
c.inside_println_arg = false
/*
// TODO: optimize `struct T{} fn (t &T) str() string {return 'abc'} mut a := []&T{} a << &T{} println(a[0])`

View File

@ -0,0 +1,3 @@
vlib/v/checker/tests/char_str.vv:1:1: error: calling `.str()` on type `char` is not allowed, use its address or cast it to an integer instead
1 | char(91).str()
| ~~~~~~~~~~~~~~

View File

@ -0,0 +1 @@
char(91).str()

View File

@ -0,0 +1,3 @@
vlib/v/checker/tests/print_char.vv:1:1: error: `println` cannot print type `char` directly, print its address or cast it to an integer instead
1 | println(char(91))
| ~~~~~~~~~~~~~~~~~

View File

@ -0,0 +1 @@
println(char(91))