mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: check error for cast function to string (#14375)
This commit is contained in:
parent
901b8f0c24
commit
5bc4fea9e0
@ -3026,6 +3026,9 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
|
||||
ft := c.table.type_to_str(from_type)
|
||||
c.error('cannot cast sumtype `$ft` to string, use `${snexpr}.str()` instead.',
|
||||
node.pos)
|
||||
} else if final_from_sym.kind == .function {
|
||||
fnexpr := node.expr.str()
|
||||
c.error('cannot cast function `$fnexpr` to string', node.pos)
|
||||
} else if to_type != ast.string_type && from_type == ast.string_type
|
||||
&& (!(to_sym.kind == .alias && final_to_sym.name == 'string')) {
|
||||
mut error_msg := 'cannot cast a string to a type `$final_to_sym.name`, that is not an alias of string'
|
||||
|
5
vlib/v/checker/tests/cast_function_to_string_err.out
Normal file
5
vlib/v/checker/tests/cast_function_to_string_err.out
Normal file
@ -0,0 +1,5 @@
|
||||
vlib/v/checker/tests/cast_function_to_string_err.vv:2:10: error: cannot cast function `main` to string
|
||||
1 | fn main() {
|
||||
2 | println(string(main))
|
||||
| ~~~~~~~~~~~~
|
||||
3 | }
|
3
vlib/v/checker/tests/cast_function_to_string_err.vv
Normal file
3
vlib/v/checker/tests/cast_function_to_string_err.vv
Normal file
@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println(string(main))
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user