From 2c063ea19141163d7ea6a167abc355e7b4341ee4 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 1 Jul 2025 14:13:03 +0300 Subject: [PATCH] cgen,markused: skip `struct none {` in cgen, if `none` is not used by V code reachable from `fn main() {` (#24824) --- vlib/v/ast/table.v | 1 + vlib/v/gen/c/cgen.v | 2 +- vlib/v/markused/markused.v | 4 ++++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index 0d7c2dc85f..55e67b9200 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -40,6 +40,7 @@ pub mut: used_veb_types []Type // veb context types, filled in by checker 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 + used_none int // how many times `none` was used, filled in by markused external_types bool // true, when external type is used // json bool // json is imported debugger bool // debugger is used diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 3c200e9d41..75425ff731 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -6635,7 +6635,7 @@ fn (mut g Gen) write_types(symbols []&ast.TypeSymbol) { if sym.name.starts_with('C.') { continue } - if sym.kind == .none { + if sym.kind == .none && (!g.pref.skip_unused || g.table.used_features.used_none > 0) { g.type_definitions.writeln('struct none {') g.type_definitions.writeln('\tEMPTY_STRUCT_DECLARATION;') g.type_definitions.writeln('};') diff --git a/vlib/v/markused/markused.v b/vlib/v/markused/markused.v index 4cb9ecec3e..e9067f109a 100644 --- a/vlib/v/markused/markused.v +++ b/vlib/v/markused/markused.v @@ -540,6 +540,10 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a } } + table.used_features.used_none = walker.used_none + if walker.used_none == 0 { + walker.used_fns.delete('${int(ast.none_type)}.str') + } table.used_features.used_fns = walker.used_fns.move() table.used_features.used_consts = walker.used_consts.move() table.used_features.used_globals = walker.used_globals.move()