checker: fix -skip-unused with struct inits with default expressions

pull/8672/head^2
Delyan Angelov 2021-02-10 18:25:52 +02:00
parent eb7009b60a
commit 4646c414d8
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 19 additions and 5 deletions

View File

@ -131,6 +131,7 @@ fn (mut c Checker) mark_used(ast_files []ast.File) {
}
mut walker := mark_used_walker.Walker{
table: c.table
files: ast_files
all_fns: all_fns
all_consts: all_consts

View File

@ -5,9 +5,11 @@ module mark_used_walker
// This module walks the entire program starting at fn main and marks used (called) functions.
// Unused functions can be safely skipped by the backends to save CPU time and space.
import v.ast
import v.table
pub struct Walker {
pub mut:
table &table.Table
used_fns map[string]bool // used_fns['println'] == true
used_consts map[string]bool // used_consts['os.args'] == true
n_maps int
@ -258,8 +260,19 @@ fn (mut w Walker) expr(node ast.Expr) {
w.expr(node.where_expr)
}
ast.StructInit {
// eprintln('>>>> ast.StructInit: $node')
w.expr(node.update_expr)
sym := w.table.get_type_symbol(node.typ)
if sym.kind == .struct_ {
info := sym.info as table.Struct
for ifield in info.fields {
if ifield.has_default_expr {
defex := ast.fe2ex(ifield.default_expr)
w.expr(defex)
}
}
}
if node.has_update_expr {
w.expr(node.update_expr)
}
for sif in node.fields {
w.expr(sif.expr)
}
@ -321,13 +334,13 @@ pub fn (mut w Walker) fn_decl(mut node ast.FnDecl) {
}
pub fn (mut w Walker) call_expr(mut node ast.CallExpr) {
for arg in node.args {
w.expr(arg.expr)
}
if node.language == .c {
return
}
w.expr(node.left)
for arg in node.args {
w.expr(arg.expr)
}
w.or_block(node.or_block)
//
fn_name := if node.is_method { node.receiver_type.str() + '.' + node.name } else { node.name }