diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 78bd3f8b37..934d38a1ba 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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' { diff --git a/vlib/v/checker/tests/fn_init_sig.out b/vlib/v/checker/tests/fn_init_sig.out new file mode 100644 index 0000000000..08cb17b952 --- /dev/null +++ b/vlib/v/checker/tests/fn_init_sig.out @@ -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 | } diff --git a/vlib/v/checker/tests/fn_init_sig.vv b/vlib/v/checker/tests/fn_init_sig.vv new file mode 100644 index 0000000000..bff6d491c7 --- /dev/null +++ b/vlib/v/checker/tests/fn_init_sig.vv @@ -0,0 +1,6 @@ +fn init() int { + return 1 +} +pub fn init() int { + return 1 +}