Skip to content

Commit ea9db0e

Browse files
committed
Add Option to Dict.keys field
1 parent 82892f3 commit ea9db0e

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

compiler/ast/asdl_rs.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,20 +227,24 @@ def visitConstructor(self, cons, parent, depth):
227227
if cons.fields:
228228
self.emit(f"{cons.name} {{", depth)
229229
for f in cons.fields:
230-
self.visit(f, parent, "", depth + 1)
230+
self.visit(f, parent, "", depth + 1, cons.name)
231231
self.emit("},", depth)
232232
else:
233233
self.emit(f"{cons.name},", depth)
234234

235-
def visitField(self, field, parent, vis, depth):
235+
def visitField(self, field, parent, vis, depth, constructor=None):
236236
typ = get_rust_type(field.type)
237237
fieldtype = self.typeinfo.get(field.type)
238238
if fieldtype and fieldtype.has_userdata:
239239
typ = f"{typ}<U>"
240240
# don't box if we're doing Vec<T>, but do box if we're doing Vec<Option<Box<T>>>
241241
if fieldtype and fieldtype.boxed and (not (parent.product or field.seq) or field.opt):
242242
typ = f"Box<{typ}>"
243-
if field.opt:
243+
if field.opt or (
244+
# Add `Option` to allow `Dict.keys` to contain `None` for dictionary unpacking
245+
# in a dict literal.
246+
constructor == "Dict" and field.name == "keys"
247+
):
244248
typ = f"Option<{typ}>"
245249
if field.seq:
246250
typ = f"Vec<{typ}>"

0 commit comments

Comments
 (0)