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

Implement const_values_equal for arrays #1167

Merged
merged 6 commits into from Jun 30, 2018
Merged

Conversation

Hejsil
Copy link
Sponsor Contributor

@Hejsil Hejsil commented Jun 28, 2018

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

* This allows arrays to be passed by value at comptime
Copy link
Member

@andrewrk andrewrk left a 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;
Copy link
Member

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.

@Hejsil
Copy link
Sponsor Contributor Author

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
Copy link
Member

@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.

@Hejsil
Copy link
Sponsor Contributor Author

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
Copy link
Member

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
Copy link
Sponsor Contributor Author

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
@Hejsil Hejsil deleted the comptime-array-by-value branch June 30, 2018 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants