Skip to content

Commit

Permalink
Fix refcounted definition
Browse files Browse the repository at this point in the history
  • Loading branch information
jairov4 committed Nov 23, 2024
1 parent 73ddcbe commit 587cfa7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
30 changes: 21 additions & 9 deletions mypyc/ir/rtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ class RType:
#
# TODO: This shouldn't be specific to C or a string
c_undefined: str
# If unboxed: does the unboxed version use reference counting?
is_refcounted = True

@property
def is_refcounted(self) -> bool:
# If unboxed: does the unboxed version use reference counting?
return True

# C type; use Emitter.ctype() to access
_ctype: str
# If True, error/undefined value overlaps with a valid value. To
Expand Down Expand Up @@ -204,7 +208,7 @@ def __init__(

self.name = name
self.is_unboxed = is_unboxed
self.is_refcounted = is_refcounted
self._is_refcounted = is_refcounted
self.is_native_int = is_native_int
self.is_signed = is_signed
self._ctype = ctype
Expand Down Expand Up @@ -233,6 +237,10 @@ def __init__(
else:
assert False, "Unrecognized ctype: %r" % ctype

@property
def is_refcounted(self) -> bool:
return self._is_refcounted

def accept(self, visitor: RTypeVisitor[T]) -> T:
return visitor.visit_rprimitive(self)

Expand Down Expand Up @@ -822,12 +830,17 @@ class RInstance(RType):

is_unboxed = False

def __init__(self, class_ir: ClassIR) -> None:
def __init__(self, class_ir: ClassIR, is_refcounted: bool = True) -> None:
# name is used for formatting the name in messages and debug output
# so we want the fullname for precision.
self.name = class_ir.fullname
self.class_ir = class_ir
self._ctype = "PyObject *"
self._is_refcounted = is_refcounted

@property
def is_refcounted(self) -> bool:
return self._is_refcounted

def accept(self, visitor: RTypeVisitor[T]) -> T:
return visitor.visit_rinstance(self)
Expand Down Expand Up @@ -885,10 +898,6 @@ def accept(self, visitor: RTypeVisitor[T]) -> T:
def is_refcounted(self) -> bool:
return any(t.is_refcounted for t in self.class_ir.all_attributes().values())

@is_refcounted.setter
def is_refcounted(self, value: bool) -> None:
raise NotImplementedError("is_refcounted is read-only for RInstanceValue")

def __repr__(self) -> str:
return "<RInstanceValue %s>" % self.name

Expand Down Expand Up @@ -1005,7 +1014,10 @@ def __init__(self, item_type: RType, length: int) -> None:
self.item_type = item_type
# Number of items
self.length = length
self.is_refcounted = False

@property
def is_refcounted(self) -> bool:
return False

def accept(self, visitor: RTypeVisitor[T]) -> T:
return visitor.visit_rarray(self)
Expand Down
3 changes: 1 addition & 2 deletions mypyc/transform/value_type_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ def patch_value_type_init_methods(ir: FuncIR, options: CompilerOptions) -> None:
return

# patch the type of the self parameter to be a reference to the value type
ref_type = RInstance(cl)
# the refcounted flag is set to False because we only need to initialize the
# attributes of the value type, but it is not expected to be refcounted
ref_type.is_refcounted = False
ref_type = RInstance(cl, is_refcounted=False)
ir.args[0].type = ref_type
ir.arg_regs[0].type = ref_type

0 comments on commit 587cfa7

Please sign in to comment.