-
Notifications
You must be signed in to change notification settings - Fork 3k
less undefined #4492
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
less undefined #4492
Conversation
a359fc2
to
827f7f9
Compare
22ff74f
to
3bb3d9f
Compare
/// This is wrong! Avoid using this! | ||
/// Any use of this should be replaced with the type itself to the optional type. | ||
/// The undefind is unspecified value, cant be compared to anything include itself. | ||
/// In Debug mode, | ||
/// Zig writes 0xaa bytes to undefined memory. | ||
/// This is to catch bugs early, and to help detect use of undefined memory in a debugger. | ||
/// However, this behavior is only an implementation feature, not a language semantic, | ||
/// so it is not guaranteed to be *observable* to code. | ||
/// ref: https://ziglang.org/documentation/master/#undefined | ||
pub fn assertDefined(val: anytype) void { | ||
if (comptime !Environment.allow_assert) return; | ||
const Type = @TypeOf(val); |
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.
We should use the optional type when we need this
@dylan-conway Please take a look. Sorry for so many changes in a single PR, but they're pretty straightforward. |
This comment was marked as resolved.
This comment was marked as resolved.
I think this is an interesting start but we shouldn't replace all usages of undefined with optional. We can do this more gradually. It'll be too difficult to confidently merge in this current state. When they're not pointers, marking things as optional can increase the size and alignment of structs which can in of itself cause new bugs |
This PR only changes ptr fields in the struct that default initialized to undefined to optional ptr, as an optional pointer is guaranteed to be the same size as a pointer. The null of the optional is guaranteed to be address 0, and only few non ptr type changes, so it should not cause any new bugs |
What does this PR do?
How did you verify your code works?
This PR includes:
undefined
changes tofoo: ?ptr = null
, as.?
will panic insafemode
instead ofUB
. Plus an optional pointer is guaranteed to be the same size as a pointer. The null of the optional is guaranteed to be address 0= undefined
is removed.?
should be optimized away in non-safe mode, so these changes should not impact performance, just make use of these nullable pointers more explicitCloses: #4426