Skip to content

Commit

Permalink
checker: unwrap generics for map and array methods (fix compiler pani…
Browse files Browse the repository at this point in the history
…c, described in #20058) (#20059)
  • Loading branch information
Delta456 authored Dec 4, 2023
1 parent 1c10d2f commit cce21a4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
18 changes: 9 additions & 9 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -1732,10 +1732,10 @@ fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
} else if (left_sym.kind == .map || final_left_sym.kind == .map)
&& method_name in ['clone', 'keys', 'values', 'move', 'delete'] {
if left_sym.kind == .map {
return c.map_builtin_method_call(mut node, left_type, c.table.sym(left_type))
return c.map_builtin_method_call(mut node, unwrapped_left_type, c.table.sym(unwrapped_left_type))
} else if left_sym.info is ast.Alias {
parent_type := left_sym.info.parent_type
return c.map_builtin_method_call(mut node, parent_type, c.table.final_sym(left_type))
parent_type := c.unwrap_generic(left_sym.info.parent_type)
return c.map_builtin_method_call(mut node, parent_type, c.table.final_sym(unwrapped_left_type))
}
} else if left_sym.kind == .array && method_name in ['insert', 'prepend'] {
if method_name == 'insert' {
Expand Down Expand Up @@ -1767,7 +1767,7 @@ fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
}
} else if final_left_sym.kind == .array
&& method_name in ['filter', 'map', 'sort', 'sorted', 'contains', 'any', 'all', 'first', 'last', 'pop'] {
return c.array_builtin_method_call(mut node, left_type, final_left_sym)
return c.array_builtin_method_call(mut node, unwrapped_left_type, final_left_sym)
} else if c.pref.backend.is_js() && left_sym.name.starts_with('Promise[')
&& method_name == 'wait' {
info := left_sym.info as ast.Struct
Expand Down Expand Up @@ -2621,9 +2621,9 @@ fn (mut c Checker) map_builtin_method_call(mut node ast.CallExpr, left_type ast.
c.fail_if_immutable(mut node.left)
}
if node.left.is_auto_deref_var() || left_type.has_flag(.shared_f) {
ret_type = left_type.deref()
ret_type = node.left_type.deref()
} else {
ret_type = left_type
ret_type = node.left_type
}
ret_type = ret_type.clear_flag(.shared_f)
}
Expand Down Expand Up @@ -2658,7 +2658,7 @@ fn (mut c Checker) map_builtin_method_call(mut node ast.CallExpr, left_type ast.
}
else {}
}
node.receiver_type = left_type.ref()
node.receiver_type = node.left_type.ref()
node.return_type = ret_type
return node.return_type
}
Expand Down Expand Up @@ -2877,9 +2877,9 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as
node.return_type = array_info.elem_type
if method_name == 'pop' {
c.fail_if_immutable(mut node.left)
node.receiver_type = left_type.ref()
node.receiver_type = node.left_type.ref()
} else {
node.receiver_type = left_type
node.receiver_type = node.left_type
}
} else if method_name == 'delete' {
c.fail_if_immutable(mut node.left)
Expand Down
12 changes: 12 additions & 0 deletions vlib/v/tests/generic_fn_call_map_keys_values_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
fn generic[T](a T) {
assert a.keys() == ['aa', 'bb']
assert a.values() == [11, 22]
}

fn test_generic_fn_call_map_keys_values() {
a := {
'aa': 11
'bb': 22
}
generic(a)
}

0 comments on commit cce21a4

Please sign in to comment.