diff --git a/TESTS.md b/TESTS.md index b0a4120cab..f1ce44234a 100644 --- a/TESTS.md +++ b/TESTS.md @@ -80,8 +80,14 @@ Run `vlib` module tests, *including* the compiler tests. ## `v vlib/v/compiler_errors_test.v` This runs tests for: -* `checker/tests/*.vv` -* `parser/tests/*.vv` +* `vlib/v/checker/tests/*.vv` +* `vlib/v/parser/tests/*.vv` + +### Special folders that compiler_errors_test.v will try to +run/compile with specific options: + +vlib/v/checker/tests/globals_run/ - `-enable-globals run`; +results stored in `.run.out` files, matching the .vv ones. ## `v test-all` diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 8340ddbadf..94ae2ccd0c 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2413,6 +2413,7 @@ pub fn (mut c Checker) fn_call(mut call_expr ast.CallExpr) ast.Type { sym := c.table.get_type_symbol(obj.typ) if sym.kind == .function { found = true + func = (sym.info as ast.FnType).func } } } diff --git a/vlib/v/checker/tests/globals_run/function_stored_in_global.run.out b/vlib/v/checker/tests/globals_run/function_stored_in_global.run.out new file mode 100644 index 0000000000..586eac76aa --- /dev/null +++ b/vlib/v/checker/tests/globals_run/function_stored_in_global.run.out @@ -0,0 +1,4 @@ +[vlib/v/checker/tests/globals_run/function_stored_in_global.vv:14] voidptr(main.abc) == voidptr(cpu_get_id): true +[vlib/v/checker/tests/globals_run/function_stored_in_global.vv:15] main.cpu_get_id(): 123 +[vlib/v/checker/tests/globals_run/function_stored_in_global.vv:16] abc(): 123 +[vlib/v/checker/tests/globals_run/function_stored_in_global.vv:17] abc() == main.cpu_get_id(): true diff --git a/vlib/v/checker/tests/globals_run/function_stored_in_global.vv b/vlib/v/checker/tests/globals_run/function_stored_in_global.vv new file mode 100644 index 0000000000..e74825ec64 --- /dev/null +++ b/vlib/v/checker/tests/globals_run/function_stored_in_global.vv @@ -0,0 +1,20 @@ +__global ( + cpu_get_id fn () u64 +) +fn current() u64 { + return cpu_get_id() +} + +fn abc() u64 { + return 123 +} + +fn main() { + cpu_get_id = abc + dump(voidptr(abc) == voidptr(cpu_get_id)) + dump(cpu_get_id()) + dump(abc()) + dump(abc() == cpu_get_id()) + assert abc() == cpu_get_id() + assert voidptr(abc) == voidptr(cpu_get_id) +} diff --git a/vlib/v/checker/tests/globals_run/global_array_indexed_by_global_fn.run.out b/vlib/v/checker/tests/globals_run/global_array_indexed_by_global_fn.run.out new file mode 100644 index 0000000000..7968dd12ab --- /dev/null +++ b/vlib/v/checker/tests/globals_run/global_array_indexed_by_global_fn.run.out @@ -0,0 +1,3 @@ +[vlib/v/checker/tests/globals_run/global_array_indexed_by_global_fn.vv:30] cpu_locals.map(it.x): [123, 456] +[vlib/v/checker/tests/globals_run/global_array_indexed_by_global_fn.vv:33] x.x: 123 +[vlib/v/checker/tests/globals_run/global_array_indexed_by_global_fn.vv:36] y.x: 456 diff --git a/vlib/v/checker/tests/globals_run/global_array_indexed_by_global_fn.vv b/vlib/v/checker/tests/globals_run/global_array_indexed_by_global_fn.vv new file mode 100644 index 0000000000..272df4c7b8 --- /dev/null +++ b/vlib/v/checker/tests/globals_run/global_array_indexed_by_global_fn.vv @@ -0,0 +1,40 @@ +struct Local { + x int +} + +__global ( + cpu_locals []&Local +) + +__global ( + cpu_get_id fn () u64 + cpu_set_id fn (u64) +) + +fn abc0() u64 { + return 0 +} + +fn abc1() u64 { + return 1 +} + +pub fn current() &Local { + return cpu_locals[cpu_get_id()] +} + +fn main() { + cpu_locals = []&Local{} + cpu_locals << &Local{123} + cpu_locals << &Local{456} + dump(cpu_locals.map(it.x)) + cpu_get_id = abc0 + x := current() + dump(x.x) + cpu_get_id = abc1 + y := current() + dump(y.x) + assert x != y + assert x.x == 123 + assert y.x == 456 +} diff --git a/vlib/v/compiler_errors_test.v b/vlib/v/compiler_errors_test.v index e3e1b4156e..706a411bfa 100644 --- a/vlib/v/compiler_errors_test.v +++ b/vlib/v/compiler_errors_test.v @@ -57,6 +57,7 @@ fn test_all() { scanner_dir := 'vlib/v/scanner/tests' module_dir := '$checker_dir/modules' global_dir := '$checker_dir/globals' + global_run_dir := '$checker_dir/globals_run' run_dir := '$checker_dir/run' skip_unused_dir := 'vlib/v/tests/skip_unused' // @@ -64,6 +65,7 @@ fn test_all() { parser_tests := get_tests_in_dir(parser_dir, false) scanner_tests := get_tests_in_dir(scanner_dir, false) global_tests := get_tests_in_dir(global_dir, false) + global_run_tests := get_tests_in_dir(global_run_dir, false) module_tests := get_tests_in_dir(module_dir, true) run_tests := get_tests_in_dir(run_dir, false) skip_unused_dir_tests := get_tests_in_dir(skip_unused_dir, false) @@ -77,6 +79,8 @@ fn test_all() { tasks.add('', scanner_dir, '-prod', '.out', scanner_tests, false) tasks.add('', checker_dir, '-enable-globals run', '.run.out', ['globals_error.vv'], false) + tasks.add('', global_run_dir, '-enable-globals run', '.run.out', global_run_tests, + false) tasks.add('', global_dir, '-enable-globals', '.out', global_tests, false) tasks.add('', module_dir, '-prod run', '.out', module_tests, true) tasks.add('', run_dir, 'run', '.run.out', run_tests, false)