Skip to content

Commit 22ff74f

Browse files
committed
even more underfined -> null
1 parent 970e599 commit 22ff74f

22 files changed

+106
-97
lines changed

src/allocators.zig

+3-3
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ pub fn BSSList(comptime ValueType: type, comptime _count: anytype) type {
195195

196196
allocator: Allocator,
197197
mutex: Mutex = Mutex.init(),
198-
head: *OverflowBlock = undefined,
198+
head: ?*OverflowBlock = null,
199199
tail: OverflowBlock = OverflowBlock{},
200200
backing_buf: [count]ValueType = undefined,
201201
used: u32 = 0,
@@ -230,12 +230,12 @@ pub fn BSSList(comptime ValueType: type, comptime _count: anytype) type {
230230

231231
fn appendOverflow(self: *Self, value: ValueType) !*ValueType {
232232
instance.used += 1;
233-
return self.head.append(value) catch brk: {
233+
return self.head.?.append(value) catch brk: {
234234
var new_block = try self.allocator.create(OverflowBlock);
235235
new_block.* = OverflowBlock{};
236236
new_block.prev = self.head;
237237
self.head = new_block;
238-
break :brk self.head.append(value);
238+
break :brk self.head.?.append(value);
239239
};
240240
}
241241

src/baby_list.zig

+15-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const bun = @import("root").bun;
88
pub fn BabyList(comptime Type: type) type {
99
return struct {
1010
const ListType = @This();
11-
ptr: [*]Type = undefined,
11+
ptr: ?[*]Type = null,
1212
len: u32 = 0,
1313
cap: u32 = 0,
1414

@@ -21,7 +21,7 @@ pub fn BabyList(comptime Type: type) type {
2121
}
2222

2323
pub fn available(this: *@This()) []Type {
24-
return this.ptr[this.len..this.cap];
24+
return this.ptr.?[this.len..this.cap];
2525
}
2626

2727
pub fn deinitWithAllocator(this: *@This(), allocator: std.mem.Allocator) void {
@@ -30,7 +30,7 @@ pub fn BabyList(comptime Type: type) type {
3030
}
3131

3232
pub fn contains(this: @This(), item: []const Type) bool {
33-
return this.len > 0 and @intFromPtr(item.ptr) >= @intFromPtr(this.ptr) and @intFromPtr(item.ptr) < @intFromPtr(this.ptr) + this.len;
33+
return this.len > 0 and @intFromPtr(item.ptr) >= @intFromPtr(this.ptr.?) and @intFromPtr(item.ptr) < @intFromPtr(this.ptr.?) + this.len;
3434
}
3535

3636
pub inline fn initConst(items: []const Type) ListType {
@@ -52,7 +52,7 @@ pub fn BabyList(comptime Type: type) type {
5252
pub fn popOrNull(this: *@This()) ?Type {
5353
if (this.len == 0) return null;
5454
this.len -= 1;
55-
return this.ptr[this.len];
55+
return this.ptr.?[this.len];
5656
}
5757

5858
pub fn clone(this: @This(), allocator: std.mem.Allocator) !@This() {
@@ -66,7 +66,7 @@ pub fn BabyList(comptime Type: type) type {
6666
}
6767

6868
pub fn appendAssumeCapacity(this: *@This(), value: Type) void {
69-
this.ptr[this.len] = value;
69+
this.ptr.?[this.len] = value;
7070
this.len += 1;
7171
std.debug.assert(this.cap >= this.len);
7272
}
@@ -81,7 +81,7 @@ pub fn BabyList(comptime Type: type) type {
8181
}
8282

8383
pub fn appendSliceAssumeCapacity(this: *@This(), values: []const Type) void {
84-
var tail = this.ptr[this.len .. this.len + values.len];
84+
var tail = this.ptr.?[this.len .. this.len + values.len];
8585
std.debug.assert(this.cap >= this.len + @as(u32, @truncate(values.len)));
8686
bun.copy(Type, tail, values);
8787
this.len += @as(u32, @truncate(values.len));
@@ -154,39 +154,39 @@ pub fn BabyList(comptime Type: type) type {
154154

155155
pub fn list(this: ListType) std.ArrayListUnmanaged(Type) {
156156
return std.ArrayListUnmanaged(Type){
157-
.items = this.ptr[0..this.len],
157+
.items = this.ptr.?[0..this.len],
158158
.capacity = this.cap,
159159
};
160160
}
161161

162162
pub fn listManaged(this: ListType, allocator: std.mem.Allocator) std.ArrayList(Type) {
163163
return std.ArrayList(Type){
164-
.items = this.ptr[0..this.len],
164+
.items = this.ptr.?[0..this.len],
165165
.capacity = this.cap,
166166
.allocator = allocator,
167167
};
168168
}
169169

170170
pub inline fn first(this: ListType) ?*Type {
171-
return if (this.len > 0) this.ptr[0] else @as(?*Type, null);
171+
return if (this.len > 0) this.ptr.?[0] else @as(?*Type, null);
172172
}
173173

174174
pub inline fn last(this: ListType) ?*Type {
175-
return if (this.len > 0) &this.ptr[this.len - 1] else @as(?*Type, null);
175+
return if (this.len > 0) &this.ptr.?[this.len - 1] else @as(?*Type, null);
176176
}
177177

178178
pub inline fn first_(this: ListType) Type {
179-
return this.ptr[0];
179+
return this.ptr.?[0];
180180
}
181181

182182
pub inline fn at(this: ListType, index: usize) *const Type {
183183
std.debug.assert(index < this.len);
184-
return &this.ptr[index];
184+
return &this.ptr.?[index];
185185
}
186186

187187
pub inline fn mut(this: ListType, index: usize) *Type {
188188
std.debug.assert(index < this.len);
189-
return &this.ptr[index];
189+
return &this.ptr.?[index];
190190
}
191191

192192
pub fn one(allocator: std.mem.Allocator, value: Type) !ListType {
@@ -200,7 +200,7 @@ pub fn BabyList(comptime Type: type) type {
200200
}
201201

202202
pub inline fn @"[0]"(this: ListType) Type {
203-
return this.ptr[0];
203+
return this.ptr.?[0];
204204
}
205205
const OOM = error{OutOfMemory};
206206

@@ -218,7 +218,7 @@ pub fn BabyList(comptime Type: type) type {
218218

219219
pub inline fn slice(this: ListType) []Type {
220220
@setRuntimeSafety(false);
221-
return this.ptr[0..this.len];
221+
return this.ptr.?[0..this.len];
222222
}
223223

224224
pub fn write(this: *@This(), allocator: std.mem.Allocator, str: []const u8) !u32 {

src/bun.js/base.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ pub fn dump(value: JSValue, globalObject: *JSC.JSGlobalObject) !void {
394394
pub const JSStringList = std.ArrayList(js.JSStringRef);
395395

396396
pub const ArrayBuffer = extern struct {
397-
ptr: [*]u8 = undefined,
397+
ptr: ?[*]u8 = null,
398398
offset: u32 = 0,
399399
len: u32 = 0,
400400
byte_len: u32 = 0,
@@ -586,7 +586,7 @@ pub const ArrayBuffer = extern struct {
586586
/// new ArrayBuffer(view.buffer, view.byteOffset, view.byteLength)
587587
/// ```
588588
pub inline fn byteSlice(this: *const @This()) []u8 {
589-
return this.ptr[this.offset .. this.offset + this.byte_len];
589+
return this.ptr.?[this.offset .. this.offset + this.byte_len];
590590
}
591591

592592
/// The equivalent of

src/bun.js/event_loop.zig

+6-6
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,12 @@ const AsyncIO = @import("root").bun.AsyncIO;
365365

366366
// This type must be unique per JavaScript thread
367367
pub const GarbageCollectionController = struct {
368-
gc_timer: *uws.Timer = undefined,
368+
gc_timer: ?*uws.Timer = null,
369369
gc_last_heap_size: usize = 0,
370370
gc_last_heap_size_on_repeating_timer: usize = 0,
371371
heap_size_didnt_change_for_repeating_timer_ticks_count: u8 = 0,
372372
gc_timer_state: GCTimerState = GCTimerState.pending,
373-
gc_repeating_timer: *uws.Timer = undefined,
373+
gc_repeating_timer: ?*uws.Timer = null,
374374
gc_timer_interval: i32 = 0,
375375
gc_repeating_timer_fast: bool = true,
376376

@@ -387,13 +387,13 @@ pub const GarbageCollectionController = struct {
387387
}
388388
} else |_| {}
389389
}
390-
this.gc_repeating_timer.set(this, onGCRepeatingTimer, gc_timer_interval, gc_timer_interval);
390+
this.gc_repeating_timer.?.set(this, onGCRepeatingTimer, gc_timer_interval, gc_timer_interval);
391391
this.gc_timer_interval = gc_timer_interval;
392392
}
393393

394394
pub fn scheduleGCTimer(this: *GarbageCollectionController) void {
395395
this.gc_timer_state = .scheduled;
396-
this.gc_timer.set(this, onGCTimer, 16, 0);
396+
this.gc_timer.?.set(this, onGCTimer, 16, 0);
397397
}
398398

399399
pub fn bunVM(this: *GarbageCollectionController) *VirtualMachine {
@@ -419,11 +419,11 @@ pub const GarbageCollectionController = struct {
419419
pub fn updateGCRepeatTimer(this: *GarbageCollectionController, comptime setting: @Type(.EnumLiteral)) void {
420420
if (setting == .fast and !this.gc_repeating_timer_fast) {
421421
this.gc_repeating_timer_fast = true;
422-
this.gc_repeating_timer.set(this, onGCRepeatingTimer, this.gc_timer_interval, this.gc_timer_interval);
422+
this.gc_repeating_timer.?.set(this, onGCRepeatingTimer, this.gc_timer_interval, this.gc_timer_interval);
423423
this.heap_size_didnt_change_for_repeating_timer_ticks_count = 0;
424424
} else if (setting == .slow and this.gc_repeating_timer_fast) {
425425
this.gc_repeating_timer_fast = false;
426-
this.gc_repeating_timer.set(this, onGCRepeatingTimer, 30_000, 30_000);
426+
this.gc_repeating_timer.?.set(this, onGCRepeatingTimer, 30_000, 30_000);
427427
this.heap_size_didnt_change_for_repeating_timer_ticks_count = 0;
428428
}
429429
}

src/bun.js/test/snapshot.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ pub const Snapshots = struct {
153153

154154
if (export_default.data == .e_call) {
155155
const function_call = export_default.data.e_call;
156-
if (function_call.args.len == 2 and function_call.args.ptr[0].data == .e_function) {
157-
const arg_function_stmts = function_call.args.ptr[0].data.e_function.func.body.stmts;
156+
if (function_call.args.len == 2 and function_call.args.ptr.?[0].data == .e_function) {
157+
const arg_function_stmts = function_call.args.ptr.?[0].data.e_function.func.body.stmts;
158158
for (arg_function_stmts) |stmt| {
159159
switch (stmt.data) {
160160
.s_expr => |expr| {

src/bun.js/webcore/encoding.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ pub const TextEncoder = struct {
205205
if (array.isEmpty()) {
206206
array = JSC.JSValue.createUninitializedUint8Array(globalThis, length);
207207
array.ensureStillAlive();
208-
@memcpy(array.asArrayBuffer(globalThis).?.ptr[0..length], buf_to_use[0..length]);
208+
@memcpy(array.asArrayBuffer(globalThis).?.ptr.?[0..length], buf_to_use[0..length]);
209209
}
210210

211211
return array;

src/bun.js/webcore/streams.zig

+4-4
Original file line numberDiff line numberDiff line change
@@ -2534,7 +2534,7 @@ pub fn HTTPServerWritable(comptime ssl: bool) type {
25342534
}
25352535

25362536
fn readableSlice(this: *@This()) []const u8 {
2537-
return this.buffer.ptr[this.offset..this.buffer.cap][0..this.buffer.len];
2537+
return this.buffer.ptr.?[this.offset..this.buffer.cap][0..this.buffer.len];
25382538
}
25392539

25402540
pub fn onWritable(this: *@This(), write_offset_: c_ulong, _: *UWSResponse) callconv(.C) bool {
@@ -3760,16 +3760,16 @@ pub const AutoSizer = struct {
37603760

37613761
pub fn resize(this: *AutoSizer, size: usize) ![]u8 {
37623762
const available = this.buffer.cap - this.buffer.len;
3763-
if (available >= size) return this.buffer.ptr[this.buffer.len..this.buffer.cap][0..size];
3763+
if (available >= size) return this.buffer.ptr.?[this.buffer.len..this.buffer.cap][0..size];
37643764
const to_grow = size -| available;
37653765
if (to_grow + @as(usize, this.buffer.cap) > this.max)
3766-
return this.buffer.ptr[this.buffer.len..this.buffer.cap];
3766+
return this.buffer.ptr.?[this.buffer.len..this.buffer.cap];
37673767

37683768
var list = this.buffer.listManaged(this.allocator);
37693769
const prev_len = list.items.len;
37703770
try list.ensureTotalCapacity(to_grow + @as(usize, this.buffer.cap));
37713771
this.buffer.update(list);
3772-
return this.buffer.ptr[prev_len..@as(usize, this.buffer.cap)];
3772+
return this.buffer.ptr.?[prev_len..@as(usize, this.buffer.cap)];
37733773
}
37743774
};
37753775

src/bun.zig

+9
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,15 @@ pub fn clone(val: anytype, allocator: std.mem.Allocator) !@TypeOf(val) {
534534
}
535535
pub const StringBuilder = @import("./string_builder.zig");
536536

537+
/// This is wrong! Avoid using this!
538+
/// Any use of this should be replaced with the type itself to the optional type.
539+
/// The undefind is unspecified value, cant be compared to anything include itself.
540+
/// In Debug mode,
541+
/// Zig writes 0xaa bytes to undefined memory.
542+
/// This is to catch bugs early, and to help detect use of undefined memory in a debugger.
543+
/// However, this behavior is only an implementation feature, not a language semantic,
544+
/// so it is not guaranteed to be *observable* to code.
545+
/// ref: https://ziglang.org/documentation/master/#undefined
537546
pub fn assertDefined(val: anytype) void {
538547
if (comptime !Environment.allow_assert) return;
539548
const Type = @TypeOf(val);

src/bun_dev_http_server.zig

+3-3
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const PosixRequestContext = struct {
103103
conn: std.net.Stream,
104104
allocator: std.mem.Allocator,
105105
arena: ThreadlocalArena,
106-
req_body_node: *RequestDataPool.Node = undefined,
106+
req_body_node: ?*RequestDataPool.Node = null,
107107
log: logger.Log,
108108
bundler: *Bundler,
109109
keep_alive: bool = true,
@@ -1252,7 +1252,7 @@ const PosixRequestContext = struct {
12521252
const CacheSet = @import("./cache.zig").Set;
12531253
threadlocal var websocket_printer: JSPrinter.BufferWriter = undefined;
12541254
pub fn handle(self: *WebsocketHandler) void {
1255-
var req_body = self.ctx.req_body_node;
1255+
var req_body = self.ctx.req_body_node.?;
12561256
defer {
12571257
js_ast.Stmt.Data.Store.reset();
12581258
js_ast.Expr.Data.Store.reset();
@@ -3045,7 +3045,7 @@ pub const Server = struct {
30453045

30463046
defer {
30473047
if (!req_ctx.controlled) {
3048-
req_ctx.req_body_node.release();
3048+
req_ctx.req_body_node.?.release();
30493049
req_ctx.arena.deinit();
30503050
}
30513051
}

src/bundler/bundle_v2.zig

+7-7
Original file line numberDiff line numberDiff line change
@@ -3179,7 +3179,7 @@ const LinkerGraph = struct {
31793179
.top_level_symbol_to_parts_overlay = &top_level_symbol_to_parts_overlay,
31803180
};
31813181

3182-
js_ast.DeclaredSymbol.forEachTopLevelSymbol(&parts.ptr[part_id].declared_symbols, &ctx, Iterator.next);
3182+
js_ast.DeclaredSymbol.forEachTopLevelSymbol(&parts.ptr.?[part_id].declared_symbols, &ctx, Iterator.next);
31833183

31843184
return part_id;
31853185
}
@@ -4212,7 +4212,7 @@ const LinkerContext = struct {
42124212
@panic("Internal error: expected at least one part for lazy export");
42134213
}
42144214

4215-
var part: *js_ast.Part = &parts.ptr[1];
4215+
var part: *js_ast.Part = &parts.ptr.?[1];
42164216

42174217
if (part.stmts.len == 0) {
42184218
@panic("Internal error: expected at least one statement in the lazy export");
@@ -4278,8 +4278,8 @@ const LinkerContext = struct {
42784278
// end up actually being used at this point (since import binding hasn't
42794279
// happened yet). So we need to wait until after tree shaking happens.
42804280
const generated = try this.generateNamedExportInFile(source_index, module_ref, name, name);
4281-
parts.ptr[generated[1]].stmts = this.allocator.alloc(Stmt, 1) catch unreachable;
4282-
parts.ptr[generated[1]].stmts[0] = Stmt.alloc(
4281+
parts.ptr.?[generated[1]].stmts = this.allocator.alloc(Stmt, 1) catch unreachable;
4282+
parts.ptr.?[generated[1]].stmts[0] = Stmt.alloc(
42834283
S.Local,
42844284
S.Local{
42854285
.is_export = true,
@@ -4315,8 +4315,8 @@ const LinkerContext = struct {
43154315
) catch unreachable,
43164316
"default",
43174317
);
4318-
parts.ptr[generated[1]].stmts = this.allocator.alloc(Stmt, 1) catch unreachable;
4319-
parts.ptr[generated[1]].stmts[0] = Stmt.alloc(
4318+
parts.ptr.?[generated[1]].stmts = this.allocator.alloc(Stmt, 1) catch unreachable;
4319+
parts.ptr.?[generated[1]].stmts[0] = Stmt.alloc(
43204320
S.ExportDefault,
43214321
S.ExportDefault{
43224322
.default_name = .{
@@ -7961,7 +7961,7 @@ const LinkerContext = struct {
79617961
stmt.data.s_local.is_export = false;
79627962
} else if (FeatureFlags.unwrap_commonjs_to_esm and s.was_commonjs_export and wrap == .cjs) {
79637963
std.debug.assert(stmt.data.s_local.decls.len == 1);
7964-
const decl = stmt.data.s_local.decls.ptr[0];
7964+
const decl = stmt.data.s_local.decls.ptr.?[0];
79657965
if (decl.value) |decl_value| {
79667966
stmt = Stmt.alloc(
79677967
S.SExpr,

src/cli/create_command.zig

+4-4
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,7 @@ pub const CreateCommand = struct {
11671167
// "bun.macros.react-relay.graphql"
11681168
if (needs.bun_macro_relay and !needs_bun_prop and !needs_bun_macros_prop) {
11691169
// "graphql" is the only valid one for now, so anything else in this object is invalid.
1170-
bun_relay_prop.?.data.e_object = InjectionPrefill.bun_macros_relay_object.properties.ptr[0].value.?.data.e_object;
1170+
bun_relay_prop.?.data.e_object = InjectionPrefill.bun_macros_relay_object.properties.ptr.?[0].value.?.data.e_object;
11711171
needs_bun_macros_prop = false;
11721172
needs_bun_prop = false;
11731173
needs.bun_macro_relay = false;
@@ -1339,7 +1339,7 @@ pub const CreateCommand = struct {
13391339
var i: usize = 0;
13401340
var property_i: usize = 0;
13411341
while (i < package_json_expr.data.e_object.properties.len) : (i += 1) {
1342-
const property: js_ast.G.Property = package_json_expr.data.e_object.properties.ptr[i];
1342+
const property: js_ast.G.Property = package_json_expr.data.e_object.properties.ptr.?[i];
13431343
const key = property.key.?.asString(ctx.allocator).?;
13441344

13451345
if (strings.eqlComptime(key, "scripts")) {
@@ -1378,7 +1378,7 @@ pub const CreateCommand = struct {
13781378
}
13791379

13801380
if (key.len == 0 or !strings.eqlComptime(key, "bun-create")) {
1381-
package_json_expr.data.e_object.properties.ptr[property_i] = property;
1381+
package_json_expr.data.e_object.properties.ptr.?[property_i] = property;
13821382
property_i += 1;
13831383
continue;
13841384
}
@@ -1452,7 +1452,7 @@ pub const CreateCommand = struct {
14521452
}
14531453
}
14541454
}
1455-
package_json_expr.data.e_object.properties = js_ast.G.Property.List.init(package_json_expr.data.e_object.properties.ptr[0..property_i]);
1455+
package_json_expr.data.e_object.properties = js_ast.G.Property.List.init(package_json_expr.data.e_object.properties.ptr.?[0..property_i]);
14561456
}
14571457

14581458
var package_json_writer = JSPrinter.NewFileWriter(package_json_file.?);

src/install/lockfile.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -3450,8 +3450,8 @@ pub const Package = extern struct {
34503450
switch (obj.properties.len) {
34513451
0 => {},
34523452
1 => {
3453-
const bin_name = obj.properties.ptr[0].key.?.asString(allocator) orelse break :bin;
3454-
const value = obj.properties.ptr[0].value.?.asString(allocator) orelse break :bin;
3453+
const bin_name = obj.properties.ptr.?[0].key.?.asString(allocator) orelse break :bin;
3454+
const value = obj.properties.ptr.?[0].value.?.asString(allocator) orelse break :bin;
34553455

34563456
package.bin = .{
34573457
.tag = .named_file,

0 commit comments

Comments
 (0)