checker: check error for cast function to string (#14375)
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'
|
||||
|
|
|
@ -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 | }
|
|
@ -0,0 +1,3 @@
|
|||
fn main() {
|
||||
println(string(main))
|
||||
}
|
Loading…
Reference in New Issue