def _sanity_check_every_location_is_marked(ir_blocks):
    """Ensure that every new location is marked with a MarkLocation block."""
    # Exactly one MarkLocation block is found between any block that starts an interval of blocks
    # that all affect the same query position, and the first subsequent block that affects a
    # different position in the query. Such intervals include the following examples:
    # - from Fold to Unfold
    # - from QueryRoot to Traverse/Recurse
    # - from one Traverse to the next Traverse
    # - from Traverse to Backtrack
    found_start_block = False
    mark_location_blocks_count = 0

    start_interval_types = (QueryRoot, Traverse, Recurse, Fold)
    end_interval_types = (Backtrack, ConstructResult, Recurse, Traverse, Unfold)

    for block in ir_blocks:
        # Terminate started intervals before opening new ones.
        if isinstance(block, end_interval_types) and found_start_block:
            found_start_block = False
            if mark_location_blocks_count != 1:
                raise AssertionError(u'Expected 1 MarkLocation block between traversals, found: '
                                     u'{} {}'.format(mark_location_blocks_count, ir_blocks))

        # Now consider opening new intervals or processing MarkLocation blocks.
        if isinstance(block, MarkLocation):
            mark_location_blocks_count += 1
        elif isinstance(block, start_interval_types):
            found_start_block = True
            mark_location_blocks_count = 0
Ensure that every new location is marked with a MarkLocation block.