From 495adc9cee03aa711b96f1b41b32073670375b91 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 4 Jan 2025 12:36:00 -0300 Subject: [PATCH] checker: fix assign expected type on rechecking enum assigns (fix #23366) (#23367) --- vlib/v/checker/assign.v | 4 ++ .../tests/enums/enum_assign_on_anon_fn_test.v | 43 +++++++++++++++++++ 2 files changed, 47 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..68bc08c951a799 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,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 { + 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 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 +}