Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: fix infix expr in method of mut receiver variable (fix #20220) #20225

Merged
merged 1 commit into from
Dec 20, 2023

Conversation

yuyi98
Copy link
Member

@yuyi98 yuyi98 commented Dec 20, 2023

This PR fix infix expr in method of mut receiver variable (fix #20220).

  • Fix infix expr in method of mut receiver variable.
  • Add test.
@[heap]
struct UI {}

pub fn (ui &UI) draw_rect() {
	println('[]')
}

@[heap]
pub interface Node {
	id u64
	draw()
mut:
	ui &UI
	init(ui &UI) !
}

@[heap]
pub struct Item {
pub:
	id u64 = 1
mut:
	ui   &UI = unsafe { nil }
	body []&Node
}

pub fn (mut i Item) init(ui &UI) ! {
	assert i != unsafe { nil } // This assert generates a C gen error
	i.ui = ui
	for mut child in i.body {
		child.init(ui)!
	}
}

pub fn (i &Item) draw() {
	assert i != unsafe { nil }
	for child in i.body {
		child.draw()
	}
}

@[heap]
pub struct Rectangle {
	Item
pub mut:
	field f32
}

pub fn (r &Rectangle) draw() {
	assert r != unsafe { nil }
	r.ui.draw_rect()
	r.Item.draw()
}

fn main() {
	ui := &UI{}
	mut rect := &Rectangle{}

	rect.init(ui)!

	rect.draw()
	assert true
}

PS D:\Test\v\tt1> v run .
[]

Copy link
Member

@Delta456 Delta456 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 ecb1dc1 into vlang:master Dec 20, 2023
54 checks passed
@larpon
Copy link
Contributor

larpon commented Dec 20, 2023

Thanks @yuyi98 ! 🙏

@yuyi98 yuyi98 deleted the fix_auto_unref_var branch December 20, 2023 15:17
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.

Using assert on mutable receiver var that implements an interface and returns an Option results in CGen error
4 participants