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

master
yuyi 2022-05-09 12:32:51 +08:00 committed by GitHub
parent 106487d62f
commit 8519996201
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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
}
}
.sum_type, .interface_ {}
else {
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
}