From 2b058edc989ace96e0795e54022583b89b113755 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 17 Jan 2021 06:24:03 +0100 Subject: [PATCH] checker: do not require ref field init with update expr --- vlib/v/checker/checker.v | 52 +++++++++++++++++++++------------------- vlib/v/parser/comptime.v | 9 ++++--- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index b9299fbcc4..52e8fc3b90 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -633,7 +633,7 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) table.Type { if field.has_default_expr || field.name in inited_fields { continue } - if field.typ.is_ptr() && !c.pref.translated { + if field.typ.is_ptr() && !struct_init.has_update_expr && !c.pref.translated { c.error('reference field `${type_sym.name}.$field.name` must be initialized', struct_init.pos) } @@ -3299,29 +3299,7 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type { return c.at_expr(mut node) } ast.ComptimeCall { - node.sym = c.table.get_type_symbol(c.unwrap_generic(c.expr(node.left))) - if node.is_embed { - c.file.embedded_files << node.embed_file - return c.table.find_type_idx('v.embed_file.EmbedFileData') - } - if node.is_vweb { - // TODO assoc parser bug - pref := *c.pref - pref2 := { - pref | - is_vweb: true - } - mut c2 := new_checker(c.table, pref2) - c2.check(node.vweb_tmpl) - c.warnings << c2.warnings - c.errors << c2.errors - c.nr_warnings += c2.nr_warnings - c.nr_errors += c2.nr_errors - } - if node.method_name == 'html' { - return c.table.find_type_idx('vweb.Result') - } - return table.string_type + return c.comptime_call(mut node) } ast.ComptimeSelector { node.left_type = c.unwrap_generic(c.expr(node.left)) @@ -3556,6 +3534,32 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) table.Type { return node.typ } +fn (mut c Checker) comptime_call(mut node ast.ComptimeCall) table.Type { + node.sym = c.table.get_type_symbol(c.unwrap_generic(c.expr(node.left))) + if node.is_embed { + c.file.embedded_files << node.embed_file + return c.table.find_type_idx('v.embed_file.EmbedFileData') + } + if node.is_vweb { + // TODO assoc parser bug + pref := *c.pref + pref2 := { + pref | + is_vweb: true + } + mut c2 := new_checker(c.table, pref2) + c2.check(node.vweb_tmpl) + c.warnings << c2.warnings + c.errors << c2.errors + c.nr_warnings += c2.nr_warnings + c.nr_errors += c2.nr_errors + } + if node.method_name == 'html' { + return c.table.find_type_idx('vweb.Result') + } + return table.string_type +} + fn (mut c Checker) at_expr(mut node ast.AtExpr) table.Type { match node.kind { .fn_name { diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index 85da8c818c..26f4adf37b 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -156,8 +156,8 @@ fn (mut p Parser) comp_call() ast.ComptimeCall { println('\n\n') } mut file := parse_comptime(v_code, p.table, p.pref, scope, p.global_scope) - file = { - file | + file = ast.File{ + ...file path: tmpl_path } // copy vars from current fn scope into vweb_tmpl scope @@ -170,7 +170,10 @@ fn (mut p Parser) comp_call() ast.ComptimeCall { if obj is ast.Var { mut v := obj v.pos = stmt.body_pos - tmpl_scope.register(v) + tmpl_scope.register(ast.Var{ + ...v + is_used: true + }) // set the controller action var to used // if it's unused in the template it will warn v.is_used = true