checker: fix error for match sumtype that referenced before (#14338)

yuyi 2022-05-09 12:32:51 +08:00 committed by Jef Roosens
parent 3d4dde4af1
commit 10474ced5f
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 43 additions and 0 deletions

View File

@ -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
} }

View 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
}