Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker: fix assign expected type on rechecking enum assigns #23367

Merged
merged 3 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading