Skip to content

Commit 24e0a6c

Browse files
committed
checker: fix vls test compilation
1 parent 0d9ac1f commit 24e0a6c

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

vlib/v/checker/fn.v

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,15 @@ pub fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
15521552
c.table.register_fn_concrete_types(method.fkey(), concrete_types)
15531553
}
15541554

1555+
// resolve return generics struct to concrete type
1556+
if method.generic_names.len > 0 && method.return_type.has_flag(.generic)
1557+
&& !isnil(c.table.cur_fn) && c.table.cur_fn.generic_names.len == 0 {
1558+
node.return_type = c.table.unwrap_generic_type(method.return_type, method.generic_names,
1559+
concrete_types)
1560+
} else {
1561+
node.return_type = method.return_type
1562+
}
1563+
15551564
if node.concrete_types.len > 0 && node.concrete_types.all(!it.has_flag(.generic))
15561565
&& method.return_type.has_flag(.generic) && method.generic_names.len > 0
15571566
&& method.generic_names.len == node.concrete_types.len {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import json
2+
3+
pub struct NotificationMessage<T> {
4+
pub:
5+
method string
6+
params T
7+
}
8+
9+
struct Abc {}
10+
11+
pub fn (x &Abc) notification_at<T>() ?NotificationMessage<T> {
12+
return json.decode(NotificationMessage<T>, '{}')
13+
}
14+
15+
pub fn (x &Abc) generic_method<T>(method_name string) ?NotificationMessage<T> {
16+
return x.notification_at<T>()
17+
}
18+
19+
struct Res {}
20+
21+
pub fn (mut x Abc) diagnostics() ?Res {
22+
got := x.generic_method<Res>('xyz')?
23+
return got.params
24+
}
25+
26+
fn test_generic_method_returning_optional() ? {
27+
mut a := Abc{}
28+
a.diagnostics()?
29+
assert true
30+
}

0 commit comments

Comments
 (0)