Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser, checker: check $veb.html('index.html') (fix #21929) #21961

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion vlib/v/checker/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,17 @@ fn (mut c Checker) comptime_call(mut node ast.ComptimeCall) ast.Type {
c.table.cur_fn = save_cur_fn
}
if node.method_name == 'html' {
rtyp := c.table.find_type_idx('vweb.Result')
ret_sym := c.table.sym(c.table.cur_fn.return_type)
if ret_sym.cname !in ['veb__Result', 'vweb__Result', 'x__vweb__Result'] {
ct_call := if node.is_veb { 'veb' } else { 'vweb' }
c.error('`\$${ct_call}.html()` must be called inside a web method, e.g. `fn (mut app App) foo(mut ctx Context) ${ct_call}.Result { return \$${ct_call}.html(\'index.html\') }`',
node.pos)
}
rtyp := if node.is_veb {
c.table.find_type_idx('veb.Result')
} else {
c.table.find_type_idx('vweb.Result')
}
node.result_type = rtyp
return rtyp
}
Expand Down
13 changes: 13 additions & 0 deletions vlib/v/checker/tests/comptime_veb_vweb_call_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
vlib/v/checker/tests/comptime_veb_vweb_call_err.vv:5:2: error: `$veb.html()` must be called inside a web method, e.g. `fn (mut app App) foo(mut ctx Context) veb.Result { return $veb.html('index.html') }`
3 |
4 | fn main() {
5 | $veb.html('./templates/empty.html')
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 | $vweb.html('./templates/empty.html')
7 | }
vlib/v/checker/tests/comptime_veb_vweb_call_err.vv:6:2: error: `$vweb.html()` must be called inside a web method, e.g. `fn (mut app App) foo(mut ctx Context) vweb.Result { return $vweb.html('index.html') }`
4 | fn main() {
5 | $veb.html('./templates/empty.html')
6 | $vweb.html('./templates/empty.html')
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 | }
7 changes: 7 additions & 0 deletions vlib/v/checker/tests/comptime_veb_vweb_call_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import veb
import vweb

fn main() {
$veb.html('./templates/empty.html')
$vweb.html('./templates/empty.html')
}
1 change: 1 addition & 0 deletions vlib/v/checker/tests/templates/empty.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html></html>
10 changes: 7 additions & 3 deletions vlib/v/parser/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,18 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall {
return err_node
}
import_mods := p.ast_imports.map(it.mod)
if name == 'vweb' && 'vweb' !in import_mods && 'x.vweb' !in import_mods {
p.error_with_pos('`\$vweb` cannot be used without importing vweb', start_pos.extend(p.prev_tok.pos()))
return err_node
if name == 'vweb' {
if 'vweb' !in import_mods && 'x.vweb' !in import_mods {
p.error_with_pos('`\$vweb` cannot be used without importing vweb', start_pos.extend(p.prev_tok.pos()))
return err_node
}
p.register_used_import('vweb')
} else if name == 'veb' {
if 'veb' !in import_mods {
p.error_with_pos('`\$veb` cannot be used without importing veb', start_pos.extend(p.prev_tok.pos()))
return err_node
}
p.register_used_import('veb')
is_veb = true
}
p.check(.dot)
Expand Down
Loading