mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: fix error for match sumtype that referenced before (#14338)
This commit is contained in:
parent
106487d62f
commit
8519996201
@ -3656,6 +3656,7 @@ pub fn (mut c Checker) mark_as_referenced(mut node ast.Expr, as_interface bool)
|
|||||||
node.obj.is_auto_heap = true
|
node.obj.is_auto_heap = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.sum_type, .interface_ {}
|
||||||
else {
|
else {
|
||||||
node.obj.is_auto_heap = true
|
node.obj.is_auto_heap = true
|
||||||
}
|
}
|
||||||
|
42
vlib/v/tests/match_reference_sumtype_var_test.v
Normal file
42
vlib/v/tests/match_reference_sumtype_var_test.v
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
struct Player {
|
||||||
|
mut:
|
||||||
|
x int
|
||||||
|
y int
|
||||||
|
level int
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Enemy {
|
||||||
|
mut:
|
||||||
|
x int
|
||||||
|
y int
|
||||||
|
damage f64
|
||||||
|
}
|
||||||
|
|
||||||
|
type PlayerOrEnemy = Enemy | Player
|
||||||
|
|
||||||
|
fn test_match_reference_sumtype_var() {
|
||||||
|
mut entity := PlayerOrEnemy(Player{10, 12, 3})
|
||||||
|
|
||||||
|
x_move := 11
|
||||||
|
y_move := 22
|
||||||
|
|
||||||
|
mut ref := &entity
|
||||||
|
|
||||||
|
match mut entity {
|
||||||
|
Player {
|
||||||
|
entity.x += x_move
|
||||||
|
entity.y += y_move
|
||||||
|
|
||||||
|
println('Player is moved to $entity.x, $entity.y and its level is $entity.level')
|
||||||
|
}
|
||||||
|
Enemy {
|
||||||
|
entity.x += x_move
|
||||||
|
entity.y += y_move
|
||||||
|
|
||||||
|
println('Enemy is moved to $entity.x, $entity.y and its damage is $entity.damage')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println(typeof(ref).name)
|
||||||
|
assert true
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user