Skip to content

Commit

Permalink
checker: fix assign expected type on rechecking enum assigns (fix #23366
Browse files Browse the repository at this point in the history
) (#23367)
  • Loading branch information
felipensp authored Jan 4, 2025
1 parent 5e95b07 commit 495adc9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions vlib/v/checker/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions vlib/v/tests/enums/enum_assign_on_anon_fn_test.v
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 495adc9

Please sign in to comment.