checker: allow casting strings to string aliases

pull/5450/head
Alexander Medvednikov 2020-06-25 14:55:53 +02:00
parent ce6f687f65
commit a8b0dfb38a
2 changed files with 10 additions and 7 deletions

View File

@ -703,7 +703,7 @@ pub struct CastExpr {
pub:
expr Expr // `buf`
arg Expr // `n` in `string(buf, n)`
typ table.Type // `string`
typ table.Type // `string` TODO rename to `type_to_cast_to`
pos token.Position
pub mut:
typname string

View File

@ -1962,14 +1962,17 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type {
node.pos)
}
if node.expr_type == table.string_type {
mut error_msg := 'cannot cast a string'
if node.expr is ast.StringLiteral {
str_lit := node.expr as ast.StringLiteral
if str_lit.val.len == 1 {
error_msg += ", for denoting characters use `$str_lit.val` instead of '$str_lit.val'"
cast_to_type_sym := c.table.get_type_symbol(node.typ)
if cast_to_type_sym.kind != .alias {
mut error_msg := 'cannot cast a string'
if node.expr is ast.StringLiteral {
str_lit := node.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, node.pos)
}
c.error(error_msg, node.pos)
}
if node.has_arg {
c.expr(node.arg)