-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Side effects of function analysis should be applied at the call site #2710
Comments
I note above that this wasn't a workaround for me, but I can restructure to avoid comptime varargs and thus work around this so this shouldn't block me on anything. |
Hang on now. #5675 presents a use case for comptime mutable pointer arguments. I don't think we should be so heavy-handed. |
We discussed this in a design meeting and decided that, anywhere observable, functions which mutate comptime state through pointers should behave as if they were analyzed at the call site. So in the following example: const warn = @import("std").debug.warn;
fn inc(comptime foo: *usize) void {
warn("{}\n", foo.*);
foo.* += 1;
}
pub fn main() void {
comptime var bar: usize = 0;
inc(&bar);
inc(&bar);
bar += 1;
warn("{}\n", bar);
} The first call to So in short, it behaves the way you wanted it to when you first wrote the code. |
I want to point out that the language spec already requires this concept, in order to detect when to not memoize comptime function calls. Memoizing comptime function calls is necessary for functions that return types to work properly, e.g. |
Edit: Original title was "Add compiler error for comptime pointer parameter with side-effects". The correct behavior is described in this comment.
This outputs
0
which is unexpected behavior to me. I would have expected theinc
call would implicitly be called at compile-time.Explicitly calling the function at comptime resolves this. This isn't a suitable workaround for me unfortunately since I'm actually passing varargs as well which do not work at comptime.
I presume the compiler doesn't see that
inc
has side-effects for its input, and the runtime call simply becomes an empty function after it is resolved. Thinking a bit more, I think the existing behavior is correct, and it is more the lack of a compiler error which is the issue.A workaround is to avoid comptime pointer parameters and return the values as needed. This correctly catches the issue with a compiler error.
May be fundamentally the same as #906.
The text was updated successfully, but these errors were encountered: