Skip to content

Commit ca7bca8

Browse files
authored
type_resolver: clear option flag if present in comptime $for (fix #25761) (#25763)
1 parent 730f685 commit ca7bca8

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
struct Options {
2+
a ?string
3+
b ?int
4+
c ?f64
5+
}
6+
7+
fn unwrap_not_none_field_types[T](t T) []string {
8+
mut arr := []string{}
9+
$for f in T.fields {
10+
v := t.$(f.name)
11+
$if f is $option {
12+
if v != none {
13+
arr << typeof(v).name
14+
}
15+
}
16+
}
17+
return arr
18+
}
19+
20+
fn test_main() {
21+
arr := unwrap_not_none_field_types(Options{
22+
a: 'x'
23+
b: 1
24+
c: 2.3
25+
})
26+
assert arr.join(' ') == 'string int f64'
27+
}

vlib/v/type_resolver/comptime_resolver.v

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,21 @@ pub fn (t &ResolverInfo) is_comptime_variant_var(node ast.Ident) bool {
7171

7272
// typeof_type resolves type for typeof() expr where field.typ is resolved to real type instead of int type to make type(field.typ).name working
7373
pub fn (mut t TypeResolver) typeof_type(node ast.Expr, default_type ast.Type) ast.Type {
74+
if node is ast.Ident {
75+
if t.info.inside_comptime_for && t.info.comptime_for_field_var != '' {
76+
if node.obj is ast.Var {
77+
obj_typ := node.obj.typ
78+
field_typ := t.info.comptime_for_field_type
79+
if (obj_typ.has_flag(.option) && field_typ.has_flag(.option))
80+
|| (obj_typ.clear_flag(.option).idx() == field_typ.clear_flag(.option).idx()
81+
&& obj_typ.has_flag(.option)) {
82+
return field_typ.clear_flag(.option)
83+
}
84+
}
85+
}
86+
}
7487
if t.info.is_comptime(node) {
75-
return t.get_type(node)
88+
return t.get_type(node).clear_flag(.option)
7689
} else if node is ast.SelectorExpr && node.expr_type != 0 {
7790
if node.expr is ast.Ident && node.is_field_typ {
7891
return t.get_type_from_comptime_var(node.expr)

0 commit comments

Comments
 (0)