From a46cf10e9222b1faa8de3c9ab3b66b690ec853e9 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 28 May 2022 16:50:37 +0800 Subject: [PATCH] checker: fix declare assign literal with closure (#14544) --- vlib/v/checker/assign.v | 2 +- .../tests/assign_literal_with_closure_test.v | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/assign_literal_with_closure_test.v diff --git a/vlib/v/checker/assign.v b/vlib/v/checker/assign.v index 21739730b6..9e0df64dc7 100644 --- a/vlib/v/checker/assign.v +++ b/vlib/v/checker/assign.v @@ -305,7 +305,7 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) { } } } - left_type_unwrapped := c.unwrap_generic(left_type) + left_type_unwrapped := c.unwrap_generic(ast.mktyp(left_type)) right_type_unwrapped := c.unwrap_generic(right_type) if right_type_unwrapped == 0 { // right type was a generic `T` diff --git a/vlib/v/tests/assign_literal_with_closure_test.v b/vlib/v/tests/assign_literal_with_closure_test.v new file mode 100644 index 0000000000..1fc5f209b5 --- /dev/null +++ b/vlib/v/tests/assign_literal_with_closure_test.v @@ -0,0 +1,26 @@ +fn sma(period int) fn (f64) f64 { + mut i := 0 + mut sum := 0.0 + mut storage := []f64{len: 0, cap: period} + + return fn [mut storage, mut sum, mut i, period] (input f64) f64 { + if storage.len < period { + sum += input + storage << input + } + + sum += input - storage[i] + storage[i], i = input, (i + 1) % period + return sum / f64(storage.len) + } +} + +fn test_assign_literal_with_closure() { + sma3 := sma(3) + sma5 := sma(5) + println('x sma3 sma5') + for x in [f64(1), 2, 3, 4, 5, 5, 4, 3, 2, 1] { + println('${x:5.3f} ${sma3(x):5.3f} ${sma5(x):5.3f}') + } + assert true +}