Skip to content

Commit

Permalink
v.markused: fix -skip-unused error with aggregate method call (fix #…
Browse files Browse the repository at this point in the history
…22852 - building vsql) (#22883)
  • Loading branch information
felipensp authored Nov 18, 2024
1 parent 5bba92a commit 983511f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
21 changes: 21 additions & 0 deletions vlib/v/markused/walker.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
Empty file.
Empty file.
20 changes: 20 additions & 0 deletions vlib/v/tests/skip_unused/aggregate_call.vv
Original file line number Diff line number Diff line change
@@ -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()
}
}
}

0 comments on commit 983511f

Please sign in to comment.