vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.75k stars 2.16k forks source link

compiler panic for `|i, e| '${i}: ${e}'` lambda expr used as callback in a generic function #22497

Open spytheman opened 1 day ago

spytheman commented 1 day ago

V doctor:

V full version: V 0.4.8 51b471b.0cf3a44
OS: linux, Ubuntu 20.04.6 LTS
Processor: 2 cpus, 64bit, little endian, Intel(R) Core(TM) i3-3225 CPU @ 3.30GHz

getwd: /space/v/oo
vexe: /space/v/oo/v
vexe mtime: 2024-10-12 05:47:32

vroot: OK, value: /space/v/oo
VMODULES: OK, value: /home/delian/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.46.0
Git vroot status: weekly.2024.41-15-g0cf3a445
.git/config present: true

CC version: cc (Ubuntu 10.5.0-1ubuntu1~20.04) 10.5.0
thirdparty/tcc status: thirdparty-linux-amd64 0134e9b9

What did you do? ./v -g -o vdbg cmd/v && ./vdbg bug.v

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 main() {
    arr := [`a`, `b`, `c`, `d`]
    mapped_arr := mapi(arr, |i, e| '${i}: ${e}') //panics
    dump(mapped_arr)
}

// mapi[rune,string](arr, |i, e| '${i}: ${e}')  // works
// mapi(arr, fn (i int, e rune) string { return '${i}: ${e}' }) // works

What did you expect to see?

a compiled program

What did you see instead?

================ V panic ================
   module: builtin
 function: get()
  message: array.get: index out of range (i == 1, a.len == 1)
     file: /space/v/oo/vlib/builtin/array.v:454
   v hash: 0cf3a44
=========================================
/tmp/v_1000/../../../../../../space/v/oo/vlib/builtin/builtin.c.v:87: at panic_debug: Backtrace
/tmp/v_1000/../../../../../../space/v/oo/vlib/builtin/array.v:454: by array_get
/tmp/v_1000/../../../../../../space/v/oo/vlib/v/checker/check_types.v:1099: by v__checker__Checker_infer_fn_generic_types
/tmp/v_1000/../../../../../../space/v/oo/vlib/v/checker/fn.v:1602: by v__checker__Checker_fn_call
/tmp/v_1000/../../../../../../space/v/oo/vlib/v/checker/fn.v:652: by v__checker__Checker_call_expr
/tmp/v_1000/../../../../../../space/v/oo/vlib/v/checker/checker.v:2854: by v__checker__Checker_expr
/tmp/v_1000/../../../../../../space/v/oo/vlib/v/checker/assign.v:34: by v__checker__Checker_assign_stmt
/tmp/v_1000/../../../../../../space/v/oo/vlib/v/checker/checker.v:2108: by v__checker__Checker_stmt
/tmp/v_1000/../../../../../../space/v/oo/vlib/v/checker/checker.v:2715: by v__checker__Checker_stmts_ending_with_expression
/tmp/v_1000/../../../../../../space/v/oo/vlib/v/checker/checker.v:2683: by v__checker__Checker_stmts
/tmp/v_1000/../../../../../../space/v/oo/vlib/v/checker/fn.v:496: by v__checker__Checker_fn_decl
/tmp/v_1000/../../../../../../space/v/oo/vlib/v/checker/checker.v:2195: by v__checker__Checker_stmt
/tmp/v_1000/../../../../../../space/v/oo/vlib/v/checker/checker.v:267: by v__checker__Checker_check
/tmp/v_1000/../../../../../../space/v/oo/vlib/v/checker/checker.v:329: by v__checker__Checker_check_files
/tmp/v_1000/../../../../../../space/v/oo/vlib/v/builder/builder.v:131: by v__builder__Builder_middle_stages
/tmp/v_1000/../../../../../../space/v/oo/vlib/v/builder/builder.v:158: by v__builder__Builder_front_and_middle_stages
/tmp/v_1000/../../../../../../space/v/oo/vlib/v/builder/cbuilder/cbuilder.v:71: by v__builder__cbuilder__gen_c
/tmp/v_1000/../../../../../../space/v/oo/vlib/v/builder/cbuilder/cbuilder.v:59: by v__builder__cbuilder__build_c
/tmp/v_1000/../../../../../../space/v/oo/vlib/v/builder/cbuilder/cbuilder.v:49: by v__builder__cbuilder__compile_c
/tmp/v_1000/../../../../../../space/v/oo/vlib/v/builder/rebuilding.v:324: by v__builder__Builder_rebuild
/tmp/v_1000/../../../../../../space/v/oo/vlib/v/builder/compile.v:17: by v__builder__compile
/tmp/v_1000/../../../../../../space/v/oo/cmd/v/v.v:187: by main__rebuild
/tmp/v_1000/../../../../../../space/v/oo/cmd/v/v.v:145: by main__main
/tmp/v_1000/../../../../../../tmp/v_1000/vdbg.01J9ZM9EF63XZSKVN55J4MHB16.tmp.c:59228: by main

[!NOTE] You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote. Other reactions and those to comments will not be taken into account.

spytheman commented 1 day ago

This bug was discovered by @heyimtcn and reported in the V Discord https://discord.com/channels/592103645835821068/700746775962714232/1294224298965340191 . I am only filing it here.