Skip to content

Commit 5d99138

Browse files
authored
checker: fix generic fn with generic fn call returning generic map (fix #20106) (#20150)
1 parent d2fdbaf commit 5d99138

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

examples/graphs/bellman-ford.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn print_sol(dist []int) {
5353
// to all other vertices using Bellman-Ford algorithm. The
5454
// function also detects negative weight cycle
5555
fn bellman_ford[T](graph [][]T, src int) {
56-
mut edges := build_map_edges_from_graph(graph)
56+
mut edges := build_map_edges_from_graph[int](graph)
5757
// this function was done to adapt a graph representation
5858
// by a adjacency matrix, to list of adjacency (using a MAP)
5959
n_edges := edges.len // number of EDGES

vlib/v/checker/fn.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1471,7 +1471,7 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
14711471
}
14721472

14731473
if func.generic_names.len > 0 {
1474-
if has_generic {
1474+
if has_generic || node.concrete_types.any(it.has_flag(.generic)) {
14751475
if typ := c.table.resolve_generic_to_concrete(func.return_type, func.generic_names,
14761476
node.concrete_types)
14771477
{
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
fn generic1[K, V](a map[K]V) string {
2+
t := expect_map[K, V](a)
3+
dump(t)
4+
return '${t}'
5+
}
6+
7+
fn generic2[K, V](a map[K]V) string {
8+
t := expect_map(a)
9+
dump(t)
10+
return '${t}'
11+
}
12+
13+
fn expect_map[K, V](a map[K]V) map[K]V {
14+
return a
15+
}
16+
17+
fn test_generics_with_generics_fn_return_map_type() {
18+
a := {
19+
'a': 1
20+
}
21+
b := {
22+
1: 'a'
23+
}
24+
assert generic1(a) == "{'a': 1}"
25+
assert generic1(b) == "{1: 'a'}"
26+
assert generic2(a) == "{'a': 1}"
27+
assert generic2(b) == "{1: 'a'}"
28+
}

vlib/v/tests/multiple_generic_resolve_test.v

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@ fn pre_start[T, R](app T, config R) string {
99
return start(app, config.val)
1010
}
1111

12-
fn start[T, R](app T, otherthing R) R {
12+
fn start[T, R](app T, otherthing R) string {
1313
println(otherthing)
14-
return otherthing
14+
return '${otherthing}'
1515
}
1616

17-
fn test_main() {
17+
fn test_multiple_generic_resolve() {
1818
app := App{}
1919
testval := 'hello'
2020
config := Config[string]{
2121
val: testval
2222
}
23+
2324
assert pre_start(app, config) == 'hello'
2425
}

0 commit comments

Comments
 (0)