Skip to content

Commit 495adc9

Browse files
authored
checker: fix assign expected type on rechecking enum assigns (fix #23366) (#23367)
1 parent 5e95b07 commit 495adc9

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

vlib/v/checker/assign.v

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
2424
mut right_len := node.right.len
2525
mut right_first_type := ast.void_type
2626
old_recheck := c.inside_recheck
27+
2728
// check if we are rechecking an already checked expression on generic rechecking
2829
c.inside_recheck = old_recheck || node.right_types.len > 0
2930
defer {
@@ -75,6 +76,9 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
7576
c.error('cannot use `<-` on the right-hand side of an assignment, as it does not return any values',
7677
right.pos)
7778
} else if c.inside_recheck {
79+
if i < node.right_types.len {
80+
c.expected_type = node.right_types[i]
81+
}
7882
mut right_type := c.expr(mut right)
7983
right_type_sym := c.table.sym(right_type)
8084
// fixed array returns an struct, but when assigning it must be the array type
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
pub type FnGridObject = fn (mut object GridObject)
2+
3+
@[flag]
4+
pub enum GridObjectAuto {
5+
drag
6+
drop
7+
scale_on_hover
8+
pickup
9+
sound
10+
off // auto "deactivation"
11+
}
12+
13+
@[params]
14+
pub struct GridObjectConfig {
15+
auto GridObjectAuto = ~GridObjectAuto.zero()
16+
on ?FnGridObject
17+
}
18+
19+
@[heap]
20+
struct GridObject {
21+
mut:
22+
config GridObjectConfig
23+
auto GridObjectAuto = ~GridObjectAuto.zero()
24+
}
25+
26+
pub fn on(config GridObjectConfig) &GridObject {
27+
mut ob := &GridObject{}
28+
ob.config = config
29+
ob.auto = config.auto
30+
if func := ob.config.on {
31+
func(mut ob)
32+
}
33+
return ob
34+
}
35+
36+
fn test_main() {
37+
_ := on(
38+
on: fn (mut o GridObject) {
39+
o.auto = .off | .pickup | .sound
40+
}
41+
)
42+
assert true
43+
}

0 commit comments

Comments
 (0)