1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: do not allow string(int)

This commit is contained in:
Alexander Medvednikov 2020-05-05 15:26:42 +02:00
parent c5d295c378
commit a0ca68ce3f
5 changed files with 9 additions and 2 deletions

View File

@ -18,7 +18,7 @@ const (
fn C.symlink(charptr, charptr) int
fn init_os_args(argc int, argv &byteptr) []string {
fn init_os_args(argc int, argv &&byte) []string {
mut args := []string{}
//mut args := []string(make(0, argc, sizeof(string)))
//mut args := []string{len:argc}

View File

@ -678,6 +678,7 @@ pub:
expr Expr // `buf`
arg Expr // `n` in `string(buf, n)`
typ table.Type // `string`
pos token.Position
mut:
typname string
expr_type table.Type // `byteptr`

View File

@ -1516,6 +1516,10 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type {
c.expr(it.arg)
}
it.typname = c.table.get_type_symbol(it.typ).name
if it.typ == table.string_type && !it.has_arg && it.expr_type.is_number() && !it.expr_type.is_ptr() {
// s := c.table.get_type_symbol(it.expr_type)
c.error('use `number.str()` instead of `string(number)`', it.pos)
}
return it.typ
}
ast.CallExpr {

View File

@ -674,6 +674,7 @@ pub fn (mut p Parser) name_expr() ast.Expr {
expr: expr
arg: arg
has_arg: has_arg
pos: p.tok.position()
}
p.expr_mod = ''
return node

View File

@ -195,7 +195,8 @@ pub const (
signed_integer_type_idxs = [i8_type_idx, i16_type_idx, int_type_idx, i64_type_idx]
unsigned_integer_type_idxs = [byte_type_idx, u16_type_idx, u32_type_idx, u64_type_idx]
float_type_idxs = [f32_type_idx, f64_type_idx]
number_type_idxs = [i8_type_idx, i16_type_idx, int_type_idx, i64_type_idx, byte_type_idx,
number_type_idxs = [i8_type_idx, i16_type_idx, int_type_idx,
i64_type_idx, byte_type_idx,
u16_type_idx,
u32_type_idx,
u64_type_idx,