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,6 +1962,8 @@ 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 {
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' mut error_msg := 'cannot cast a string'
if node.expr is ast.StringLiteral { if node.expr is ast.StringLiteral {
str_lit := node.expr as ast.StringLiteral str_lit := node.expr as ast.StringLiteral
@ -1971,6 +1973,7 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type {
} }
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)
} }