cgen: fix option ptr unwrapping (#21415)

This commit is contained in:
Felipe Pena 2024-05-06 05:02:37 -03:00 committed by GitHub
parent 5667437f39
commit f8f7c99e2d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 91 additions and 2 deletions

View File

@ -4901,7 +4901,7 @@ fn (mut g Gen) ident(node ast.Ident) {
g.write(g.get_ternary_name(name))
if is_auto_heap {
g.write('))')
if is_option {
if is_option && node.or_expr.kind != .absent {
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 {
g.writeln('\treturn;')
} 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()
g.writeln('\t${styp} ${err_obj};')
g.writeln('\tmemcpy(&${err_obj}, &${cvar_name}, sizeof(_option));')

View 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})!')
}