Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions vlib/v/checker/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,15 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) {

if field.has_default_expr {
c.expected_type = field.typ
default_expr_type := c.expr(field.default_expr)
if !field.typ.has_flag(.option) && !field.typ.has_flag(.result) {
c.check_expr_opt_call(field.default_expr, default_expr_type)
c.check_expr_opt_call(field.default_expr, field.default_expr_typ)
}
struct_sym.info.fields[i].default_expr_typ = default_expr_type
interface_implemented := sym.kind == .interface_
&& c.type_implements(default_expr_type, field.typ, field.pos)
c.check_expected(default_expr_type, field.typ) or {
&& c.type_implements(field.default_expr_typ, field.typ, field.pos)
c.check_expected(field.default_expr_typ, field.typ) or {
if sym.kind == .interface_ && interface_implemented {
if !c.inside_unsafe && !default_expr_type.is_real_pointer() {
if c.table.sym(default_expr_type).kind != .interface_ {
if !c.inside_unsafe && !field.default_expr_typ.is_real_pointer() {
if c.table.sym(field.default_expr_typ).kind != .interface_ {
c.mark_as_referenced(mut &node.fields[i].default_expr,
true)
}
Expand Down
37 changes: 37 additions & 0 deletions vlib/v/tests/embed_struct_field_default_value_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
struct Papa {
fam_name string
}

pub struct Child {
Papa
pub mut:
activity Activity = Fun.roll
age u8 = 2
}

type Activity = Fun | Other

pub enum Fun {
run
roll
jump
}

pub struct Other {}

// Same struct without embedding just works.
pub struct Human {
fam_name string
pub mut:
activity Activity = Fun.roll
age u8 = 2
}

fn test_embed_struct_field_default_value() {
c := Child{}
println(c.activity)
assert c.activity == Activity(Fun.roll)
h := Human{}
println(h.activity)
assert h.activity == Activity(Fun.roll)
}