1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: minor optionals fix (#5714)

This commit is contained in:
yuyi 2020-07-07 20:21:02 +08:00 committed by GitHub
parent 68e01d87be
commit dc24327b1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 8 deletions

View File

@ -2494,8 +2494,7 @@ fn (mut g Gen) return_statement(node ast.Return) {
if fn_return_is_optional {
tmp := g.new_tmp_var()
styp := g.typ(g.fn_decl.return_type)
g.writeln('$styp $tmp;')
g.writeln('${tmp}.ok = true;')
g.writeln('$styp $tmp = {.ok = true};')
g.writeln('return $tmp;')
} else {
g.writeln('return;')
@ -2508,7 +2507,7 @@ fn (mut g Gen) return_statement(node ast.Return) {
mut is_regular_option := g.typ(node.types[0]) == 'Option'
if optional_none || is_regular_option {
tmp := g.new_tmp_var()
g.write('/*opt promotion*/ Option $tmp = ')
g.write('Option $tmp = ')
g.expr_with_cast(node.exprs[0], node.types[0], g.fn_decl.return_type)
g.writeln(';')
styp := g.typ(g.fn_decl.return_type)
@ -2527,7 +2526,7 @@ fn (mut g Gen) return_statement(node ast.Return) {
opt_type = g.typ(g.fn_decl.return_type)
// Create a tmp for this option
opt_tmp = g.new_tmp_var()
g.write('$opt_type $opt_tmp;')
g.writeln('$opt_type $opt_tmp;')
styp = g.base_type(g.fn_decl.return_type)
g.write('opt_ok2(&($styp/*X*/[]) { ')
} else {
@ -2596,8 +2595,8 @@ fn (mut g Gen) return_statement(node ast.Return) {
opt_type := g.typ(g.fn_decl.return_type)
// Create a tmp for this option
opt_tmp := g.new_tmp_var()
g.write('$opt_type $opt_tmp;')
g.write('/*:)$return_sym.name*/opt_ok2(&($styp[]) { ')
g.writeln('$opt_type $opt_tmp;')
g.write('opt_ok2(&($styp[]) { ')
if !g.fn_decl.return_type.is_ptr() && node.types[0].is_ptr() {
// Automatic Dereference for optional
g.write('*')

View File

@ -249,7 +249,8 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
g.inside_call = false
}
gen_or := node.or_block.kind != .absent
cur_line := if gen_or && g.is_assign_rhs {
is_gen_or_and_assign_rhs := gen_or && g.is_assign_rhs
cur_line := if is_gen_or_and_assign_rhs {
line := g.go_before_stmt(0)
g.out.write(tabs[g.indent])
line
@ -268,7 +269,9 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
}
if gen_or {
g.or_block(tmp_opt, node.or_block, node.return_type)
g.write('\n\t$cur_line$tmp_opt')
if is_gen_or_and_assign_rhs {
g.write('\n$cur_line$tmp_opt')
}
}
}