mirror of
https://github.com/vlang/v.git
synced 2025-09-10 16:00:31 -04:00
checker: disallow using builtin type names for const names (#16599)
This commit is contained in:
parent
5fc7b6d3d6
commit
ada8643ac5
@ -76,7 +76,7 @@ pub fn (db Connection) @select(config orm.SelectConfig, data orm.QueryData, wher
|
|||||||
f := unsafe { fields[i] }
|
f := unsafe { fields[i] }
|
||||||
field_types << unsafe { FieldType(f.@type) }
|
field_types << unsafe { FieldType(f.@type) }
|
||||||
match types[i] {
|
match types[i] {
|
||||||
orm.string {
|
orm.type_string {
|
||||||
mysql_bind.buffer_type = C.MYSQL_TYPE_BLOB
|
mysql_bind.buffer_type = C.MYSQL_TYPE_BLOB
|
||||||
mysql_bind.buffer_length = FieldType.type_blob.get_len()
|
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'] {
|
orm.type_idx['bool'] {
|
||||||
primitive = *(unsafe { &bool(data) })
|
primitive = *(unsafe { &bool(data) })
|
||||||
}
|
}
|
||||||
orm.string {
|
orm.type_string {
|
||||||
primitive = unsafe { cstring_to_vstring(&char(data)) }
|
primitive = unsafe { cstring_to_vstring(&char(data)) }
|
||||||
}
|
}
|
||||||
orm.time {
|
orm.time {
|
||||||
@ -322,7 +322,7 @@ fn mysql_type_from_v(typ int) !string {
|
|||||||
orm.type_idx['f64'] {
|
orm.type_idx['f64'] {
|
||||||
'DOUBLE'
|
'DOUBLE'
|
||||||
}
|
}
|
||||||
orm.string {
|
orm.type_string {
|
||||||
'TEXT'
|
'TEXT'
|
||||||
}
|
}
|
||||||
orm.serial {
|
orm.serial {
|
||||||
|
@ -4,8 +4,8 @@ import time
|
|||||||
import v.ast
|
import v.ast
|
||||||
|
|
||||||
pub const (
|
pub const (
|
||||||
num64 = [ast.i64_type_idx, ast.u64_type_idx]
|
num64 = [ast.i64_type_idx, ast.u64_type_idx]
|
||||||
nums = [
|
nums = [
|
||||||
ast.i8_type_idx,
|
ast.i8_type_idx,
|
||||||
ast.i16_type_idx,
|
ast.i16_type_idx,
|
||||||
ast.int_type_idx,
|
ast.int_type_idx,
|
||||||
@ -14,14 +14,14 @@ pub const (
|
|||||||
ast.u32_type_idx,
|
ast.u32_type_idx,
|
||||||
ast.bool_type_idx,
|
ast.bool_type_idx,
|
||||||
]
|
]
|
||||||
float = [
|
float = [
|
||||||
ast.f32_type_idx,
|
ast.f32_type_idx,
|
||||||
ast.f64_type_idx,
|
ast.f64_type_idx,
|
||||||
]
|
]
|
||||||
string = ast.string_type_idx
|
type_string = ast.string_type_idx
|
||||||
time = -2
|
time = -2
|
||||||
serial = -1
|
serial = -1
|
||||||
type_idx = {
|
type_idx = {
|
||||||
'i8': ast.i8_type_idx
|
'i8': ast.i8_type_idx
|
||||||
'i16': ast.i16_type_idx
|
'i16': ast.i16_type_idx
|
||||||
'int': ast.int_type_idx
|
'int': ast.int_type_idx
|
||||||
|
@ -263,7 +263,7 @@ fn sql_type_from_v(typ int) !string {
|
|||||||
'INT64'
|
'INT64'
|
||||||
} else if typ in orm.float {
|
} else if typ in orm.float {
|
||||||
'DOUBLE'
|
'DOUBLE'
|
||||||
} else if typ == orm.string {
|
} else if typ == orm.type_string {
|
||||||
'TEXT'
|
'TEXT'
|
||||||
} else if typ == -1 {
|
} else if typ == -1 {
|
||||||
'SERIAL'
|
'SERIAL'
|
||||||
|
@ -208,7 +208,7 @@ fn pg_type_from_v(typ int) !string {
|
|||||||
orm.float[1] {
|
orm.float[1] {
|
||||||
'DOUBLE PRECISION'
|
'DOUBLE PRECISION'
|
||||||
}
|
}
|
||||||
orm.string {
|
orm.type_string {
|
||||||
'TEXT'
|
'TEXT'
|
||||||
}
|
}
|
||||||
orm.serial {
|
orm.serial {
|
||||||
@ -274,7 +274,7 @@ fn str_to_primitive(str string, typ int) !orm.Primitive {
|
|||||||
orm.type_idx['f64'] {
|
orm.type_idx['f64'] {
|
||||||
return orm.Primitive(str.f64())
|
return orm.Primitive(str.f64())
|
||||||
}
|
}
|
||||||
orm.string {
|
orm.type_string {
|
||||||
return orm.Primitive(str)
|
return orm.Primitive(str)
|
||||||
}
|
}
|
||||||
orm.time {
|
orm.time {
|
||||||
|
@ -152,7 +152,7 @@ fn (stmt Stmt) sqlite_select_column(idx int, typ int) !orm.Primitive {
|
|||||||
primitive = stmt.get_i64(idx)
|
primitive = stmt.get_i64(idx)
|
||||||
} else if typ in orm.float {
|
} else if typ in orm.float {
|
||||||
primitive = stmt.get_f64(idx)
|
primitive = stmt.get_f64(idx)
|
||||||
} else if typ == orm.string {
|
} else if typ == orm.type_string {
|
||||||
primitive = stmt.get_text(idx).clone()
|
primitive = stmt.get_text(idx).clone()
|
||||||
} else if typ == orm.time {
|
} else if typ == orm.time {
|
||||||
d := stmt.get_int(idx)
|
d := stmt.get_int(idx)
|
||||||
@ -170,7 +170,7 @@ fn sqlite_type_from_v(typ int) !string {
|
|||||||
'INTEGER'
|
'INTEGER'
|
||||||
} else if typ in orm.float {
|
} else if typ in orm.float {
|
||||||
'REAL'
|
'REAL'
|
||||||
} else if typ == orm.string {
|
} else if typ == orm.type_string {
|
||||||
'TEXT'
|
'TEXT'
|
||||||
} else {
|
} else {
|
||||||
error('Unknown type ${typ}')
|
error('Unknown type ${typ}')
|
||||||
|
@ -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)
|
c.warn('const block must have at least 1 declaration', node.pos)
|
||||||
}
|
}
|
||||||
for field in node.fields {
|
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
|
// TODO Check const name once the syntax is decided
|
||||||
if field.name in c.const_names {
|
if field.name in c.const_names {
|
||||||
name_pos := token.Pos{
|
name_pos := token.Pos{
|
||||||
|
11
vlib/v/checker/tests/reserved_type_name_const_err.out
Normal file
11
vlib/v/checker/tests/reserved_type_name_const_err.out
Normal 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 | )
|
5
vlib/v/checker/tests/reserved_type_name_const_err.vv
Normal file
5
vlib/v/checker/tests/reserved_type_name_const_err.vv
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
const int = 3
|
||||||
|
|
||||||
|
const (
|
||||||
|
f64 = 'string'
|
||||||
|
)
|
Loading…
x
Reference in New Issue
Block a user