def _infer_for(node, context, solver):
"""Infer the type for a for loop node
Limitation:
- The iterable can't be a tuple.
For example: the following is not allowed:
for x in (1, 2.0, "string"):
....
"""
iter_type = expr.infer(node.iter, context, solver)
# Infer the target in the loop, inside the global context
# Cases:
# - Var name. Ex: for i in range(5)..
# - Tuple. Ex: for (a,b) in [(1,"st"), (3,"st2")]..
# - List. Ex: for [a,b] in [(1, "st"), (3, "st2")]..
target_type = solver.new_z3_const("for_target")
solver.add(axioms.for_loop(iter_type, target_type, solver.z3_types),
fail_message="For loop in line {}".format(node.lineno))
_infer_assignment_target(node.target, context, target_type, solver)
return _infer_control_flow(node, context, solver)
评论列表
文章目录