Skip to content

missing compile error for runtime iteration over compile-time-only array elements #11632

Closed
@andrewrk

Description

@andrewrk

Zig Version: 0.10.0-dev.2159+f5edf78ee

test "functions in an array" {
    const TestFn = fn () void;
    const test_fns = [_]TestFn{ foo, bar };
    for (test_fns) |testFn| {
        testFn();
    }
}

fn foo() void {}
fn bar() void {}
$ ./stage2/bin/zig test test3.zig 
thread 2885489 panic: Segmentation fault at address 0x0
???:?:?: 0x3eb6328 in ??? (???)

Panicked during a panic: integer overflow
Inner panic stack:
/home/andy/Downloads/zig/lib/std/debug.zig:169:69: 0x3314122 in std.debug.dumpStackTraceFromBase (zig)
            printSourceAtAddress(debug_info, stderr, return_address - 1, tty_config) catch return;
                                                                    ^
/home/andy/Downloads/zig/src/crash_report.zig:327:45: 0x31af4a2 in crash_report.StackContext.dumpStackTrace (zig)
                debug.dumpStackTraceFromBase(ex.bp, ex.ip);
                                            ^
/home/andy/Downloads/zig/src/crash_report.zig:479:39: 0x31af050 in crash_report.PanicSwitch.reportStack (zig)
        state.panic_ctx.dumpStackTrace();
                                      ^
/home/andy/Downloads/zig/src/crash_report.zig:553:9: 0x2fc80e0 in crash_report.PanicSwitch.initPanic (zig)
        @call(.{}, func, args);
        ^
/home/andy/Downloads/zig/src/crash_report.zig:553:9: 0x2cc1845 in crash_report.PanicSwitch.dispatch (zig)
        @call(.{}, func, args);
        ^
/home/andy/Downloads/zig/src/crash_report.zig:254:25: 0x2fc9577 in crash_report.handleSegfaultPosix (zig)
    PanicSwitch.dispatch(null, stack_ctx, error_msg);
                        ^
Aborted (core dumped)

This is an important case to get right because it's code that is valid for stage1 which now has different function type semantics in stage2. It should be resolved in one of two ways:

  • A compile error explaining that testFn is a runtime-known value and so its type must not be a compile-time-only type. A hint should say that inline for would solve this problem. A hint should also say that function bodies are comptime-only while function pointers are OK for runtime, and that & can be used to convert a function body to its pointer.
  • Or it should implicitly cause the for to become an inline for.

For the former, you can see stage1 already handles a similar case pretty well:

test "functions in an array" {
    const types = [_]type{ i32, bool };
    for (types) |T| {
        _ = T;
    }
}
$ ./stage1/bin/zig test test3.zig 
./test3.zig:6:5: error: values of type 'type' must be comptime known, but index value is runtime known
    for (types) |T| {
    ^

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions