cgen: fix generic option/result reference return (#21922)

This commit is contained in:
Felipe Pena 2024-07-24 12:56:49 -03:00 committed by GitHub
parent ec7ee482f4
commit eb63cda0a9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 2 deletions

View File

@ -1161,7 +1161,7 @@ fn (mut g Gen) option_type_name(t ast.Type) (string, string) {
} else {
styp = '${c.option_name}_${base}'
}
if t.is_ptr() {
if t.is_ptr() || t.has_flag(.generic) {
styp = styp.replace('*', '_ptr')
}
return styp, base
@ -1183,7 +1183,7 @@ fn (mut g Gen) result_type_name(t ast.Type) (string, string) {
} else {
styp = '${c.result_name}_${base}'
}
if t.is_ptr() {
if t.is_ptr() || t.has_flag(.generic) {
styp = styp.replace('*', '_ptr')
}
return styp, base

View File

@ -0,0 +1,30 @@
struct Stack[T] {
mut:
elements []T
}
pub fn (stack Stack[T]) is_empty() bool {
return stack.elements.len == 0
}
pub fn (stack Stack[T]) peek() ?T {
return if !stack.is_empty() { stack.elements.last() } else { none }
}
pub fn (stack Stack[T]) peek2() !T {
return if !stack.is_empty() { stack.elements.last() } else { error('Stack is empty') }
}
@[heap]
struct Element {
mut:
name string
value string
}
fn test_main() {
mut parent := &Element{
name: 'parent element'
}
mut stack := Stack[&Element]{}
}