Skip to content

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

Merged
spytheman merged 1 commit intovlang:masterfrom
yuyi98:fix_struct_init_default
Mar 26, 2023
Merged

checker: fix embedded struct field with default value (fix #17758)#17777
spytheman merged 1 commit intovlang:masterfrom
yuyi98:fix_struct_init_default

Conversation

@yuyi98
Copy link
Copy Markdown
Member

@yuyi98 yuyi98 commented Mar 26, 2023

This PR fix embedded struct field with default value (fix #17758).

  • Fix embedded struct field with default value.
  • Add test.
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 main() {
	c := Child{}
	println(c.activity)
	assert c.activity == Activity(Fun.roll)
	h := Human{}
	println(h.activity)
	assert h.activity == Activity(Fun.roll)
}

PS D:\Test\v\tt1> v run .
Activity(roll)
Activity(roll)

Copy link
Copy Markdown
Contributor

@spytheman spytheman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work.

@spytheman spytheman merged commit 130f35c into vlang:master Mar 26, 2023
@yuyi98 yuyi98 deleted the fix_struct_init_default branch March 26, 2023 11:45
l1mey112 pushed a commit to l1mey112/v that referenced this pull request Jun 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Embedded structs break default sum type values if they are followed by other fields with default values

2 participants