From 01aa09d515b902e459360a003e8f7b6568ae61bf Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Tue, 16 Feb 2021 11:46:12 +0000 Subject: [PATCH] checker: do not allow copying any map lvalue (#8662) --- cmd/tools/repeat.v | 6 +++--- cmd/tools/vdoctor.v | 19 ++++++++----------- vlib/net/http/http.v | 4 ++-- vlib/v/checker/checker.v | 8 ++++---- vlib/v/gen/js/js.v | 2 +- vlib/v/markused/markused.v | 4 ++-- 6 files changed, 20 insertions(+), 23 deletions(-) diff --git a/cmd/tools/repeat.v b/cmd/tools/repeat.v index ee8e750076..12f2a197fc 100644 --- a/cmd/tools/repeat.v +++ b/cmd/tools/repeat.v @@ -256,7 +256,7 @@ fn (mut context Context) run() { summary[k] = s } // merge current raw results to the previous ones - old_oms := context.results[icmd].oms + old_oms := context.results[icmd].oms.move() mut new_oms := map[string][]int{} for k, v in m { if old_oms[k].len == 0 { @@ -266,7 +266,7 @@ fn (mut context Context) run() { new_oms[k] << v } } - context.results[icmd].oms = new_oms + context.results[icmd].oms = new_oms.move() // println('') } } @@ -276,7 +276,7 @@ fn (mut context Context) run() { for k, v in context.results[icmd].oms { new_full_summary[k] = new_aints(v, context.nmins, context.nmaxs) } - context.results[icmd].summary = new_full_summary + context.results[icmd].summary = new_full_summary.move() } } diff --git a/cmd/tools/vdoctor.v b/cmd/tools/vdoctor.v index dc5af3e586..644f2a8fba 100644 --- a/cmd/tools/vdoctor.v +++ b/cmd/tools/vdoctor.v @@ -35,13 +35,12 @@ fn (mut a App) collect_info() { }) } if os_kind == 'linux' { - info := a.cpu_info() mut cpu_details := '' if cpu_details == '' { - cpu_details = info['model name'] + cpu_details = a.cpu_info('model name') } if cpu_details == '' { - cpu_details = info['hardware'] + cpu_details = a.cpu_info('hardware') } if cpu_details == '' { cpu_details = os.uname().machine @@ -61,8 +60,7 @@ fn (mut a App) collect_info() { }) if os_kind == 'linux' { os_details = a.get_linux_os_name() - info := a.cpu_info() - if 'hypervisor' in info['flags'] { + if 'hypervisor' in a.cpu_info('flags') { if 'microsoft' in wsl_check { // WSL 2 is a Managed VM and Full Linux Kernel // See https://docs.microsoft.com/en-us/windows/wsl/compare-versions @@ -233,16 +231,15 @@ fn (mut a App) get_linux_os_name() string { return os_details } -fn (mut a App) cpu_info() map[string]string { +fn (mut a App) cpu_info(key string) string { if a.cached_cpuinfo.len > 0 { - return a.cached_cpuinfo + return a.cached_cpuinfo[key] } info := os.exec('cat /proc/cpuinfo') or { - return a.cached_cpuinfo + return a.cached_cpuinfo[key] } - vals := a.parse(info.output, ':') - a.cached_cpuinfo = vals - return vals + a.cached_cpuinfo = a.parse(info.output, ':') + return a.cached_cpuinfo[key] } fn (mut a App) git_info() string { diff --git a/vlib/net/http/http.v b/vlib/net/http/http.v index 91a0b44c52..2274bb94a4 100644 --- a/vlib/net/http/http.v +++ b/vlib/net/http/http.v @@ -163,8 +163,8 @@ fn fetch_with_method(method Method, url string, _config FetchConfig) ?Response { fn build_url_from_fetch(_url string, config FetchConfig) ?string { mut url := urllib.parse(_url) ? - params := config.params - if params.keys().len == 0 { + params := unsafe {config.params} + if params.len == 0 { return url.str() } mut pieces := []string{} diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 66dece85c6..bd9c8f7e7d 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2717,8 +2717,8 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) { assign_stmt.pos) } if left_sym.kind == .map && !c.inside_unsafe && assign_stmt.op in [.assign, .decl_assign] - && right_sym.kind == .map && (left is ast.Ident && !left.is_blank_ident()) - && right is ast.Ident { + && right_sym.kind == .map && !right_type.is_ptr() && !left.is_blank_ident() + && right.is_lvalue() { // Do not allow `a = b` c.error('cannot copy map: call `move` or `clone` method first (or use `unsafe`)', right.position()) @@ -5596,7 +5596,7 @@ fn (mut c Checker) sql_expr(mut node ast.SqlExpr) table.Type { sub_structs[int(f.typ)] = n } node.fields = fields - node.sub_structs = sub_structs + node.sub_structs = sub_structs.move() if node.has_where { c.expr(node.where_expr) } @@ -5648,7 +5648,7 @@ fn (mut c Checker) sql_stmt(mut node ast.SqlStmt) table.Type { sub_structs[int(f.typ)] = n } node.fields = fields - node.sub_structs = sub_structs + node.sub_structs = sub_structs.move() c.expr(node.db_expr) if node.kind == .update { for expr in node.update_exprs { diff --git a/vlib/v/gen/js/js.v b/vlib/v/gen/js/js.v index 69e1c09f42..e5fe87000c 100644 --- a/vlib/v/gen/js/js.v +++ b/vlib/v/gen/js/js.v @@ -115,7 +115,7 @@ pub fn gen(files []ast.File, table &table.Table, pref &pref.Preferences) string out += '/** @namespace $name */\n' } out += 'const $name = (function (' - imports := g.namespaces[node.name].imports + imports := unsafe {g.namespaces[node.name].imports} for i, key in imports.keys() { if i > 0 { out += ', ' diff --git a/vlib/v/markused/markused.v b/vlib/v/markused/markused.v index 82d21edb0b..500417431c 100644 --- a/vlib/v/markused/markused.v +++ b/vlib/v/markused/markused.v @@ -178,8 +178,8 @@ pub fn mark_used(mut the_table table.Table, pref &pref.Preferences, ast_files [] } } - the_table.used_fns = walker.used_fns - the_table.used_consts = walker.used_consts + the_table.used_fns = walker.used_fns.move() + the_table.used_consts = walker.used_consts.move() $if trace_skip_unused ? { eprintln('>> the_table.used_fns: $the_table.used_fns.keys()')