Skip to content

Commit

Permalink
cgen: fix generic option/result reference return (vlang#21922)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Jul 24, 2024
1 parent ec7ee48 commit eb63cda
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
30 changes: 30 additions & 0 deletions vlib/v/tests/option_ret_ptr_generic_test.v
Original file line number Diff line number Diff line change
@@ -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]{}
}

0 comments on commit eb63cda

Please sign in to comment.