def test_replace_nonreplaceable_vars():
    """
    The conversion should error out if an invalid replacement is invoked with nonreplaceable vars
    """
    constexpr_op = "constexpr_sparse_to_dense"
    @mb.program(input_specs=[mb.TensorSpec(shape=(4, 2))])
    def prog(x):
        constexpr = CONSTEXPR_FUNCS[constexpr_op]((4, 2))
        return mb.add(x=x, y=constexpr)

    block = prog.functions["main"]
    constexpr_op = block.find_ops(op_type=constexpr_op)[0]

    with block:
        const = mb.const(val=np.random.rand(4, 2), before_op=constexpr_op)
        expected_err_str = "might potentially be removed during the replacement of those vars."
        with pytest.raises(ValueError, match=expected_err_str):
            block.replace_uses_of_var_after_op(
                anchor_op=constexpr_op,
                old_var=constexpr_op.outputs[0],
                new_var=const
            )

    The conversion should error out if an invalid replacement is invoked with nonreplaceable vars
    
coremltools/converters/mil/mil/tests/test_block.py