Skip to main content

Mojo function

call_location

call_location[*, inline_count: Int = 1]() -> SourceLocation

Returns the location for where the caller of this function is called.

An optional inline_count parameter can be specified to skip over that many levels of calling functions.

This should only be used when enclosed in a series of @always_inline or @always_inline("nodebug") function calls, where the layers of calling functions is no fewer than inline_count.

For example, when inline_count = 1, only the caller of this function needs to be @always_inline or @always_inline("nodebug"). This function will return the source location of the caller's invocation.

When inline_count = 2, the caller of the caller of this function also needs to be inlined. This function will return the source location of the caller's caller's invocation.

This currently doesn't work when the inline_count-th wrapping caller is called in a parameter expression.

Example:

from reflection import call_location

@always_inline  # Required for call_location() to work
fn assert_positive(value: Int) raises:
    # call_location() returns where assert_positive() was called,
    # not where call_location() itself is called.
    if value <= 0:
        raise Error(call_location().prefix("value must be positive"))

fn main():
    try:
        assert_positive(-1)  # Error will point to THIS line
    except e:
        print(e)

Parameters:

  • inline_count (Int): The number of inline call levels to skip.

Returns:

SourceLocation: The location information of where the caller of this function (i.e. the function whose body call_location() is used in) is called.

Was this page helpful?