Remove unnecessary boxing of ASDL product children

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
This commit is contained in:
Anders Kaseorg 2022-12-11 15:39:47 -08:00
parent 6e89b3ab1a
commit f4672e4256
5 changed files with 15 additions and 17 deletions

View file

@ -89,6 +89,7 @@ class TypeInfo:
self.has_userdata = None
self.children = set()
self.boxed = False
self.product = False
def __repr__(self):
return f"<TypeInfo: {self.name}>"
@ -145,6 +146,7 @@ class FindUserdataTypesVisitor(asdl.VisitorBase):
info.has_userdata = True
if len(product.fields) > 2:
info.boxed = True
info.product = True
self.add_children(name, product.fields)
def add_children(self, name, fields):
@ -236,7 +238,7 @@ class StructVisitor(TypeInfoEmitVisitor):
if fieldtype and fieldtype.has_userdata:
typ = f"{typ}<U>"
# don't box if we're doing Vec<T>, but do box if we're doing Vec<Option<Box<T>>>
if fieldtype and fieldtype.boxed and (not field.seq or field.opt):
if fieldtype and fieldtype.boxed and (not (parent.product or field.seq) or field.opt):
typ = f"Box<{typ}>"
if field.opt:
typ = f"Option<{typ}>"