checker: do not allow copying any map lvalue (#8662)

pull/8790/head
Nick Treleaven 2021-02-16 11:46:12 +00:00 committed by GitHub
parent 51c286df5a
commit 01aa09d515
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 20 additions and 23 deletions

View File

@ -256,7 +256,7 @@ fn (mut context Context) run() {
summary[k] = s summary[k] = s
} }
// merge current raw results to the previous ones // 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{} mut new_oms := map[string][]int{}
for k, v in m { for k, v in m {
if old_oms[k].len == 0 { if old_oms[k].len == 0 {
@ -266,7 +266,7 @@ fn (mut context Context) run() {
new_oms[k] << v new_oms[k] << v
} }
} }
context.results[icmd].oms = new_oms context.results[icmd].oms = new_oms.move()
// println('') // println('')
} }
} }
@ -276,7 +276,7 @@ fn (mut context Context) run() {
for k, v in context.results[icmd].oms { for k, v in context.results[icmd].oms {
new_full_summary[k] = new_aints(v, context.nmins, context.nmaxs) 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()
} }
} }

View File

@ -35,13 +35,12 @@ fn (mut a App) collect_info() {
}) })
} }
if os_kind == 'linux' { if os_kind == 'linux' {
info := a.cpu_info()
mut cpu_details := '' mut cpu_details := ''
if cpu_details == '' { if cpu_details == '' {
cpu_details = info['model name'] cpu_details = a.cpu_info('model name')
} }
if cpu_details == '' { if cpu_details == '' {
cpu_details = info['hardware'] cpu_details = a.cpu_info('hardware')
} }
if cpu_details == '' { if cpu_details == '' {
cpu_details = os.uname().machine cpu_details = os.uname().machine
@ -61,8 +60,7 @@ fn (mut a App) collect_info() {
}) })
if os_kind == 'linux' { if os_kind == 'linux' {
os_details = a.get_linux_os_name() os_details = a.get_linux_os_name()
info := a.cpu_info() if 'hypervisor' in a.cpu_info('flags') {
if 'hypervisor' in info['flags'] {
if 'microsoft' in wsl_check { if 'microsoft' in wsl_check {
// WSL 2 is a Managed VM and Full Linux Kernel // WSL 2 is a Managed VM and Full Linux Kernel
// See https://docs.microsoft.com/en-us/windows/wsl/compare-versions // 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 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 { if a.cached_cpuinfo.len > 0 {
return a.cached_cpuinfo return a.cached_cpuinfo[key]
} }
info := os.exec('cat /proc/cpuinfo') or { info := os.exec('cat /proc/cpuinfo') or {
return a.cached_cpuinfo return a.cached_cpuinfo[key]
} }
vals := a.parse(info.output, ':') a.cached_cpuinfo = a.parse(info.output, ':')
a.cached_cpuinfo = vals return a.cached_cpuinfo[key]
return vals
} }
fn (mut a App) git_info() string { fn (mut a App) git_info() string {

View File

@ -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 { fn build_url_from_fetch(_url string, config FetchConfig) ?string {
mut url := urllib.parse(_url) ? mut url := urllib.parse(_url) ?
params := config.params params := unsafe {config.params}
if params.keys().len == 0 { if params.len == 0 {
return url.str() return url.str()
} }
mut pieces := []string{} mut pieces := []string{}

View File

@ -2717,8 +2717,8 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
assign_stmt.pos) assign_stmt.pos)
} }
if left_sym.kind == .map && !c.inside_unsafe && assign_stmt.op in [.assign, .decl_assign] 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_sym.kind == .map && !right_type.is_ptr() && !left.is_blank_ident()
&& right is ast.Ident { && right.is_lvalue() {
// Do not allow `a = b` // Do not allow `a = b`
c.error('cannot copy map: call `move` or `clone` method first (or use `unsafe`)', c.error('cannot copy map: call `move` or `clone` method first (or use `unsafe`)',
right.position()) right.position())
@ -5596,7 +5596,7 @@ fn (mut c Checker) sql_expr(mut node ast.SqlExpr) table.Type {
sub_structs[int(f.typ)] = n sub_structs[int(f.typ)] = n
} }
node.fields = fields node.fields = fields
node.sub_structs = sub_structs node.sub_structs = sub_structs.move()
if node.has_where { if node.has_where {
c.expr(node.where_expr) 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 sub_structs[int(f.typ)] = n
} }
node.fields = fields node.fields = fields
node.sub_structs = sub_structs node.sub_structs = sub_structs.move()
c.expr(node.db_expr) c.expr(node.db_expr)
if node.kind == .update { if node.kind == .update {
for expr in node.update_exprs { for expr in node.update_exprs {

View File

@ -115,7 +115,7 @@ pub fn gen(files []ast.File, table &table.Table, pref &pref.Preferences) string
out += '/** @namespace $name */\n' out += '/** @namespace $name */\n'
} }
out += 'const $name = (function (' out += 'const $name = (function ('
imports := g.namespaces[node.name].imports imports := unsafe {g.namespaces[node.name].imports}
for i, key in imports.keys() { for i, key in imports.keys() {
if i > 0 { if i > 0 {
out += ', ' out += ', '

View File

@ -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_fns = walker.used_fns.move()
the_table.used_consts = walker.used_consts the_table.used_consts = walker.used_consts.move()
$if trace_skip_unused ? { $if trace_skip_unused ? {
eprintln('>> the_table.used_fns: $the_table.used_fns.keys()') eprintln('>> the_table.used_fns: $the_table.used_fns.keys()')