output_ptr Main 断言错误 - Cairo-lang

output_ptr Assertion Error in Main - Cairo-lang

我正在通过官方Cairo language tutorial。当我到达 15 拼图的 build_dict 函数时,我有点困惑所以想打印一些东西看看。

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

我在 loc_tuple 中添加了用于打印第一行的行。但是,Cairo 编译器给我以下错误:

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'].

我尝试了各种 serialize_word 语句并且 none 似乎有效。这个问题以前从未出现在其他 serialize_word 语句中,包括在本教程的前面部分。

在代码顶部声明 %builtins output,这样编译器就会知道您在主函数中使用了隐式参数 (output_ptr) 并期待它。 仅当您声明要使用隐式参数时,Cairo 编译器才能处理它们。参见 here