Skip to content

Commit 602b097

Browse files
authored
markused: fixing missing info tracking on walker for {...foo}, struct embeds, map[k]Struct and others (#23008)
1 parent a66c4c9 commit 602b097

File tree

1 file changed

+58
-23
lines changed

1 file changed

+58
-23
lines changed

vlib/v/markused/walker.v

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ pub fn (mut w Walker) mark_global_as_used(ckey string) {
8080
w.used_globals[ckey] = true
8181
gfield := w.all_globals[ckey] or { return }
8282
w.expr(gfield.expr)
83+
if !gfield.has_expr && gfield.typ != 0 {
84+
sym := w.table.sym(gfield.typ)
85+
if sym.info is ast.Struct {
86+
w.a_struct_info(sym.name, sym.info)
87+
}
88+
}
8389
}
8490

8591
pub fn (mut w Walker) mark_root_fns(all_fn_root_names []string) {
@@ -434,6 +440,9 @@ fn (mut w Walker) expr(node_ ast.Expr) {
434440
ast.MapInit {
435441
w.exprs(node.keys)
436442
w.exprs(node.vals)
443+
if node.has_update_expr {
444+
w.expr(node.update_expr)
445+
}
437446
w.features.used_maps++
438447
}
439448
ast.MatchExpr {
@@ -489,9 +498,8 @@ fn (mut w Walker) expr(node_ ast.Expr) {
489498
return
490499
}
491500
sym := w.table.sym(node.typ)
492-
if sym.kind == .struct {
493-
info := sym.info as ast.Struct
494-
w.a_struct_info(sym.name, info)
501+
if sym.info is ast.Struct {
502+
w.a_struct_info(sym.name, sym.info)
495503
}
496504
if node.has_update_expr {
497505
w.expr(node.update_expr)
@@ -557,15 +565,40 @@ pub fn (mut w Walker) a_struct_info(sname string, info ast.Struct) {
557565
}
558566
if ifield.typ != 0 {
559567
fsym := w.table.sym(ifield.typ)
560-
if fsym.kind == .map {
561-
w.features.used_maps++
562-
} else if fsym.kind == .array {
563-
w.features.used_arrays++
564-
} else if fsym.kind == .struct {
565-
w.a_struct_info(fsym.name, fsym.struct_info())
568+
match fsym.info {
569+
ast.Struct {
570+
w.a_struct_info(fsym.name, fsym.info)
571+
}
572+
ast.Alias {
573+
value_sym := w.table.final_sym(ifield.typ)
574+
if value_sym.info is ast.Struct {
575+
w.a_struct_info(value_sym.name, value_sym.info)
576+
}
577+
}
578+
ast.Array, ast.ArrayFixed {
579+
w.features.used_arrays++
580+
value_sym := w.table.final_sym(w.table.value_type(ifield.typ))
581+
if value_sym.info is ast.Struct {
582+
w.a_struct_info(value_sym.name, value_sym.info)
583+
}
584+
}
585+
ast.Map {
586+
w.features.used_maps++
587+
value_sym := w.table.final_sym(w.table.value_type(ifield.typ))
588+
if value_sym.info is ast.Struct {
589+
w.a_struct_info(value_sym.name, value_sym.info)
590+
}
591+
}
592+
else {}
566593
}
567594
}
568595
}
596+
for embed in info.embeds {
597+
sym := w.table.final_sym(embed)
598+
if sym.info is ast.Struct {
599+
w.a_struct_info(sym.name, sym.info)
600+
}
601+
}
569602
}
570603

571604
pub fn (mut w Walker) fn_decl(mut node ast.FnDecl) {
@@ -595,10 +628,13 @@ pub fn (mut w Walker) call_expr(mut node ast.CallExpr) {
595628
if node.is_method && node.left_type != 0 {
596629
left_sym := w.table.sym(node.left_type)
597630
if left_sym.info is ast.Aggregate {
598-
for types in left_sym.info.types {
599-
fn_name := '${types.idx()}.${node.name}'
600-
if !w.used_fns[fn_name] {
601-
w.mark_aggregate_call_used(fn_name, types)
631+
for receiver_type in left_sym.info.types {
632+
receiver_sym := w.table.sym(receiver_type)
633+
if m := receiver_sym.find_method(node.name) {
634+
fn_name := '${int(m.receiver_type)}.${node.name}'
635+
if !w.used_fns[fn_name] {
636+
w.fn_by_name(fn_name)
637+
}
602638
}
603639
}
604640
} else if left_sym.info is ast.Interface {
@@ -612,6 +648,13 @@ pub fn (mut w Walker) call_expr(mut node ast.CallExpr) {
612648
w.fn_by_name(fn_embed)
613649
}
614650
}
651+
} else if node.from_embed_types.len != 0 && !node.left_type.has_flag(.generic) {
652+
_, embed_types := w.table.find_method_from_embeds(w.table.final_sym(node.left_type),
653+
node.name) or { ast.Fn{}, []ast.Type{} }
654+
if embed_types.len != 0 {
655+
fn_embed := '${int(embed_types.last())}.${node.name}'
656+
w.fn_by_name(fn_embed)
657+
}
615658
}
616659
}
617660
w.expr(node.left)
@@ -639,6 +682,8 @@ pub fn (mut w Walker) call_expr(mut node ast.CallExpr) {
639682
if const_fn_name in w.all_consts {
640683
w.mark_const_as_used(const_fn_name)
641684
}
685+
} else if node.is_fn_var {
686+
w.mark_global_as_used(node.name)
642687
}
643688
w.mark_fn_as_used(fn_name)
644689
if node.is_method && node.receiver_type.has_flag(.generic) && node.receiver_concrete_type != 0
@@ -656,16 +701,6 @@ pub fn (mut w Walker) call_expr(mut node ast.CallExpr) {
656701
}
657702
}
658703

659-
// visit aggregate type method declaration
660-
pub fn (mut w Walker) mark_aggregate_call_used(fn_name string, left_type ast.Type) {
661-
if w.used_fns[fn_name] {
662-
return
663-
}
664-
w.mark_fn_as_used(fn_name)
665-
stmt := w.all_fns[fn_name] or { return }
666-
w.stmts(stmt.stmts)
667-
}
668-
669704
pub fn (mut w Walker) fn_by_name(fn_name string) {
670705
if w.used_fns[fn_name] {
671706
return

0 commit comments

Comments
 (0)