From eeeef4103067f7b92abdf9891bcc3f1399390546 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Thu, 29 May 2025 04:09:34 -0300 Subject: [PATCH] markused: fix `x := t.wait()`, when `t := go fn () string {` (fix #24577) (#24580) --- vlib/v/ast/table.v | 1 + vlib/v/checker/fn.v | 6 ++++++ vlib/v/markused/markused.v | 3 +++ vlib/v/tests/skip_unused/thread_waiter.run.out | 2 ++ .../skip_unused/thread_waiter.skip_unused.run.out | 2 ++ vlib/v/tests/skip_unused/thread_waiter.vv | 10 ++++++++++ 6 files changed, 24 insertions(+) create mode 100644 vlib/v/tests/skip_unused/thread_waiter.run.out create mode 100644 vlib/v/tests/skip_unused/thread_waiter.skip_unused.run.out create mode 100644 vlib/v/tests/skip_unused/thread_waiter.vv diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index a0b5f39e6..0d7c2dc85 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -32,6 +32,7 @@ pub mut: map_update bool // {...foo} interpolation bool // '${foo} ${bar}' option_or_result bool // has panic call + waiter bool // has thread waiter print_types map[int]bool // print() idx types used_fns map[string]bool // filled in by markused used_consts map[string]bool // filled in by markused diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 691c6323d..418a54b3b 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -2726,6 +2726,9 @@ fn (mut c Checker) spawn_expr(mut node ast.SpawnExpr) ast.Type { c.error('option handling cannot be done in `spawn` call. Do it when calling `.wait()`', node.call_expr.or_block.pos) } + if node.is_expr { + c.table.used_features.waiter = true + } // Make sure there are no mutable arguments for arg in node.call_expr.args { if arg.is_mut && !arg.typ.is_ptr() { @@ -2761,6 +2764,9 @@ fn (mut c Checker) go_expr(mut node ast.GoExpr) ast.Type { c.error('option handling cannot be done in `go` call. Do it when calling `.wait()`', node.call_expr.or_block.pos) } + if node.is_expr { + c.table.used_features.waiter = true + } // Make sure there are no mutable arguments for arg in node.call_expr.args { if arg.is_mut && !arg.typ.is_ptr() { diff --git a/vlib/v/markused/markused.v b/vlib/v/markused/markused.v index b1c97103c..8f9a44e92 100644 --- a/vlib/v/markused/markused.v +++ b/vlib/v/markused/markused.v @@ -168,6 +168,9 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a builderptr_idx + '.write_rune', ] } + if table.used_features.waiter { + core_fns << 'free' + } if !table.used_features.arr_init { table.used_features.arr_init = table.used_features.print_types.keys().any(table.type_to_str(it).contains('[]')) } diff --git a/vlib/v/tests/skip_unused/thread_waiter.run.out b/vlib/v/tests/skip_unused/thread_waiter.run.out new file mode 100644 index 000000000..ecee9cdee --- /dev/null +++ b/vlib/v/tests/skip_unused/thread_waiter.run.out @@ -0,0 +1,2 @@ +string +hi diff --git a/vlib/v/tests/skip_unused/thread_waiter.skip_unused.run.out b/vlib/v/tests/skip_unused/thread_waiter.skip_unused.run.out new file mode 100644 index 000000000..ecee9cdee --- /dev/null +++ b/vlib/v/tests/skip_unused/thread_waiter.skip_unused.run.out @@ -0,0 +1,2 @@ +string +hi diff --git a/vlib/v/tests/skip_unused/thread_waiter.vv b/vlib/v/tests/skip_unused/thread_waiter.vv new file mode 100644 index 000000000..04d598e5c --- /dev/null +++ b/vlib/v/tests/skip_unused/thread_waiter.vv @@ -0,0 +1,10 @@ +module main + +fn main() { + t := go fn () string { + return 'hi' + }() + x := t.wait() + println(typeof(x).name) + println(x) +} -- 2.39.5