checker: fix generic option array arg passing to []T (fix #24423) (#24457)

This commit is contained in:
Felipe Pena 2025-05-12 07:00:03 -03:00 committed by GitHub
parent 2dd7de41c1
commit e93c344b56
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 0 deletions

View File

@ -1852,6 +1852,12 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
c.handle_generic_lambda_arg(node, mut call_arg.expr)
continue
}
// passing []?T to []T
if !unwrap_typ.has_flag(.variadic) && unwrap_sym.kind == .array
&& c.table.final_sym(utyp).kind == .array
&& c.check_basic(c.table.value_type(utyp).clear_flag(.option), c.table.value_type(unwrap_typ)) {
continue
}
c.error('${err.msg()} in argument ${i + 1} to `${fn_name}`', call_arg.pos)
}
}
@ -2607,6 +2613,12 @@ fn (mut c Checker) method_call(mut node ast.CallExpr, mut continue_check &bool)
continue
}
}
// passing []?T to []T
if !exp_arg_typ.has_flag(.variadic) && param_typ_sym.kind == .array
&& c.table.final_sym(got_arg_typ).kind == .array
&& c.check_basic(c.table.value_type(got_arg_typ).clear_flag(.option), c.table.value_type(exp_arg_typ)) {
continue
}
c.error('${err.msg()} in argument ${i + 1} to `${left_sym.name}.${method_name}`',
arg.pos)
}

View File

@ -0,0 +1,31 @@
struct Decoder {}
pub fn decode[T](val string) !T {
mut decoder := Decoder{}
mut result := T{}
decoder.decode_value(mut result)!
return result
}
fn (mut decoder Decoder) decode_value[T](mut val T) ! {
$if T.unaliased_typ is $array {
// checking wrongly. `decode_array` think that `[]?int` is `[]int`
decoder.decode_array(mut val)!
return
} $else $if T.unaliased_typ is $struct {
$for field in T.fields {
decoder.decode_value(mut val.$(field.name))!
}
}
}
fn (mut decoder Decoder) decode_array[T](mut val []T) ! {}
struct Foo {
int []int
oint []?int
}
fn test_main() {
decode[Foo]('')!
}