checker: check error for cast function to string (#14375)

yuyi 2022-05-12 15:04:38 +08:00 committed by Jef Roosens
parent a91bd5a5bb
commit a8dceb74ec
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 11 additions and 0 deletions

View File

@ -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'

View 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 | }

View File

@ -0,0 +1,3 @@
fn main() {
println(string(main))
}