From 53f920f524890c7599c6f4b74aad060650458fd0 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 4 Jan 2025 09:05:40 -0300 Subject: [PATCH 1/2] fix --- vlib/v/checker/assign.v | 2 + .../tests/enums/enum_assign_on_anon_fn_test.v | 43 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 vlib/v/tests/enums/enum_assign_on_anon_fn_test.v diff --git a/vlib/v/checker/assign.v b/vlib/v/checker/assign.v index bdb270be7c0951..c6af5541e6aca0 100644 --- a/vlib/v/checker/assign.v +++ b/vlib/v/checker/assign.v @@ -24,6 +24,7 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) { mut right_len := node.right.len mut right_first_type := ast.void_type old_recheck := c.inside_recheck + // check if we are rechecking an already checked expression on generic rechecking c.inside_recheck = old_recheck || node.right_types.len > 0 defer { @@ -75,6 +76,7 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) { c.error('cannot use `<-` on the right-hand side of an assignment, as it does not return any values', right.pos) } else if c.inside_recheck { + c.expected_type = node.right_types[i] mut right_type := c.expr(mut right) right_type_sym := c.table.sym(right_type) // fixed array returns an struct, but when assigning it must be the array type diff --git a/vlib/v/tests/enums/enum_assign_on_anon_fn_test.v b/vlib/v/tests/enums/enum_assign_on_anon_fn_test.v new file mode 100644 index 00000000000000..2ed040f11467a7 --- /dev/null +++ b/vlib/v/tests/enums/enum_assign_on_anon_fn_test.v @@ -0,0 +1,43 @@ +pub type FnGridObject = fn (mut object GridObject) + +@[flag] +pub enum GridObjectAuto { + drag + drop + scale_on_hover + pickup + sound + off // auto "deactivation" +} + +@[params] +pub struct GridObjectConfig { + auto GridObjectAuto = ~GridObjectAuto.zero() + on ?FnGridObject +} + +@[heap] +struct GridObject { +mut: + config GridObjectConfig + auto GridObjectAuto = ~GridObjectAuto.zero() +} + +pub fn on(config GridObjectConfig) &GridObject { + mut ob := &GridObject{} + ob.config = config + ob.auto = config.auto + if func := ob.config.on { + func(mut ob) + } + return ob +} + +fn test_main() { + _ := on( + on: fn (mut o GridObject) { + o.auto = .off | .pickup | .sound + } + ) + assert true +} From 3ff4c0f12325d4c63c0e76b174b302b886ce06f4 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 4 Jan 2025 09:17:16 -0300 Subject: [PATCH 2/2] fix --- vlib/v/checker/assign.v | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/vlib/v/checker/assign.v b/vlib/v/checker/assign.v index c6af5541e6aca0..68bc08c951a799 100644 --- a/vlib/v/checker/assign.v +++ b/vlib/v/checker/assign.v @@ -76,7 +76,9 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) { c.error('cannot use `<-` on the right-hand side of an assignment, as it does not return any values', right.pos) } else if c.inside_recheck { - c.expected_type = node.right_types[i] + if i < node.right_types.len { + c.expected_type = node.right_types[i] + } mut right_type := c.expr(mut right) right_type_sym := c.table.sym(right_type) // fixed array returns an struct, but when assigning it must be the array type