From 0e0408f9b22153b87ace466493ef2080514a1a7b Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Tue, 26 Nov 2024 04:25:48 -0300 Subject: [PATCH] v: fix `-skip-unused` feature for some tests (#22976) --- vlib/v/ast/table.v | 2 ++ vlib/v/checker/checker.v | 4 +++- vlib/v/checker/comptime.v | 6 ++++++ vlib/v/debug/debug.v | 1 + vlib/v/markused/markused.v | 6 +++++- 5 files changed, 17 insertions(+), 2 deletions(-) diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index f6caee842..2a3071f74 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -33,6 +33,8 @@ pub mut: used_maps int // how many times maps were used, filled in by markused used_arrays int // how many times arrays were used, filled in by markused // json bool // json is imported + debugger bool // debugger is used + comptime_calls map[string]bool // resolved $method() names } @[unsafe] diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 50a6604e0..946fc60d4 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2150,7 +2150,9 @@ fn (mut c Checker) stmt(mut node ast.Stmt) { } } ast.NodeError {} - ast.DebuggerStmt {} + ast.DebuggerStmt { + c.table.used_features.debugger = true + } ast.AsmStmt { c.asm_stmt(mut node) } diff --git a/vlib/v/checker/comptime.v b/vlib/v/checker/comptime.v index b99964f23..b4e0bd26d 100644 --- a/vlib/v/checker/comptime.v +++ b/vlib/v/checker/comptime.v @@ -129,6 +129,12 @@ fn (mut c Checker) comptime_call(mut node ast.ComptimeCall) ast.Type { if c.inside_anon_fn && 'method' !in c.cur_anon_fn.inherited_vars.map(it.name) { c.error('undefined ident `method` in the anonymous function', node.pos) } + if c.pref.skip_unused && node.method_name == 'method' { + sym := c.table.sym(c.unwrap_generic(node.left_type)) + if m := sym.find_method(c.comptime.comptime_for_method.name) { + c.table.used_features.comptime_calls['${int(c.unwrap_generic(m.receiver_type))}.${c.comptime.comptime_for_method.name}'] = true + } + } for i, mut arg in node.args { // check each arg expression node.args[i].typ = c.expr(mut arg.expr) diff --git a/vlib/v/debug/debug.v b/vlib/v/debug/debug.v index 60487ac26..3cf1af332 100644 --- a/vlib/v/debug/debug.v +++ b/vlib/v/debug/debug.v @@ -8,6 +8,7 @@ import readline import strings import term +@[markused] __global g_debugger = Debugger{} // Debugger holds the V debug information for REPL diff --git a/vlib/v/markused/markused.v b/vlib/v/markused/markused.v index fd630cd43..eeda79b15 100644 --- a/vlib/v/markused/markused.v +++ b/vlib/v/markused/markused.v @@ -159,6 +159,9 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a $if trace_skip_unused_all_fns ? { println('k: ${k} | mfn: ${mfn.name}') } + if k in table.used_features.comptime_calls { + all_fn_root_names << k + } // _noscan functions/methods are selected when the `-gc boehm` is on: if allow_noscan && is_noscan_whitelisted && mfn.name.ends_with('_noscan') { all_fn_root_names << k @@ -183,7 +186,8 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a // call .str or .auto_str methods for user types: if k.ends_with('.str') || k.ends_with('.auto_str') { if table.used_features.auto_str - || table.used_features.print_types[mfn.receiver.typ.idx()] { + || table.used_features.print_types[mfn.receiver.typ.idx()] + || table.used_features.debugger { all_fn_root_names << k } continue -- 2.39.5