Skip to content

Commit

Permalink
checker: fix generic fn with generic fn call returning generic map (fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Dec 12, 2023
1 parent d2fdbaf commit 5d99138
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/graphs/bellman-ford.v
Expand Up @@ -53,7 +53,7 @@ fn print_sol(dist []int) {
// to all other vertices using Bellman-Ford algorithm. The
// function also detects negative weight cycle
fn bellman_ford[T](graph [][]T, src int) {
mut edges := build_map_edges_from_graph(graph)
mut edges := build_map_edges_from_graph[int](graph)
// this function was done to adapt a graph representation
// by a adjacency matrix, to list of adjacency (using a MAP)
n_edges := edges.len // number of EDGES
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/checker/fn.v
Expand Up @@ -1471,7 +1471,7 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
}

if func.generic_names.len > 0 {
if has_generic {
if has_generic || node.concrete_types.any(it.has_flag(.generic)) {
if typ := c.table.resolve_generic_to_concrete(func.return_type, func.generic_names,
node.concrete_types)
{
Expand Down
@@ -0,0 +1,28 @@
fn generic1[K, V](a map[K]V) string {
t := expect_map[K, V](a)
dump(t)
return '${t}'
}

fn generic2[K, V](a map[K]V) string {
t := expect_map(a)
dump(t)
return '${t}'
}

fn expect_map[K, V](a map[K]V) map[K]V {
return a
}

fn test_generics_with_generics_fn_return_map_type() {
a := {
'a': 1
}
b := {
1: 'a'
}
assert generic1(a) == "{'a': 1}"
assert generic1(b) == "{1: 'a'}"
assert generic2(a) == "{'a': 1}"
assert generic2(b) == "{1: 'a'}"
}
7 changes: 4 additions & 3 deletions vlib/v/tests/multiple_generic_resolve_test.v
Expand Up @@ -9,16 +9,17 @@ fn pre_start[T, R](app T, config R) string {
return start(app, config.val)
}

fn start[T, R](app T, otherthing R) R {
fn start[T, R](app T, otherthing R) string {
println(otherthing)
return otherthing
return '${otherthing}'
}

fn test_main() {
fn test_multiple_generic_resolve() {
app := App{}
testval := 'hello'
config := Config[string]{
val: testval
}

assert pre_start(app, config) == 'hello'
}

0 comments on commit 5d99138

Please sign in to comment.