@@ -459,7 +459,9 @@ fn (mut c Checker) alias_type_decl(node ast.AliasTypeDecl) {
459459 if c.file.mod.name != 'builtin' {
460460 c.check_valid_pascal_case (node.name, 'type alias' , node.pos)
461461 }
462- c.ensure_type_exists (node.parent_type, node.type_pos) or { return }
462+ if ! c.ensure_type_exists (node.parent_type, node.type_pos) {
463+ return
464+ }
463465 mut parent_typ_sym := c.table.sym (node.parent_type)
464466 if node.parent_type.has_flag (.result) {
465467 c.add_error_detail ('Result types cannot be stored and have to be unwrapped immediately' )
@@ -551,13 +553,15 @@ fn (mut c Checker) fn_type_decl(node ast.FnTypeDecl) {
551553 typ_sym := c.table.sym (node.typ)
552554 fn_typ_info := typ_sym.info as ast.FnType
553555 fn_info := fn_typ_info.func
554- c.ensure_type_exists (fn_info.return_type, fn_info.return_type_pos) or {}
556+ c.ensure_type_exists (fn_info.return_type, fn_info.return_type_pos)
555557 ret_sym := c.table.sym (fn_info.return_type)
556558 if ret_sym.kind == .placeholder {
557559 c.error ('unknown type `${ret_sym.name} `' , fn_info.return_type_pos)
558560 }
559561 for arg in fn_info.params {
560- c.ensure_type_exists (arg.typ, arg.type_pos) or { return }
562+ if ! c.ensure_type_exists (arg.typ, arg.type_pos) {
563+ return
564+ }
561565 arg_sym := c.table.sym (arg.typ)
562566 if arg_sym.kind == .placeholder {
563567 c.error ('unknown type `${arg_sym.name} `' , arg.type_pos)
@@ -569,7 +573,7 @@ fn (mut c Checker) sum_type_decl(node ast.SumTypeDecl) {
569573 c.check_valid_pascal_case (node.name, 'sum type' , node.pos)
570574 mut names_used := []string {}
571575 for variant in node.variants {
572- c.ensure_type_exists (variant.typ, variant.pos) or {}
576+ c.ensure_type_exists (variant.typ, variant.pos)
573577 sym := c.table.sym (variant.typ)
574578 if variant.typ.is_ptr () {
575579 variant_name := sym.name.all_after_last ('.' )
@@ -761,7 +765,9 @@ fn (mut c Checker) fail_if_immutable(mut expr ast.Expr) (string, token.Pos) {
761765 return '' , expr.pos
762766 }
763767 // retrieve ast.Field
764- c.ensure_type_exists (expr.expr_type, expr.pos) or { return '' , expr.pos }
768+ if ! c.ensure_type_exists (expr.expr_type, expr.pos) {
769+ return '' , expr.pos
770+ }
765771 mut typ_sym := c.table.final_sym (c.unwrap_generic (expr.expr_type))
766772 match typ_sym.kind {
767773 .struct_ {
@@ -2492,14 +2498,14 @@ pub fn (mut c Checker) expr(mut node ast.Expr) ast.Type {
24922498 expr_type_sym := c.table.sym (node.expr_type)
24932499 type_sym := c.table.sym (node.typ)
24942500 if expr_type_sym.kind == .sum_type {
2495- c.ensure_type_exists (node.typ, node.pos) or {}
2501+ c.ensure_type_exists (node.typ, node.pos)
24962502 if ! c.table.sumtype_has_variant (node.expr_type, node.typ, true ) {
24972503 addr := '&' .repeat (node.typ.nr_muls ())
24982504 c.error ('cannot cast `${expr_type_sym.name} ` to `${addr}${type_sym.name} `' ,
24992505 node.pos)
25002506 }
25012507 } else if expr_type_sym.kind == .interface_ && type_sym.kind == .interface_ {
2502- c.ensure_type_exists (node.typ, node.pos) or {}
2508+ c.ensure_type_exists (node.typ, node.pos)
25032509 } else if node.expr_type.clear_flag (.option) != node.typ.clear_flag (.option) {
25042510 mut s := 'cannot cast non-sum type `${expr_type_sym.name} ` using `as`'
25052511 if type_sym.kind == .sum_type {
@@ -2865,7 +2871,7 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
28652871 }
28662872
28672873 if to_sym.language != .c {
2868- c.ensure_type_exists (to_type, node.pos) or {}
2874+ c.ensure_type_exists (to_type, node.pos)
28692875
28702876 if to_sym.info is ast.Alias && to_sym.info.parent_type.has_flag (.option)
28712877 && ! to_type.has_flag (.option) {
@@ -4548,10 +4554,10 @@ fn (mut c Checker) trace(fbase string, message string) {
45484554 }
45494555}
45504556
4551- fn (mut c Checker) ensure_generic_type_specify_type_names (typ ast.Type, pos token.Pos) ? {
4557+ fn (mut c Checker) ensure_generic_type_specify_type_names (typ ast.Type, pos token.Pos) bool {
45524558 if typ == 0 {
45534559 c.error ('unknown type' , pos)
4554- return none
4560+ return false
45554561 }
45564562
45574563 c.ensure_generic_type_level++
@@ -4561,7 +4567,7 @@ fn (mut c Checker) ensure_generic_type_specify_type_names(typ ast.Type, pos toke
45614567 if c.ensure_generic_type_level > checker.expr_level_cutoff_limit {
45624568 c.error ('checker: too many levels of Checker.ensure_generic_type_specify_type_names calls: ${c.ensure_generic_type_level} ' ,
45634569 pos)
4564- return none
4570+ return false
45654571 }
45664572
45674573 sym := c.table.final_sym (typ)
@@ -4574,66 +4580,82 @@ fn (mut c Checker) ensure_generic_type_specify_type_names(typ ast.Type, pos toke
45744580 match sym.kind {
45754581 .function {
45764582 fn_info := sym.info as ast.FnType
4577- c.ensure_generic_type_specify_type_names (fn_info.func.return_type, fn_info.func.return_type_pos)?
4583+ if ! c.ensure_generic_type_specify_type_names (fn_info.func.return_type, fn_info.func.return_type_pos) {
4584+ return false
4585+ }
45784586 for param in fn_info.func.params {
4579- c.ensure_generic_type_specify_type_names (param.typ, param.type_pos)?
4587+ if ! c.ensure_generic_type_specify_type_names (param.typ, param.type_pos) {
4588+ return false
4589+ }
45804590 }
45814591 }
45824592 .array {
4583- c.ensure_generic_type_specify_type_names ((sym.info as ast.Array ).elem_type,
4584- pos)?
4593+ if ! c.ensure_generic_type_specify_type_names ((sym.info as ast.Array ).elem_type,
4594+ pos) {
4595+ return false
4596+ }
45854597 }
45864598 .array_fixed {
4587- c.ensure_generic_type_specify_type_names ((sym.info as ast.ArrayFixed ).elem_type,
4588- pos)?
4599+ if ! c.ensure_generic_type_specify_type_names ((sym.info as ast.ArrayFixed ).elem_type,
4600+ pos) {
4601+ return false
4602+ }
45894603 }
45904604 .map {
45914605 info := sym.info as ast.Map
4592- c.ensure_generic_type_specify_type_names (info.key_type, pos)?
4593- c.ensure_generic_type_specify_type_names (info.value_type, pos)?
4606+ if ! c.ensure_generic_type_specify_type_names (info.key_type, pos) {
4607+ return false
4608+ }
4609+ if ! c.ensure_generic_type_specify_type_names (info.value_type, pos) {
4610+ return false
4611+ }
45944612 }
45954613 .sum_type {
45964614 info := sym.info as ast.SumType
45974615 if info.generic_types.len > 0 && ! typ.has_flag (.generic) && info.concrete_types.len == 0 {
45984616 c.error ('`${sym.name} ` type is generic sumtype, must specify the generic type names, e.g. ${sym.name} [T], ${sym.name} [int]' ,
45994617 pos)
4618+ return false
46004619 }
46014620 }
46024621 .struct_ {
46034622 info := sym.info as ast.Struct
46044623 if info.generic_types.len > 0 && ! typ.has_flag (.generic) && info.concrete_types.len == 0 {
46054624 c.error ('`${sym.name} ` type is generic struct, must specify the generic type names, e.g. ${sym.name} [T], ${sym.name} [int]' ,
46064625 pos)
4626+ return false
46074627 }
46084628 }
46094629 .interface_ {
46104630 info := sym.info as ast.Interface
46114631 if info.generic_types.len > 0 && ! typ.has_flag (.generic) && info.concrete_types.len == 0 {
46124632 c.error ('`${sym.name} ` type is generic interface, must specify the generic type names, e.g. ${sym.name} [T], ${sym.name} [int]' ,
46134633 pos)
4634+ return false
46144635 }
46154636 }
46164637 else {}
46174638 }
4639+ return true
46184640}
46194641
4620- fn (mut c Checker) ensure_type_exists (typ ast.Type, pos token.Pos) ? {
4642+ fn (mut c Checker) ensure_type_exists (typ ast.Type, pos token.Pos) bool {
46214643 if typ == 0 {
46224644 c.error ('unknown type' , pos)
4623- return
4645+ return false
46244646 }
46254647 sym := c.table.sym (typ)
46264648 if ! c.is_builtin_mod && sym.kind == .struct_ && ! sym.is_pub && sym.mod != c.mod {
46274649 c.error ('struct `${sym.name} ` was declared as private to module `${sym.mod} `, so it can not be used inside module `${c.mod} `' ,
46284650 pos)
4629- return
4651+ return false
46304652 }
46314653 match sym.kind {
46324654 .placeholder {
46334655 if sym.language == .v && ! sym.name.starts_with ('C.' ) {
46344656 c.error (util.new_suggestion (sym.name, c.table.known_type_names ()).say ('unknown type `${sym.name} `' ),
46354657 pos)
4636- return
4658+ return false
46374659 }
46384660 }
46394661 .int_literal, .float_literal {
@@ -4646,35 +4668,50 @@ fn (mut c Checker) ensure_type_exists(typ ast.Type, pos token.Pos) ? {
46464668 'unknown type `${sym.name} `.\n Did you mean `f64`?'
46474669 }
46484670 c.error (msg, pos)
4649- return
4671+ return false
46504672 }
46514673 }
46524674 .function {
46534675 fn_info := sym.info as ast.FnType
4654- c.ensure_type_exists (fn_info.func.return_type, fn_info.func.return_type_pos)?
4676+ if ! c.ensure_type_exists (fn_info.func.return_type, fn_info.func.return_type_pos) {
4677+ return false
4678+ }
46554679 for param in fn_info.func.params {
4656- c.ensure_type_exists (param.typ, param.type_pos)?
4680+ if ! c.ensure_type_exists (param.typ, param.type_pos) {
4681+ return false
4682+ }
46574683 }
46584684 }
46594685 .array {
4660- c.ensure_type_exists ((sym.info as ast.Array ).elem_type, pos)?
4686+ if ! c.ensure_type_exists ((sym.info as ast.Array ).elem_type, pos) {
4687+ return false
4688+ }
46614689 }
46624690 .array_fixed {
4663- c.ensure_type_exists ((sym.info as ast.ArrayFixed ).elem_type, pos)?
4691+ if ! c.ensure_type_exists ((sym.info as ast.ArrayFixed ).elem_type, pos) {
4692+ return false
4693+ }
46644694 }
46654695 .map {
46664696 info := sym.info as ast.Map
4667- c.ensure_type_exists (info.key_type, pos)?
4668- c.ensure_type_exists (info.value_type, pos)?
4697+ if ! c.ensure_type_exists (info.key_type, pos) {
4698+ return false
4699+ }
4700+ if ! c.ensure_type_exists (info.value_type, pos) {
4701+ return false
4702+ }
46694703 }
46704704 .sum_type {
46714705 info := sym.info as ast.SumType
46724706 for concrete_typ in info.concrete_types {
4673- c.ensure_type_exists (concrete_typ, pos)?
4707+ if ! c.ensure_type_exists (concrete_typ, pos) {
4708+ return false
4709+ }
46744710 }
46754711 }
46764712 else {}
46774713 }
4714+ return true
46784715}
46794716
46804717// return true if a violation of a shared variable access rule is detected
0 commit comments