checker: fix generics fn infer nested generic fn (#12519)

pull/12546/head
yuyi 2021-11-20 02:31:55 +08:00 committed by GitHub
parent 2794aa623a
commit eec8788333
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 1 deletions

View File

@ -614,7 +614,8 @@ pub fn (mut c Checker) infer_fn_generic_types(func ast.Fn, mut node ast.CallExpr
if node.args.len <= arg_i {
break
}
arg := node.args[arg_i]
mut arg := node.args[arg_i]
arg.typ = c.unwrap_generic(arg.typ)
param_type_sym := c.table.get_type_symbol(param.typ)
if param.typ.has_flag(.generic) && param_type_sym.name == gt_name {

View File

@ -0,0 +1,49 @@
import json
import os
fn test_generics_fn_infer_nested_generic_fn() {
mut spawns := []ItemSpawn{}
load_item_spawns(mut spawns)
println(spawns)
assert spawns.len == 0
}
fn load_item_spawns(mut spawns []ItemSpawn) {
walk<ItemSpawn>('./data/items/spawns/', mut spawns)
}
fn parse_json<T>(file string, mut array []T) {
data := os.read_file(file) or {
panic('error reading file $file')
return
}
decoded_data := json.decode([]T, data) or {
eprintln('Failed to parse item spawns')
return
}
for d in decoded_data {
array << d
}
}
fn walk<T>(path string, mut array []T) {
if !os.is_dir(path) {
return
}
mut files := os.ls(path) or { return }
for file in files {
p := path + os.path_separator + file
if os.is_dir(p) && !os.is_link(p) {
walk(p, mut array)
} else if os.exists(p) {
parse_json(p, mut array)
}
}
}
struct ItemSpawn {
id int
amount int = 1
x int
y int
}