mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: check casting array to number (#15750)
This commit is contained in:
parent
840370f345
commit
ac64318890
@ -2506,6 +2506,10 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
|
||||
snexpr := node.expr.str()
|
||||
tt := c.table.type_to_str(to_type)
|
||||
c.error('cannot cast string to `$tt`, use `${snexpr}.str` instead.', node.pos)
|
||||
} else if final_from_sym.kind == .array && !from_type.is_ptr() && to_type != ast.string_type {
|
||||
ft := c.table.type_to_str(from_type)
|
||||
tt := c.table.type_to_str(to_type)
|
||||
c.error('cannot cast array `$ft` to `$tt`', node.pos)
|
||||
}
|
||||
|
||||
if to_sym.kind == .rune && from_sym.is_string() {
|
||||
|
7
vlib/v/checker/tests/cast_array_to_number_err.out
Normal file
7
vlib/v/checker/tests/cast_array_to_number_err.out
Normal file
@ -0,0 +1,7 @@
|
||||
vlib/v/checker/tests/cast_array_to_number_err.vv:5:8: error: cannot cast array `[]u8` to `i16`
|
||||
3 | fn main() {
|
||||
4 | bytes := '010000150107120508d37445a0d7e5c5071980710c64310d9e12043000777369ff0424ab78b91a05164b00e50034003300'
|
||||
5 | yr := i16(hx.decode(bytes.substr(6,8))?)
|
||||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
6 | println(bytes)
|
||||
7 | println('yr: $yr')
|
8
vlib/v/checker/tests/cast_array_to_number_err.vv
Normal file
8
vlib/v/checker/tests/cast_array_to_number_err.vv
Normal file
@ -0,0 +1,8 @@
|
||||
import encoding.hex as hx
|
||||
|
||||
fn main() {
|
||||
bytes := '010000150107120508d37445a0d7e5c5071980710c64310d9e12043000777369ff0424ab78b91a05164b00e50034003300'
|
||||
yr := i16(hx.decode(bytes.substr(6,8))?)
|
||||
println(bytes)
|
||||
println('yr: $yr')
|
||||
}
|
Loading…
Reference in New Issue
Block a user