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: pub:
expr Expr // `buf` expr Expr // `buf`
arg Expr // `n` in `string(buf, n)` 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 pos token.Position
pub mut: pub mut:
typname string typname string

View File

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