Skip to main content
Log in

Mojo function

debug_assert

debug_assert[: origin.set, //, cond: fn() capturing -> Bool, write_mode: Int = 0, assert_mode: StringSlice[StaticConstantOrigin] = __init__[__mlir_type.!kgen.string]("none"), cpu_only: Bool = False, *Ts: Writable = *?](*messages: *Ts, *, location: Optional[_SourceLocation] = Optional(None))

Asserts that the condition is true at run time.

If the condition is false, the assertion displays the given message and causes the program to exit.

You can pass in multiple arguments to generate a formatted message. No string allocation occurs unless the assertion is triggered.

x = 0
debug_assert(x > 0, "expected x to be more than 0 but got: ", x)
x = 0
debug_assert(x > 0, "expected x to be more than 0 but got: ", x)

Normal assertions are off by default—they only run when the program is compiled with all assertions enabled. You can set the assert_mode to safe to create an assertion that's on by default:

debug_assert[assert_mode="safe"](
x > 0, "expected x to be more than 0 but got: ", x
)
debug_assert[assert_mode="safe"](
x > 0, "expected x to be more than 0 but got: ", x
)

Use the ASSERT variable to turn assertions on or off when building or running a Mojo program:

mojo -D ASSERT=all main.mojo
mojo -D ASSERT=all main.mojo

The ASSERT variable takes the following values:

  • all: Turn on all assertions.
  • safe: Turn on "safe" assertions only. This is the default.
  • none: Turn off all assertions, for performance at the cost of safety.
  • warn: Turn on all assertions, but print any errors instead of exiting.

To ensure that you have no run-time penalty from your assertions even when they're disabled, make sure there are no side effects in your message and condition expressions. For example:

person = "name: john, age: 50"
name = "john"
debug_assert(String("name: ") + name == person, "unexpected name")
person = "name: john, age: 50"
name = "john"
debug_assert(String("name: ") + name == person, "unexpected name")

This will have a run-time penalty due to allocating a String in the condition expression, even when assertions are disabled. To avoid this, put the condition inside a closure so it runs only when the assertion is turned on:

fn check_name() capturing -> Bool:
return String("name: ") + name == person

debug_assert[check_name]("unexpected name")
fn check_name() capturing -> Bool:
return String("name: ") + name == person

debug_assert[check_name]("unexpected name")

If you need to allocate, and so don't want the assert to ever run on GPU, you can set it to CPU only:

debug_assert[check_name, cpu_only=True]("unexpected name")
debug_assert[check_name, cpu_only=True]("unexpected name")

For compile-time assertions, see constrained().

Parameters:

  • cond (fn() capturing -> Bool): The function to invoke to check if the assertion holds.
  • write_mode (Int): Determines whether to keep values in register or not.
  • assert_mode (StringSlice[StaticConstantOrigin]): Determines when the assert is turned on.
    • default ("none"): Turned on when compiled with -D ASSERT=all.
    • "safe": Turned on by default.
  • cpu_only (Bool): If true, only run the assert on CPU.
  • *Ts (Writable): The element types for the message arguments.

Args:

  • *messages (*Ts): A set of Writable arguments to convert to a String message.
  • location (Optional[_SourceLocation]): The location of the error (defaults to __call_location).

debug_assert[write_mode: Int = 0, assert_mode: StringSlice[StaticConstantOrigin] = __init__[__mlir_type.!kgen.string]("none"), cpu_only: Bool = False, *Ts: Writable = *?](cond: Bool, *messages: *Ts, *, location: Optional[_SourceLocation] = Optional(None))

Asserts that the condition is true at run time.

If the condition is false, the assertion displays the given message and causes the program to exit.

You can pass in multiple arguments to generate a formatted message. No string allocation occurs unless the assertion is triggered.

x = 0
debug_assert(x > 0, "expected x to be more than 0 but got: ", x)
x = 0
debug_assert(x > 0, "expected x to be more than 0 but got: ", x)

Normal assertions are off by default—they only run when the program is compiled with all assertions enabled. You can set the assert_mode to safe to create an assertion that's on by default:

debug_assert[assert_mode="safe"](
x > 0, "expected x to be more than 0 but got: ", x
)
debug_assert[assert_mode="safe"](
x > 0, "expected x to be more than 0 but got: ", x
)

Use the ASSERT variable to turn assertions on or off when building or running a Mojo program:

mojo -D ASSERT=all main.mojo
mojo -D ASSERT=all main.mojo

The ASSERT variable takes the following values:

  • all: Turn on all assertions.
  • safe: Turn on "safe" assertions only. This is the default.
  • none: Turn off all assertions, for performance at the cost of safety.
  • warn: Turn on all assertions, but print any errors instead of exiting.

To ensure that you have no run-time penalty from your assertions even when they're disabled, make sure there are no side effects in your message and condition expressions. For example:

person = "name: john, age: 50"
name = "john"
debug_assert(String("name: ") + name == person, "unexpected name")
person = "name: john, age: 50"
name = "john"
debug_assert(String("name: ") + name == person, "unexpected name")

This will have a run-time penalty due to allocating a String in the condition expression, even when assertions are disabled. To avoid this, put the condition inside a closure so it runs only when the assertion is turned on:

fn check_name() capturing -> Bool:
return String("name: ") + name == person

debug_assert[check_name]("unexpected name")
fn check_name() capturing -> Bool:
return String("name: ") + name == person

debug_assert[check_name]("unexpected name")

If you need to allocate, and so don't want the assert to ever run on GPU, you can set it to CPU only:

debug_assert[check_name, cpu_only=True]("unexpected name")
debug_assert[check_name, cpu_only=True]("unexpected name")

For compile-time assertions, see constrained().

Parameters:

  • write_mode (Int): Determines whether to keep values in register or not.
  • assert_mode (StringSlice[StaticConstantOrigin]): Determines when the assert is turned on.
    • default ("none"): Turned on when compiled with -D ASSERT=all.
    • "safe": Turned on by default.
  • cpu_only (Bool): If true, only run the assert on CPU.
  • *Ts (Writable): The element types for the message arguments.

Args:

  • cond (Bool): The bool value to assert.
  • *messages (*Ts): A set of Writable arguments to convert to a String message.
  • location (Optional[_SourceLocation]): The location of the error (defaults to __call_location).

Was this page helpful?