1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: make sure vweb actions return vweb.Result

This commit is contained in:
Alexander Medvednikov 2023-07-19 20:09:45 +03:00
parent 1ed0cd9a74
commit a794dea809
3 changed files with 36 additions and 0 deletions

View File

@ -410,6 +410,19 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
c.table.fns[node.name].dep_names = dep_names c.table.fns[node.name].dep_names = dep_names
} }
} }
// vweb checks
if node.attrs.len > 0 && c.file.imports.filter(it.mod == 'vweb').len > 0 {
// If it's a vweb action (has the ['/url'] attribute), make sure it returns a vweb.Result
for attr in node.attrs {
if attr.name.starts_with('/') {
if c.table.sym(node.return_type).name != 'vweb.Result' {
c.error('vweb actions must return `vweb.Result`', node.pos)
}
break
}
}
}
} }
// check_same_type_ignoring_pointers util function to check if the Types are the same, including all // check_same_type_ignoring_pointers util function to check if the Types are the same, including all
@ -2175,6 +2188,7 @@ fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
} }
} }
} }
return node.return_type return node.return_type
} }

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/vweb_missing_result.vv:9:1: error: vweb actions must return `vweb.Result`
7 | // actions must return results
8 | ['/foo/:bar']
9 | pub fn (mut app App) foo(a string) {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 | }
11 |

View File

@ -0,0 +1,15 @@
import vweb
struct App {
vweb.Context
}
// actions must return results
['/foo/:bar']
pub fn (mut app App) foo(a string) {
}
fn main() {
port := 8181
vweb.run[App](&App{}, port)
}