checker: fix generic fn with generic fn call returning generic map (fix #20106) (#20150)

This commit is contained in:
yuyi 2023-12-12 17:41:58 +08:00 committed by GitHub
parent d2fdbaf67e
commit 5d99138cb2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 5 deletions

View file

@ -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

View file

@ -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)
{

View file

@ -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'}"
}

View file

@ -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'
}