From d11fb8497ae9b0a293b0cba43598b8979367f539 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 5 Apr 2021 21:21:46 +0300 Subject: [PATCH] ci: fix some of `v test-cleancode` 3 --- cmd/tools/vtest-cleancode.v | 2 ++ vlib/builtin/linux_bare/linuxsys_bare.v | 10 +++++----- vlib/builtin/linux_bare/mm_bare.v | 14 +++++++------- vlib/builtin/linux_bare/string_bare.v | 16 ++++++++-------- vlib/builtin/map.v | 10 +++++----- vlib/builtin/map_test.v | 6 +++--- vlib/builtin/option.v | 2 +- vlib/builtin/sorted_map.v | 2 +- vlib/v/checker/checker.v | 6 +++++- 9 files changed, 37 insertions(+), 31 deletions(-) diff --git a/cmd/tools/vtest-cleancode.v b/cmd/tools/vtest-cleancode.v index 49ff869e08..95eed82fd0 100644 --- a/cmd/tools/vtest-cleancode.v +++ b/cmd/tools/vtest-cleancode.v @@ -18,6 +18,8 @@ const ( ] verify_known_failing_exceptions = [ 'vlib/builtin/int_test.v' /* special number formatting that should be tested */, + 'vlib/builtin/int.v' /* vfmt converts `pub fn (nn byteptr) str() string {` to `nn &byte` and that conflicts with `nn byte` */, + 'vlib/builtin/string_charptr_byteptr_helpers.v' /* a temporary shim to ease the byteptr=>&byte transition */, 'vlib/gg/m4/graphic.v' /* has hand crafted meaningful formatting of matrices */, 'vlib/gg/m4/m4_test.v' /* has hand crafted meaningful formatting of matrices */, 'vlib/gg/m4/matrix.v' /* has hand crafted meaningful formatting of matrices */, diff --git a/vlib/builtin/linux_bare/linuxsys_bare.v b/vlib/builtin/linux_bare/linuxsys_bare.v index 7c4e1aa415..10748113b0 100644 --- a/vlib/builtin/linux_bare/linuxsys_bare.v +++ b/vlib/builtin/linux_bare/linuxsys_bare.v @@ -337,16 +337,16 @@ fn split_int_errno(rc_in u64) (i64, Errno) { } // 0 sys_read unsigned int fd char *buf size_t count -pub fn sys_read(fd i64, buf byteptr, count u64) (i64, Errno) { +pub fn sys_read(fd i64, buf &byte, count u64) (i64, Errno) { return split_int_errno(sys_call3(0, u64(fd), u64(buf), count)) } // 1 sys_write unsigned int fd, const char *buf, size_t count -pub fn sys_write(fd i64, buf byteptr, count u64) (i64, Errno) { +pub fn sys_write(fd i64, buf &byte, count u64) (i64, Errno) { return split_int_errno(sys_call3(1, u64(fd), u64(buf), count)) } -pub fn sys_open(filename byteptr, flags i64, mode int) (i64, Errno) { +pub fn sys_open(filename &byte, flags i64, mode int) (i64, Errno) { // 2 sys_open const char *filename int flags int mode return split_int_errno(sys_call3(2, u64(filename), u64(flags), u64(mode))) } @@ -357,10 +357,10 @@ pub fn sys_close(fd i64) Errno { } // 9 sys_mmap unsigned long addr unsigned long len unsigned long prot unsigned long flags unsigned long fd unsigned long off -pub fn sys_mmap(addr byteptr, len u64, prot Mm_prot, flags Map_flags, fildes u64, off u64) (byteptr, Errno) { +pub fn sys_mmap(addr &byte, len u64, prot Mm_prot, flags Map_flags, fildes u64, off u64) (&byte, Errno) { rc := sys_call6(9, u64(addr), len, u64(prot), u64(flags), fildes, off) a, e := split_int_errno(rc) - return byteptr(a), e + return &byte(a), e } pub fn sys_munmap(addr voidptr, len u64) Errno { diff --git a/vlib/builtin/linux_bare/mm_bare.v b/vlib/builtin/linux_bare/mm_bare.v index a651fdc908..cee5f99857 100644 --- a/vlib/builtin/linux_bare/mm_bare.v +++ b/vlib/builtin/linux_bare/mm_bare.v @@ -11,7 +11,7 @@ pub fn mm_pages(size u64) u32 { return u32(pages) } -pub fn mm_alloc(size u64) (byteptr, Errno) { +pub fn mm_alloc(size u64) (&byte, Errno) { pages := mm_pages(size) n_bytes := u64(pages * u32(Linux_mem.page_size)) @@ -19,12 +19,12 @@ pub fn mm_alloc(size u64) (byteptr, Errno) { if e == .enoerror { mut ap := &int(a) *ap = pages - return byteptr(a + 4), e + return &byte(a + 4), e } - return byteptr(0), e + return &byte(0), e } -pub fn mm_free(addr byteptr) Errno { +pub fn mm_free(addr &byte) Errno { ap := &int(addr - 4) size := u64(*ap) * u64(Linux_mem.page_size) @@ -32,8 +32,8 @@ pub fn mm_free(addr byteptr) Errno { } pub fn mem_copy(dest0 voidptr, src0 voidptr, n int) voidptr { - mut dest := byteptr(dest0) - src := byteptr(src0) + mut dest := &byte(dest0) + src := &byte(src0) for i in 0 .. n { dest[i] = src[i] } @@ -41,7 +41,7 @@ pub fn mem_copy(dest0 voidptr, src0 voidptr, n int) voidptr { } [unsafe] -pub fn malloc(n int) byteptr { +pub fn malloc(n int) &byte { if n < 0 { panic('malloc(<0)') } diff --git a/vlib/builtin/linux_bare/string_bare.v b/vlib/builtin/linux_bare/string_bare.v index 122dd989f9..06f95d429e 100644 --- a/vlib/builtin/linux_bare/string_bare.v +++ b/vlib/builtin/linux_bare/string_bare.v @@ -2,17 +2,17 @@ module builtin pub struct string { pub: - str byteptr + str &byte len int } -pub fn strlen(s byteptr) int { +pub fn strlen(s &byte) int { mut i := 0 for ; s[i] != 0; i++ {} return i } -pub fn tos(s byteptr, len int) string { +pub fn tos(s &byte, len int) string { if s == 0 { panic('tos(): nil string') } @@ -49,7 +49,7 @@ pub fn tos_clone(s byteptr) string { // Same as `tos`, but calculates the length. Called by `string(bytes)` casts. // Used only internally. -pub fn tos2(s byteptr) string { +pub fn tos2(s &byte) string { if s == 0 { panic('tos2: nil string') } @@ -59,13 +59,13 @@ pub fn tos2(s byteptr) string { } } -pub fn tos3(s charptr) string { +pub fn tos3(s &char) string { if s == 0 { panic('tos3: nil string') } return string{ - str: byteptr(s) - len: strlen(byteptr(s)) + str: &byte(s) + len: strlen(&byte(s)) } } @@ -85,7 +85,7 @@ pub fn string_ne(s1 string, s2 string) bool { return !string_eq(s1, s2) } -pub fn i64_tos(buf byteptr, len int, n0 i64, base int) string { +pub fn i64_tos(buf &byte, len int, n0 i64, base int) string { if base < 2 { panic('base must be >= 2') } diff --git a/vlib/builtin/map.v b/vlib/builtin/map.v index c8ad84bcfd..f2d982a623 100644 --- a/vlib/builtin/map.v +++ b/vlib/builtin/map.v @@ -349,8 +349,8 @@ fn new_map_2(key_bytes int, value_bytes int, hash_fn MapHashFn, key_eq_fn MapEqF fn new_map_init_2(hash_fn MapHashFn, key_eq_fn MapEqFn, clone_fn MapCloneFn, free_fn MapFreeFn, n int, key_bytes int, value_bytes int, keys voidptr, values voidptr) map { mut out := new_map_2(key_bytes, value_bytes, hash_fn, key_eq_fn, clone_fn, free_fn) // TODO pre-allocate n slots - mut pkey := byteptr(keys) - mut pval := byteptr(values) + mut pkey := &byte(keys) + mut pval := &byte(values) for _ in 0 .. n { unsafe { out.set_1(pkey, pval) @@ -513,7 +513,7 @@ fn (mut m map) rehash() { fn (mut m map) cached_rehash(old_cap u32) { old_metas := m.metas metasize := int(sizeof(u32) * (m.even_index + 2 + m.extra_metas)) - m.metas = unsafe { &u32(vcalloc(metasize)) } + m.metas = unsafe { &u32(vcalloc(metasize)) } old_extra_metas := m.extra_metas for i := u32(0); i <= old_cap + old_extra_metas; i += 2 { if unsafe { old_metas[i] } == 0 { @@ -688,7 +688,7 @@ pub fn (mut m map) delete_1(key voidptr) { // delete this pub fn (m &map) keys() []string { mut keys := []string{len: m.len} - mut item := unsafe { byteptr(keys.data) } + mut item := unsafe { &byte(keys.data) } for i := 0; i < m.key_values.len; i++ { if !m.key_values.has_index(i) { continue @@ -705,7 +705,7 @@ pub fn (m &map) keys() []string { // Returns all keys in the map. fn (m &map) keys_1() array { mut keys := __new_array(m.len, 0, m.key_bytes) - mut item := unsafe { byteptr(keys.data) } + mut item := unsafe { &byte(keys.data) } if m.key_values.deletes == 0 { for i := 0; i < m.key_values.len; i++ { unsafe { diff --git a/vlib/builtin/map_test.v b/vlib/builtin/map_test.v index 8b41284542..469596d29c 100644 --- a/vlib/builtin/map_test.v +++ b/vlib/builtin/map_test.v @@ -196,9 +196,9 @@ fn test_various_map_value() { mut m14 := map[string]voidptr{} m14['test'] = voidptr(0) assert m14['test'] == voidptr(0) - mut m15 := map[string]byteptr{} - m15['test'] = byteptr(0) - assert m15['test'] == byteptr(0) + mut m15 := map[string]&byte{} + m15['test'] = &byte(0) + assert m15['test'] == &byte(0) mut m16 := map[string]i64{} m16['test'] = i64(0) assert m16['test'] == i64(0) diff --git a/vlib/builtin/option.v b/vlib/builtin/option.v index 054965f560..6ab693ac2f 100644 --- a/vlib/builtin/option.v +++ b/vlib/builtin/option.v @@ -67,7 +67,7 @@ fn opt_ok(data voidptr, mut option Option, size int) { unsafe { *option = Option{} // use err to get the end of OptionBase and then memcpy into it - C.memcpy(byteptr(&option.err) + sizeof(IError), data, size) + C.memcpy(&byte(&option.err) + sizeof(IError), data, size) } } diff --git a/vlib/builtin/sorted_map.v b/vlib/builtin/sorted_map.v index bb28d1dc99..2567a3cca4 100644 --- a/vlib/builtin/sorted_map.v +++ b/vlib/builtin/sorted_map.v @@ -49,7 +49,7 @@ fn new_sorted_map_init(n int, value_bytes int, keys &string, values voidptr) Sor mut out := new_sorted_map(n, value_bytes) for i in 0 .. n { unsafe { - out.set(keys[i], byteptr(values) + i * value_bytes) + out.set(keys[i], &byte(values) + i * value_bytes) } } return out diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index a9aa4e578d..544f83cc8c 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3126,7 +3126,11 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) { && left_sym.kind != .interface_ { // Dual sides check (compatibility check) c.check_expected(right_type_unwrapped, left_type_unwrapped) or { - c.error('cannot assign to `$left`: $err.msg', right.position()) + // allow for ptr += 2 + if !left_type_unwrapped.is_ptr() && !right_type_unwrapped.is_int() + && assign_stmt.op !in [.plus_assign, .minus_assign] { + c.error('cannot assign to `$left`: $err.msg', right.position()) + } } } if left_sym.kind == .interface_ {