-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Compiler crash on mem.eql with undefined slice #1573
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
Comments
There is an easy fix in https://github.com/ziglang/zig/blob/master/src/analyze.cpp#L5807-L5814, return false if any of the fields is undef. This is in the context of Applying it, the compilation error will actually trigger two times, I assume once when instanciating the function and once when executing it at comptime. Is that okay ? |
Making Another thing is to consider the source code and ask, "what is the best thing for the compiler to do, to help the user the most?" Zig should be able to figure out at compile time that this code dereferences an undefined pointer, which is undefined behavior. So whatever our solution is, a test for if it's a good solution is how obvious it is that the compile error tells the user they are dereferencing an undefined pointer. Looking up the stack with a debugger, I see that it comes from the function auto entry = ira->codegen->memoized_fn_eval_table.maybe_get(exec_scope); which means that the undefined value is interfering with Zig's ability to cache comptime function calls. Thinking about what should happen with regards to comptime {
var x: i32 = undefined;
foo(x);
}
fn foo(x: i32) void {} This case gives:
So now I'm wondering, why is it an error simply to pass an undefined value to a comptime function call? Shouldn't the error be when the value is dereferenced? This seems related to #1947. Putting a breakpoint on ConstExprValue *arg_val = ir_resolve_const(ira, casted_arg, UndefBad); which means that Zig is giving a compile error for parameters that are undefined when doing a comptime function call, but it is not looking recursively into aggregate data types to find undefined values. So this is an inconsistency that we should resolve. Looking at the rules that #1947 specify, there's no reason for undefined parameters to cause undefined behavior. This is actually an incorrect compile error. So now I change
This makes sense - it's the same problem noted above where the undefined value is interfering with Zig's ability to memoize the comptime function call. So now the task is to update the codebase to make Update comptime {
const a: i32 = undefined;
const b: i32 = undefined;
@compileLog(a == b);
} which currently gives this output:
This behavior is also planned to be changed with #1947. The expected result is So if we take this approach, we have to make sure that we do not accidentally propagate this new way of treating undefined values to callers of
This is a separate problem, which I would be happy to discuss, but I don't think you should have to worry about it for this issue. Hope that helps @Sahnvour, let me know if I can provide any more guidance. |
I think I get your concerns, but I'm a bit confused about what your final take really is. Assuming you are not entirely satisfied by
Since |
This now gives a proper compile error. /usr/lib/zig/std/mem.zig:351:10: error: use of undefined value here causes undefined behavior
if (a.len != b.len) return false;
... etc No runtime error since #211 hasn't been implemented yet. |
calling mem.eql with a constant undefined slice, crashes the compiler.
interestingly this fails to compile, complaining about the use of an undefined value:
little backtrace:
maybe somewhat related to #1293
The text was updated successfully, but these errors were encountered: