Skip to content

Commit

Permalink
checker: disallow module name duplicates in local names (#18118)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Sep 8, 2023
1 parent 3a2994b commit 1e38cc0
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 28 deletions.
10 changes: 5 additions & 5 deletions examples/sokol/06_obj_viewer/modules/obj/obj.v
Expand Up @@ -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
Expand Down Expand Up @@ -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]!
}
Expand Down
16 changes: 8 additions & 8 deletions vlib/arrays/arrays.v
Expand Up @@ -128,23 +128,23 @@ 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
}
}

if length > 0 {
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
}
Expand Down
8 changes: 4 additions & 4 deletions vlib/json/json_primitives.v
Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions vlib/orm/orm.v
Expand Up @@ -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
Expand Down Expand Up @@ -402,21 +402,21 @@ 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}'
c++
}
}

if orm.has_offset {
if cfg.has_offset {
str += ' OFFSET ${qm}'
if num {
str += '${c}'
Expand Down
4 changes: 4 additions & 0 deletions vlib/v/checker/assign.v
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions vlib/v/checker/checker.v
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions vlib/v/checker/fn.v
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions vlib/v/checker/tests/mod_name_duplicate_const_err.out
@@ -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'
3 changes: 3 additions & 0 deletions vlib/v/checker/tests/mod_name_duplicate_const_err.vv
@@ -0,0 +1,3 @@
module foo

const foo = 'bar'
13 changes: 13 additions & 0 deletions vlib/v/checker/tests/mod_name_duplicate_param_err.out
@@ -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) {
5 changes: 5 additions & 0 deletions vlib/v/checker/tests/mod_name_duplicate_param_err.vv
@@ -0,0 +1,5 @@
module foo

fn bar(foo string) {
println(foo)
}
13 changes: 13 additions & 0 deletions vlib/v/checker/tests/mod_name_duplicate_var_err.out
@@ -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() {
6 changes: 6 additions & 0 deletions vlib/v/checker/tests/mod_name_duplicate_var_err.vv
@@ -0,0 +1,6 @@
module foo

fn bar() {
foo := 'bar'
println(foo)
}

0 comments on commit 1e38cc0

Please sign in to comment.