mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
parent
8f0242e6af
commit
514a9a7906
3 changed files with 20 additions and 1 deletions
|
@ -6718,6 +6718,12 @@ fn (mut g Gen) gen_or_block_stmts(cvar_name string, cast_typ string, stmts []ast
|
|||
if expr_stmt.expr.is_return_used {
|
||||
g.write('*(${cast_typ}*) ${cvar_name}.data = ')
|
||||
}
|
||||
} else if g.inside_opt_or_res && return_is_option && g.inside_assign {
|
||||
g.write('_option_ok(&(${cast_typ}[]) { ')
|
||||
g.expr_with_cast(expr_stmt.expr, expr_stmt.typ, return_type.clear_option_and_result())
|
||||
g.writeln(' }, (${option_name}*)&${cvar_name}, sizeof(${cast_typ}));')
|
||||
g.indent--
|
||||
return
|
||||
} else {
|
||||
g.write('*(${cast_typ}*) ${cvar_name}.data = ')
|
||||
}
|
||||
|
|
|
@ -342,7 +342,11 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) {
|
|||
g.or_block(tmp_opt, node.or_expr, elem_type)
|
||||
}
|
||||
if !g.is_amp {
|
||||
g.write('\n${cur_line}(*(${elem_type_str}*)${tmp_opt}.data)')
|
||||
if g.inside_opt_or_res && elem_type.has_flag(.option) && g.inside_assign {
|
||||
g.write('\n${cur_line}(*(${elem_type_str}*)&${tmp_opt})')
|
||||
} else {
|
||||
g.write('\n${cur_line}(*(${elem_type_str}*)${tmp_opt}.data)')
|
||||
}
|
||||
} else {
|
||||
g.write('\n${cur_line}*${tmp_opt_ptr}')
|
||||
}
|
||||
|
|
9
vlib/v/tests/assign/assign_option_of_array_index_test.v
Normal file
9
vlib/v/tests/assign/assign_option_of_array_index_test.v
Normal file
|
@ -0,0 +1,9 @@
|
|||
fn make_option() ?string {
|
||||
return none
|
||||
}
|
||||
|
||||
fn test_assign_option_of_array_index() {
|
||||
arr := [make_option()]
|
||||
unwrapped := arr[99] or { 'unknown' } // <- out of bounds access!
|
||||
assert '${unwrapped}' == "Option('unknown')"
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue