From a794dea809ffb0d487cae1822dc05ffe51d1cacd Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 19 Jul 2023 20:09:45 +0300 Subject: [PATCH] checker: make sure vweb actions return vweb.Result --- vlib/v/checker/fn.v | 14 ++++++++++++++ vlib/v/checker/tests/vweb_missing_result.out | 7 +++++++ vlib/v/checker/tests/vweb_missing_result.vv | 15 +++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 vlib/v/checker/tests/vweb_missing_result.out create mode 100644 vlib/v/checker/tests/vweb_missing_result.vv diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index f8a76b9e81..abce5b0691 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -410,6 +410,19 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) { 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 @@ -2175,6 +2188,7 @@ fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type { } } } + return node.return_type } diff --git a/vlib/v/checker/tests/vweb_missing_result.out b/vlib/v/checker/tests/vweb_missing_result.out new file mode 100644 index 0000000000..25d269bbf8 --- /dev/null +++ b/vlib/v/checker/tests/vweb_missing_result.out @@ -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 | diff --git a/vlib/v/checker/tests/vweb_missing_result.vv b/vlib/v/checker/tests/vweb_missing_result.vv new file mode 100644 index 0000000000..220f513bb3 --- /dev/null +++ b/vlib/v/checker/tests/vweb_missing_result.vv @@ -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) +}