1
0
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:
yuyi 2022-09-14 01:41:20 +08:00 committed by GitHub
parent 840370f345
commit ac64318890
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 0 deletions

View File

@ -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() {

View 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')

View 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')
}