diff --git a/cmd/tools/vtest-all.v b/cmd/tools/vtest-all.v index 8f8151dafe..0562d2f2d4 100644 --- a/cmd/tools/vtest-all.v +++ b/cmd/tools/vtest-all.v @@ -169,6 +169,11 @@ fn get_all_commands() []Command { okmsg: 'V compiles hello_world.v on the JS backend, with -skip-unused' rmfile: 'hw_skip_unused.js' } + res << Command{ + line: '$vexe -skip-unused examples/2048' + okmsg: 'V can compile 2048 with -skip-unused.' + rmfile: 'examples/2048/2048' + } } res << Command{ line: '$vexe -o vtmp cmd/v' diff --git a/cmd/tools/vtest-self.v b/cmd/tools/vtest-self.v index 3bcbe95228..5031d2e6f0 100644 --- a/cmd/tools/vtest-self.v +++ b/cmd/tools/vtest-self.v @@ -155,6 +155,7 @@ const ( skip_on_musl = [ 'vlib/v/tests/profile/profile_test.v', 'vlib/gg/draw_fns_api_test.v', + 'vlib/v/tests/skip_unused/gg_code.vv', ] skip_on_ubuntu_musl = [ //'vlib/v/gen/js/jsgen_test.v', diff --git a/vlib/v/compiler_errors_test.v b/vlib/v/compiler_errors_test.v index 2159903a70..97c5ce641f 100644 --- a/vlib/v/compiler_errors_test.v +++ b/vlib/v/compiler_errors_test.v @@ -19,6 +19,11 @@ const skip_on_cstrict = [ const skip_on_ubuntu_musl = [ 'vlib/v/checker/tests/vweb_tmpl_used_var.vv', 'vlib/v/checker/tests/vweb_routing_checks.vv', + 'vlib/v/tests/skip_unused/gg_code.vv', +] + +const skip_on_ci_musl = [ + 'vlib/v/tests/skip_unused/gg_code.vv', ] const vexe = os.getenv('VEXE') @@ -37,6 +42,8 @@ const github_job = os.getenv('GITHUB_JOB') const v_ci_ubuntu_musl = os.getenv('V_CI_UBUNTU_MUSL').len > 0 +const v_ci_musl = os.getenv('V_CI_MUSL').len > 0 + const v_ci_cstrict = os.getenv('V_CI_CSTRICT').len > 0 struct TaskDescription { @@ -217,6 +224,9 @@ fn (mut tasks Tasks) run() { if v_ci_ubuntu_musl { m_skip_files << skip_on_ubuntu_musl } + if v_ci_musl { + m_skip_files << skip_on_ci_musl + } if v_ci_cstrict { m_skip_files << skip_on_cstrict } diff --git a/vlib/v/markused/walker.v b/vlib/v/markused/walker.v index 74d7b20c69..bc2ba19c76 100644 --- a/vlib/v/markused/walker.v +++ b/vlib/v/markused/walker.v @@ -13,6 +13,7 @@ pub mut: used_fns map[string]bool // used_fns['println'] == true used_consts map[string]bool // used_consts['os.args'] == true used_globals map[string]bool + used_structs map[string]bool n_asserts int pref &pref.Preferences = unsafe { nil } mut: @@ -402,17 +403,7 @@ fn (mut w Walker) expr(node_ ast.Expr) { sym := w.table.sym(node.typ) if sym.kind == .struct_ { info := sym.info as ast.Struct - for ifield in info.fields { - if ifield.has_default_expr { - w.expr(ifield.default_expr) - } - if ifield.typ != 0 { - fsym := w.table.sym(ifield.typ) - if fsym.kind == .map { - w.table.used_maps++ - } - } - } + w.a_struct_info(sym.name, info) } if node.has_update_expr { w.expr(node.update_expr) @@ -463,6 +454,27 @@ fn (mut w Walker) expr(node_ ast.Expr) { } } +pub fn (mut w Walker) a_struct_info(sname string, info ast.Struct) { + if sname in w.used_structs { + return + } + w.used_structs[sname] = true + for ifield in info.fields { + if ifield.has_default_expr { + w.expr(ifield.default_expr) + } + if ifield.typ != 0 { + fsym := w.table.sym(ifield.typ) + if fsym.kind == .map { + w.table.used_maps++ + } + if fsym.kind == .struct_ { + w.a_struct_info(fsym.name, fsym.struct_info()) + } + } + } +} + pub fn (mut w Walker) fn_decl(mut node ast.FnDecl) { if node.language == .c { return diff --git a/vlib/v/tests/skip_unused/gg_code.run.out b/vlib/v/tests/skip_unused/gg_code.run.out new file mode 100644 index 0000000000..d86bac9de5 --- /dev/null +++ b/vlib/v/tests/skip_unused/gg_code.run.out @@ -0,0 +1 @@ +OK diff --git a/vlib/v/tests/skip_unused/gg_code.skip_unused.run.out b/vlib/v/tests/skip_unused/gg_code.skip_unused.run.out new file mode 100644 index 0000000000..d86bac9de5 --- /dev/null +++ b/vlib/v/tests/skip_unused/gg_code.skip_unused.run.out @@ -0,0 +1 @@ +OK diff --git a/vlib/v/tests/skip_unused/gg_code.vv b/vlib/v/tests/skip_unused/gg_code.vv new file mode 100644 index 0000000000..df821c44b6 --- /dev/null +++ b/vlib/v/tests/skip_unused/gg_code.vv @@ -0,0 +1,23 @@ +module main + +import gg +import gx + +[console] +fn main() { + x := gg.new_context( + bg_color: gx.rgb(174, 198, 255) + window_title: 'GG Hello' + frame_fn: fn (mut ctx gg.Context) { + ctx.begin() + ctx.draw_text_def(10, 25, 'Hello') + ctx.end() + if ctx.frame > 10 { + println('OK') + exit(0) + } + } + ) + _ := x.user_data + println('OK') +}