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
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
4 changes: 2 additions & 2 deletions vlib/v/gen/c/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ fn (mut g Gen) gen_plain_infix_expr(node ast.InfixExpr) {
typ_str := g.typ(node.promoted_type)
g.write('(${typ_str})(')
}
if node.left_type.is_ptr() && node.left.is_auto_deref_var() {
if node.left_type.is_ptr() && node.left.is_auto_deref_var() && !node.right_type.is_pointer() {
g.write('*')
} else if !g.inside_interface_deref && node.left is ast.Ident
&& g.table.is_interface_var(node.left.obj) {
Expand All @@ -1065,7 +1065,7 @@ fn (mut g Gen) gen_plain_infix_expr(node ast.InfixExpr) {
}
g.expr(node.left)
g.write(' ${node.op.str()} ')
if node.right_type.is_ptr() && node.right.is_auto_deref_var() {
if node.right_type.is_ptr() && node.right.is_auto_deref_var() && !node.left_type.is_pointer() {
g.write('*')
g.expr(node.right)
} else {
Expand Down
62 changes: 62 additions & 0 deletions vlib/v/tests/infix_expr_in_mut_receiver_method_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
@[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 test_infix_expr_in_mut_receiver_method() {
ui := &UI{}
mut rect := &Rectangle{}

rect.init(ui)!

rect.draw()
assert true
}
Loading