Skip to main content

Mojo module

location

Implements utilities to capture and represent source code location.

This module provides compile-time and runtime introspection of source locations:

  • SourceLocation - A struct holding file name, line, and column information.
  • source_location() - Returns the location where this function is called.
  • call_location() - Returns the caller's location (for use in inlined functions).

These utilities are useful for error reporting, logging, debugging, and building custom assertion functions that report meaningful locations to users.

Example using source_location() to get the current location:

from reflection import source_location

fn main():
    var loc = source_location()
    print(loc)  # Prints: /path/to/file.mojo:5:15
    print("Line:", loc.line, "Column:", loc.col)

Example using call_location() for a custom assertion that reports the caller's location. Note that @always_inline is required for call_location() to work - the function must be inlined so the compiler can capture the caller's location:

from reflection import call_location

@always_inline  # Required for call_location() to work
fn my_assert(cond: Bool, msg: String = "assertion failed") raises:
    if not cond:
        raise Error(call_location().prefix(msg))

def main():
    var x = 5
    my_assert(x > 10, "x must be > 10")  # Error points to THIS line

Structs

Functions

Was this page helpful?