Skip to content

Commit

Permalink
[mypyc] Simplify code generated for SetAttr and non-pointer type (#12916
Browse files Browse the repository at this point in the history
)

Previously we sometimes generated an if statement with an empty body.
  • Loading branch information
JukkaL committed Jun 1, 2022
1 parent c205528 commit 1636a05
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 4 additions & 3 deletions mypyc/codegen/emitfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,13 @@ def visit_set_attr(self, op: SetAttr) -> None:
else:
# ...and struct access for normal attributes.
attr_expr = self.get_attr_expr(obj, op, decl_cl)
if not op.is_init:
if not op.is_init and attr_rtype.is_refcounted:
# This is not an initalization (where we know that the attribute was
# previously undefined), so decref the old value.
always_defined = cl.is_always_defined(op.attr)
if not always_defined:
self.emitter.emit_undefined_attr_check(attr_rtype, attr_expr, '!=')
if attr_rtype.is_refcounted:
self.emitter.emit_dec_ref(attr_expr, attr_rtype)
self.emitter.emit_dec_ref(attr_expr, attr_rtype)
if not always_defined:
self.emitter.emit_line('}')
# This steals the reference to src, so we don't need to increment the arg
Expand Down
7 changes: 7 additions & 0 deletions mypyc/test/test_emitfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,13 @@ def test_set_attr(self) -> None:
cpy_r_r0 = 1;
""")

def test_set_attr_non_refcounted(self) -> None:
self.assert_emit(
SetAttr(self.r, 'x', self.b, 1),
"""((mod___AObject *)cpy_r_r)->_x = cpy_r_b;
cpy_r_r0 = 1;
""")

def test_dict_get_item(self) -> None:
self.assert_emit(CallC(dict_get_item_op.c_function_name, [self.d, self.o2],
dict_get_item_op.return_type, dict_get_item_op.steals,
Expand Down

0 comments on commit 1636a05

Please sign in to comment.