@@ -1050,15 +1050,15 @@ fn (mut c Checker) check_map_and_filter(is_map bool, elem_typ table.Type, call_e
1050
1050
match arg_expr {
1051
1051
ast.AnonFn {
1052
1052
if arg_expr.decl.params.len > 1 {
1053
- c.error ('function needs exactly 1 argument' , call_expr .pos)
1053
+ c.error ('function needs exactly 1 argument' , arg_expr.decl .pos)
1054
1054
} else if is_map &&
1055
1055
(arg_expr.decl.return_type != elem_typ || arg_expr.decl.params[0 ].typ != elem_typ) {
1056
1056
c.error ('type mismatch, should use `fn(a $elem_sym.source_name ) $elem_sym.source_name {...}`' ,
1057
- call_expr .pos)
1057
+ arg_expr.decl .pos)
1058
1058
} else if ! is_map &&
1059
1059
(arg_expr.decl.return_type != table.bool_type || arg_expr.decl.params[0 ].typ != elem_typ) {
1060
1060
c.error ('type mismatch, should use `fn(a $elem_sym.source_name ) bool {...}`' ,
1061
- call_expr .pos)
1061
+ arg_expr.decl .pos)
1062
1062
}
1063
1063
}
1064
1064
ast.Ident {
@@ -1071,11 +1071,11 @@ fn (mut c Checker) check_map_and_filter(is_map bool, elem_typ table.Type, call_e
1071
1071
c.error ('function needs exactly 1 argument' , call_expr.pos)
1072
1072
} else if is_map && (func.return_type != elem_typ || func.params[0 ].typ != elem_typ) {
1073
1073
c.error ('type mismatch, should use `fn(a $elem_sym.source_name ) $elem_sym.source_name {...}`' ,
1074
- call_expr .pos)
1074
+ arg_expr .pos)
1075
1075
} else if ! is_map &&
1076
1076
(func.return_type != table.bool_type || func.params[0 ].typ != elem_typ) {
1077
1077
c.error ('type mismatch, should use `fn(a $elem_sym.source_name ) bool {...}`' ,
1078
- call_expr .pos)
1078
+ arg_expr .pos)
1079
1079
}
1080
1080
}
1081
1081
}
@@ -1110,13 +1110,15 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type {
1110
1110
is_sort := method_name == 'sort'
1111
1111
if is_filter_map || is_sort {
1112
1112
array_info := left_type_sym.info as table.Array
1113
- mut scope := c.file.scope.innermost (call_expr.pos.pos)
1113
+ args_pos := call_expr.pos.pos + call_expr.name.len
1114
+ mut scope := c.file.scope.innermost (args_pos)
1114
1115
if is_filter_map {
1115
- scope.update_var_type ('it' , array_info.elem_type)
1116
+ // position of `it` doesn't matter
1117
+ scope_register_it (mut scope, call_expr.pos, array_info.elem_type)
1116
1118
} else if is_sort {
1117
1119
c.fail_if_immutable (call_expr.left)
1118
- scope. update_var_type ( 'a' , array_info.elem_type)
1119
- scope. update_var_type ( 'b' , array_info.elem_type)
1120
+ // position of `a` and `b` doesn't matter, they're the same
1121
+ scope_register_ab ( mut scope, call_expr.pos , array_info.elem_type)
1120
1122
// Verify `.sort(a < b)`
1121
1123
if call_expr.args.len > 0 {
1122
1124
if call_expr.args[0 ].expr ! is ast.InfixExpr {
@@ -2251,11 +2253,28 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
2251
2253
}
2252
2254
}
2253
2255
2254
- fn (mut c Checker) open_scope (mut parent ast.Scope, start_pos int ) & ast.Scope {
2255
- mut s := ast.new_scope (parent, start_pos)
2256
- s.end_pos = parent.end_pos
2257
- parent.children << s
2258
- return s
2256
+ fn scope_register_it (mut s ast.Scope, pos token.Position, typ table.Type) {
2257
+ s.register ('it' , ast.Var{
2258
+ name: 'it'
2259
+ pos: pos
2260
+ typ: typ
2261
+ is_used: true
2262
+ })
2263
+ }
2264
+
2265
+ fn scope_register_ab (mut s ast.Scope, pos token.Position, typ table.Type) {
2266
+ s.register ('a' , ast.Var{
2267
+ name: 'a'
2268
+ pos: pos
2269
+ typ: typ
2270
+ is_used: true
2271
+ })
2272
+ s.register ('b' , ast.Var{
2273
+ name: 'b'
2274
+ pos: pos
2275
+ typ: typ
2276
+ is_used: true
2277
+ })
2259
2278
}
2260
2279
2261
2280
fn (mut c Checker) check_array_init_para_type (para string , expr ast.Expr, pos token.Position) {
0 commit comments