cgen: fix c error when use reserved keyword as if guard var (#12871)

pull/12873/head
zakuro 2021-12-17 11:50:52 +09:00 committed by GitHub
parent f87f8ec3f7
commit 2f7ac7e407
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 2 deletions

View File

@ -5332,10 +5332,11 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
is_auto_heap = v.is_auto_heap
}
}
left_var_name := c_name(branch.cond.var_name)
if is_auto_heap {
g.writeln('\t$base_type* $branch.cond.var_name = HEAP($base_type, *($base_type*)${var_name}.data);')
g.writeln('\t$base_type* $left_var_name = HEAP($base_type, *($base_type*)${var_name}.data);')
} else {
g.writeln('\t$base_type $branch.cond.var_name = *($base_type*)${var_name}.data;')
g.writeln('\t$base_type $left_var_name = *($base_type*)${var_name}.data;')
}
}
}

View File

@ -0,0 +1,11 @@
fn array_n_opt(n int) ?[]int {
return if n >= 0 { []int{len: n} } else { none }
}
fn test_reserved_keyword_of_if_guard() {
if array := array_n_opt(2) {
assert array.len == 2
} else {
assert false
}
}