Commit 4183c6f1a5

Andrew Kelley <superjoe30@gmail.com>
2017-12-24 04:08:53
move std/debug.zig to a subdirectory
self hosted compiler parser tests do some fuzz testing
1 parent 9dae796
src-self-hosted/parser.zig
@@ -1119,18 +1119,24 @@ fn testCanonical(source: []const u8) {
         break :x failing_allocator.index;
     };
 
-    // TODO make this pass
-    //var fail_index = needed_alloc_count;
-    //while (fail_index != 0) {
-    //    fail_index -= 1;
-    //    var fixed_allocator = mem.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
-    //    var failing_allocator = std.debug.FailingAllocator.init(&fixed_allocator.allocator, fail_index);
-    //    if (testParse(source, &failing_allocator.allocator)) |_| {
-    //        @panic("non-deterministic memory usage");
-    //    } else |err| {
-    //        assert(err == error.OutOfMemory);
-    //    }
-    //}
+    var fail_index: usize = 0;
+    while (fail_index < needed_alloc_count) : (fail_index += 1) {
+        var fixed_allocator = mem.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
+        var failing_allocator = std.debug.FailingAllocator.init(&fixed_allocator.allocator, fail_index);
+        if (testParse(source, &failing_allocator.allocator)) |_| {
+            @panic("non-deterministic memory usage");
+        } else |err| {
+            assert(err == error.OutOfMemory);
+            // TODO make this pass
+            //if (failing_allocator.allocated_bytes != failing_allocator.freed_bytes) {
+            //    warn("\nfail_index: {}/{}\nallocated bytes: {}\nfreed bytes: {}\nallocations: {}\ndeallocations: {}\n",
+            //        fail_index, needed_alloc_count,
+            //        failing_allocator.allocated_bytes, failing_allocator.freed_bytes,
+            //        failing_allocator.index, failing_allocator.deallocations);
+            //    @panic("memory leak detected");
+            //}
+        }
+    }
 }
 
 test "zig fmt" {
std/debug/failing_allocator.zig
@@ -0,0 +1,64 @@
+const std = @import("../index.zig");
+const mem = std.mem;
+
+/// Allocator that fails after N allocations, useful for making sure out of
+/// memory conditions are handled correctly.
+pub const FailingAllocator = struct {
+    allocator: mem.Allocator,
+    index: usize,
+    fail_index: usize,
+    internal_allocator: &mem.Allocator,
+    allocated_bytes: usize,
+    freed_bytes: usize,
+    deallocations: usize,
+
+    pub fn init(allocator: &mem.Allocator, fail_index: usize) -> FailingAllocator {
+        return FailingAllocator {
+            .internal_allocator = allocator,
+            .fail_index = fail_index,
+            .index = 0,
+            .allocated_bytes = 0,
+            .freed_bytes = 0,
+            .deallocations = 0,
+            .allocator = mem.Allocator {
+                .allocFn = alloc,
+                .reallocFn = realloc,
+                .freeFn = free,
+            },
+        };
+    }
+
+    fn alloc(allocator: &mem.Allocator, n: usize, alignment: u29) -> %[]u8 {
+        const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
+        if (self.index == self.fail_index) {
+            return error.OutOfMemory;
+        }
+        const result = %return self.internal_allocator.allocFn(self.internal_allocator, n, alignment);
+        self.allocated_bytes += result.len;
+        self.index += 1;
+        return result;
+    }
+
+    fn realloc(allocator: &mem.Allocator, old_mem: []u8, new_size: usize, alignment: u29) -> %[]u8 {
+        const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
+        if (new_size <= old_mem.len) {
+            self.freed_bytes += old_mem.len - new_size;
+            return self.internal_allocator.reallocFn(self.internal_allocator, old_mem, new_size, alignment);
+        }
+        if (self.index == self.fail_index) {
+            return error.OutOfMemory;
+        }
+        const result = %return self.internal_allocator.reallocFn(self.internal_allocator, old_mem, new_size, alignment);
+        self.allocated_bytes += new_size - old_mem.len;
+        self.deallocations += 1;
+        self.index += 1;
+        return result;
+    }
+
+    fn free(allocator: &mem.Allocator, bytes: []u8) {
+        const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
+        self.freed_bytes += bytes.len;
+        self.deallocations += 1;
+        return self.internal_allocator.freeFn(self.internal_allocator, bytes);
+    }
+};
std/debug.zig → std/debug/index.zig
@@ -1,10 +1,10 @@
-const std = @import("index.zig");
+const std = @import("../index.zig");
 const math = std.math;
 const mem = std.mem;
 const io = std.io;
 const os = std.os;
-const elf = @import("elf.zig");
-const DW = @import("dwarf.zig");
+const elf = std.elf;
+const DW = std.dwarf;
 const ArrayList = std.ArrayList;
 const builtin = @import("builtin");
 
@@ -992,59 +992,3 @@ fn readILeb128(in_stream: &io.InStream) -> %i64 {
 pub const global_allocator = &global_fixed_allocator.allocator;
 var global_fixed_allocator = mem.FixedBufferAllocator.init(global_allocator_mem[0..]);
 var global_allocator_mem: [100 * 1024]u8 = undefined;
-
-/// Allocator that fails after N allocations, useful for making sure out of
-/// memory conditions are handled correctly.
-pub const FailingAllocator = struct {
-    allocator: mem.Allocator,
-    index: usize,
-    fail_index: usize,
-    internal_allocator: &mem.Allocator,
-    allocated_bytes: usize,
-
-    pub fn init(allocator: &mem.Allocator, fail_index: usize) -> FailingAllocator {
-        return FailingAllocator {
-            .internal_allocator = allocator,
-            .fail_index = fail_index,
-            .index = 0,
-            .allocated_bytes = 0,
-            .allocator = mem.Allocator {
-                .allocFn = alloc,
-                .reallocFn = realloc,
-                .freeFn = free,
-            },
-        };
-    }
-
-    fn alloc(allocator: &mem.Allocator, n: usize, alignment: u29) -> %[]u8 {
-        const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
-        if (self.index == self.fail_index) {
-            return error.OutOfMemory;
-        }
-        self.index += 1;
-        const result = %return self.internal_allocator.allocFn(self.internal_allocator, n, alignment);
-        self.allocated_bytes += result.len;
-        return result;
-    }
-
-    fn realloc(allocator: &mem.Allocator, old_mem: []u8, new_size: usize, alignment: u29) -> %[]u8 {
-        const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
-        if (new_size <= old_mem.len) {
-            self.allocated_bytes -= old_mem.len - new_size;
-            return self.internal_allocator.reallocFn(self.internal_allocator, old_mem, new_size, alignment);
-        }
-        if (self.index == self.fail_index) {
-            return error.OutOfMemory;
-        }
-        self.index += 1;
-        const result = %return self.internal_allocator.reallocFn(self.internal_allocator, old_mem, new_size, alignment);
-        self.allocated_bytes += new_size - old_mem.len;
-        return result;
-    }
-
-    fn free(allocator: &mem.Allocator, bytes: []u8) {
-        const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
-        self.allocated_bytes -= bytes.len;
-        return self.internal_allocator.freeFn(self.internal_allocator, bytes);
-    }
-};
std/fmt/errol/index.zig
@@ -1,10 +1,11 @@
+const std = @import("../../index.zig");
 const enum3 = @import("enum3.zig").enum3;
 const enum3_data = @import("enum3.zig").enum3_data;
 const lookup_table = @import("lookup.zig").lookup_table;
 const HP = @import("lookup.zig").HP;
-const math = @import("../../math/index.zig");
-const mem = @import("../../mem.zig");
-const assert = @import("../../debug.zig").assert;
+const math = std.math;
+const mem = std.mem;
+const assert = std.debug.assert;
 
 pub const FloatDecimal = struct {
     digits: []u8,
std/fmt/index.zig
@@ -1,7 +1,8 @@
-const math = @import("../math/index.zig");
-const debug = @import("../debug.zig");
+const std = @import("../index.zig");
+const math = std.math;
+const debug = std.debug;
 const assert = debug.assert;
-const mem = @import("../mem.zig");
+const mem = std.mem;
 const builtin = @import("builtin");
 const errol3 = @import("errol/index.zig").errol3;
 
std/math/acos.zig
@@ -2,8 +2,9 @@
 //
 // - acos(x)   = nan if x < -1 or x > 1
 
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn acos(x: var) -> @typeOf(x) {
     const T = @typeOf(x);
std/math/acosh.zig
@@ -4,8 +4,9 @@
 // - acosh(nan) = nan
 
 const builtin = @import("builtin");
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn acosh(x: var) -> @typeOf(x) {
     const T = @typeOf(x);
std/math/asin.zig
@@ -3,8 +3,9 @@
 // - asin(+-0) = +-0
 // - asin(x)   = nan if x < -1 or x > 1
 
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn asin(x: var) -> @typeOf(x) {
     const T = @typeOf(x);
std/math/asinh.zig
@@ -4,8 +4,9 @@
 // - asinh(+-inf) = +-inf
 // - asinh(nan)   = nan
 
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn asinh(x: var) -> @typeOf(x) {
     const T = @typeOf(x);
std/math/atan.zig
@@ -3,8 +3,9 @@
 // - atan(+-0)   = +-0
 // - atan(+-inf) = +-pi/2
 
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn atan(x: var) -> @typeOf(x) {
     const T = @typeOf(x);
std/math/atan2.zig
@@ -18,8 +18,9 @@
 //  atan2(+inf, x)    = +pi/2
 //  atan2(-inf, x)    = -pi/2
 
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 fn atan2(comptime T: type, x: T, y: T) -> T {
     return switch (T) {
std/math/atanh.zig
@@ -4,8 +4,9 @@
 // - atanh(x)   = nan if |x| > 1 with signal
 // - atanh(nan) = nan
 
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn atanh(x: var) -> @typeOf(x) {
     const T = @typeOf(x);
std/math/cbrt.zig
@@ -4,8 +4,9 @@
 // - cbrt(+-inf) = +-inf
 // - cbrt(nan)   = nan
 
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn cbrt(x: var) -> @typeOf(x) {
     const T = @typeOf(x);
std/math/ceil.zig
@@ -5,8 +5,9 @@
 // - ceil(nan)   = nan
 
 const builtin = @import("builtin");
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn ceil(x: var) -> @typeOf(x) {
     const T = @typeOf(x);
std/math/copysign.zig
@@ -1,5 +1,6 @@
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn copysign(comptime T: type, x: T, y: T) -> T {
     return switch (T) {
std/math/cos.zig
@@ -4,8 +4,9 @@
 // - cos(nan)   = nan
 
 const builtin = @import("builtin");
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn cos(x: var) -> @typeOf(x) {
     const T = @typeOf(x);
std/math/cosh.zig
@@ -5,9 +5,10 @@
 // - cosh(nan)   = nan
 
 const builtin = @import("builtin");
-const math = @import("index.zig");
+const std = @import("../index.zig");
+const math = std.math;
 const expo2 = @import("expo2.zig").expo2;
-const assert = @import("../debug.zig").assert;
+const assert = std.debug.assert;
 
 pub fn cosh(x: var) -> @typeOf(x) {
     const T = @typeOf(x);
std/math/exp.zig
@@ -3,8 +3,9 @@
 // - exp(+inf) = +inf
 // - exp(nan)  = nan
 
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn exp(x: var) -> @typeOf(x) {
     const T = @typeOf(x);
std/math/exp2.zig
@@ -3,8 +3,9 @@
 // - exp2(+inf) = +inf
 // - exp2(nan)  = nan
 
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn exp2(x: var) -> @typeOf(x) {
     const T = @typeOf(x);
std/math/expm1.zig
@@ -4,8 +4,9 @@
 // - expm1(-inf) = -1
 // - expm1(nan)  = nan
 
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn expm1(x: var) -> @typeOf(x) {
     const T = @typeOf(x);
std/math/fabs.zig
@@ -3,8 +3,9 @@
 // - fabs(+-inf) = +inf
 // - fabs(nan)   = nan
 
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn fabs(x: var) -> @typeOf(x) {
     const T = @typeOf(x);
std/math/floor.zig
@@ -5,8 +5,9 @@
 // - floor(nan)   = nan
 
 const builtin = @import("builtin");
-const assert = @import("../debug.zig").assert;
-const math = @import("index.zig");
+const assert = std.debug.assert;
+const std = @import("../index.zig");
+const math = std.math;
 
 pub fn floor(x: var) -> @typeOf(x) {
     const T = @typeOf(x);
std/math/fma.zig
@@ -1,5 +1,6 @@
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn fma(comptime T: type, x: T, y: T, z: T) -> T {
     return switch (T) {
std/math/frexp.zig
@@ -4,8 +4,9 @@
 // - frexp(+-inf) = +-inf, 0
 // - frexp(nan)   = nan, undefined
 
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 fn frexp_result(comptime T: type) -> type {
     return struct {
std/math/hypot.zig
@@ -5,8 +5,9 @@
 // - hypot(nan, y)    = nan
 // - hypot(x, nan)    = nan
 
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn hypot(comptime T: type, x: T, y: T) -> T {
     return switch (T) {
std/math/ilogb.zig
@@ -4,8 +4,9 @@
 // - ilogb(0)     = @maxValue(i32)
 // - ilogb(nan)   = @maxValue(i32)
 
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn ilogb(x: var) -> i32 {
     const T = @typeOf(x);
std/math/index.zig
@@ -1,6 +1,7 @@
 const builtin = @import("builtin");
+const std = @import("../index.zig");
 const TypeId = builtin.TypeId;
-const assert = @import("../debug.zig").assert;
+const assert = std.debug.assert;
 
 pub const e = 2.71828182845904523536028747135266249775724709369995;
 pub const pi = 3.14159265358979323846264338327950288419716939937510;
std/math/inf.zig
@@ -1,5 +1,6 @@
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn inf(comptime T: type) -> T {
     return switch (T) {
std/math/isfinite.zig
@@ -1,5 +1,6 @@
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn isFinite(x: var) -> bool {
     const T = @typeOf(x);
std/math/isinf.zig
@@ -1,5 +1,6 @@
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn isInf(x: var) -> bool {
     const T = @typeOf(x);
std/math/isnan.zig
@@ -1,5 +1,6 @@
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn isNan(x: var) -> bool {
     const T = @typeOf(x);
std/math/isnormal.zig
@@ -1,5 +1,6 @@
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn isNormal(x: var) -> bool {
     const T = @typeOf(x);
std/math/ln.zig
@@ -5,8 +5,9 @@
 // - ln(x)     = nan if x < 0
 // - ln(nan)   = nan
 
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 const builtin = @import("builtin");
 const TypeId = builtin.TypeId;
 
std/math/log.zig
@@ -1,7 +1,8 @@
-const math = @import("index.zig");
+const std = @import("../index.zig");
+const math = std.math;
 const builtin = @import("builtin");
 const TypeId = builtin.TypeId;
-const assert = @import("../debug.zig").assert;
+const assert = std.debug.assert;
 
 pub fn log(comptime T: type, base: T, x: T) -> T {
     if (base == 2) {
std/math/log10.zig
@@ -5,8 +5,9 @@
 // - log10(x)     = nan if x < 0
 // - log10(nan)   = nan
 
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 const builtin = @import("builtin");
 const TypeId = builtin.TypeId;
 
std/math/log1p.zig
@@ -6,8 +6,9 @@
 // - log1p(x)     = nan if x < -1
 // - log1p(nan)   = nan
 
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn log1p(x: var) -> @typeOf(x) {
     const T = @typeOf(x);
std/math/log2.zig
@@ -5,8 +5,9 @@
 // - log2(x)     = nan if x < 0
 // - log2(nan)   = nan
 
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 const builtin = @import("builtin");
 const TypeId = builtin.TypeId;
 
std/math/modf.zig
@@ -3,8 +3,9 @@
 // - modf(+-inf) = +-inf, nan
 // - modf(nan)   = nan, nan
 
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 fn modf_result(comptime T: type) -> type {
     return struct {
std/math/pow.zig
@@ -22,8 +22,9 @@
 //  pow(x, y)      = nan for finite x < 0 and finite non-integer y
 
 const builtin = @import("builtin");
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 // This implementation is taken from the go stlib, musl is a bit more complex.
 pub fn pow(comptime T: type, x: T, y: T) -> T {
std/math/round.zig
@@ -5,8 +5,9 @@
 // - round(nan)   = nan
 
 const builtin = @import("builtin");
-const assert = @import("../debug.zig").assert;
-const math = @import("index.zig");
+const assert = std.debug.assert;
+const std = @import("../index.zig");
+const math = std.math;
 
 pub fn round(x: var) -> @typeOf(x) {
     const T = @typeOf(x);
std/math/scalbn.zig
@@ -1,5 +1,6 @@
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn scalbn(x: var, n: i32) -> @typeOf(x) {
     const T = @typeOf(x);
std/math/signbit.zig
@@ -1,5 +1,6 @@
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn signbit(x: var) -> bool {
     const T = @typeOf(x);
std/math/sin.zig
@@ -5,8 +5,9 @@
 // - sin(nan)   = nan
 
 const builtin = @import("builtin");
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn sin(x: var) -> @typeOf(x) {
     const T = @typeOf(x);
std/math/sinh.zig
@@ -5,8 +5,9 @@
 // - sinh(nan)   = nan
 
 const builtin = @import("builtin");
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 const expo2 = @import("expo2.zig").expo2;
 
 pub fn sinh(x: var) -> @typeOf(x) {
std/math/sqrt.zig
@@ -5,8 +5,9 @@
 // - sqrt(x)     = nan if x < 0
 // - sqrt(nan)   = nan
 
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 const builtin = @import("builtin");
 const TypeId = builtin.TypeId;
 
std/math/tan.zig
@@ -5,8 +5,9 @@
 // - tan(nan)   = nan
 
 const builtin = @import("builtin");
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn tan(x: var) -> @typeOf(x) {
     const T = @typeOf(x);
std/math/tanh.zig
@@ -5,8 +5,9 @@
 // - sinh(nan)   = nan
 
 const builtin = @import("builtin");
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 const expo2 = @import("expo2.zig").expo2;
 
 pub fn tanh(x: var) -> @typeOf(x) {
std/math/trunc.zig
@@ -4,8 +4,9 @@
 // - trunc(+-inf) = +-inf
 // - trunc(nan)   = nan
 
-const math = @import("index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const math = std.math;
+const assert = std.debug.assert;
 
 pub fn trunc(x: var) -> @typeOf(x) {
     const T = @typeOf(x);
std/os/darwin.zig
@@ -1,5 +1,6 @@
-const c = @import("../c/index.zig");
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const c = std.c;
+const assert = std.debug.assert;
 
 pub use @import("darwin_errno.zig");
 
std/os/index.zig
@@ -1,3 +1,4 @@
+const std = @import("../index.zig");
 const builtin = @import("builtin");
 const Os = builtin.Os;
 const is_windows = builtin.os == Os.windows;
@@ -37,22 +38,22 @@ pub const createWindowsEnvBlock = windows_util.createWindowsEnvBlock;
 
 pub const FileHandle = if (is_windows) windows.HANDLE else i32;
 
-const debug = @import("../debug.zig");
+const debug = std.debug;
 const assert = debug.assert;
 
-const c = @import("../c/index.zig");
+const c = std.c;
 
-const mem = @import("../mem.zig");
+const mem = std.mem;
 const Allocator = mem.Allocator;
 
-const BufMap = @import("../buf_map.zig").BufMap;
-const cstr = @import("../cstr.zig");
+const BufMap = std.BufMap;
+const cstr = std.cstr;
 
-const io = @import("../io.zig");
-const base64 = @import("../base64.zig");
-const ArrayList = @import("../array_list.zig").ArrayList;
-const Buffer = @import("../buffer.zig").Buffer;
-const math = @import("../index.zig").math;
+const io = std.io;
+const base64 = std.base64;
+const ArrayList = std.ArrayList;
+const Buffer = std.Buffer;
+const math = std.math;
 
 error SystemResources;
 error AccessDenied;
std/os/linux.zig
@@ -1,4 +1,5 @@
-const assert = @import("../debug.zig").assert;
+const std = @import("../index.zig");
+const assert = std.debug.assert;
 const builtin = @import("builtin");
 const arch = switch (builtin.arch) {
     builtin.Arch.x86_64 => @import("linux_x86_64.zig"),
std/os/path.zig
@@ -1,16 +1,16 @@
+const std = @import("../index.zig");
 const builtin = @import("builtin");
 const Os = builtin.Os;
-const debug = @import("../debug.zig");
+const debug = std.debug;
 const assert = debug.assert;
-const mem = @import("../mem.zig");
-const fmt = @import("../fmt/index.zig");
+const mem = std.mem;
+const fmt = std.fmt;
 const Allocator = mem.Allocator;
-const os = @import("index.zig");
-const math = @import("../math/index.zig");
+const os = std.os;
+const math = std.math;
 const posix = os.posix;
 const windows = os.windows;
-const c = @import("../c/index.zig");
-const cstr = @import("../cstr.zig");
+const cstr = std.cstr;
 
 pub const sep_windows = '\\';
 pub const sep_posix = '/';
std/special/compiler_rt/fixunsdfdi_test.zig
@@ -1,5 +1,5 @@
 const __fixunsdfdi = @import("fixunsdfdi.zig").__fixunsdfdi;
-const assert = @import("../../debug.zig").assert;
+const assert = @import("../../index.zig").debug.assert;
 
 fn test__fixunsdfdi(a: f64, expected: u64) {
     const x = __fixunsdfdi(a);
std/special/compiler_rt/fixunsdfsi_test.zig
@@ -1,5 +1,5 @@
 const __fixunsdfsi = @import("fixunsdfsi.zig").__fixunsdfsi;
-const assert = @import("../../debug.zig").assert;
+const assert = @import("../../index.zig").debug.assert;
 
 fn test__fixunsdfsi(a: f64, expected: u32) {
     const x = __fixunsdfsi(a);
std/special/compiler_rt/fixunsdfti_test.zig
@@ -1,5 +1,5 @@
 const __fixunsdfti = @import("fixunsdfti.zig").__fixunsdfti;
-const assert = @import("../../debug.zig").assert;
+const assert = @import("../../index.zig").debug.assert;
 
 fn test__fixunsdfti(a: f64, expected: u128) {
     const x = __fixunsdfti(a);
std/special/compiler_rt/fixunssfdi_test.zig
@@ -1,5 +1,5 @@
 const __fixunssfdi = @import("fixunssfdi.zig").__fixunssfdi;
-const assert = @import("../../debug.zig").assert;
+const assert = @import("../../index.zig").debug.assert;
 
 fn test__fixunssfdi(a: f32, expected: u64) {
     const x = __fixunssfdi(a);
std/special/compiler_rt/fixunssfsi_test.zig
@@ -1,5 +1,5 @@
 const __fixunssfsi = @import("fixunssfsi.zig").__fixunssfsi;
-const assert = @import("../../debug.zig").assert;
+const assert = @import("../../index.zig").debug.assert;
 
 fn test__fixunssfsi(a: f32, expected: u32) {
     const x = __fixunssfsi(a);
std/special/compiler_rt/fixunssfti_test.zig
@@ -1,5 +1,5 @@
 const __fixunssfti = @import("fixunssfti.zig").__fixunssfti;
-const assert = @import("../../debug.zig").assert;
+const assert = @import("../../index.zig").debug.assert;
 
 fn test__fixunssfti(a: f32, expected: u128) {
     const x = __fixunssfti(a);
std/special/compiler_rt/fixunstfdi_test.zig
@@ -1,5 +1,5 @@
 const __fixunstfdi = @import("fixunstfdi.zig").__fixunstfdi;
-const assert = @import("../../debug.zig").assert;
+const assert = @import("../../index.zig").debug.assert;
 
 fn test__fixunstfdi(a: f128, expected: u64) {
     const x = __fixunstfdi(a);
std/special/compiler_rt/fixunstfsi_test.zig
@@ -1,5 +1,5 @@
 const __fixunstfsi = @import("fixunstfsi.zig").__fixunstfsi;
-const assert = @import("../../debug.zig").assert;
+const assert = @import("../../index.zig").debug.assert;
 
 fn test__fixunstfsi(a: f128, expected: u32) {
     const x = __fixunstfsi(a);
std/special/compiler_rt/fixunstfti_test.zig
@@ -1,5 +1,5 @@
 const __fixunstfti = @import("fixunstfti.zig").__fixunstfti;
-const assert = @import("../../debug.zig").assert;
+const assert = @import("../../index.zig").debug.assert;
 
 fn test__fixunstfti(a: f128, expected: u128) {
     const x = __fixunstfti(a);
std/special/compiler_rt/index.zig
@@ -68,7 +68,7 @@ comptime {
     }
 }
 
-const assert = @import("../../debug.zig").assert;
+const assert = @import("../../index.zig").debug.assert;
 
 const __udivmoddi4 = @import("udivmoddi4.zig").__udivmoddi4;
 
std/array_list.zig
@@ -1,6 +1,7 @@
-const debug = @import("debug.zig");
+const std = @import("index.zig");
+const debug = std.debug;
 const assert = debug.assert;
-const mem = @import("mem.zig");
+const mem = std.mem;
 const Allocator = mem.Allocator;
 
 pub fn ArrayList(comptime T: type) -> type {
std/base64.zig
@@ -1,5 +1,6 @@
-const assert = @import("debug.zig").assert;
-const mem = @import("mem.zig");
+const std = @import("index.zig");
+const assert = std.debug.assert;
+const mem = std.mem;
 
 pub const standard_alphabet_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 pub const standard_pad_char = '=';
std/buffer.zig
@@ -1,10 +1,11 @@
-const debug = @import("debug.zig");
-const mem = @import("mem.zig");
+const std = @import("index.zig");
+const debug = std.debug;
+const mem = std.mem;
 const Allocator = mem.Allocator;
 const assert = debug.assert;
-const ArrayList = @import("array_list.zig").ArrayList;
+const ArrayList = std.ArrayList;
 
-const fmt = @import("fmt/index.zig");
+const fmt = std.fmt;
 
 /// A buffer that allocates memory and maintains a null byte at the end.
 pub const Buffer = struct {
std/cstr.zig
@@ -1,5 +1,6 @@
-const debug = @import("debug.zig");
-const mem = @import("mem.zig");
+const std = @import("index.zig");
+const debug = std.debug;
+const mem = std.mem;
 const assert = debug.assert;
 
 pub fn len(ptr: &const u8) -> usize {
std/hash_map.zig
@@ -1,7 +1,8 @@
-const debug = @import("debug.zig");
+const std = @import("index.zig");
+const debug = std.debug;
 const assert = debug.assert;
-const math = @import("math/index.zig");
-const mem = @import("mem.zig");
+const math = std.math;
+const mem = std.mem;
 const Allocator = mem.Allocator;
 const builtin = @import("builtin");
 
std/heap.zig
@@ -1,10 +1,11 @@
-const debug = @import("debug.zig");
+const std = @import("index.zig");
+const debug = std.debug;
 const assert = debug.assert;
-const mem = @import("mem.zig");
-const os = @import("os/index.zig");
+const mem = std.mem;
+const os = std.os;
 const builtin = @import("builtin");
 const Os = builtin.Os;
-const c = @import("c/index.zig");
+const c = std.c;
 
 const Allocator = mem.Allocator;
 
std/index.zig
@@ -11,7 +11,7 @@ pub const base64 = @import("base64.zig");
 pub const build = @import("build.zig");
 pub const c = @import("c/index.zig");
 pub const cstr = @import("cstr.zig");
-pub const debug = @import("debug.zig");
+pub const debug = @import("debug/index.zig");
 pub const dwarf = @import("dwarf.zig");
 pub const elf = @import("elf.zig");
 pub const empty_import = @import("empty.zig");
@@ -39,7 +39,7 @@ test "std" {
     _ = @import("build.zig");
     _ = @import("c/index.zig");
     _ = @import("cstr.zig");
-    _ = @import("debug.zig");
+    _ = @import("debug/index.zig");
     _ = @import("dwarf.zig");
     _ = @import("elf.zig");
     _ = @import("empty.zig");
std/linked_list.zig
@@ -1,6 +1,7 @@
-const debug = @import("debug.zig");
+const std = @import("index.zig");
+const debug = std.debug;
 const assert = debug.assert;
-const mem = @import("mem.zig");
+const mem = std.mem;
 const Allocator = mem.Allocator;
 
 /// Generic doubly linked list.
std/mem.zig
@@ -1,6 +1,7 @@
-const debug = @import("debug.zig");
+const std = @import("index.zig");
+const debug = std.debug;
 const assert = debug.assert;
-const math = @import("math/index.zig");
+const math = std.math;
 const builtin = @import("builtin");
 
 error OutOfMemory;
std/net.zig
@@ -1,6 +1,7 @@
-const linux = @import("os/linux.zig");
-const assert = @import("debug.zig").assert;
-const endian = @import("endian.zig");
+const std = @import("index.zig");
+const linux = std.os.linux;
+const assert = std.debug.assert;
+const endian = std.endian;
 
 error SigInterrupt;
 error Io;
std/rand.zig
@@ -1,8 +1,9 @@
+const std = @import("index.zig");
 const builtin = @import("builtin");
-const assert = @import("debug.zig").assert;
+const assert = std.debug.assert;
 const rand_test = @import("rand_test.zig");
-const mem = @import("mem.zig");
-const math = @import("math/index.zig");
+const mem = std.mem;
+const math = std.math;
 
 pub const MT19937_32 = MersenneTwister(
     u32, 624, 397, 31,
build.zig
@@ -172,7 +172,8 @@ pub fn installStdLib(b: &Builder) {
         "c/linux.zig",
         "c/windows.zig",
         "cstr.zig",
-        "debug.zig",
+        "debug/failing_allocator.zig",
+        "debug/index.zig",
         "dwarf.zig",
         "elf.zig",
         "empty.zig",
CMakeLists.txt
@@ -516,7 +516,8 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/c/index.zig" DESTINATION "${ZIG_STD_DEST}
 install(FILES "${CMAKE_SOURCE_DIR}/std/c/linux.zig" DESTINATION "${ZIG_STD_DEST}/c")
 install(FILES "${CMAKE_SOURCE_DIR}/std/c/windows.zig" DESTINATION "${ZIG_STD_DEST}/c")
 install(FILES "${CMAKE_SOURCE_DIR}/std/cstr.zig" DESTINATION "${ZIG_STD_DEST}")
-install(FILES "${CMAKE_SOURCE_DIR}/std/debug.zig" DESTINATION "${ZIG_STD_DEST}")
+install(FILES "${CMAKE_SOURCE_DIR}/std/debug/index.zig" DESTINATION "${ZIG_STD_DEST}/debug")
+install(FILES "${CMAKE_SOURCE_DIR}/std/debug/failing_allocator.zig" DESTINATION "${ZIG_STD_DEST}/debug")
 install(FILES "${CMAKE_SOURCE_DIR}/std/dwarf.zig" DESTINATION "${ZIG_STD_DEST}")
 install(FILES "${CMAKE_SOURCE_DIR}/std/elf.zig" DESTINATION "${ZIG_STD_DEST}")
 install(FILES "${CMAKE_SOURCE_DIR}/std/empty.zig" DESTINATION "${ZIG_STD_DEST}")