checker: check string(1) cast error
parent
5bd3045498
commit
51f3f31a4e
|
@ -777,7 +777,9 @@ fn (expr Expr) position() token.Position {
|
|||
AssignExpr {
|
||||
return it.pos
|
||||
}
|
||||
// ast.CastExpr { }
|
||||
CastExpr {
|
||||
return it.pos
|
||||
}
|
||||
Assoc {
|
||||
return it.pos
|
||||
}
|
||||
|
|
|
@ -1560,6 +1560,12 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type {
|
|||
}
|
||||
ast.CastExpr {
|
||||
it.expr_type = c.expr(it.expr)
|
||||
sym := c.table.get_type_symbol(it.expr_type)
|
||||
if it.typ == table.string_type && !(sym.kind in [.byte, .byteptr] ||
|
||||
sym.kind == .array && sym.name == 'array_byte') {
|
||||
type_name := c.table.type_to_str(it.expr_type)
|
||||
c.error('cannot cast type `$type_name` to string', it.pos)
|
||||
}
|
||||
if it.has_arg {
|
||||
c.expr(it.arg)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
vlib/v/checker/tests/cast_string_err.v:2:14: error: cannot cast type `int` to string
|
||||
1 | fn main() {
|
||||
2 | a := string(1)
|
||||
| ^
|
||||
3 | println(a)
|
||||
4 | }
|
|
@ -0,0 +1,4 @@
|
|||
fn main() {
|
||||
a := string(1)
|
||||
println(a)
|
||||
}
|
|
@ -674,7 +674,7 @@ pub fn (mut p Parser) name_expr() ast.Expr {
|
|||
expr: expr
|
||||
arg: arg
|
||||
has_arg: has_arg
|
||||
pos: p.tok.position()
|
||||
pos: expr.position()
|
||||
}
|
||||
p.expr_mod = ''
|
||||
return node
|
||||
|
|
Loading…
Reference in New Issue