From 99635cfba922475d7e639680c1cd90149608a896 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Wed, 19 Feb 2025 12:41:27 -0300 Subject: [PATCH] checker: fix missing check for method that returns veb.Result (fix #23647) (#23762) --- vlib/v/checker/fn.v | 2 +- vlib/v/checker/tests/veb_ctx_on_fn_err.out | 12 +++++ vlib/v/checker/tests/veb_ctx_on_fn_err.vv | 51 ++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 vlib/v/checker/tests/veb_ctx_on_fn_err.out create mode 100644 vlib/v/checker/tests/veb_ctx_on_fn_err.vv diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 0fc8ad28d11720..1205fc20803fe3 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -478,7 +478,7 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) { c.fn_scope = node.scope // Register implicit context var typ_veb_result := c.table.get_veb_result_type_idx() // c.table.find_type('veb.Result') - if node.return_type == typ_veb_result { + if node.is_method && node.return_type == typ_veb_result { // Find a custom user Context type first mut ctx_idx := c.table.find_type('main.Context') if ctx_idx < 1 { diff --git a/vlib/v/checker/tests/veb_ctx_on_fn_err.out b/vlib/v/checker/tests/veb_ctx_on_fn_err.out new file mode 100644 index 00000000000000..eec31c5e9a3803 --- /dev/null +++ b/vlib/v/checker/tests/veb_ctx_on_fn_err.out @@ -0,0 +1,12 @@ +vlib/v/checker/tests/veb_ctx_on_fn_err.vv:50:9: error: undefined ident: `ctx` + 48 | fn serve_file(name string) veb.Result { + 49 | content := os.read_file(name) or { panic('Error reading ${name}') } + 50 | return ctx.html(content) + | ~~~ + 51 | } +vlib/v/checker/tests/veb_ctx_on_fn_err.vv:50:2: error: `ctx.html(content)` used as value + 48 | fn serve_file(name string) veb.Result { + 49 | content := os.read_file(name) or { panic('Error reading ${name}') } + 50 | return ctx.html(content) + | ~~~~~~~~~~~~~~~~~~~~~~~~ + 51 | } diff --git a/vlib/v/checker/tests/veb_ctx_on_fn_err.vv b/vlib/v/checker/tests/veb_ctx_on_fn_err.vv new file mode 100644 index 00000000000000..16b59b8e2e9531 --- /dev/null +++ b/vlib/v/checker/tests/veb_ctx_on_fn_err.vv @@ -0,0 +1,51 @@ +module main + +import veb +import os + +pub struct User { +pub mut: + name string + id int +} + +// Our context struct must embed `veb.Context`! +pub struct Context { + veb.Context +pub mut: + // In the context struct we store data that could be different + // for each request. Like a User struct or a session id + user User + session_id string +} + +pub struct App { +pub: + // In the app struct we store data that should be accessible by all endpoints. + // For example, a database or configuration values. + secret_key string +} + +// This is how endpoints are defined in veb. This is the index route + +fn main() { + mut app := &App{ + secret_key: 'secret' + } + // Pass the App and context type and start the web server on port 8080 + veb.run[App, Context](mut app, 8080) +} + +@['/foo'] +pub fn (app &App) world(mut ctx Context) veb.Result { + return ctx.text('World') +} + +pub fn (app &App) index(mut ctx Context) veb.Result { + return serve_file('html/index.html') +} + +fn serve_file(name string) veb.Result { + content := os.read_file(name) or { panic('Error reading ${name}') } + return ctx.html(content) +}