@@ -9,20 +9,24 @@ import v.pref
99
1010pub struct Walker {
1111pub mut :
12- table & ast.Table = unsafe { nil }
13- features & ast.UsedFeatures = unsafe { nil }
14- used_fns map [string ]bool // used_fns['println'] == true
15- trace_enabled bool
16- used_consts map [string ]bool // used_consts['os.args'] == true
17- used_globals map [string ]bool
18- used_fields map [string ]bool
19- used_syms map [int ]bool
20- used_none int // _option_none
21- used_option int // _option_ok
22- used_result int // _result_ok
23- used_panic int // option/result propagation
24- used_closures int // fn [x] (){}, and `instance.method` used in an expression
25- pref & pref.Preferences = unsafe { nil }
12+ table & ast.Table = unsafe { nil }
13+ features & ast.UsedFeatures = unsafe { nil }
14+ used_fns map [string ]bool // used_fns['println'] == true
15+ trace_enabled bool
16+ used_consts map [string ]bool // used_consts['os.args'] == true
17+ used_globals map [string ]bool
18+ used_fields map [string ]bool
19+ used_structs map [string ]bool
20+ used_types map [ast.Type]bool
21+ used_syms map [int ]bool
22+ used_arr_method map [string ]bool
23+ used_map_method map [string ]bool
24+ used_none int // _option_none
25+ used_option int // _option_ok
26+ used_result int // _result_ok
27+ used_panic int // option/result propagation
28+ used_closures int // fn [x] (){}, and `instance.method` used in an expression
29+ pref & pref.Preferences = unsafe { nil }
2630mut :
2731 all_fns map [string ]ast.FnDecl
2832 all_consts map [string ]ast.ConstField
@@ -551,8 +555,8 @@ fn (mut w Walker) expr(node_ ast.Expr) {
551555 } else if ! node.is_setter && ! w.uses_map_getter {
552556 w.mark_builtin_map_method_as_used ('get' )
553557 }
554- w.mark_by_sym (w.table. sym (sym .info.key_type) )
555- w.mark_by_sym (w.table. sym (sym .info.value_type) )
558+ w.mark_by_type ( sym.info.key_type)
559+ w.mark_by_type ( sym.info.value_type)
556560 w.features.used_maps++
557561 } else if sym.kind in [.array, .array_fixed, .any] {
558562 if ! w.is_direct_array_access || w.features.auto_str_arr {
@@ -565,9 +569,9 @@ fn (mut w Walker) expr(node_ ast.Expr) {
565569 }
566570 }
567571 if sym.info is ast.Array {
568- w.mark_by_sym (w.table. sym (sym .info.elem_type) )
572+ w.mark_by_type ( sym.info.elem_type)
569573 } else if sym.info is ast.ArrayFixed {
570- w.mark_by_sym (w.table. sym (sym .info.elem_type) )
574+ w.mark_by_type ( sym.info.elem_type)
571575 }
572576 if ! w.uses_arr_range_index {
573577 w.uses_arr_range_index = true
@@ -1000,8 +1004,18 @@ pub fn (mut w Walker) call_expr(mut node ast.CallExpr) {
10001004 // All []Type or map[Type]Another types are typedefs to those `map` and `array` types, and all map and array methods
10011005 // are actually methods on the `builtin` concrete types.
10021006 match lsym.kind {
1003- .array { w.mark_builtin_array_method_as_used (node.name) }
1004- .map { w.mark_builtin_map_method_as_used (node.name) }
1007+ .array {
1008+ if ! w.used_arr_method[node.name] {
1009+ w.mark_builtin_array_method_as_used (node.name)
1010+ w.used_arr_method[node.name] = true
1011+ }
1012+ }
1013+ .map {
1014+ if ! w.used_map_method[node.name] {
1015+ w.mark_builtin_map_method_as_used (node.name)
1016+ w.used_map_method[node.name] = true
1017+ }
1018+ }
10051019 else {}
10061020 }
10071021 }
@@ -1040,7 +1054,6 @@ pub fn (mut w Walker) fn_by_name(fn_name string) {
10401054 }
10411055 mut stmt := w.all_fns[fn_name] or { return }
10421056 w.fn_decl (mut stmt)
1043- // w.stmts(stmt.stmts)
10441057}
10451058
10461059pub fn (mut w Walker) struct_fields (sfields []ast.StructField) {
@@ -1096,10 +1109,11 @@ pub fn (mut w Walker) mark_by_sym_name(name string) {
10961109
10971110@[inline]
10981111pub fn (mut w Walker) mark_by_type (typ ast.Type) {
1099- if typ == 0 || typ.has_flag (.generic) {
1112+ if typ == 0 || typ.has_flag (.generic) || typ in w.used_types {
11001113 return
11011114 }
11021115 w.mark_by_sym (w.table.sym (typ))
1116+ w.used_types[typ] = true
11031117}
11041118
11051119pub fn (mut w Walker) mark_by_sym (isym ast.TypeSymbol) {
@@ -1121,15 +1135,7 @@ pub fn (mut w Walker) mark_by_sym(isym ast.TypeSymbol) {
11211135 w.used_none++
11221136 }
11231137 }
1124- match fsym.info {
1125- ast.Map {
1126- w.features.used_maps++
1127- w.mark_by_sym (fsym)
1128- }
1129- else {
1130- w.mark_by_sym (fsym)
1131- }
1132- }
1138+ w.mark_by_sym (fsym)
11331139 }
11341140 if ! w.features.auto_str_ptr && ifield.typ.is_ptr ()
11351141 && isym.idx in w.features.print_types {
0 commit comments