checker: do not allow casting strings

pull/4923/head
Alexander Medvednikov 2020-05-16 22:52:41 +02:00
parent 6e4ae5a5e6
commit 5b15b8ccc9
1 changed files with 8 additions and 1 deletions

View File

@ -1687,7 +1687,14 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type {
c.error('cannot cast type `$type_name` to string, use `x.str()` instead', it.pos) c.error('cannot cast type `$type_name` to string, use `x.str()` instead', it.pos)
} }
if it.expr_type == table.string_type { if it.expr_type == table.string_type {
c.error('cannot cast a string', it.pos) mut error_msg := 'cannot cast a string'
if it.expr is ast.StringLiteral {
str_lit := it.expr as ast.StringLiteral
if str_lit.val.len == 1 {
error_msg += ", for denoting characters use `$str_lit.val` instead of '$str_lit.val'"
}
}
c.error(error_msg, it.pos)
} }
if it.has_arg { if it.has_arg {
c.expr(it.arg) c.expr(it.arg)