Skip to content

Commit ae4a904

Browse files
authored
v: missing -skip-unused fixes (#22978)
1 parent 066384c commit ae4a904

File tree

10 files changed

+233
-98
lines changed

10 files changed

+233
-98
lines changed

vlib/builtin/map.v

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ mut:
8888
values &u8 = unsafe { nil }
8989
}
9090

91-
@[inline; markused]
91+
@[inline]
9292
fn new_dense_array(key_bytes int, value_bytes int) DenseArray {
9393
cap := 8
9494
return DenseArray{
@@ -103,18 +103,18 @@ fn new_dense_array(key_bytes int, value_bytes int) DenseArray {
103103
}
104104
}
105105

106-
@[inline; markused]
106+
@[inline]
107107
fn (d &DenseArray) key(i int) voidptr {
108108
return unsafe { voidptr(d.keys + i * d.key_bytes) }
109109
}
110110

111111
// for cgen
112-
@[inline; markused]
112+
@[inline]
113113
fn (d &DenseArray) value(i int) voidptr {
114114
return unsafe { voidptr(d.values + i * d.value_bytes) }
115115
}
116116

117-
@[inline; markused]
117+
@[inline]
118118
fn (d &DenseArray) has_index(i int) bool {
119119
return d.deletes == 0 || unsafe { d.all_deleted[i] } == 0
120120
}
@@ -259,7 +259,6 @@ fn map_free_string(pkey voidptr) {
259259
fn map_free_nop(_ voidptr) {
260260
}
261261

262-
@[markused]
263262
fn new_map(key_bytes int, value_bytes int, hash_fn MapHashFn, key_eq_fn MapEqFn, clone_fn MapCloneFn, free_fn MapFreeFn) map {
264263
metasize := int(sizeof(u32) * (init_capicity + extra_metas_inc))
265264
// for now assume anything bigger than a pointer is a string

vlib/v/ast/table.v

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ pub struct UsedFeatures {
1212
pub mut:
1313
interfaces bool // interface
1414
dump bool // dump()
15-
builtin_types bool // uses any builtin type
1615
index bool // string[0]
1716
range_index bool // string[0..1]
1817
cast_ptr bool // &u8(...)
18+
asserts bool // assert expr
1919
as_cast bool // expr as Type
2020
anon_fn bool // fn () { }
2121
auto_str bool // auto str fns
@@ -24,6 +24,11 @@ pub mut:
2424
arr_first bool // arr.first()
2525
arr_last bool // arr.last()
2626
arr_pop bool // arr.pop()
27+
arr_delete bool // arr.delete()
28+
arr_init bool // [1, 2, 3]
29+
arr_map bool // []map[key]value
30+
map_update bool // {...foo}
31+
interpolation bool // '${foo} ${bar}'
2732
option_or_result bool // has panic call
2833
print_types map[int]bool // print() idx types
2934
used_fns map[string]bool // filled in by markused
@@ -32,6 +37,7 @@ pub mut:
3237
used_veb_types []Type // veb context types, filled in by checker
3338
used_maps int // how many times maps were used, filled in by markused
3439
used_arrays int // how many times arrays were used, filled in by markused
40+
used_modules map[string]bool // filled in checker
3541
// json bool // json is imported
3642
debugger bool // debugger is used
3743
comptime_calls map[string]bool // resolved $method() names

vlib/v/checker/checker.v

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,11 +2302,20 @@ fn (mut c Checker) stmt(mut node ast.Stmt) {
23022302

23032303
fn (mut c Checker) assert_stmt(mut node ast.AssertStmt) {
23042304
if node.is_used {
2305-
c.table.used_features.auto_str = true
2305+
c.table.used_features.asserts = true
23062306
}
23072307
cur_exp_typ := c.expected_type
23082308
c.expected_type = ast.bool_type
23092309
assert_type := c.check_expr_option_or_result_call(node.expr, c.expr(mut node.expr))
2310+
if c.pref.skip_unused && !c.table.used_features.auto_str && !c.is_builtin_mod
2311+
&& mut node.expr is ast.InfixExpr {
2312+
if !c.table.sym(c.unwrap_generic(node.expr.left_type)).has_method('str') {
2313+
c.table.used_features.auto_str = true
2314+
}
2315+
if !c.table.sym(c.unwrap_generic(node.expr.right_type)).has_method('str') {
2316+
c.table.used_features.auto_str = true
2317+
}
2318+
}
23102319
if assert_type != ast.bool_type_idx {
23112320
atype_name := c.table.sym(assert_type).name
23122321
c.error('assert can be used only with `bool` expressions, but found `${atype_name}` instead',
@@ -2949,6 +2958,7 @@ pub fn (mut c Checker) expr(mut node ast.Expr) ast.Type {
29492958
return c.concat_expr(mut node)
29502959
}
29512960
ast.DumpExpr {
2961+
c.table.used_features.dump = true
29522962
c.expected_type = ast.string_type
29532963
node.expr_type = c.expr(mut node.expr)
29542964
if c.pref.skip_unused && !c.is_builtin_mod {
@@ -3268,7 +3278,8 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
32683278
to_type
32693279
}
32703280
final_to_is_ptr := to_type.is_ptr() || final_to_type.is_ptr()
3271-
if to_type.is_ptr() {
3281+
if c.pref.skip_unused && !c.is_builtin_mod && c.mod !in ['strings', 'math.bits']
3282+
&& to_type.is_ptr() {
32723283
c.table.used_features.cast_ptr = true
32733284
}
32743285
if to_type.has_flag(.result) {
@@ -4763,9 +4774,11 @@ fn (mut c Checker) index_expr(mut node ast.IndexExpr) ast.Type {
47634774
}
47644775
else {}
47654776
}
4766-
c.table.used_features.index = true
4767-
if node.index is ast.RangeExpr {
4768-
c.table.used_features.range_index = true
4777+
if !c.is_builtin_mod && c.mod !in ['strings', 'math.bits'] {
4778+
if node.index is ast.RangeExpr {
4779+
c.table.used_features.range_index = true
4780+
}
4781+
c.table.used_features.index = true
47694782
}
47704783
is_aggregate_arr := typ_sym.kind == .aggregate
47714784
&& (typ_sym.info as ast.Aggregate).types.filter(c.table.type_kind(it) !in [.array, .array_fixed, .string, .map]).len == 0

vlib/v/checker/containers.v

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
1313
}
1414
// `x := []string{}` (the type was set in the parser)
1515
if node.typ != ast.void_type {
16+
if !c.is_builtin_mod && c.mod !in ['builtin', 'strings', 'strconv', 'math.bits'] {
17+
c.table.used_features.arr_init = true
18+
}
1619
if node.elem_type != 0 {
1720
elem_sym := c.table.sym(node.elem_type)
1821

@@ -61,6 +64,11 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
6164
c.warn('byte is deprecated, use u8 instead', node.elem_type_pos)
6265
}
6366
}
67+
ast.Map {
68+
if c.pref.skip_unused && !c.is_builtin_mod {
69+
c.table.used_features.arr_map = true
70+
}
71+
}
6472
else {}
6573
}
6674
}
@@ -143,6 +151,9 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
143151
}
144152
// `[1,2,3]`
145153
if node.exprs.len > 0 && node.elem_type == ast.void_type {
154+
if !c.is_builtin_mod && c.mod !in ['builtin', 'strings', 'strconv', 'math.bits'] {
155+
c.table.used_features.arr_init = true
156+
}
146157
mut expected_value_type := ast.void_type
147158
mut expecting_interface_array := false
148159
mut expecting_sumtype_array := false
@@ -495,6 +506,7 @@ fn (mut c Checker) map_init(mut node ast.MapInit) ast.Type {
495506
}
496507

497508
if (node.keys.len > 0 && node.vals.len > 0) || node.has_update_expr {
509+
c.table.used_features.map_update = true
498510
mut map_type := ast.void_type
499511
use_expected_type := c.expected_type != ast.void_type && !c.inside_const
500512
&& c.table.sym(c.expected_type).kind == .map && !(c.inside_fn_arg

vlib/v/checker/fn.v

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -751,8 +751,23 @@ fn (mut c Checker) call_expr(mut node ast.CallExpr) ast.Type {
751751
c.inside_or_block_value = true
752752
c.check_or_expr(node.or_block, typ, c.expected_or_type, node)
753753
c.inside_or_block_value = old_inside_or_block_value
754+
} else if node.or_block.kind == .propagate_option || node.or_block.kind == .propagate_result {
755+
if c.pref.skip_unused && !c.is_builtin_mod && c.mod != 'strings' {
756+
c.table.used_features.option_or_result = true
757+
}
754758
}
755759
c.expected_or_type = old_expected_or_type
760+
if c.pref.skip_unused && !c.is_builtin_mod && c.mod == 'main' {
761+
if node.is_method {
762+
type_str := c.table.type_to_str(node.left_type)
763+
if c.table.sym(node.left_type).is_builtin()
764+
&& type_str !in c.table.used_features.used_modules {
765+
c.table.used_features.used_modules[type_str] = true
766+
}
767+
} else if node.name.contains('.') {
768+
c.table.used_features.used_modules[node.name.all_before('.')] = true
769+
}
770+
}
756771

757772
if !c.inside_const && c.table.cur_fn != unsafe { nil } && !c.table.cur_fn.is_main
758773
&& !c.table.cur_fn.is_test {
@@ -1401,7 +1416,8 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
14011416
// println / eprintln / panic can print anything
14021417
if node.args.len > 0 && fn_name in print_everything_fns {
14031418
c.builtin_args(mut node, fn_name, func)
1404-
if c.pref.skip_unused && !c.is_builtin_mod && node.args[0].expr !is ast.StringLiteral {
1419+
if c.pref.skip_unused && !c.is_builtin_mod && c.mod != 'math.bits'
1420+
&& node.args[0].expr !is ast.StringLiteral {
14051421
if !c.table.sym(c.unwrap_generic(node.args[0].typ)).has_method('str') {
14061422
c.table.used_features.auto_str = true
14071423
if node.args[0].typ.is_ptr() {
@@ -2162,9 +2178,6 @@ fn (mut c Checker) method_call(mut node ast.CallExpr, mut continue_check &bool)
21622178
continue_check = false
21632179
return ast.void_type
21642180
}
2165-
if c.pref.skip_unused && !c.is_builtin_mod && c.mod != 'strings' {
2166-
c.table.used_features.builtin_types = true
2167-
}
21682181
c.expected_type = left_type
21692182
mut is_generic := left_type.has_flag(.generic)
21702183
node.left_type = left_type
@@ -2949,6 +2962,9 @@ fn (mut c Checker) check_expected_arg_count(mut node ast.CallExpr, f &ast.Fn) !
29492962
}
29502963
if f.is_variadic {
29512964
min_required_params--
2965+
if c.pref.skip_unused && !c.is_builtin_mod {
2966+
c.table.used_features.arr_init = true
2967+
}
29522968
} else {
29532969
has_decompose := node.args.any(it.expr is ast.ArrayDecompose)
29542970
if has_decompose {
@@ -3565,7 +3581,7 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as
35653581
}
35663582
node.return_type = ast.int_type
35673583
} else if method_name in ['first', 'last', 'pop'] {
3568-
if c.pref.skip_unused {
3584+
if c.pref.skip_unused && !c.is_builtin_mod {
35693585
if method_name == 'first' {
35703586
c.table.used_features.arr_first = true
35713587
}
@@ -3587,6 +3603,9 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as
35873603
node.receiver_type = node.left_type
35883604
}
35893605
} else if method_name == 'delete' {
3606+
if c.pref.skip_unused && !c.is_builtin_mod {
3607+
c.table.used_features.arr_delete = true
3608+
}
35903609
c.check_for_mut_receiver(mut node.left)
35913610
unwrapped_left_sym := c.table.sym(unwrapped_left_type)
35923611
if method := c.table.find_method(unwrapped_left_sym, method_name) {

vlib/v/checker/for.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) {
190190
'declare a key and a value variable when ranging a map: `for key, val in map {`\n' +
191191
'use `_` if you do not need the variable', node.pos)
192192
}
193+
if !c.is_builtin_mod && c.mod != 'strings' {
194+
c.table.used_features.used_maps++
195+
}
193196
if node.key_var.len > 0 {
194197
key_type := match sym.kind {
195198
.map { sym.map_info().key_type }

vlib/v/checker/str.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ fn (mut c Checker) string_inter_lit(mut node ast.StringInterLiteral) ast.Type {
6262
} else {
6363
c.table.used_features.print_types[ftyp.idx()] = true
6464
}
65+
c.table.used_features.interpolation = true
6566
}
6667
c.fail_if_unreadable(expr, ftyp, 'interpolation object')
6768
node.expr_types << ftyp

vlib/v/gen/c/cgen.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6660,7 +6660,7 @@ fn (mut g Gen) write_init_function() {
66606660
g.write('\tas_cast_type_indexes = ')
66616661
g.writeln(g.as_cast_name_table())
66626662
}
6663-
if !g.pref.is_shared && (!g.pref.skip_unused || g.table.used_features.builtin_types) {
6663+
if !g.pref.is_shared && (!g.pref.skip_unused || g.table.used_features.used_modules.len > 0) {
66646664
// shared object does not need this
66656665
g.writeln('\tbuiltin_init();')
66666666
}

0 commit comments

Comments
 (0)