IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: 1.0.0
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

check_slice_bounds

def check_slice_bounds[cpu_default: Bool = True](slice: ContiguousSlice, len: Int, location: OptionalReg[SourceLocation] = None) -> Tuple[Int, Int]

Bounds check for slice indexing, which is on by default for CPU, and off by default for GPU.

Resolves slice's optional start/end against len and aborts if the resolved start or end index is out of bounds, or if start is greater than end. ContiguousSlice does not support negative (from-the-end) indices, so a negative start or end is always out of bounds.

Parameters:

  • cpu_default (Bool): If the bounds check is on by default on CPU.

Args:

  • slice (ContiguousSlice): The slice to resolve and bounds check.
  • len (Int): The size of the container.
  • location (OptionalReg[SourceLocation]): SourceLocation shown on assert error. Defaults to showing the callsite two levels of function calls above this one. So if check_slice_bounds is called inside a __getitem__ method, it will show the source location where the incorrect slice was provided.

Returns:

Tuple[Int, Int]: The slice's resolved (start, end).