Skip to content

Commit ae5bf7e

Browse files
authored
checker: unwrap for base type if ft.typ and inferred_typ are both opt (fix #25517) (#25728)
1 parent d9158e7 commit ae5bf7e

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

vlib/v/checker/check_types.v

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,12 @@ fn (mut c Checker) infer_struct_generic_types(typ ast.Type, node ast.StructInit)
845845
if field_sym.name == gt_name {
846846
for t in node.init_fields {
847847
if ft.name == t.name && t.typ != 0 {
848-
concrete_types << ast.mktyp(t.typ)
848+
// unwrap to get base type for T if both are options
849+
mut inferred_typ := ast.mktyp(t.typ)
850+
if ft.typ.has_flag(.option) && inferred_typ.has_flag(.option) {
851+
inferred_typ = inferred_typ.clear_flag(.option)
852+
}
853+
concrete_types << inferred_typ
849854
continue gname
850855
}
851856
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
struct Response[T] {
2+
code int
3+
msg string
4+
data ?T
5+
}
6+
7+
struct UserInfo {
8+
name string
9+
age int
10+
}
11+
12+
fn g[T](x T) string {
13+
return 'abc'
14+
}
15+
16+
fn response_to_string[T](response Response[T]) string {
17+
return g(response)
18+
}
19+
20+
fn test_main() {
21+
res := response_to_string(Response{1, 'success', ?UserInfo{'Jay Chou', 46}})
22+
assert res == 'abc'
23+
}

0 commit comments

Comments
 (0)