mirror of
https://github.com/vlang/v.git
synced 2025-09-15 10:27:19 -04:00
cgen: fix generic option/result reference return (#21922)
This commit is contained in:
parent
ec7ee482f4
commit
eb63cda0a9
@ -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
|
||||
|
30
vlib/v/tests/option_ret_ptr_generic_test.v
Normal file
30
vlib/v/tests/option_ret_ptr_generic_test.v
Normal 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]{}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user