@@ -1026,6 +1026,28 @@ fn (mut c Checker) type_implements(typ ast.Type, interface_type ast.Type, pos to
1026
1026
return true
1027
1027
}
1028
1028
1029
+ fn (mut c Checker) expr_or_block_err (kind ast.OrKind, expr_name string , pos token.Pos, is_field bool ) {
1030
+ obj_does_not_return_or_is_not := if is_field {
1031
+ 'field `${expr_name} ` is not'
1032
+ } else {
1033
+ 'function `${expr_name} ` does not return'
1034
+ }
1035
+ match kind {
1036
+ .absent {}
1037
+ .block {
1038
+ c.error ('unexpected `or` block, the ${obj_does_not_return_or_is_not} an Option or a Result' ,
1039
+ pos)
1040
+ }
1041
+ .propagate_option {
1042
+ c.error ('unexpected `?`, the ${obj_does_not_return_or_is_not} an Option' ,
1043
+ pos)
1044
+ }
1045
+ .propagate_result {
1046
+ c.error ('unexpected `!`, the ${obj_does_not_return_or_is_not} a Result' , pos)
1047
+ }
1048
+ }
1049
+ }
1050
+
1029
1051
// return the actual type of the expression, once the option is handled
1030
1052
fn (mut c Checker) check_expr_opt_call (expr ast.Expr, ret_type ast.Type) ast.Type {
1031
1053
if expr is ast.CallExpr {
@@ -1057,15 +1079,8 @@ fn (mut c Checker) check_expr_opt_call(expr ast.Expr, ret_type ast.Type) ast.Typ
1057
1079
}
1058
1080
}
1059
1081
return ret_type.clear_flag (.result)
1060
- } else if expr.or_block.kind == .block {
1061
- c.error ('unexpected `or` block, the function `${expr.name} ` does not return an Option or a Result' ,
1062
- expr.or_block.pos)
1063
- } else if expr.or_block.kind == .propagate_option {
1064
- c.error ('unexpected `?`, the function `${expr.name} ` does not return an Option' ,
1065
- expr.or_block.pos)
1066
- } else if expr.or_block.kind == .propagate_result {
1067
- c.error ('unexpected `!`, the function `${expr.name} ` does not return a Result' ,
1068
- expr.or_block.pos)
1082
+ } else {
1083
+ c.expr_or_block_err (expr.or_block.kind, expr.name, expr.or_block.pos, false )
1069
1084
}
1070
1085
} else if expr is ast.SelectorExpr && c.table.sym (ret_type).kind != .chan {
1071
1086
if expr.typ.has_flag (.option) || expr.typ.has_flag (.result) {
@@ -1089,14 +1104,9 @@ fn (mut c Checker) check_expr_opt_call(expr ast.Expr, ret_type ast.Type) ast.Typ
1089
1104
}
1090
1105
}
1091
1106
return ret_type.clear_flag (.result)
1092
- } else if expr.or_block.kind == .block {
1093
- c.error ('unexpected `or` block, the field `${expr.field_name} ` is neither an Option, nor a Result' ,
1094
- expr.or_block.pos)
1095
- } else if expr.or_block.kind == .propagate_option {
1096
- c.error ('unexpected `?`, the field `${expr.field_name} ` is not an Option' ,
1097
- expr.or_block.pos)
1098
- } else if expr.or_block.kind == .propagate_result {
1099
- c.error ('unexpected `!`, Result fields are not supported' , expr.or_block.pos)
1107
+ } else {
1108
+ c.expr_or_block_err (expr.or_block.kind, expr.field_name, expr.or_block.pos,
1109
+ true )
1100
1110
}
1101
1111
} else if expr is ast.IndexExpr {
1102
1112
if expr.or_expr.kind != .absent {
@@ -2511,16 +2521,8 @@ pub fn (mut c Checker) expr(node_ ast.Expr) ast.Type {
2511
2521
}
2512
2522
}
2513
2523
if ! ret_type.has_flag (.option) && ! ret_type.has_flag (.result) {
2514
- if node.or_block.kind == .block {
2515
- c.error ('unexpected `or` block, the function `${node.name} ` does not return an Option or a Result' ,
2516
- node.or_block.pos)
2517
- } else if node.or_block.kind == .propagate_option {
2518
- c.error ('unexpected `?`, the function `${node.name} ` does not return an Option or a Result' ,
2519
- node.or_block.pos)
2520
- } else if node.or_block.kind == .propagate_result {
2521
- c.error ('unexpected `!`, the function `${node.name} ` does not return an Option or a Result' ,
2522
- node.or_block.pos)
2523
- }
2524
+ c.expr_or_block_err (node.or_block.kind, node.name, node.or_block.pos,
2525
+ false )
2524
2526
}
2525
2527
if node.or_block.kind != .absent {
2526
2528
if ret_type.has_flag (.option) {
@@ -2710,18 +2712,9 @@ pub fn (mut c Checker) expr(node_ ast.Expr) ast.Type {
2710
2712
if c.table.sym (ret_type).kind == .chan {
2711
2713
return ret_type
2712
2714
}
2713
-
2714
2715
if ! ret_type.has_flag (.option) && ! ret_type.has_flag (.result) {
2715
- if node.or_block.kind == .block {
2716
- c.error ('unexpected `or` block, the field `${node.field_name} ` is neither an Option, nor a Result' ,
2717
- node.or_block.pos)
2718
- } else if node.or_block.kind == .propagate_option {
2719
- c.error ('unexpected `?`, the field `${node.field_name} ` is neither an Option, nor a Result' ,
2720
- node.or_block.pos)
2721
- } else if node.or_block.kind == .propagate_result {
2722
- c.error ('unexpected `!`, the field `${node.field_name} ` is neither an Option, nor a Result' ,
2723
- node.or_block.pos)
2724
- }
2716
+ c.expr_or_block_err (node.or_block.kind, node.field_name, node.or_block.pos,
2717
+ true )
2725
2718
}
2726
2719
if node.or_block.kind != .absent {
2727
2720
if ret_type.has_flag (.option) {
0 commit comments