Repository navigation
Description
Consider the following code sample, with the source name comptime-test.zig (this is minimized, so no comptime yet :)):
const std = @import("std");
pub fn Foo() []const []const u8 {
var rows: []const []const u8 = &[_][]const u8{};
rows = rows ++ [_][]const u8{&[_]u8{'1', '2', '3'}, &[_]u8{'4', '5', '6'}};
rows = rows ++ [_][]const u8{&[_]u8{'1', '2', '3'}, &[_]u8{'4', '5', '6'}};
return rows;
}
pub fn Bar() type {
var rows: []const []const u8 = &[_][]const u8{};
rows = rows ++ [_][]const u8{&[_]u8{'1', '2', '3'}, &[_]u8{'4', '5', '6'}};
return struct {
rows: []const []const u8 = rows
};
}
test "foo" {
const stdout = std.io.getStdOut().writer();
var foo = Foo();
try stdout.print("{}\n", .{foo[0]});
}
test "bar" {
const stdout = std.io.getStdOut().writer();
var bar = Bar(){};
try stdout.print("{}\n", .{bar.rows[0]});
}zig test comptime-test.zig --test-filter foo will reject the above source code with error: unable to evaluate constant expression. I'm not certain what syntactic changes I need to make for the compiler to accept this array concatenation.
william@xubuntu-dtrain:~/Projects/zig/aoc-2016/comptime-test$ zig test comptime-test.zig --test-filter foo
./comptime-test.zig:5:12: error: unable to evaluate constant expression
rows = rows ++ [_][]const u8{&[_]u8{'1', '2', '3'}, &[_]u8{'4', '5', '6'}};
^
./comptime-test.zig:5:17: note: referenced here
rows = rows ++ [_][]const u8{&[_]u8{'1', '2', '3'}, &[_]u8{'4', '5', '6'}};
On the other hand, zig test comptime-test.zig --test-filter bar will succeed, and the test will print out the contents of [_]u8{'1', '2', '3'} as a string (I assume the lack of null terminator doesn't matter here because the array length is encoded in the type):
william@xubuntu-dtrain:~/Projects/zig/aoc-2016/comptime-test$ zig test comptime-test.zig --test-filter bar
Test [1/1] test "bar"... 123
All 1 tests passed.
I'm not certain which behavior is desirable, but I don't think these two functions should have diverging behavior based on whether the return type is generic or not. bar does what I intended, so it feels like the array concatenation in foo should work as well?
Other Information
Version:
william@xubuntu-dtrain:~/Projects/zig/aoc-2016/comptime-test$ zig version
0.6.0+cff14dc2c
OS:
william@xubuntu-dtrain:~/Projects/zig/aoc-2016/comptime-test$ uname -a
Linux xubuntu-dtrain 4.15.0-66-generic #75~16.04.1-Ubuntu SMP Tue Oct 1 14:01:08 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
Activity
fengb commentedon Sep 4, 2020
I think this is expected behavior. Any function that returns
typeis implied to be comptime only and thus works, even though you didn't explicitly mark it as such.cr1901 commentedon Sep 5, 2020
Looks like I made my example too simple. Indeed, if I rewrite
Fooas:the tests succeed:
A less-reduced example, more in line with the code I was trying to study:
Is there any reason why
Fooshould fail to compile even when I add thecomptimeandinlinequalifiers, whenBarsucceeds?pfgithub commentedon Sep 5, 2020
Right now the best way to do this is by wrapping the body of the function in a comptime block, or using comptime when calling the function
or
In the future, #425 or something like it might get accepted and implemented and that will make things easier
cr1901 commentedon Sep 5, 2020
For the sake of completeness, the following also works:
My issue is that I didn't know
KEYWORD_comptime Exprwas accepted by the grammar, and in fact is accepted in many places thatKEYWORD_comptime BlockExprStatementis not. Such as inside the condition of anif-statement orwhile-loop :).This can probably be closed then. I think my final follow-up is: why isn't the call to
it.next()already comptime sinceithas been declared ascomptime?andrewrk commentedon Oct 4, 2020
Related: #4094
There is definitely a problem here. Zig sometimes tries to generate runtime versions of functions that are only ever called at comptime. It's unclear how to solve yet. Will leave this open until either there is a solution or it becomes an acknowledged and permanent flaw in the language.
[-]Zig Compiler rejects or accepts array concatenation based on return type.[/-][+]Function only called at comptime is emitted to binary[/+]Prevent analysis of functions only referenced at comptime
Prevent analysis of functions only referenced at comptime
Prevent analysis of functions only referenced at comptime
Prevent analysis of functions only referenced at comptime
Prevent analysis of functions only referenced at comptime
Prevent analysis of functions only referenced at comptime
Prevent analysis of functions only referenced at comptime