1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix embedded struct field with default value (#17777)

This commit is contained in:
yuyi 2023-03-26 16:33:01 +08:00 committed by GitHub
parent 34f5f05efa
commit 130f35c776
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 7 deletions

View File

@ -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)
}

View File

@ -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)
}