From a0ca68ce3f50982186cc047d36fe8c846d2ff240 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 5 May 2020 15:26:42 +0200 Subject: [PATCH] checker: do not allow `string(int)` --- vlib/os/os_nix.c.v | 2 +- vlib/v/ast/ast.v | 1 + vlib/v/checker/checker.v | 4 ++++ vlib/v/parser/parser.v | 1 + vlib/v/table/atypes.v | 3 ++- 5 files changed, 9 insertions(+), 2 deletions(-) diff --git a/vlib/os/os_nix.c.v b/vlib/os/os_nix.c.v index 70ecb81424..1bdccd3ed6 100644 --- a/vlib/os/os_nix.c.v +++ b/vlib/os/os_nix.c.v @@ -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} diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 254d3638ff..df6afb00db 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -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` diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 8ff5b6f45f..81abf3e21d 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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 { diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 49fae86e7b..0a52a1bc4c 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -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 diff --git a/vlib/v/table/atypes.v b/vlib/v/table/atypes.v index f1507f927d..f130a11547 100644 --- a/vlib/v/table/atypes.v +++ b/vlib/v/table/atypes.v @@ -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,