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

Implement const_values_equal for arrays #1167

Merged
merged 6 commits into from Jun 30, 2018

Conversation

Projects
None yet
2 participants
@Hejsil
Member

Hejsil commented Jun 28, 2018

This allows arrays (and structs having array fields) to be passed by value at comptime

Implement const_values_equal for array type
* This allows arrays to be passed by value at comptime
@andrewrk

Thanks! Looks good, but I think we should handle the array of undefined case.

case TypeTableEntryIdArray: {
assert(a->type->data.array.len == b->type->data.array.len);
size_t len = a->type->data.array.len;
ConstExprValue *a_elems = a->data.x_array.s_none.elements;

This comment has been minimized.

@andrewrk

andrewrk Jun 28, 2018

Member

I think before you access s_none you should assert that the array is not ConstArraySpecialUndef

This is an optimization so that if the user has var blah: [2 * 1024 * 1024 * 1024]u8 = undefined, we don't bother allocating 2 GiB of memory at compile time. Probably before we invoke const_values_equal for arrays, we should either expand the undef (there's a function for that somewhere), or emit a compile error for trying to do an equality comparison with undefined at compile-time. Seems like the latter would be more appropriate.

Let me know if you need help coming up with a test case for this.

@Hejsil

This comment has been minimized.

Member

Hejsil commented Jun 29, 2018

@andrewrk I've just push a change that recursively checks for undefined in ir_resolve_const. This catches the undefined array case, but will also catch the more general "pass undefined to comptime parameter" case.

Before i look into the failing tests, i'd like to know if this like the right solution. The compiler currently already stops undefined from being passed at comptime, but maybe we want that to work?

@andrewrk

This comment has been minimized.

Member

andrewrk commented Jun 29, 2018

@Hejsil I think it should probably work how Valgrind treats undefined. Values can be undefined and that's fine. You can set values to undefined or pass undefined to a comptime function call. But if control flow tries to use an undefined value to decide whether to branch, then it is a compile error. Various other operations would be compile errors too, such as trying to deref a pointer which is undefined, or null-unwrap an optional whose value is undefined.

changes

@Hejsil

This comment has been minimized.

Member

Hejsil commented Jun 29, 2018

What about this case then?

fn a(comptime i: u8) u8 { return i }

test "" {
    _ = a(undefined);
}

Currently, this fails at callsite, but I would assume this should be made to work then?

@andrewrk

This comment has been minimized.

Member

andrewrk commented Jun 29, 2018

Yeah I think this should work. What do you think?

Btw I don't mean to make this pull request extra work. We can merge your stuff and open a new issue for the extra changes. (As long as tests are in a passing state)

@Hejsil

This comment has been minimized.

Member

Hejsil commented Jun 29, 2018

The above example was mostly concerned with the caching of comptime functions and undefined, but I guess we just don't cache values that have some undefined value.

I'll probably revert the latest two commits and merge, then open a bug for passing undefined arrays to comptime parameters.

@Hejsil Hejsil merged commit 42033ea into master Jun 30, 2018

3 of 4 checks passed

continuous-integration/travis-ci/pr The Travis CI build failed
Details
continuous-integration/appveyor/branch AppVeyor build succeeded
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details

@Hejsil Hejsil deleted the comptime-array-by-value branch Jun 30, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment