checker: disallow module name duplicates in local names (#18118)

This commit is contained in:
Turiiya 2023-09-08 22:33:35 +02:00 committed by GitHub
parent 3a2994bc80
commit 1e38cc0986
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 96 additions and 28 deletions

View file

@ -138,7 +138,7 @@ fn (mut m ObjPart) parse_floats(row string, start_index int) m4.Vec4 {
}
// read and manage all the faes from an .obj file data
fn (mut p Part) parse_faces(row string, start_index int, obj ObjPart) {
fn (mut p Part) parse_faces(row string, start_index int, obj_part ObjPart) {
mut i := start_index + 1
mut res := [][3]int{}
mut v := 0
@ -171,15 +171,15 @@ fn (mut p Part) parse_faces(row string, start_index int, obj ObjPart) {
// manage negative indexes
// NOTE: not well suporeted now
if v < 0 {
// println("${obj.v.len} ${obj.v.len-c}")
v = obj.v.len - v + 1
// println("${obj_part.v.len} ${obj_part.v.len-c}")
v = obj_part.v.len - v + 1
// exit(0)
}
if n < 0 {
n = obj.vn.len - n + 1
n = obj_part.vn.len - n + 1
}
if t < 0 {
t = obj.vt.len - t + 1
t = obj_part.vt.len - t + 1
}
res << [v - 1, n - 1, t - 1]!
}

View file

@ -128,12 +128,12 @@ pub fn append[T](a []T, b []T) []T {
//
// NOTE: An error will be generated if the type annotation is omitted.
// Example: arrays.group[int]([1, 2, 3], [4, 5, 6]) // => [[1, 4], [2, 5], [3, 6]]
pub fn group[T](arrays ...[]T) [][]T {
mut length := if arrays.len > 0 { arrays[0].len } else { 0 }
pub fn group[T](arrs ...[]T) [][]T {
mut length := if arrs.len > 0 { arrs[0].len } else { 0 }
// calculate length of output by finding shortest input array
for ndx in 1 .. arrays.len {
if arrays[ndx].len < length {
length = arrays[ndx].len
for ndx in 1 .. arrs.len {
if arrs[ndx].len < length {
length = arrs[ndx].len
}
}
@ -141,10 +141,10 @@ pub fn group[T](arrays ...[]T) [][]T {
mut arr := [][]T{cap: length}
// append all combined arrays into the resultant array
for ndx in 0 .. length {
mut grouped := []T{cap: arrays.len}
mut grouped := []T{cap: arrs.len}
// combine each list item for the ndx position into one array
for arr_ndx in 0 .. arrays.len {
grouped << arrays[arr_ndx][ndx]
for arr_ndx in 0 .. arrs.len {
grouped << arrs[arr_ndx][ndx]
}
arr << grouped
}

View file

@ -252,15 +252,15 @@ fn json_parse(s string) &C.cJSON {
// json_string := json_print(encode_User(user))
[markused]
fn json_print(json &C.cJSON) string {
s := C.cJSON_PrintUnformatted(json)
fn json_print(data &C.cJSON) string {
s := C.cJSON_PrintUnformatted(data)
r := unsafe { tos_clone(&u8(s)) }
C.cJSON_free(s)
return r
}
fn json_print_pretty(json &C.cJSON) string {
s := C.cJSON_Print(json)
fn json_print_pretty(data &C.cJSON) string {
s := C.cJSON_Print(data)
r := unsafe { tos_clone(&u8(s)) }
C.cJSON_free(s)
return r

View file

@ -350,25 +350,25 @@ pub fn orm_stmt_gen(sql_dialect SQLDialect, table string, q string, kind StmtKin
// orm - See SelectConfig
// q, num, qm, start_pos - see orm_stmt_gen
// where - See QueryData
pub fn orm_select_gen(orm SelectConfig, q string, num bool, qm string, start_pos int, where QueryData) string {
pub fn orm_select_gen(cfg SelectConfig, q string, num bool, qm string, start_pos int, where QueryData) string {
mut str := 'SELECT '
if orm.is_count {
if cfg.is_count {
str += 'COUNT(*)'
} else {
for i, field in orm.fields {
for i, field in cfg.fields {
str += '${q}${field}${q}'
if i < orm.fields.len - 1 {
if i < cfg.fields.len - 1 {
str += ', '
}
}
}
str += ' FROM ${q}${orm.table}${q}'
str += ' FROM ${q}${cfg.table}${q}'
mut c := start_pos
if orm.has_where {
if cfg.has_where {
str += ' WHERE '
for i, field in where.fields {
mut pre_par := false
@ -402,13 +402,13 @@ pub fn orm_select_gen(orm SelectConfig, q string, num bool, qm string, start_pos
// Note: do not order, if the user did not want it explicitly,
// ordering is *slow*, especially if there are no indexes!
if orm.has_order {
if cfg.has_order {
str += ' ORDER BY '
str += '${q}${orm.order}${q} '
str += orm.order_type.to_str()
str += '${q}${cfg.order}${q} '
str += cfg.order_type.to_str()
}
if orm.has_limit {
if cfg.has_limit {
str += ' LIMIT ${qm}'
if num {
str += '${c}'
@ -416,7 +416,7 @@ pub fn orm_select_gen(orm SelectConfig, q string, num bool, qm string, start_pos
}
}
if orm.has_offset {
if cfg.has_offset {
str += ' OFFSET ${qm}'
if num {
str += '${c}'

View file

@ -374,6 +374,10 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
c.warn('duplicate of a const name `${full_name}`', left.pos)
}
}
if left.name == left.mod && left.name != 'main' {
c.add_error_detail('Module name duplicates will become errors after 2023/10/31.')
c.note('duplicate of a module name `${left.name}`', left.pos)
}
// Check if variable name is already registered as imported module symbol
if c.check_import_sym_conflict(left.name) {
c.error('duplicate of an import symbol `${left.name}`', left.pos)

View file

@ -1652,6 +1652,15 @@ fn (mut c Checker) const_decl(mut node ast.ConstDecl) {
field.expr.pos())
}
}
const_name := field.name.all_after_last('.')
if const_name == c.mod && const_name != 'main' {
name_pos := token.Pos{
...field.pos
len: util.no_cur_mod(field.name, c.mod).len
}
c.add_error_detail('Module name duplicates will become errors after 2023/10/31.')
c.note('duplicate of a module name `${field.name}`', name_pos)
}
c.const_names << field.name
}
for i, mut field in node.fields {

View file

@ -262,6 +262,10 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
}
}
}
if param.name == node.mod && param.name != 'main' {
c.add_error_detail('Module name duplicates will become errors after 2023/10/31.')
c.note('duplicate of a module name `${param.name}`', param.pos)
}
// Check if parameter name is already registered as imported module symbol
if c.check_import_sym_conflict(param.name) {
c.error('duplicate of an import symbol `${param.name}`', param.pos)

View file

@ -0,0 +1,11 @@
vlib/v/checker/tests/mod_name_duplicate_const_err.vv:3:7: notice: duplicate of a module name `foo.foo`
1 | module foo
2 |
3 | const foo = 'bar'
| ~~~
Details: Module name duplicates will become errors after 2023/10/31.
vlib/v/checker/tests/mod_name_duplicate_const_err.vv:1:1: error: project must include a `main` module or be a shared library (compile with `v -shared`)
1 | module foo
| ^
2 |
3 | const foo = 'bar'

View file

@ -0,0 +1,3 @@
module foo
const foo = 'bar'

View file

@ -0,0 +1,13 @@
vlib/v/checker/tests/mod_name_duplicate_param_err.vv:3:8: notice: duplicate of a module name `foo`
1 | module foo
2 |
3 | fn bar(foo string) {
| ~~~
4 | println(foo)
5 | }
Details: Module name duplicates will become errors after 2023/10/31.
vlib/v/checker/tests/mod_name_duplicate_param_err.vv:1:1: error: project must include a `main` module or be a shared library (compile with `v -shared`)
1 | module foo
| ^
2 |
3 | fn bar(foo string) {

View file

@ -0,0 +1,5 @@
module foo
fn bar(foo string) {
println(foo)
}

View file

@ -0,0 +1,13 @@
vlib/v/checker/tests/mod_name_duplicate_var_err.vv:4:2: notice: duplicate of a module name `foo`
2 |
3 | fn bar() {
4 | foo := 'bar'
| ~~~
5 | println(foo)
6 | }
Details: Module name duplicates will become errors after 2023/10/31.
vlib/v/checker/tests/mod_name_duplicate_var_err.vv:1:1: error: project must include a `main` module or be a shared library (compile with `v -shared`)
1 | module foo
| ^
2 |
3 | fn bar() {

View file

@ -0,0 +1,6 @@
module foo
fn bar() {
foo := 'bar'
println(foo)
}