From ada8643ac545b15bb152fc42dbcfb8c6cccc7608 Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Tue, 6 Dec 2022 19:14:25 +0530 Subject: [PATCH] checker: disallow using builtin type names for const names (#16599) --- vlib/mysql/orm.v | 6 +++--- vlib/orm/orm.v | 14 +++++++------- vlib/orm/orm_fn_test.v | 2 +- vlib/pg/orm.v | 4 ++-- vlib/sqlite/orm.v | 4 ++-- vlib/v/checker/checker.v | 3 +++ .../checker/tests/reserved_type_name_const_err.out | 11 +++++++++++ .../checker/tests/reserved_type_name_const_err.vv | 5 +++++ 8 files changed, 34 insertions(+), 15 deletions(-) create mode 100644 vlib/v/checker/tests/reserved_type_name_const_err.out create mode 100644 vlib/v/checker/tests/reserved_type_name_const_err.vv diff --git a/vlib/mysql/orm.v b/vlib/mysql/orm.v index 9808e66f44..c7f4337157 100644 --- a/vlib/mysql/orm.v +++ b/vlib/mysql/orm.v @@ -76,7 +76,7 @@ pub fn (db Connection) @select(config orm.SelectConfig, data orm.QueryData, wher f := unsafe { fields[i] } field_types << unsafe { FieldType(f.@type) } match types[i] { - orm.string { + orm.type_string { mysql_bind.buffer_type = C.MYSQL_TYPE_BLOB mysql_bind.buffer_length = FieldType.type_blob.get_len() } @@ -276,7 +276,7 @@ fn buffer_to_primitive(data_list []&u8, types []int, field_types []FieldType) ![ orm.type_idx['bool'] { primitive = *(unsafe { &bool(data) }) } - orm.string { + orm.type_string { primitive = unsafe { cstring_to_vstring(&char(data)) } } orm.time { @@ -322,7 +322,7 @@ fn mysql_type_from_v(typ int) !string { orm.type_idx['f64'] { 'DOUBLE' } - orm.string { + orm.type_string { 'TEXT' } orm.serial { diff --git a/vlib/orm/orm.v b/vlib/orm/orm.v index bcffc14d57..9f19b04748 100644 --- a/vlib/orm/orm.v +++ b/vlib/orm/orm.v @@ -4,8 +4,8 @@ import time import v.ast pub const ( - num64 = [ast.i64_type_idx, ast.u64_type_idx] - nums = [ + num64 = [ast.i64_type_idx, ast.u64_type_idx] + nums = [ ast.i8_type_idx, ast.i16_type_idx, ast.int_type_idx, @@ -14,14 +14,14 @@ pub const ( ast.u32_type_idx, ast.bool_type_idx, ] - float = [ + float = [ ast.f32_type_idx, ast.f64_type_idx, ] - string = ast.string_type_idx - time = -2 - serial = -1 - type_idx = { + type_string = ast.string_type_idx + time = -2 + serial = -1 + type_idx = { 'i8': ast.i8_type_idx 'i16': ast.i16_type_idx 'int': ast.int_type_idx diff --git a/vlib/orm/orm_fn_test.v b/vlib/orm/orm_fn_test.v index 91f7548159..34c41e599b 100644 --- a/vlib/orm/orm_fn_test.v +++ b/vlib/orm/orm_fn_test.v @@ -263,7 +263,7 @@ fn sql_type_from_v(typ int) !string { 'INT64' } else if typ in orm.float { 'DOUBLE' - } else if typ == orm.string { + } else if typ == orm.type_string { 'TEXT' } else if typ == -1 { 'SERIAL' diff --git a/vlib/pg/orm.v b/vlib/pg/orm.v index f8ff566655..9c8f3ac353 100644 --- a/vlib/pg/orm.v +++ b/vlib/pg/orm.v @@ -208,7 +208,7 @@ fn pg_type_from_v(typ int) !string { orm.float[1] { 'DOUBLE PRECISION' } - orm.string { + orm.type_string { 'TEXT' } orm.serial { @@ -274,7 +274,7 @@ fn str_to_primitive(str string, typ int) !orm.Primitive { orm.type_idx['f64'] { return orm.Primitive(str.f64()) } - orm.string { + orm.type_string { return orm.Primitive(str) } orm.time { diff --git a/vlib/sqlite/orm.v b/vlib/sqlite/orm.v index 2e164889b7..559f0a0fa9 100644 --- a/vlib/sqlite/orm.v +++ b/vlib/sqlite/orm.v @@ -152,7 +152,7 @@ fn (stmt Stmt) sqlite_select_column(idx int, typ int) !orm.Primitive { primitive = stmt.get_i64(idx) } else if typ in orm.float { primitive = stmt.get_f64(idx) - } else if typ == orm.string { + } else if typ == orm.type_string { primitive = stmt.get_text(idx).clone() } else if typ == orm.time { d := stmt.get_int(idx) @@ -170,7 +170,7 @@ fn sqlite_type_from_v(typ int) !string { 'INTEGER' } else if typ in orm.float { 'REAL' - } else if typ == orm.string { + } else if typ == orm.type_string { 'TEXT' } else { error('Unknown type ${typ}') diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 2e70799044..1df582cddb 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1393,6 +1393,9 @@ fn (mut c Checker) const_decl(mut node ast.ConstDecl) { c.warn('const block must have at least 1 declaration', node.pos) } for field in node.fields { + if checker.reserved_type_names_chk.matches(util.no_cur_mod(field.name, c.mod)) { + c.error('invalid use of reserved type `${field.name}` as a const name', field.pos) + } // TODO Check const name once the syntax is decided if field.name in c.const_names { name_pos := token.Pos{ diff --git a/vlib/v/checker/tests/reserved_type_name_const_err.out b/vlib/v/checker/tests/reserved_type_name_const_err.out new file mode 100644 index 0000000000..e68e0414e4 --- /dev/null +++ b/vlib/v/checker/tests/reserved_type_name_const_err.out @@ -0,0 +1,11 @@ +vlib/v/checker/tests/reserved_type_name_const_err.vv:1:7: error: invalid use of reserved type `int` as a const name + 1 | const int = 3 + | ~~~~~~~ + 2 | + 3 | const ( +vlib/v/checker/tests/reserved_type_name_const_err.vv:4:2: error: invalid use of reserved type `f64` as a const name + 2 | + 3 | const ( + 4 | f64 = 'string' + | ~~~~~~~~~~~~~~ + 5 | ) diff --git a/vlib/v/checker/tests/reserved_type_name_const_err.vv b/vlib/v/checker/tests/reserved_type_name_const_err.vv new file mode 100644 index 0000000000..7ff4b58b36 --- /dev/null +++ b/vlib/v/checker/tests/reserved_type_name_const_err.vv @@ -0,0 +1,5 @@ +const int = 3 + +const ( + f64 = 'string' +)