# Buggy code
def parfor_defs(parfor, use_set=None, def_set=None):
    """list variables written in this parfor by recursively
    calling compute_use_defs() on body and combining block defs.
    """
    if use_set is None:
        use_set = set()
    if def_set is None:
        def_set = set()
    blocks = wrap_parfor_blocks(parfor)
    uses, defs = compute_use_defs(blocks)
    cfg = compute_cfg_from_blocks(blocks)
    last_label = max(blocks.keys())
    unwrap_parfor_blocks(parfor)

    # Conservatively, only add defs for blocks that are definitely executed
    # Go through blocks in order, as if they are statements of the block that
    # includes the parfor, and update uses/defs.

    # no need for topo order of ir_utils
    topo_order = cfg.topo_order()
    # blocks that dominate last block are definitely executed
    definitely_executed = cfg.dominators()[last_label]
    # except loop bodies that might not execute
    for loop in cfg.loops().values():
        definitely_executed -= loop.body
    for label in topo_order:
        if label in definitely_executed:
            # see compute_use_defs() in analysis.py
            # variables defined in the block that includes the parfor are not
            # uses of that block (are not potentially live in the beginning of
            # the block)
            use_set.update(uses[label] - def_set)
            def_set.update(defs[label])
        else:
            use_set.update(uses[label] - def_set)

    # treat loop variables and size variables as use
    loop_vars = {l.start.name for l in parfor.loop_nests if isinstance(l.start, ir.Var)}
    loop_vars |= {l.stop.name for l in parfor.loop_nests if isinstance(l.stop, ir.Var)}
    loop_vars |= {l.step.name for l in parfor.loop_nests if isinstance(l.step, ir.Var)}
    use_set.update(loop_vars)
    use_set |= get_parfor_pattern_vars(parfor)

    return analysis._use_defs_result(usemap=use_set, defmap=def_set)


# Traceback (AssertionError)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
./numba/core/errors.py in new_error_context(fmt_, *args, **kwargs)
800     try:
--> 801         yield
802     except NumbaError as e:

./numba/core/lowering.py in lower_block(self, block)
266                                    loc=self.loc, errcls_=defaulterrcls):
--> 267                 self.lower_inst(inst)
268         self.post_block(block)

./numba/core/lowering.py in lower_inst(self, inst)
479                 if isinstance(inst, _class):
--> 480                     func(self, inst)
481                     return

./numba/parfors/parfor_lowering.py in _lower_parfor_parallel(lowerer, parfor)
239             lowerer, parfor, typemap, typingctx, targetctx, flags, {},
--> 240             bool(alias_map), index_var_typ, parfor.races)
241     finally:

./numba/parfors/parfor_lowering.py in _create_gufunc_for_parfor_body(lowerer, parfor, typemap, typingctx, targetctx, flags, locals, has_aliases, index_var_typ, races)
933         warnings.warn(NumbaParallelSafetyWarning(msg, loc))
--> 934     replace_var_with_array(races, loop_body, typemap, lowerer.fndesc.calltypes)
935

./numba/parfors/parfor_lowering.py in replace_var_with_array(vars, loop_body, typemap, calltypes)
1363 def replace_var_with_array(vars, loop_body, typemap, calltypes):
-> 1364     replace_var_with_array_internal(vars, loop_body, typemap, calltypes)
1365     for v in vars:

./numba/parfors/parfor_lowering.py in replace_var_with_array_internal(vars, loop_body, typemap, calltypes)
1360     for label, block in loop_body.items():
-> 1361         block.body = replace_var_with_array_in_block(vars, block, typemap, calltypes)
1362

./numba/parfors/parfor_lowering.py in replace_var_with_array_in_block(vars, block, typemap, calltypes)
1346
-> 1347             setitem_node = ir.SetItem(inst.target, const_var, inst.value, inst.loc)
1348             calltypes[setitem_node] = signature(

./numba/core/ir.py in __init__(self, target, index, value, loc)
590         assert isinstance(index, Var)
--> 591         assert isinstance(value, Var)
592         assert isinstance(loc, Loc)

AssertionError:

# Fixed code
def parfor_defs(parfor, use_set=None, def_set=None):
    """list variables written in this parfor by recursively
    calling compute_use_defs() on body and combining block defs.
    """
    if use_set is None:
        use_set = set()
    if def_set is None:
        def_set = set()
    blocks = wrap_parfor_blocks(parfor)
    uses, defs = compute_use_defs(blocks)
    cfg = compute_cfg_from_blocks(blocks)
    last_label = max(blocks.keys())
    unwrap_parfor_blocks(parfor)

    # Conservatively, only add defs for blocks that are definitely executed
    # Go through blocks in order, as if they are statements of the block that
    # includes the parfor, and update uses/defs.

    # no need for topo order of ir_utils
    topo_order = cfg.topo_order()
    # blocks that dominate last block are definitely executed
    definitely_executed = cfg.dominators()[last_label]
    # except loop bodies that might not execute
    for loop in cfg.loops().values():
        definitely_executed -= loop.body
    for label in topo_order:
        if label in definitely_executed:
            # see compute_use_defs() in analysis.py
            # variables defined in the block that includes the parfor are not
            # uses of that block (are not potentially live in the beginning of
            # the block)
            use_set.update(uses[label] - def_set)
            def_set.update(defs[label])
        else:
            use_set.update(uses[label] - def_set)

    # treat loop variables and size variables as use
    loop_vars = {l.start.name for l in parfor.loop_nests if isinstance(l.start, ir.Var)}
    loop_vars |= {l.stop.name for l in parfor.loop_nests if isinstance(l.stop, ir.Var)}
    loop_vars |= {l.step.name for l in parfor.loop_nests if isinstance(l.step, ir.Var)}
    use_set.update(loop_vars - def_set)
    use_set |= get_parfor_pattern_vars(parfor)

    return analysis._use_defs_result(usemap=use_set, defmap=def_set)