-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
TypeInfo.Pointer sentinel value is not always optional #3816
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
This is actually working as designed, because the sentinel: var, If you change the test to: const std = @import("std");
const TypeInfo = @import("builtin").TypeInfo;
fn ptrInfo(sentinel: var) TypeInfo.Pointer {
return @typeInfo(@Type(TypeInfo{
.Pointer = .{
.is_const = false,
.is_volatile = false,
.alignment = 0,
.child = u8,
.is_allowzero = false,
.size = .Many,
.sentinel = sentinel,
},
})).Pointer;
}
pub fn main() void {
std.debug.warn("{}\n", @typeName(@typeOf(ptrInfo(0).sentinel)));
std.debug.warn("{}\n", @typeName(@typeOf(ptrInfo(@as(u8, 0)).sentinel)));
std.debug.warn("{}\n", @typeName(@typeOf(ptrInfo(@as(?u8, 0)).sentinel)));
} Then the output is:
|
Ok. I think this might warrant some special notice in the zig documentation. Something like: The |
I've created a PR to clarify the behavior here: #3861 |
Another thing I realized is that with this behavior, we are breaking what I thought was going to be an invariant. Namely that for any fn typeInfoInvariant(comptime ti: TypeInfo) void {
std.debug.assert(ti == @typeInfo(@Type(ti)));
} Since the If we're not sure, maybe we should repoen this issue? |
The important invariant is |
In this example program, the three calls to
ptrInfo
should all have the same sentinel value type, but each one produces a different type:This causes a problem in
ir.cpp
because it always expects this to be an optional type.The text was updated successfully, but these errors were encountered: