From d4bae356baddac964804f2bd9bd7e1ee5a28cb3e Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Thu, 26 Sep 2019 00:59:50 +1000 Subject: [PATCH] compiler/vlib: add error for no new vars in loop ("_,_") & remove "." from errors --- compiler/main.v | 6 +++--- compiler/msvc.v | 2 +- compiler/parser.v | 15 +++++++++------ compiler/table.v | 4 ++-- compiler/tests/repl/chained_fields.repl | 2 +- vlib/crypto/rand/rand.v | 2 +- vlib/encoding/csv/reader.v | 2 +- vlib/os/os_win.v | 2 +- vlib/szip/szip.v | 10 +++++----- vlib/vweb/assets/assets.v | 2 +- 10 files changed, 25 insertions(+), 22 deletions(-) diff --git a/compiler/main.v b/compiler/main.v index 76a9c93209..c2f2fa1d3b 100644 --- a/compiler/main.v +++ b/compiler/main.v @@ -593,7 +593,7 @@ fn (v mut V) add_v_files_to_compile() { import_path := '$ModPath/vlib/$mod_path' vfiles := v.v_files_from_dir(import_path) if vfiles.len == 0 { - verror('cannot import module $mod (no .v files in "$import_path").') + verror('cannot import module $mod (no .v files in "$import_path")') } // Add all imports referenced by these libs for file in vfiles { @@ -613,7 +613,7 @@ fn (v mut V) add_v_files_to_compile() { import_path := v.find_module_path(mod) vfiles := v.v_files_from_dir(import_path) if vfiles.len == 0 { - verror('cannot import module $mod (no .v files in "$import_path").') + verror('cannot import module $mod (no .v files in "$import_path")') } // Add all imports referenced by these libs for file in vfiles { @@ -632,7 +632,7 @@ fn (v mut V) add_v_files_to_compile() { deps_resolved := dep_graph.resolve() if !deps_resolved.acyclic { deps_resolved.display() - verror('Import cycle detected.') + verror('Import cycle detected') } // add imports in correct order for mod in deps_resolved.imports() { diff --git a/compiler/msvc.v b/compiler/msvc.v index f6bd5aafd6..d2937f25cf 100644 --- a/compiler/msvc.v +++ b/compiler/msvc.v @@ -213,7 +213,7 @@ pub fn (v mut V) cc_msvc() { if !v.pref.is_debug && v.out_name_c != 'v.c' && v.out_name_c != 'v_macos.c' { os.rm(v.out_name_c) } - verror('Cannot find MSVC on this OS.') + verror('Cannot find MSVC on this OS') return } diff --git a/compiler/parser.v b/compiler/parser.v index 4d8fdf8d4b..fc282a7690 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -354,7 +354,7 @@ fn (p mut Parser) import_statement() { p.error('bad import format') } if p.peek() == .number && p.scanner.text[p.scanner.pos + 1] == `.` { - p.error('bad import format. module/submodule names cannot begin with a number.') + p.error('bad import format. module/submodule names cannot begin with a number') } mut mod := p.check_name().trim_space() mut mod_alias := mod @@ -1291,7 +1291,7 @@ fn ($v.name mut $v.typ) $p.cur_fn.name (...) { ') } } - p.error('`$v.name` is immutable.') + p.error('`$v.name` is immutable') } if !v.is_changed { p.mark_var_changed(v) @@ -1379,7 +1379,7 @@ fn (p mut Parser) var_decl() { for i, name in names { if name == '_' { if names.len == 1 { - p.error('no new variables on left side of :=') + p.error('no new variables on left side of `:=`') } continue } @@ -1400,7 +1400,7 @@ fn (p mut Parser) var_decl() { if names.len > 1 { if names.len != types.len { mr_fn := p.cgen.cur_line.find_between('=', '(').trim_space() - p.error('assignment mismatch: ${names.len} variables but `$mr_fn` returns $types.len values.') + p.error('assignment mismatch: ${names.len} variables but `$mr_fn` returns $types.len values') } p.gen(';\n') p.gen('$typ $name = ${mr_var_name}.var_$i') @@ -1599,7 +1599,7 @@ fn (p mut Parser) name_expr() string { // Variable for { // TODO remove if name == '_' { - p.error('cannot use `_` as value.') + p.error('cannot use `_` as value') } mut v := p.find_var_check_new_var(name) or { break } if ptr { @@ -2634,7 +2634,7 @@ fn (p mut Parser) string_expr() { if fspec == 's' { //println('custom str F=$cformat | format_specifier: "$fspec" | typ: $typ ') if typ != 'string' { - p.error('only V strings can be formatted with a :${cformat} format, but you have given "${val}", which has type ${typ}.') + p.error('only V strings can be formatted with a :${cformat} format, but you have given "${val}", which has type ${typ}') } args = args.all_before_last('${val}.len, ${val}.str') + '${val}.str' } @@ -3137,6 +3137,9 @@ fn (p mut Parser) for_st() { i := p.check_name() p.check(.comma) val := p.check_name() + if i == '_' && val == '_' { + p.error('no new variables on the left side of `in`') + } p.fgen(' ') p.check(.key_in) p.fgen(' ') diff --git a/compiler/table.v b/compiler/table.v index ee21282584..4e3c44ad8f 100644 --- a/compiler/table.v +++ b/compiler/table.v @@ -870,7 +870,7 @@ fn (fit mut FileImportTable) register_alias(alias string, mod string) { // NOTE: come back here // if alias in fit.imports && fit.imports[alias] == mod {} if alias in fit.imports && fit.imports[alias] != mod { - verror('cannot import $mod as $alias: import name $alias already in use in "${fit.file_path}".') + verror('cannot import $mod as $alias: import name $alias already in use in "${fit.file_path}"') } if mod.contains('.internal.') { mod_parts := mod.split('.') @@ -881,7 +881,7 @@ fn (fit mut FileImportTable) register_alias(alias string, mod string) { } internal_parent := internal_mod_parts.join('.') if !fit.module_name.starts_with(internal_parent) { - verror('module $mod can only be imported internally by libs.') + verror('module $mod can only be imported internally by libs') } } fit.imports[alias] = mod diff --git a/compiler/tests/repl/chained_fields.repl b/compiler/tests/repl/chained_fields.repl index 8784880ef5..ab7faabae5 100644 --- a/compiler/tests/repl/chained_fields.repl +++ b/compiler/tests/repl/chained_fields.repl @@ -62,7 +62,7 @@ struct B { mut: a A } -.vrepl_temp.v:13:15: `c2` is immutable. +.vrepl_temp.v:13:15: `c2` is immutable .vrepl_temp.v:16:12: cannot modify immutable field `e` (type `F`) declare the field with `mut:` struct F { diff --git a/vlib/crypto/rand/rand.v b/vlib/crypto/rand/rand.v index 8b412d2f60..bae85e2dac 100644 --- a/vlib/crypto/rand/rand.v +++ b/vlib/crypto/rand/rand.v @@ -5,7 +5,7 @@ module rand const ( - ReadError = error('crypro.rand.read() error reading random bytes.') + ReadError = error('crypro.rand.read() error reading random bytes') ) // NOTE: temp until we have []bytes(buff) diff --git a/vlib/encoding/csv/reader.v b/vlib/encoding/csv/reader.v index 4e32e75b71..d08f65e2e0 100644 --- a/vlib/encoding/csv/reader.v +++ b/vlib/encoding/csv/reader.v @@ -12,7 +12,7 @@ const ( err_comment_is_delim = error('encoding.csv: comment cannot be the same as delimiter') err_invalid_delim = error('encoding.csv: invalid delimiter') err_eof = error('encoding.csv: end of file') - err_invalid_le = error('encoding.csv: could not find any valid line endings.') + err_invalid_le = error('encoding.csv: could not find any valid line endings') ) diff --git a/vlib/os/os_win.v b/vlib/os/os_win.v index 2ec909f78d..1ec3ee839b 100644 --- a/vlib/os/os_win.v +++ b/vlib/os/os_win.v @@ -133,7 +133,7 @@ pub fn get_module_filename(handle HANDLE) ?string { return _filename default: // Must handled with GetLastError and converted by FormatMessage - return error('Cannot get file name from handle.') + return error('Cannot get file name from handle') } } panic('this should be unreachable') // TODO remove unreachable after loop diff --git a/vlib/szip/szip.v b/vlib/szip/szip.v index a31988fd2e..a1feea0664 100644 --- a/vlib/szip/szip.v +++ b/vlib/szip/szip.v @@ -56,7 +56,7 @@ pub fn open(name string, level int, mode string) ?zip_ptr { /* struct zip_t* */_p_zip := zip_ptr(C.zip_open(name.str, _nlevel, mode.str)) if _p_zip == zip_ptr(0) { - return error('szip: cannot open/create/append new zip archive.') + return error('szip: cannot open/create/append new zip archive') } return _p_zip } @@ -130,7 +130,7 @@ pub fn (zentry mut zip_ptr) name() string { pub fn (zentry mut zip_ptr) index() ?int { _index := int(C.zip_entry_index(zentry)) if _index == -1 { - return error('szip: cannot get current index of zip entry.') + return error('szip: cannot get current index of zip entry') } return _index // must be check for INVALID_VALUE } @@ -146,7 +146,7 @@ pub fn (zentry mut zip_ptr) index() ?int { pub fn (zentry mut zip_ptr) isdir() ?bool { _isdir := C.zip_entry_isdir(zentry) if _isdir == -1 { - return error('szip: cannot check entry type.') + return error('szip: cannot check entry type') } dir := bool(_isdir) // wtf V , unary lvalue return dir @@ -227,7 +227,7 @@ pub fn (zentry mut zip_ptr) read_entry() ?voidptr { mut _bsize := i64(0) res := C.zip_entry_read(zentry, &_buf, &_bsize) if res == -1 { - return error('szip: cannot read properly data from entry.') + return error('szip: cannot read properly data from entry') } return _buf } @@ -279,7 +279,7 @@ pub fn (zentry mut zip_ptr) extract_entry(path string) /*?*/bool { pub fn (zentry mut zip_ptr) total() ?int { _tentry := int(C.zip_total_entries(zentry)) if _tentry == -1 { - return error('szip: cannot count total entries.') + return error('szip: cannot count total entries') } return _tentry } diff --git a/vlib/vweb/assets/assets.v b/vlib/vweb/assets/assets.v index addd82fd8f..c830558fea 100644 --- a/vlib/vweb/assets/assets.v +++ b/vlib/vweb/assets/assets.v @@ -154,7 +154,7 @@ fn (am mut AssetManager) include(asset_type string, combine bool) string { // fn (am mut AssetManager) add(asset_type, file string) ?bool { fn (am mut AssetManager) add(asset_type, file string) bool { if !os.file_exists(file) { - // return error('vweb.assets: cannot add asset $file, it does not exist.') + // return error('vweb.assets: cannot add asset $file, it does not exist') return false } asset := Asset{