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
Conversation
* This allows arrays to be passed by value at comptime
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Looks good, but I think we should handle the array of undefined case.
src/analyze.cpp
Outdated
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
@andrewrk I've just push a change that recursively checks for undefined in 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? |
@Hejsil I think it should probably work how Valgrind treats undefined. Values can be |
What about this case then?
Currently, this fails at callsite, but I would assume this should be made to work then? |
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) |
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. |
This allows arrays (and structs having array fields) to be passed by value at comptime