checker: check that the module `init` fn, should have no params, and no return type (#8988)

pull/8999/head
Nick Treleaven 2021-02-27 09:11:20 +00:00 committed by GitHub
parent d0a64f2da7
commit a1244a9f5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 0 deletions

View File

@ -5797,6 +5797,15 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
c.ensure_type_exists(arg.typ, node.pos) or { return }
}
}
if node.language == .v && node.name.after_char(`.`) == 'init' && !node.is_method
&& node.params.len == 0 {
if node.is_pub {
c.error('fn `init` must not be public', node.pos)
}
if node.return_type != table.void_type {
c.error('fn `init` cannot have a return type', node.pos)
}
}
if node.return_type != table.Type(0) {
c.ensure_type_exists(node.return_type, node.pos) or { return }
if node.language == .v && node.is_method && node.name == 'str' {

View File

@ -0,0 +1,12 @@
vlib/v/checker/tests/fn_init_sig.vv:1:1: error: fn `init` cannot have a return type
1 | fn init() int {
| ~~~~~~~~~~~~~
2 | return 1
3 | }
vlib/v/checker/tests/fn_init_sig.vv:4:1: error: fn `init` must not be public
2 | return 1
3 | }
4 | pub fn init() int {
| ~~~~~~~~~~~~~~~~~
5 | return 1
6 | }

View File

@ -0,0 +1,6 @@
fn init() int {
return 1
}
pub fn init() int {
return 1
}