cgen: fix assert checking fn option ret with none (#21726)

This commit is contained in:
Felipe Pena 2024-06-25 19:17:57 -03:00 committed by GitHub
parent a36c0d8315
commit fd384dff48
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 2 deletions

View File

@ -2045,8 +2045,10 @@ fn (mut g Gen) expr_with_tmp_var(expr ast.Expr, expr_typ ast.Type, ret_typ ast.T
}
} else {
simple_assign =
(expr is ast.SelectorExpr || (expr is ast.Ident && !expr.is_auto_heap()))
&& ret_typ.is_ptr() && expr_typ.is_ptr() && expr_typ.has_flag(.option)
((expr is ast.SelectorExpr || (expr is ast.Ident && !expr.is_auto_heap()))
&& ret_typ.is_ptr() && expr_typ.is_ptr() && expr_typ.has_flag(.option))
|| (expr_typ == ret_typ && !(expr_typ.has_option_or_result()
&& (expr_typ.is_ptr() || expr is ast.LambdaExpr)))
// option ptr assignment simplification
if simple_assign {
g.write('${tmp_var} = ')

View File

@ -22,4 +22,5 @@ fn (mut g Gen) gen_ctemp_var(tvar ast.CTempVar) {
g.write('${styp} ${tvar.name} = ')
g.expr(tvar.orig)
g.writeln(';')
g.set_current_pos_as_last_stmt_pos()
}

View File

@ -0,0 +1,8 @@
fn find_token_by_id() ?int {
return none
}
fn test_main() {
assert find_token_by_id() == none
println('done')
}