cgen: fix generics multi_array in (#9885)

pull/9895/head
yuyi 2021-04-27 00:59:40 +08:00 committed by GitHub
parent de8c4866a4
commit 7ddf569de6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 7 deletions

View File

@ -453,9 +453,10 @@ fn (mut g Gen) gen_array_prepend(node ast.CallExpr) {
}
fn (mut g Gen) gen_array_contains_method(left_type ast.Type) string {
mut left_sym := g.table.get_type_symbol(left_type)
left_final_sym := g.table.get_final_type_symbol(left_type)
mut left_type_str := g.typ(left_type).replace('*', '')
unwrap_left_type := g.unwrap_generic(left_type)
mut left_sym := g.table.get_type_symbol(unwrap_left_type)
left_final_sym := g.table.get_final_type_symbol(unwrap_left_type)
mut left_type_str := g.typ(unwrap_left_type).replace('*', '')
fn_name := '${left_type_str}_contains'
if !left_sym.has_method('contains') {
left_info := left_final_sym.info as ast.Array
@ -494,7 +495,7 @@ fn (mut g Gen) gen_array_contains_method(left_type ast.Type) string {
left_sym.register_method(&ast.Fn{
name: 'contains'
params: [ast.Param{
typ: left_type
typ: unwrap_left_type
}, ast.Param{
typ: left_info.elem_type
}]
@ -517,8 +518,9 @@ fn (mut g Gen) gen_array_contains(node ast.CallExpr) {
}
fn (mut g Gen) gen_array_index_method(left_type ast.Type) string {
mut left_sym := g.table.get_type_symbol(left_type)
mut left_type_str := g.typ(left_type).trim('*')
unwrap_left_type := g.unwrap_generic(left_type)
mut left_sym := g.table.get_type_symbol(unwrap_left_type)
mut left_type_str := g.typ(unwrap_left_type).trim('*')
fn_name := '${left_type_str}_index'
if !left_sym.has_method('index') {
info := left_sym.info as ast.Array
@ -557,7 +559,7 @@ fn (mut g Gen) gen_array_index_method(left_type ast.Type) string {
left_sym.register_method(&ast.Fn{
name: 'index'
params: [ast.Param{
typ: left_type
typ: unwrap_left_type
}, ast.Param{
typ: info.elem_type
}]

View File

@ -0,0 +1,14 @@
fn include<T>(a T, b []T) bool {
return a in b
}
fn test_generics_multi_array_in() {
println(include(1, [1]))
assert include(1, [1])
println(include([1], [[1]]))
assert include([1], [[1]])
println(include([[1]], [[[1]]]))
assert include([[1]], [[[1]]])
}