From 969402461f39e28eaac1e90be3de6551f8035ded Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sat, 30 Dec 2023 23:33:28 +0200 Subject: [PATCH] markused: add `-skip-unused` for programs that `import x.vweb` too (do not skip unused routing methods) --- vlib/v/checker/fn.v | 2 +- vlib/v/markused/markused.v | 58 ++++++++++--------- .../v/tests/skip_unused/x_vweb_run_at.run.out | 2 + .../x_vweb_run_at.skip_unused.run.out | 2 + vlib/v/tests/skip_unused/x_vweb_run_at.vv | 23 ++++++++ 5 files changed, 59 insertions(+), 28 deletions(-) create mode 100644 vlib/v/tests/skip_unused/x_vweb_run_at.run.out create mode 100644 vlib/v/tests/skip_unused/x_vweb_run_at.skip_unused.run.out create mode 100644 vlib/v/tests/skip_unused/x_vweb_run_at.vv diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 5ae7c01c4f..499ddde81e 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -2489,7 +2489,7 @@ fn (mut c Checker) post_process_generic_fns() ! { for concrete_types in gtypes { c.table.cur_concrete_types = concrete_types c.fn_decl(mut node) - if node.name in ['vweb.run', 'vweb.run_at'] { + if node.name in ['x.vweb.run', 'x.vweb.run_at', 'vweb.run', 'vweb.run_at'] { for ct in concrete_types { if ct !in c.vweb_gen_types { c.vweb_gen_types << ct diff --git a/vlib/v/markused/markused.v b/vlib/v/markused/markused.v index cba8ed9c65..69bcf5ca9d 100644 --- a/vlib/v/markused/markused.v +++ b/vlib/v/markused/markused.v @@ -304,33 +304,8 @@ pub fn mark_used(mut table ast.Table, pref_ &pref.Preferences, ast_files []&ast. } } - // handle vweb magic router methods: - typ_vweb_result := table.find_type_idx('vweb.Result') - if typ_vweb_result != 0 { - all_fn_root_names << 'vweb.filter' - typ_vweb_context := ast.Type(table.find_type_idx('vweb.Context')).set_nr_muls(1) - all_fn_root_names << '${int(typ_vweb_context)}.html' - for vgt in table.used_vweb_types { - sym_app := table.sym(vgt) - for m in sym_app.methods { - mut skip := true - if m.name == 'before_request' { - // TODO: handle expansion of method calls in generic functions in a more universal way - skip = false - } - if m.return_type == typ_vweb_result { - skip = false - } - // - if skip { - continue - } - pvgt := vgt.set_nr_muls(1) - // eprintln('vgt: $vgt | pvgt: $pvgt | sym_app.name: $sym_app.name | m.name: $m.name') - all_fn_root_names << '${int(pvgt)}.${m.name}' - } - } - } + handle_vweb(mut table, mut all_fn_root_names, 'vweb.Result', 'vweb.filter', 'vweb.Context') + handle_vweb(mut table, mut all_fn_root_names, 'x.vweb.Result', 'x.vweb.filter', 'x.vweb.Context') // handle ORM drivers: orm_connection_implementations := table.iface_types['orm.Connection'] or { []ast.Type{} } @@ -464,3 +439,32 @@ fn all_fn_const_and_global(ast_files []&ast.File) (map[string]ast.FnDecl, map[st } return all_fns, all_consts, all_globals } + +fn handle_vweb(mut table ast.Table, mut all_fn_root_names []string, result_name string, filter_name string, context_name string) { + // handle vweb magic router methods: + result_type_idx := table.find_type_idx(result_name) + if result_type_idx != 0 { + all_fn_root_names << filter_name + typ_vweb_context := ast.Type(table.find_type_idx(context_name)).set_nr_muls(1) + all_fn_root_names << '${int(typ_vweb_context)}.html' + for vgt in table.used_vweb_types { + sym_app := table.sym(vgt) + for m in sym_app.methods { + mut skip := true + if m.name == 'before_request' { + // TODO: handle expansion of method calls in generic functions in a more universal way + skip = false + } + if m.return_type == result_type_idx { + skip = false + } + if skip { + continue + } + pvgt := vgt.set_nr_muls(1) + // eprintln('vgt: $vgt | pvgt: $pvgt | sym_app.name: $sym_app.name | m.name: $m.name') + all_fn_root_names << '${int(pvgt)}.${m.name}' + } + } + } +} diff --git a/vlib/v/tests/skip_unused/x_vweb_run_at.run.out b/vlib/v/tests/skip_unused/x_vweb_run_at.run.out new file mode 100644 index 0000000000..1b38a26841 --- /dev/null +++ b/vlib/v/tests/skip_unused/x_vweb_run_at.run.out @@ -0,0 +1,2 @@ +[Vweb] Running app on http://localhost:38090/ +done diff --git a/vlib/v/tests/skip_unused/x_vweb_run_at.skip_unused.run.out b/vlib/v/tests/skip_unused/x_vweb_run_at.skip_unused.run.out new file mode 100644 index 0000000000..1b38a26841 --- /dev/null +++ b/vlib/v/tests/skip_unused/x_vweb_run_at.skip_unused.run.out @@ -0,0 +1,2 @@ +[Vweb] Running app on http://localhost:38090/ +done diff --git a/vlib/v/tests/skip_unused/x_vweb_run_at.vv b/vlib/v/tests/skip_unused/x_vweb_run_at.vv new file mode 100644 index 0000000000..8b6ec90510 --- /dev/null +++ b/vlib/v/tests/skip_unused/x_vweb_run_at.vv @@ -0,0 +1,23 @@ +import x.vweb +import time + +pub struct App {} + +pub struct Context { + vweb.Context +} + +fn main() { + spawn fn () { + time.sleep(100 * time.millisecond) + println('done') + exit(0) + }() + mut app := &App{} + vweb.run_at[App, Context](mut app, port: 38090)! +} + +@['/'] +pub fn (app &App) index(mut ctx Context) vweb.Result { + return ctx.text('Hello World') +}