mirror of
https://github.com/vlang/v.git
synced 2025-09-12 08:57:09 -04:00
cgen: fix option ptr unwrapping (#21415)
This commit is contained in:
parent
5667437f39
commit
f8f7c99e2d
@ -4901,7 +4901,7 @@ fn (mut g Gen) ident(node ast.Ident) {
|
|||||||
g.write(g.get_ternary_name(name))
|
g.write(g.get_ternary_name(name))
|
||||||
if is_auto_heap {
|
if is_auto_heap {
|
||||||
g.write('))')
|
g.write('))')
|
||||||
if is_option {
|
if is_option && node.or_expr.kind != .absent {
|
||||||
g.write('.data')
|
g.write('.data')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -6992,7 +6992,7 @@ fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type ast.Ty
|
|||||||
if g.fn_decl.return_type == ast.void_type {
|
if g.fn_decl.return_type == ast.void_type {
|
||||||
g.writeln('\treturn;')
|
g.writeln('\treturn;')
|
||||||
} else {
|
} else {
|
||||||
styp := g.typ(g.fn_decl.return_type)
|
styp := g.typ(g.fn_decl.return_type).replace('*', '_ptr')
|
||||||
err_obj := g.new_tmp_var()
|
err_obj := g.new_tmp_var()
|
||||||
g.writeln('\t${styp} ${err_obj};')
|
g.writeln('\t${styp} ${err_obj};')
|
||||||
g.writeln('\tmemcpy(&${err_obj}, &${cvar_name}, sizeof(_option));')
|
g.writeln('\tmemcpy(&${err_obj}, &${cvar_name}, sizeof(_option));')
|
||||||
|
89
vlib/v/tests/option_ptr_unwrap_test.v
Normal file
89
vlib/v/tests/option_ptr_unwrap_test.v
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
import time
|
||||||
|
// 41s.
|
||||||
|
|
||||||
|
struct Node[T] {
|
||||||
|
mut:
|
||||||
|
data T
|
||||||
|
prev ?&Node[T]
|
||||||
|
next ?&Node[T]
|
||||||
|
}
|
||||||
|
|
||||||
|
struct LinkedList[T] {
|
||||||
|
mut:
|
||||||
|
size usize
|
||||||
|
head ?&Node[T]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_linked_list[T]() &LinkedList[T] {
|
||||||
|
return &LinkedList[T]{}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (mut li LinkedList[T]) add[T](data T) {
|
||||||
|
mut node := &Node[T]{data, none, none}
|
||||||
|
|
||||||
|
if li.head == none {
|
||||||
|
li.head = node
|
||||||
|
node.next = node
|
||||||
|
node.prev = node
|
||||||
|
} else {
|
||||||
|
node.next = li.head
|
||||||
|
node.prev = li.head?.prev
|
||||||
|
node.prev?.next = node
|
||||||
|
li.head?.prev = node
|
||||||
|
}
|
||||||
|
|
||||||
|
li.size += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (mut li LinkedList[T]) pop[T]() ?T {
|
||||||
|
if li.head == none {
|
||||||
|
return none
|
||||||
|
}
|
||||||
|
if li.size == 1 {
|
||||||
|
data := li.head?.data
|
||||||
|
li.head?.next = none
|
||||||
|
li.head?.prev = none
|
||||||
|
li.head = none
|
||||||
|
li.size -= 1
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
mut tail := li.head?.prev?
|
||||||
|
mut curr := tail.prev?
|
||||||
|
curr.next = li.head
|
||||||
|
li.head?.prev = curr
|
||||||
|
|
||||||
|
tail.next = none
|
||||||
|
tail.prev = none
|
||||||
|
li.size -= 1
|
||||||
|
|
||||||
|
return tail.data
|
||||||
|
}
|
||||||
|
|
||||||
|
@[heap]
|
||||||
|
struct Integer {
|
||||||
|
value int
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_main() {
|
||||||
|
max_itr := 2
|
||||||
|
t := time.now()
|
||||||
|
for itr in 0 .. max_itr {
|
||||||
|
mut list := new_linked_list[&Integer]()
|
||||||
|
println('Itr#${itr} list size: ${list.size}')
|
||||||
|
|
||||||
|
list.add(&Integer{10})
|
||||||
|
println('Itr#${itr} list size: ${list.size}')
|
||||||
|
list.add(&Integer{20})
|
||||||
|
println('Itr#${itr} list size: ${list.size}')
|
||||||
|
|
||||||
|
mut n := list.pop()
|
||||||
|
println('Itr#${itr} list size: ${list.size}, data: ${n?}')
|
||||||
|
n = list.pop()
|
||||||
|
println('Itr#${itr} list size: ${list.size}, data: ${n?}')
|
||||||
|
n = list.pop()
|
||||||
|
println('Itr#${itr} list size: ${list.size}, data: ${n}')
|
||||||
|
}
|
||||||
|
d := time.since(t)
|
||||||
|
println('Bye(time ${d})!')
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user