Skip to content

Commit 99635cf

Browse files
authored
checker: fix missing check for method that returns veb.Result (fix #23647) (#23762)
1 parent dfacc33 commit 99635cf

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

vlib/v/checker/fn.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
478478
c.fn_scope = node.scope
479479
// Register implicit context var
480480
typ_veb_result := c.table.get_veb_result_type_idx() // c.table.find_type('veb.Result')
481-
if node.return_type == typ_veb_result {
481+
if node.is_method && node.return_type == typ_veb_result {
482482
// Find a custom user Context type first
483483
mut ctx_idx := c.table.find_type('main.Context')
484484
if ctx_idx < 1 {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
vlib/v/checker/tests/veb_ctx_on_fn_err.vv:50:9: error: undefined ident: `ctx`
2+
48 | fn serve_file(name string) veb.Result {
3+
49 | content := os.read_file(name) or { panic('Error reading ${name}') }
4+
50 | return ctx.html(content)
5+
| ~~~
6+
51 | }
7+
vlib/v/checker/tests/veb_ctx_on_fn_err.vv:50:2: error: `ctx.html(content)` used as value
8+
48 | fn serve_file(name string) veb.Result {
9+
49 | content := os.read_file(name) or { panic('Error reading ${name}') }
10+
50 | return ctx.html(content)
11+
| ~~~~~~~~~~~~~~~~~~~~~~~~
12+
51 | }
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module main
2+
3+
import veb
4+
import os
5+
6+
pub struct User {
7+
pub mut:
8+
name string
9+
id int
10+
}
11+
12+
// Our context struct must embed `veb.Context`!
13+
pub struct Context {
14+
veb.Context
15+
pub mut:
16+
// In the context struct we store data that could be different
17+
// for each request. Like a User struct or a session id
18+
user User
19+
session_id string
20+
}
21+
22+
pub struct App {
23+
pub:
24+
// In the app struct we store data that should be accessible by all endpoints.
25+
// For example, a database or configuration values.
26+
secret_key string
27+
}
28+
29+
// This is how endpoints are defined in veb. This is the index route
30+
31+
fn main() {
32+
mut app := &App{
33+
secret_key: 'secret'
34+
}
35+
// Pass the App and context type and start the web server on port 8080
36+
veb.run[App, Context](mut app, 8080)
37+
}
38+
39+
@['/foo']
40+
pub fn (app &App) world(mut ctx Context) veb.Result {
41+
return ctx.text('World')
42+
}
43+
44+
pub fn (app &App) index(mut ctx Context) veb.Result {
45+
return serve_file('html/index.html')
46+
}
47+
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+
}

0 commit comments

Comments
 (0)