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

checker: disallow using builtin type names for const names (#16599)

This commit is contained in:
Swastik Baranwal 2022-12-06 19:14:25 +05:30 committed by GitHub
parent 5fc7b6d3d6
commit ada8643ac5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 34 additions and 15 deletions

View File

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

View File

@ -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

View File

@ -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'

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,5 @@
const int = 3
const (
f64 = 'string'
)