@@ -150,10 +150,6 @@ pub fn format(
150
150
comptime var specifier_end = 0 ;
151
151
comptime var options = FormatOptions {};
152
152
153
- // TODO: calculate this
154
- var recur_stack : [0x10000 ]u8 = undefined ;
155
- var recur_allocator = std .heap .FixedBufferAllocator .init (& recur_stack );
156
-
157
153
inline for (fmt ) | c , i | {
158
154
switch (state ) {
159
155
.Start = > switch (c ) {
@@ -211,7 +207,6 @@ pub fn format(
211
207
fmt [0.. 0],
212
208
options ,
213
209
generator ,
214
- & recur_allocator .allocator ,
215
210
default_max_depth ,
216
211
);
217
212
@@ -243,7 +238,6 @@ pub fn format(
243
238
fmt [specifier_start .. i ],
244
239
options ,
245
240
generator ,
246
- & recur_allocator .allocator ,
247
241
default_max_depth ,
248
242
);
249
243
state = .Start ;
@@ -289,7 +283,6 @@ pub fn format(
289
283
fmt [specifier_start .. specifier_end ],
290
284
options ,
291
285
generator ,
292
- & recur_allocator .allocator ,
293
286
default_max_depth ,
294
287
);
295
288
state = .Start ;
@@ -316,7 +309,6 @@ pub fn format(
316
309
fmt [specifier_start .. specifier_end ],
317
310
options ,
318
311
generator ,
319
- & recur_allocator .allocator ,
320
312
default_max_depth ,
321
313
);
322
314
state = .Start ;
@@ -352,9 +344,14 @@ pub fn formatType(
352
344
comptime fmt : []const u8 ,
353
345
options : FormatOptions ,
354
346
generator : * Generator ([]const u8 ),
355
- allocator : * mem.Allocator ,
356
- max_depth : usize ,
347
+ comptime max_depth : comptime_int ,
357
348
) void {
349
+ if (max_depth < 0 ) {
350
+ // This shouldn't ever be reached as we account for it later in the function.
351
+ // But the compiler analysis phase gets confused and generates infinite versions of this function if there's no check.
352
+ @compileError ("max_depth less than 0" );
353
+ }
354
+
358
355
if (comptime std .mem .eql (u8 , fmt , "*" )) {
359
356
return formatPtr (@TypeOf (value ).Child , @ptrToInt (value ), generator );
360
357
}
@@ -372,25 +369,16 @@ pub fn formatType(
372
369
},
373
370
.Optional = > {
374
371
if (value ) | payload | {
375
- // return @call(.{ .modifier = .always_tail }, formatType, .{ payload, fmt, options, generator, allocator, max_depth });
376
- // TODO: reenable tail call once it is fixed https://github.com/ziglang/zig/issues/4060
377
- var frame = allocator .create (
378
- @TypeOf (async formatType (payload , fmt , options , generator , allocator , max_depth )),
379
- ) catch | err | switch (err ) {
380
- error .OutOfMemory = > return generator .yield (" ??OOM?? " ),
381
- };
382
- defer allocator .destroy (frame );
383
- frame .* = async formatType (payload , fmt , options , generator , allocator , max_depth );
384
- await frame .* ;
372
+ return @call (.{ .modifier = .always_tail }, formatType , .{ payload , fmt , options , generator , max_depth });
385
373
} else {
386
374
return generator .yield ("null" );
387
375
}
388
376
},
389
377
.ErrorUnion = > {
390
378
if (value ) | payload | {
391
- return @call (.{ .modifier = .always_tail }, formatType , .{ payload , fmt , options , generator , allocator , max_depth });
379
+ return @call (.{ .modifier = .always_tail }, formatType , .{ payload , fmt , options , generator , max_depth });
392
380
} else | err | {
393
- return @call (.{ .modifier = .always_tail }, formatType , .{ err , fmt , options , generator , allocator , max_depth });
381
+ return @call (.{ .modifier = .always_tail }, formatType , .{ err , fmt , options , generator , max_depth });
394
382
}
395
383
},
396
384
.ErrorSet = > {
@@ -403,7 +391,7 @@ pub fn formatType(
403
391
// }
404
392
generator .yield (@typeName (T ));
405
393
generator .yield ("." );
406
- return @call (.{ .modifier = .always_tail }, formatType , .{ @tagName (value ), "" , options , generator , allocator , max_depth });
394
+ return @call (.{ .modifier = .always_tail }, formatType , .{ @tagName (value ), "" , options , generator , max_depth });
407
395
},
408
396
.Union = > {
409
397
// if (comptime std.meta.trait.hasFn("format")(T)) {
@@ -420,14 +408,7 @@ pub fn formatType(
420
408
generator .yield (" = " );
421
409
inline for (info .fields ) | u_field | {
422
410
if (@enumToInt (@as (UnionTagType , value )) == u_field .enum_field .? .value ) {
423
- var frame = allocator .create (
424
- @TypeOf (async formatType (@field (value , u_field .name ), "" , options , generator , allocator , max_depth - 1 )),
425
- ) catch | err | switch (err ) {
426
- error .OutOfMemory = > return generator .yield (" ??OOM?? " ),
427
- };
428
- defer allocator .destroy (frame );
429
- frame .* = async formatType (@field (value , u_field .name ), "" , options , generator , allocator , max_depth - 1 );
430
- await frame .* ;
411
+ formatType (@field (value , u_field .name ), "" , options , generator , max_depth - 1 );
431
412
}
432
413
}
433
414
generator .yield (" }" );
@@ -455,14 +436,7 @@ pub fn formatType(
455
436
generator .yield (@memberName (T , field_i ));
456
437
generator .yield (" = " );
457
438
458
- var frame = allocator .create (
459
- @TypeOf (async formatType (@field (value , @memberName (T , field_i )), "" , options , generator , allocator , max_depth - 1 )),
460
- ) catch | err | switch (err ) {
461
- error .OutOfMemory = > return generator .yield (" ??OOM?? " ),
462
- };
463
- defer allocator .destroy (frame );
464
- frame .* = async formatType (@field (value , @memberName (T , field_i )), "" , options , generator , allocator , max_depth - 1 );
465
- await frame .* ;
439
+ formatType (@field (value , @memberName (T , field_i )), "" , options , generator , max_depth - 1 );
466
440
}
467
441
generator .yield (" }" );
468
442
},
@@ -475,7 +449,7 @@ pub fn formatType(
475
449
return formatPtr (T .Child , @ptrToInt (value ), generator );
476
450
},
477
451
builtin .TypeId .Enum , builtin .TypeId .Union , builtin .TypeId .Struct = > {
478
- return @call (.{ .modifier = .always_tail }, formatType , .{ value .* , fmt , options , generator , allocator , max_depth });
452
+ return @call (.{ .modifier = .always_tail }, formatType , .{ value .* , fmt , options , generator , max_depth });
479
453
},
480
454
else = > return formatPtr (T .Child , @ptrToInt (value ), generator ),
481
455
},
@@ -513,7 +487,7 @@ pub fn formatType(
513
487
.sentinel = null ,
514
488
},
515
489
});
516
- return @call (.{ .modifier = .always_tail }, formatType , .{ @as (Slice , & value ), fmt , options , generator , allocator , max_depth });
490
+ return @call (.{ .modifier = .always_tail }, formatType , .{ @as (Slice , & value ), fmt , options , generator , max_depth });
517
491
},
518
492
.Fn = > {
519
493
return formatPtr (T , @ptrToInt (value ), generator );
0 commit comments