Skip to content

Commit 1e38cc0

Browse files
authored
checker: disallow module name duplicates in local names (#18118)
1 parent 3a2994b commit 1e38cc0

File tree

13 files changed

+96
-28
lines changed

13 files changed

+96
-28
lines changed

examples/sokol/06_obj_viewer/modules/obj/obj.v

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ fn (mut m ObjPart) parse_floats(row string, start_index int) m4.Vec4 {
138138
}
139139

140140
// read and manage all the faes from an .obj file data
141-
fn (mut p Part) parse_faces(row string, start_index int, obj ObjPart) {
141+
fn (mut p Part) parse_faces(row string, start_index int, obj_part ObjPart) {
142142
mut i := start_index + 1
143143
mut res := [][3]int{}
144144
mut v := 0
@@ -171,15 +171,15 @@ fn (mut p Part) parse_faces(row string, start_index int, obj ObjPart) {
171171
// manage negative indexes
172172
// NOTE: not well suporeted now
173173
if v < 0 {
174-
// println("${obj.v.len} ${obj.v.len-c}")
175-
v = obj.v.len - v + 1
174+
// println("${obj_part.v.len} ${obj_part.v.len-c}")
175+
v = obj_part.v.len - v + 1
176176
// exit(0)
177177
}
178178
if n < 0 {
179-
n = obj.vn.len - n + 1
179+
n = obj_part.vn.len - n + 1
180180
}
181181
if t < 0 {
182-
t = obj.vt.len - t + 1
182+
t = obj_part.vt.len - t + 1
183183
}
184184
res << [v - 1, n - 1, t - 1]!
185185
}

vlib/arrays/arrays.v

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,23 @@ pub fn append[T](a []T, b []T) []T {
128128
//
129129
// NOTE: An error will be generated if the type annotation is omitted.
130130
// Example: arrays.group[int]([1, 2, 3], [4, 5, 6]) // => [[1, 4], [2, 5], [3, 6]]
131-
pub fn group[T](arrays ...[]T) [][]T {
132-
mut length := if arrays.len > 0 { arrays[0].len } else { 0 }
131+
pub fn group[T](arrs ...[]T) [][]T {
132+
mut length := if arrs.len > 0 { arrs[0].len } else { 0 }
133133
// calculate length of output by finding shortest input array
134-
for ndx in 1 .. arrays.len {
135-
if arrays[ndx].len < length {
136-
length = arrays[ndx].len
134+
for ndx in 1 .. arrs.len {
135+
if arrs[ndx].len < length {
136+
length = arrs[ndx].len
137137
}
138138
}
139139

140140
if length > 0 {
141141
mut arr := [][]T{cap: length}
142142
// append all combined arrays into the resultant array
143143
for ndx in 0 .. length {
144-
mut grouped := []T{cap: arrays.len}
144+
mut grouped := []T{cap: arrs.len}
145145
// combine each list item for the ndx position into one array
146-
for arr_ndx in 0 .. arrays.len {
147-
grouped << arrays[arr_ndx][ndx]
146+
for arr_ndx in 0 .. arrs.len {
147+
grouped << arrs[arr_ndx][ndx]
148148
}
149149
arr << grouped
150150
}

vlib/json/json_primitives.v

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,15 @@ fn json_parse(s string) &C.cJSON {
252252

253253
// json_string := json_print(encode_User(user))
254254
[markused]
255-
fn json_print(json &C.cJSON) string {
256-
s := C.cJSON_PrintUnformatted(json)
255+
fn json_print(data &C.cJSON) string {
256+
s := C.cJSON_PrintUnformatted(data)
257257
r := unsafe { tos_clone(&u8(s)) }
258258
C.cJSON_free(s)
259259
return r
260260
}
261261

262-
fn json_print_pretty(json &C.cJSON) string {
263-
s := C.cJSON_Print(json)
262+
fn json_print_pretty(data &C.cJSON) string {
263+
s := C.cJSON_Print(data)
264264
r := unsafe { tos_clone(&u8(s)) }
265265
C.cJSON_free(s)
266266
return r

vlib/orm/orm.v

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -350,25 +350,25 @@ pub fn orm_stmt_gen(sql_dialect SQLDialect, table string, q string, kind StmtKin
350350
// orm - See SelectConfig
351351
// q, num, qm, start_pos - see orm_stmt_gen
352352
// where - See QueryData
353-
pub fn orm_select_gen(orm SelectConfig, q string, num bool, qm string, start_pos int, where QueryData) string {
353+
pub fn orm_select_gen(cfg SelectConfig, q string, num bool, qm string, start_pos int, where QueryData) string {
354354
mut str := 'SELECT '
355355

356-
if orm.is_count {
356+
if cfg.is_count {
357357
str += 'COUNT(*)'
358358
} else {
359-
for i, field in orm.fields {
359+
for i, field in cfg.fields {
360360
str += '${q}${field}${q}'
361-
if i < orm.fields.len - 1 {
361+
if i < cfg.fields.len - 1 {
362362
str += ', '
363363
}
364364
}
365365
}
366366

367-
str += ' FROM ${q}${orm.table}${q}'
367+
str += ' FROM ${q}${cfg.table}${q}'
368368

369369
mut c := start_pos
370370

371-
if orm.has_where {
371+
if cfg.has_where {
372372
str += ' WHERE '
373373
for i, field in where.fields {
374374
mut pre_par := false
@@ -402,21 +402,21 @@ pub fn orm_select_gen(orm SelectConfig, q string, num bool, qm string, start_pos
402402

403403
// Note: do not order, if the user did not want it explicitly,
404404
// ordering is *slow*, especially if there are no indexes!
405-
if orm.has_order {
405+
if cfg.has_order {
406406
str += ' ORDER BY '
407-
str += '${q}${orm.order}${q} '
408-
str += orm.order_type.to_str()
407+
str += '${q}${cfg.order}${q} '
408+
str += cfg.order_type.to_str()
409409
}
410410

411-
if orm.has_limit {
411+
if cfg.has_limit {
412412
str += ' LIMIT ${qm}'
413413
if num {
414414
str += '${c}'
415415
c++
416416
}
417417
}
418418

419-
if orm.has_offset {
419+
if cfg.has_offset {
420420
str += ' OFFSET ${qm}'
421421
if num {
422422
str += '${c}'

vlib/v/checker/assign.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,10 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
374374
c.warn('duplicate of a const name `${full_name}`', left.pos)
375375
}
376376
}
377+
if left.name == left.mod && left.name != 'main' {
378+
c.add_error_detail('Module name duplicates will become errors after 2023/10/31.')
379+
c.note('duplicate of a module name `${left.name}`', left.pos)
380+
}
377381
// Check if variable name is already registered as imported module symbol
378382
if c.check_import_sym_conflict(left.name) {
379383
c.error('duplicate of an import symbol `${left.name}`', left.pos)

vlib/v/checker/checker.v

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,15 @@ fn (mut c Checker) const_decl(mut node ast.ConstDecl) {
16521652
field.expr.pos())
16531653
}
16541654
}
1655+
const_name := field.name.all_after_last('.')
1656+
if const_name == c.mod && const_name != 'main' {
1657+
name_pos := token.Pos{
1658+
...field.pos
1659+
len: util.no_cur_mod(field.name, c.mod).len
1660+
}
1661+
c.add_error_detail('Module name duplicates will become errors after 2023/10/31.')
1662+
c.note('duplicate of a module name `${field.name}`', name_pos)
1663+
}
16551664
c.const_names << field.name
16561665
}
16571666
for i, mut field in node.fields {

vlib/v/checker/fn.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,10 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
262262
}
263263
}
264264
}
265+
if param.name == node.mod && param.name != 'main' {
266+
c.add_error_detail('Module name duplicates will become errors after 2023/10/31.')
267+
c.note('duplicate of a module name `${param.name}`', param.pos)
268+
}
265269
// Check if parameter name is already registered as imported module symbol
266270
if c.check_import_sym_conflict(param.name) {
267271
c.error('duplicate of an import symbol `${param.name}`', param.pos)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
vlib/v/checker/tests/mod_name_duplicate_const_err.vv:3:7: notice: duplicate of a module name `foo.foo`
2+
1 | module foo
3+
2 |
4+
3 | const foo = 'bar'
5+
| ~~~
6+
Details: Module name duplicates will become errors after 2023/10/31.
7+
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`)
8+
1 | module foo
9+
| ^
10+
2 |
11+
3 | const foo = 'bar'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module foo
2+
3+
const foo = 'bar'
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
vlib/v/checker/tests/mod_name_duplicate_param_err.vv:3:8: notice: duplicate of a module name `foo`
2+
1 | module foo
3+
2 |
4+
3 | fn bar(foo string) {
5+
| ~~~
6+
4 | println(foo)
7+
5 | }
8+
Details: Module name duplicates will become errors after 2023/10/31.
9+
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`)
10+
1 | module foo
11+
| ^
12+
2 |
13+
3 | fn bar(foo string) {

0 commit comments

Comments
 (0)