Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Tracked by #89
andrewrk opened this issue May 11, 2022 · 0 comments · Fixed by #11733
Closed
Tracked by #89

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

andrewrk opened this issue May 11, 2022 · 0 comments · Fixed by #11733
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@andrewrk
Copy link
Member

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| {
    ^
@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness. labels May 11, 2022
@andrewrk andrewrk added this to the 0.10.0 milestone May 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant