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

Unexpected behavior of const data in compile time loop unrolling #740

Closed
tgschultz opened this Issue Feb 3, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@tgschultz
Contributor

tgschultz commented Feb 3, 2018

const warn = @import("std").debug.warn;

//This just gives us something to vary the
//const string data with
const Iter = struct {
    Seventeen: f32,
    One: f32,
    Three: f32,
};

test "comptime loop unrolling const behavior" {
    warn("\n");
    comptime var i = 0;
    inline while(i < @memberCount(Iter)) : (i += 1) {
        const FieldName = @memberName(Iter, i);
        warn("|{}|\n", FieldName[0..]);
    }
}

Taking a slice of a const in an comptime unrolled loop yields unexpected results. Possibly related to #699.

@andrewrk

This comment has been minimized.

Member

andrewrk commented Mar 8, 2018

This is a bug in Zig when a var or const has different types in different comptime iterations of the while loop.

For example, when i is 0, @typeOf(FieldName) == [9]u8, but when i is 1, @typeOf(FieldName) == [3]u8.

A workaround until the bug is fixed is to make it an expression instead of saving it in a variable:

test "comptime loop unrolling const behavior" {
    warn("\n");
    comptime var i = 0;
    inline while(i < @memberCount(Iter)) : (i += 1) {
        warn("|{}|\n", @memberName(Iter, i)[0..]);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment