From ea9db0ebbe8d4e0cbdf9da4ee988439e669d68fe Mon Sep 17 00:00:00 2001 From: harupy Date: Sun, 15 Jan 2023 16:43:13 +0900 Subject: [PATCH] Add Option to Dict.keys field --- compiler/ast/asdl_rs.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/compiler/ast/asdl_rs.py b/compiler/ast/asdl_rs.py index ed5800f5d..a18ec651e 100755 --- a/compiler/ast/asdl_rs.py +++ b/compiler/ast/asdl_rs.py @@ -227,12 +227,12 @@ class StructVisitor(TypeInfoEmitVisitor): if cons.fields: self.emit(f"{cons.name} {{", depth) for f in cons.fields: - self.visit(f, parent, "", depth + 1) + self.visit(f, parent, "", depth + 1, cons.name) self.emit("},", depth) else: self.emit(f"{cons.name},", depth) - def visitField(self, field, parent, vis, depth): + def visitField(self, field, parent, vis, depth, constructor=None): typ = get_rust_type(field.type) fieldtype = self.typeinfo.get(field.type) if fieldtype and fieldtype.has_userdata: @@ -240,7 +240,11 @@ class StructVisitor(TypeInfoEmitVisitor): # don't box if we're doing Vec, but do box if we're doing Vec>> if fieldtype and fieldtype.boxed and (not (parent.product or field.seq) or field.opt): typ = f"Box<{typ}>" - if field.opt: + if field.opt or ( + # Add `Option` to allow `Dict.keys` to contain `None` for dictionary unpacking + # in a dict literal. + constructor == "Dict" and field.name == "keys" + ): typ = f"Option<{typ}>" if field.seq: typ = f"Vec<{typ}>"