checker: check that the module `init` fn, should have no params, and no return type (#8988)
							parent
							
								
									d0a64f2da7
								
							
						
					
					
						commit
						a1244a9f5f
					
				|  | @ -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' { | ||||
|  |  | |||
|  | @ -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 | } | ||||
|  | @ -0,0 +1,6 @@ | |||
| fn init() int { | ||||
|     return 1 | ||||
| } | ||||
| pub fn init() int { | ||||
|     return 1 | ||||
| } | ||||
		Loading…
	
		Reference in New Issue