checker: fix generic lambda with late concrete type inference (fix #22497) (#22509)

This commit is contained in:
Felipe Pena 2024-10-13 07:25:15 -03:00 committed by GitHub
parent bb99f8b57c
commit c4aaa2ef4d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 1 deletions

View File

@ -75,7 +75,7 @@ fn (mut c Checker) string_inter_lit(mut node ast.StringInterLiteral) ast.Type {
if fmt == `_` { // set default representation for type if none has been given
fmt = c.get_default_fmt(ftyp, typ)
if fmt == `_` {
if typ != ast.void_type {
if typ != ast.void_type && !(c.inside_lambda && typ.has_flag(.generic)) {
c.error('no known default format for type `${c.table.get_type_name(ftyp)}`',
node.fmt_poss[i])
}

View File

@ -287,6 +287,9 @@ fn (mut ct ComptimeInfo) comptime_get_kind_var(var ast.Ident) ?ast.ComptimeForKi
pub fn (mut ct ComptimeInfo) unwrap_generic_expr(expr ast.Expr, default_typ ast.Type) ast.Type {
match expr {
ast.StringLiteral, ast.StringInterLiteral {
return ast.string_type
}
ast.ParExpr {
return ct.unwrap_generic_expr(expr.expr, default_typ)
}

View File

@ -0,0 +1,21 @@
const result = ['0: a', '1: b', '2: c', '3: d']
fn mapi[T, U](arr []T, callback fn (int, T) U) []U {
mut mapped := []U{}
for i, el in arr {
mapped << callback(i, el)
}
return mapped
}
fn test_main() {
arr := [`a`, `b`, `c`, `d`]
arr_1 := mapi(arr, |i, e| '${i}: ${e}')
assert arr_1 == result
arr_2 := mapi[rune, string](arr, |i, e| '${i}: ${e}')
assert arr_2 == result
arr_3 := mapi(arr, fn (i int, e rune) string {
return '${i}: ${e}'
})
assert arr_3 == result
}