Skip to content

Commit 74d55ef

Browse files
authored
ast,cgen: fallback to field type when default_expr_typ is 0 (fix #25891) (#25903)
1 parent fa2383a commit 74d55ef

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

vlib/v/ast/table.v

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,6 +2219,21 @@ pub fn (mut t Table) unwrap_generic_type_ex(typ Type, generic_names []string, co
22192219
}
22202220
}
22212221
}
2222+
if fields[i].has_default_expr {
2223+
if fields[i].default_expr_typ.has_flag(.generic) {
2224+
if t_typ := t.convert_generic_type(fields[i].default_expr_typ,
2225+
t_generic_names, t_concrete_types)
2226+
{
2227+
fields[i].default_expr_typ = t_typ
2228+
}
2229+
} else if fields[i].default_expr_typ == 0
2230+
|| fields[i].default_expr_typ == nil_type {
2231+
if fields[i].default_expr.is_nil()
2232+
&& fields[i].typ.is_any_kind_of_pointer() {
2233+
fields[i].default_expr_typ = fields[i].typ
2234+
}
2235+
}
2236+
}
22222237
}
22232238
// update concrete types
22242239
for i in 0 .. ts.info.generic_types.len {

vlib/v/gen/c/cgen.v

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7689,8 +7689,15 @@ fn (mut g Gen) type_default_impl(typ_ ast.Type, decode_sumtype bool) string {
76897689
if field.has_default_expr {
76907690
mut expr_str := ''
76917691
if field_sym.kind in [.sum_type, .interface] {
7692+
default_expr_typ := if field.default_expr_typ == 0
7693+
&& field.default_expr.is_nil()
7694+
&& field.typ.is_any_kind_of_pointer() {
7695+
field.typ
7696+
} else {
7697+
field.default_expr_typ
7698+
}
76927699
expr_str = g.expr_string_with_cast(field.default_expr,
7693-
field.default_expr_typ, field.typ)
7700+
default_expr_typ, field.typ)
76947701
} else if field_sym.is_array_fixed() && g.inside_global_decl {
76957702
array_info := field_sym.array_fixed_info()
76967703
match field.default_expr {
@@ -7739,7 +7746,7 @@ fn (mut g Gen) type_default_impl(typ_ ast.Type, decode_sumtype bool) string {
77397746
zero_str := if field_sym.language == .v && field_sym.info is ast.Struct
77407747
&& field_sym.info.is_empty_struct() {
77417748
'{E_STRUCT}'
7742-
} else if field_sym.kind == .sum_type {
7749+
} else if field_sym.kind == .sum_type && !field.typ.is_ptr() {
77437750
if decode_sumtype {
77447751
g.type_default_sumtype(field.typ, field_sym)
77457752
} else {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@[has_globals]
2+
module main
3+
4+
__global queue2 Queue[Event]
5+
6+
type Event = int | u32
7+
8+
struct Queue[T] {
9+
mut:
10+
data &T
11+
}
12+
13+
fn test_main() {
14+
assert queue2.data == unsafe { nil }
15+
}

0 commit comments

Comments
 (0)