From 07adbf717c4f92828b106cd4fb5777255a7af811 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 16 Nov 2024 21:44:30 -0300 Subject: [PATCH 1/2] fix --- vlib/v/markused/walker.v | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/vlib/v/markused/walker.v b/vlib/v/markused/walker.v index 8eefe1bfd631f8..8abcc6666e167e 100644 --- a/vlib/v/markused/walker.v +++ b/vlib/v/markused/walker.v @@ -528,6 +528,17 @@ pub fn (mut w Walker) call_expr(mut node ast.CallExpr) { } return } + if node.is_method && node.left_type != 0 { + left_sym := w.table.sym(node.left_type) + if left_sym.info is ast.Aggregate { + for types in left_sym.info.types { + fn_name := '${types.idx()}.${node.name}' + if !w.used_fns[fn_name] { + w.mark_aggregate_call_used(fn_name, types) + } + } + } + } w.expr(node.left) w.or_block(node.or_block) @@ -566,6 +577,16 @@ pub fn (mut w Walker) call_expr(mut node ast.CallExpr) { } } +// visit aggregate type method declaration +pub fn (mut w Walker) mark_aggregate_call_used(fn_name string, left_type ast.Type) { + if w.used_fns[fn_name] { + return + } + w.mark_fn_as_used(fn_name) + stmt := w.all_fns[fn_name] or { return } + w.stmts(stmt.stmts) +} + pub fn (mut w Walker) fn_by_name(fn_name string) { if w.used_fns[fn_name] { return From aa7b3925e1e79251fb806750f3957538fb0c1563 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 17 Nov 2024 15:21:25 -0300 Subject: [PATCH 2/2] test --- .../tests/skip_unused/aggregate_call.run.out | 0 .../aggregate_call.skip_unused.run.out | 0 vlib/v/tests/skip_unused/aggregate_call.vv | 20 +++++++++++++++++++ 3 files changed, 20 insertions(+) create mode 100644 vlib/v/tests/skip_unused/aggregate_call.run.out create mode 100644 vlib/v/tests/skip_unused/aggregate_call.skip_unused.run.out create mode 100644 vlib/v/tests/skip_unused/aggregate_call.vv diff --git a/vlib/v/tests/skip_unused/aggregate_call.run.out b/vlib/v/tests/skip_unused/aggregate_call.run.out new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/vlib/v/tests/skip_unused/aggregate_call.skip_unused.run.out b/vlib/v/tests/skip_unused/aggregate_call.skip_unused.run.out new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/vlib/v/tests/skip_unused/aggregate_call.vv b/vlib/v/tests/skip_unused/aggregate_call.vv new file mode 100644 index 00000000000000..76b9c74605c8b5 --- /dev/null +++ b/vlib/v/tests/skip_unused/aggregate_call.vv @@ -0,0 +1,20 @@ +struct Foo {} + +struct Bar {} + +type SumType = Foo | Bar + +fn (f Foo) pstr() { +} + +fn (b Bar) pstr() { +} + +fn main() { + a := SumType(Foo{}) + match a { + Foo, Bar { + a.pstr() + } + } +}