Skip to content

Commit 7335a3a

Browse files
committed
veb: param fix
1 parent c29113f commit 7335a3a

2 files changed

Lines changed: 30 additions & 5 deletions

File tree

vlib/v/gen/c/cgen.v

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7554,7 +7554,7 @@ fn (g &Gen) has_veb_context(typ ast.Type) bool {
75547554
}
75557555

75567556
// implicit_veb_ctx_alias_target maps the checker-injected `ctx` alias back
7557-
// to the user-declared veb context parameter, when their names differ.
7557+
// to the user-declared veb context parameter or receiver, when their names differ.
75587558
@[inline]
75597559
fn (g &Gen) implicit_veb_ctx_alias_target(obj ast.Var) ?ast.Param {
75607560
if g.fn_decl == unsafe { nil } || obj.name != 'ctx' || obj.is_arg
@@ -7563,10 +7563,7 @@ fn (g &Gen) implicit_veb_ctx_alias_target(obj ast.Var) ?ast.Param {
75637563
}
75647564
mut target := ast.Param{}
75657565
mut found := 0
7566-
for i, param in g.fn_decl.params {
7567-
if g.fn_decl.is_method && i == 0 {
7568-
continue
7569-
}
7566+
for param in g.fn_decl.params {
75707567
if !g.has_veb_context(param.typ) {
75717568
continue
75727569
}

vlib/v/gen/c/coutput_test.v

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,34 @@ fn test_veb_implicit_ctx_alias_uses_user_context_name() {
373373
assert normalized.contains('veb__Result main__App_index(main__App app, main__Context* c) { main__App_log(app, *c); GC_reachable_here(&c); return main__App_nested(app, c); }')
374374
}
375375

376+
fn test_veb_implicit_ctx_alias_on_context_receiver_tmpl_not_found() {
377+
os.chdir(vroot) or {}
378+
test_dir := os.join_path(os.vtmp_dir(), 'coutput_veb_context_receiver_tmpl_not_found')
379+
os.rmdir_all(test_dir) or {}
380+
os.mkdir_all(os.join_path(test_dir, 'web'))!
381+
test_source := os.join_path(test_dir, 'main.v')
382+
os.write_file(os.join_path(test_dir, 'web', 'notfound.html'), '<h1>@ctx.req.url</h1>\n')!
383+
os.write_file(test_source,
384+
['module main', '', 'import veb', '', 'pub struct Context {', '\tveb.Context', '}', '', 'pub struct App {}', '', 'pub fn (mut c Context) not_found() veb.Result {', '\tc.res.set_status(.not_found)', "\treturn c.html(\$tmpl('web/notfound.html'))", '}', '', 'fn main() {', '\tmut app := App{}', '\tveb.run[App, Context](mut app, 8080)', '}'].join('\n') +
385+
'\n')!
386+
defer {
387+
os.rmdir_all(test_dir) or {}
388+
}
389+
test_exe := os.join_path(test_dir, 'app')
390+
compile_cmd := '${os.quoted_path(vexe)} -gc boehm_full_opt -o ${os.quoted_path(test_exe)} ${os.quoted_path(test_source)}'
391+
ensure_compilation_succeeded(os.execute(compile_cmd), compile_cmd)
392+
c_cmd := '${os.quoted_path(vexe)} -gc boehm_full_opt -o - ${os.quoted_path(test_source)}'
393+
compilation := os.execute(c_cmd)
394+
ensure_compilation_succeeded(compilation, c_cmd)
395+
not_found_start := 'veb__Result main__Context_not_found(main__Context* c) {'
396+
assert compilation.output.contains(not_found_start)
397+
not_found_body :=
398+
compilation.output.all_after(not_found_start).all_before('VV_LOC void main__main')
399+
assert !not_found_body.contains('GC_reachable_here(&ctx);')
400+
assert not_found_body.contains('GC_reachable_here(&c);')
401+
assert not_found_body.contains('return veb__Context_html(&c->Context, _tmpl_res_')
402+
}
403+
376404
fn does_line_match_one_of_generated_lines(line string, generated_c_lines []string) bool {
377405
for cline in generated_c_lines {
378406
if line == cline {

0 commit comments

Comments
 (0)