mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: prohibit char.str()
, add tests (#10615)
This commit is contained in:
parent
00333806e0
commit
f029f7e897
@ -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])`
|
||||
|
3
vlib/v/checker/tests/char_str.out
Normal file
3
vlib/v/checker/tests/char_str.out
Normal 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()
|
||||
| ~~~~~~~~~~~~~~
|
1
vlib/v/checker/tests/char_str.vv
Normal file
1
vlib/v/checker/tests/char_str.vv
Normal file
@ -0,0 +1 @@
|
||||
char(91).str()
|
3
vlib/v/checker/tests/print_char.out
Normal file
3
vlib/v/checker/tests/print_char.out
Normal 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))
|
||||
| ~~~~~~~~~~~~~~~~~
|
1
vlib/v/checker/tests/print_char.vv
Normal file
1
vlib/v/checker/tests/print_char.vv
Normal file
@ -0,0 +1 @@
|
||||
println(char(91))
|
Loading…
x
Reference in New Issue
Block a user