Scope attributesΒΆ

You can define an attribute for a code block by surrounding it with a with_attr statement as follows:

with_attr attribute_name("Attribute value"):
    # Code block.

The attribute value must be a string, and can refer to local variables only. Referring to a variable is done by putthing the variable name inside curly brackets (e.g., "x must be positive. Got: {x}.").

At present, only one attribute is supported by the Cairo runner: error_message. It allows the user to annotate a code block with an informative error message. If a runtime error originates from a code wrapped by this attribute, the VM will automatically add the corresponding error message to the error trace.

Note that if the statement that caused the error is surrounded by more than one with_attr, all of the error messages will appear in the output. This is also true for error messages wrapping function calls. Consider the following (contrived) example:

from starkware.cairo.common.math import assert_not_zero

func inverse(x) -> (res):
    with_attr error_message("x must not be zero. Got x={x}."):
        return (res=1 / x)

func assert_not_equal(a, b):
    let diff = a - b
    with_attr error_message("a and b must be distinct."):
    return ()

If you call assert_not_equal with a == b, you should get the following error:

Error message: x must not be zero. Got x=0.
:5:21: Error at pc=0:7:
Unknown value for memory cell at address 1:8.
        return (res=1 / x)
Cairo traceback (most recent call last):
Error message: a and b must be distinct.
:12:9: (pc=0:10)