Search code examples
blockchainethereumstarknetcairo-lang

output_ptr Assertion Error in Main - Cairo-lang


I am going through the official Cairo language tutorial. When I get to the build_dict function for the 15-puzzle, I am a bit confused so would like to print out some things to see.

struct Location:
    member row : felt
    member col : felt
end

...

func finalize_state(dict : DictAccess*, idx) -> (
        dict : DictAccess*):
    if idx == 0:
        return (dict=dict)
    end

    assert dict.key = idx
    assert dict.prev_value = idx - 1
    assert dict.new_value = idx - 1

    return finalize_state(
        dict=dict + DictAccess.SIZE, idx=idx - 1)
end

##### I added the {} along with context in it for output #####
func main{output_ptr : felt*}():
    alloc_locals

    local loc_tuple : (Location, Location, Location, Location, Location) = (
        Location(row=0, col=2),
        Location(row=1, col=2),
        Location(row=1, col=3),
        Location(row=2, col=3),
        Location(row=3, col=3),
        )

    # Get the value of the frame pointer register (fp) so that
    # we can use the address of loc_tuple.
    let (__fp__, _) = get_fp_and_pc()
    # Since the tuple elements are next to each other we can use the
    # address of loc_tuple as a pointer to the 5 locations.
    verify_location_list(
        loc_list=cast(&loc_tuple, Location*), n_steps=4)

    ##### Here is what I added #####
    local locs : Location* = cast(&loc_tuple, Location*)
    tempvar loc = [locs]
    tempvar row = loc.row
    serialize_word(row)
    ################################

    return ()
end

I added the lines for printing the first row in loc_tuple. However, the Cairo compiler is giving me the following errors:

Traceback (most recent call last):
  File "/Users/yijiachen/cairo_venv/bin/cairo-compile", line 10, in <module>
    sys.exit(main())
  File "/Users/yijiachen/cairo_venv/lib/python3.8/site-packages/starkware/cairo/lang/compiler/cairo_compile.py", line 397, in main
    cairo_compile_common(
  File "/Users/yijiachen/cairo_venv/lib/python3.8/site-packages/starkware/cairo/lang/compiler/cairo_compile.py", line 121, in cairo_compile_common
    assembled_program = assemble_func(
  File "/Users/yijiachen/cairo_venv/lib/python3.8/site-packages/starkware/cairo/lang/compiler/cairo_compile.py", line 367, in 
    cairo_assemble_program
    check_main_args(program)
  File "/Users/yijiachen/cairo_venv/lib/python3.8/site-packages/starkware/cairo/lang/compiler/cairo_compile.py", line 296, in check_main_args
    assert main_args == expected_builtin_ptrs, (
AssertionError: Expected main to contain the following arguments (in this order): []. Found: ['output_ptr'].

I have tried with various serialize_word statements and none seem to work. This issue never arose before with other serialize_word statements, including in earlier parts of the tutorial.


Solution

  • Declare %builtins output at the top of your code so that the compiler will know that you use an implicit argument (output_ptr) in your main function and expect it. The Cairo compiler is able to process implicit arguments only if you declare that you are going to use them. See here.