From 6ccdf895461a3737b277f511015c5461de5ab2cc Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 5 Oct 2022 18:23:47 +0800 Subject: [PATCH] cgen: fix vweb using generic method (fix #15888) (#15963) --- vlib/v/gen/c/comptime.v | 17 ++++++++++++----- vlib/vweb/vweb_app_test.v | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 3eccc27645..40d85f77ee 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -467,12 +467,19 @@ fn (mut g Gen) comptime_for(node ast.ComptimeFor) { if methods.len > 0 { g.writeln('FunctionData $node.val_var = {0};') } - for method in methods { // sym.methods { - /* - if method.return_type != vweb_result_type { // ast.void_type { - continue + typ_vweb_result := g.table.find_type_idx('vweb.Result') + for method in methods { + // filter vweb route methods (non-generic method) + if method.receiver_type != 0 && method.return_type == typ_vweb_result { + rec_sym := g.table.sym(method.receiver_type) + if rec_sym.kind == .struct_ { + if _ := g.table.find_field_with_embeds(rec_sym, 'Context') { + if method.generic_names.len > 0 { + continue + } + } + } } - */ g.comptime_for_method = method.name g.writeln('/* method $i */ {') g.writeln('\t${node.val_var}.name = _SLIT("$method.name");') diff --git a/vlib/vweb/vweb_app_test.v b/vlib/vweb/vweb_app_test.v index 6fa1e987fc..b46e35d6f0 100644 --- a/vlib/vweb/vweb_app_test.v +++ b/vlib/vweb/vweb_app_test.v @@ -64,3 +64,31 @@ fn (mut app App) time_json_pretty() { 'time': time.now().format() }) } + +struct ApiSuccessResponse { + success bool + result T +} + +fn (mut app App) json_success(result T) vweb.Result { + response := ApiSuccessResponse{ + success: true + result: result + } + + return app.json(response) +} + +// should compile, this is a helper method, not exposed as a route +fn (mut app App) some_helper(result T) ApiSuccessResponse { + response := ApiSuccessResponse{ + success: true + result: result + } + return response +} + +// should compile, the route method itself is not generic +fn (mut app App) ok() vweb.Result { + return app.json(app.some_helper(123)) +}