Commit graph

14668 commits

Author SHA1 Message Date
Matthew Lugg
b69bfe0c08
std: update for language changes
Now that zig1.wasm is updated, apply the matching standard library
changes.

The main one is in `std.builtin.Type`, where `alignment` fields now have
type `?usize` rather than `comptime_int`.

Additionally, we need to add explicit backing integers to some packed
unions, because (due to https://github.com/ziglang/zig/issues/24714)
they need explicit backing integers to be used in `extern` contexts.
This change could not happen before now, because prior to this branch,
packed unions did not allow explicit backing integer types (that is,
this branch implemented https://github.com/ziglang/zig/issues/25350).
2026-03-06 17:02:49 +00:00
Matthew Lugg
bf46ed08ff
cbe: fix type layouts, and statically assert them 2026-03-06 16:38:10 +00:00
Matthew Lugg
7561f1e004
compiler,std: various little fixes 2026-03-06 16:38:10 +00:00
Matthew Lugg
3ffa8d83a3
compiler: various lil' fixes 2026-03-06 16:38:10 +00:00
Matthew Lugg
1ccada0452
compiler: stop LLVM bossing the frontend around
I previously wrote some weird code in the compiler frontend solely
because the LLVM backend has some weird requirements, but the better
solution is to avoid those requirements. This commit does that by
introducing "alignment forward references" to `std.zig.llvm.Builder`.
Much like debug forward references, they allow you to reference an
alignment value which will be populated at a later time (and which can
be updated many times, which is important for incremental compilation).
Then, when we want to reference a type's ABI alignment while the type is
not necessarily resolved (required for `@"align"` attributes on function
parameters and function call arguments), we create a forward reference
and use `link.ConstPool` to populate it when ready.

This allows us to remove from the compiler frontend some extremely
arbitrary calls to `Sema.ensureLayoutResolved`, so that the language
specification is not being built around the particular needs of our
compiler implementation's LLVM code generation backend.
2026-03-06 16:38:09 +00:00
Matthew Lugg
111286565b
get the compiler building
The change in codegen/x86_64/CodeGen.zig was not strictly necessary (the
Sema change I did solves the error I was getting there), I just think
it's better style anyway.
2026-03-06 16:38:09 +00:00
Matthew Lugg
9f596f9dfd
aro: stop depending on ArrayList default field values 2026-03-06 16:38:09 +00:00
Matthew Lugg
319548c772
cbe: rework CType and other major refactors
The goal of these changes is to allow the C backend to support the new
lazier type resolution system implemented by the frontend. This required
a full rewrite of the `CType` abstraction, and major changes to the C
backend "linker".

The `DebugConstPool` abstraction introduced in a previous commit turns
out to be useful for the C backend to codegen types. Because this use
case is not debug information but rather general linking (albeit when
targeting an unusual object format), I have renamed the abstraction to
`ConstPool`. With it, the C linker is told when a type's layout becomes
known, and can at that point generate the corresponding C definitions,
rather than deferring this work until `flush`.

The work done in `flush` is now more-or-less *solely* focused on
collecting all of the buffers into a big array for a vectored write.
This does unfortunately involve a non-trivial graph traversal to emit
type definitions in an appropriate order, but it's still quite fast in
practice, and it operates on fairly compact dependency data. We don't
generate the actual type *definitions* in `flush`; that happens during
compilation using `ConstPool` as discussed above. (We do generate the
typedefs for underaligned types in `flush`, but that's a trivial amount
of work in most cases.)

`CType` is now an ephemeral type: it is created only when we render a
type (the logic for which has been pushed into just 2 or 3 functions in
`codegen.c`---most of the backend now operates on unmolested Zig `Type`s
instead). C types are no longer stored in a "pool", although the type
"dependencies" of generated C code (that is, the struct, unions, and
typedefs which the generated code references) are tracked (in some
simple hash sets) and given to the linker so it can codegen the types.
2026-03-06 16:38:08 +00:00
Matthew Lugg
e88727390f
llvm: some more improvements to debug info
Most importantly, adds support for `DW_TAG_typedef` to `llvm.Builder`,
and uses it to define error sets and optional pointers/errors.

Also deletes some random dead code I found.
2026-03-06 12:36:26 +00:00
Matthew Lugg
738a3d3360
compiler: get everything building
Several backends are crashing right now. I'll need to fix at least the C
backend before this branch is ready to PR.
2026-03-06 12:36:26 +00:00
Matthew Lugg
a249aec54d
compiler: fix up LLVM backend, and improve its debug info
The LLVM backend can now run the behavior tests and standard library
tests, like the x86_64 backend can. This commit required me to make a
lot of changes to how the LLVM backend lowers debug information, and
while I was doing that, I improved a few things:

* `anyerror` is now an enum type (and other error sets just wrap it), so
  error values appear by name in debuggers

* Fixed broken lowering for tagged unions with zero-width payloads

* Associate container types with source locations in all cases

* Avoid depending on the order of type resolution (using the new
  `DebugConstPool` abstraction), so debug information will contain all
  available type information rather than just the subset which happens
  to be resolved when the backend lowers that debug type
2026-03-06 12:36:26 +00:00
Matthew Lugg
dfc916753c
compiler: make dependency loop errors good 2026-03-06 12:36:23 +00:00
Matthew Lugg
bf39ead469
compiler: small misc cleanups 2026-03-06 12:36:23 +00:00
Matthew Lugg
73e24c9b37
std: remove default values from ArrayList
These were deprecated in the 0.14.0 release cycle almost a year ago, so
they can definitely be deleted now.
2026-03-06 12:36:22 +00:00
Matthew Lugg
3e21e6777e
type resolution progress 2026-03-06 12:36:21 +00:00
Matthew Lugg
372b3c33c1
std: work around language changes 2026-03-06 12:36:21 +00:00
Matthew Lugg
eae122b3aa
type resolution progress 2026-03-06 12:36:21 +00:00
Matthew Lugg
3502ddb1c7
Zir: rework container type declarations
Only AstGen and print_zir currently support the new representation, so
attempting to build the compiler will emit (many) compile errors.
2026-03-06 12:36:21 +00:00
breakmit
46658257f4 Io.Threaded.spawnPosix: implement passing file descriptors as stdio (#31379)
`std.process.spawn`: remove the TODO for nonblocking file stdio and document the behavior.

Fix a bug in Io.Uring.dup2 where the function does not return on success

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31379
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
Co-authored-by: breakmit <breakmit@noreply.codeberg.org>
Co-committed-by: breakmit <breakmit@noreply.codeberg.org>
2026-03-06 04:51:28 +01:00
Khitiara
30c8a75997 Fix #20283 2026-03-06 04:34:22 +01:00
Nathan Michaels
aa2f318935 std.mem.ReverseIterator: make public 2026-03-05 21:07:36 +01:00
Koko Bhadra
4fa465fc8f compiler_rt: optimize udivmod large-divisor case with trial quotient
Replace the O(n) shift-subtract loop with a constant-time trial
quotient approach (Knuth Algorithm D, TAOCP Vol 2 Section 4.3.1).

The old code iterates clz(b_hi)-clz(a_hi)+1 times (up to 64
iterations of 128-bit arithmetic). The new code uses a single
divwide call to get a trial quotient, then verifies with two
native-width widening multiplies.

Benchmark (Apple M1, ReleaseFast):
- Large divisor, large shift: 87ns -> 7.5ns (11.5x faster)
- Small divisor / uniform: unchanged
2026-03-05 20:22:19 +01:00
xeondev
15f0af09d0 Io.Threaded.batchAwaitConcurrent: fix poll args 2026-03-05 20:17:21 +01:00
Alex Rønne Petersen
b778826df5
std.crypto.argon2: disable kdf test
https://codeberg.org/ziglang/zig/issues/31402
2026-03-05 18:49:31 +01:00
Alex Rønne Petersen
0ae1c6b54a
std.Io.RwLock: disable smoke test on aarch64-netbsd
https://codeberg.org/ziglang/zig/issues/31393
2026-03-04 12:14:11 +01:00
Alex Rønne Petersen
ce32003625
std.Io.net.test: disable listen on a port, send bytes, receive bytes
https://codeberg.org/ziglang/zig/issues/31388
2026-03-04 01:21:08 +01:00
Robby Zambito
d2db1d45f1 fix: inline to propagate comptime-ness to call site
See conversation at: https://ziggit.dev/t/why-is-the-comptime-keyword-needed-in-this-inline-for-loop-condition/14090
2026-03-03 21:10:30 +01:00
Andrew Kelley
2d88a5a103 Merge pull request 'Another dll dependency bites the dust (advapi32.dll)' (#31384) from squeek502/zig:delete-advapi32 into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31384
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-03-03 18:26:12 +01:00
Ryan Liptak
fdde8e6394 Delete advapi32 bindings 2026-03-03 01:12:48 -08:00
Ryan Liptak
70058471e3 WindowsSdk: Use ntdll instead of advapi32 APIs for registry querying
The dependency on advapi32.dll actually silently brings along 3 other dlls at runtime (msvcrt.dll, sechost.dll, bcrypt.dll), even if no advapi32 APIs are called. So, this commit actually reduces the number of dlls loaded at runtime by 4 (but only when LLVM is not linked, since LLVM has its own dependency on advapi32.dll).

The data is not super conclusive, but the ntdll version of WindowsSdk appears to run slightly faster than the previous advapi32 version:

Benchmark 1: libc-ntdll.exe ..
  Time (mean ± σ):       6.0 ms ±   0.6 ms    [User: 3.9 ms, System: 7.1 ms]
  Range (min … max):     4.8 ms …   7.9 ms    112 runs

Benchmark 2: libc-advapi32.exe ..
  Time (mean ± σ):       7.2 ms ±   0.5 ms    [User: 5.4 ms, System: 9.2 ms]
  Range (min … max):     6.1 ms …   8.9 ms    103 runs

Summary
  'libc-ntdll.exe ..' ran
    1.21 ± 0.15 times faster than 'libc-advapi32.exe ..'

and this mostly seems to be due to changes in the implementation (the advapi32 APIs do a lot of NtQueryKey calls that the new implementation doesn't do) rather than due to the decrease in dll loading. LLVM-less zig binaries don't show the same reduction (the only difference here is the DLLs being loaded):

Benchmark 1: stage4-ntdll\bin\zig.exe version
  Time (mean ± σ):       3.0 ms ±   0.6 ms    [User: 5.3 ms, System: 4.8 ms]
  Range (min … max):     1.3 ms …   4.2 ms    112 runs

Benchmark 2: stage4-advapi32\bin\zig.exe version
  Time (mean ± σ):       3.5 ms ±   0.6 ms    [User: 6.9 ms, System: 5.5 ms]
  Range (min … max):     2.5 ms …   5.9 ms    111 runs

Summary
  'stage4-ntdll\bin\zig.exe version' ran
    1.16 ± 0.28 times faster than 'stage4-advapi32\bin\zig.exe version'

---

With the removal of the advapi32 dependency, the non-ntdll dependencies that remain in an LLVM-less Zig binary are ws2_32.dll (which brings along rpcrt4.dll at runtime), kernel32.dll (which brings along kernelbase.dll at runtime), and crypt32.dll (which brings along ucrtbase.dll at runtime).
2026-03-03 01:12:47 -08:00
Andrew Kelley
0a412853aa std.Io: fix Select cancel deadlock with no tasks 2026-03-03 02:26:23 +01:00
invlpg
0a2f6a048b fix msghdr and cmsghdr on non-Linux targets, document musl behaviour
The following assertions fail on non-Linux platforms after c0c2010535
which inserted padding based on musl definitions. This padding only
exists on musl to workaround a discrepancy betweeen the POSIX API and
Linux ABI, and is incorrect on other POSIX operating systems.

This change makes the padding musl-only, and documents the reason it
exists. With this change, the assertions pass on Linux and FreeBSD
targets. The corresponding definitions on other targets line up with the
POSIX and FreeBSD ones, so they should work there too.

```zig
const std = @import("std");
const assert = std.debug.assert;
const msghdr = std.c.msghdr;
const cmsghdr = std.c.cmsghdr;

const c = @cImport({
    @cInclude("sys/socket.h");
});

comptime {
    assert(@offsetOf(msghdr, "iovlen") == @offsetOf(c.msghdr, "msg_iovlen"));
    assert(@offsetOf(msghdr, "controllen") == @offsetOf(c.msghdr, "msg_controllen"));
    assert(@offsetOf(msghdr, "control") == @offsetOf(c.msghdr, "msg_control"));
    assert(@offsetOf(msghdr, "flags") == @offsetOf(c.msghdr, "msg_flags"));

    assert(@sizeOf(msghdr) == @sizeOf(c.msghdr));

    assert(@offsetOf(cmsghdr, "len") == @offsetOf(c.cmsghdr, "cmsg_len"));
    assert(@offsetOf(cmsghdr, "level") == @offsetOf(c.cmsghdr, "cmsg_level"));

    assert(@sizeOf(cmsghdr) == @sizeOf(c.cmsghdr));
}
```
2026-03-02 17:46:26 +01:00
Ryan Liptak
e58fea8227 tar: Fix symlink test when symlinks can't be created on Windows 2026-03-01 15:01:51 -08:00
hemisputnik
74f361a5ce std.math.big.int: address log2/log10 reviews
There were good reviews made after #31365 was merged, so this commit
addresses them separately.

1. Assert that the number is greater than zero
2. Use `constants` instead of calculating constants manually
3. Use `Const.bitCountAbs` for log2
2026-03-01 20:34:17 +01:00
Andrew Kelley
9ef1050bb3 Merge pull request 'std.math.big.int: add log2 and log10 operations' (#31365) from hemisputnik/zig:work/13642-bigint-log2-log10 into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31365
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-03-01 03:35:16 +01:00
hemisputnik
bd6f512f0c
std.math.big.int: add log10 2026-02-28 20:16:32 +02:00
Andrew Kelley
02142a54d2 std.Io: remove cancelation propagation assertions
While the general guidance remains useful, it is not the case that
error.Canceled will always pass across the Group task function boundary.

Remove the too-aggressive assertions and add unit test coverage.

Closes #30096
Closes #31340
Closes #31358
2026-02-28 05:07:54 +01:00
hemisputnik
925f82da09
std.math.big.int: add log2 2026-02-28 04:15:19 +02:00
hemisputnik
99229ceb55 std.Io.{Writer|Duration}: move duration formatting to a format method
Remove the `{D}` format specifier. It is moved into `std.Io.Duration` as
a format method.

Migration plan:

```diff
-writer.print("{D}", .{ns});
+writer.print("{f}", .{std.Io.Duration{ .nanoseconds = ns }});
```

All instances where `{D}` was used have been changed to use
`std.Io.Duration` and `{f}`.

Fixes #31281
2026-02-28 02:46:04 +01:00
Andrew Kelley
8c70fd0a57 std.Io.Select: introduce cancelDiscard
and make the return value of `cancel` return queue items.

I don't think it's possible to make `cancel` not deadlock with an empty
queue buffer without introducing a new Group primitive.

This is the best I could come up with based on existing primitives.
Let's see if applications find these APIs palatable.
2026-02-28 01:46:43 +01:00
mintonmu
e779ca7223 fix(docs/wasm):zig std html render error function 'a' 2026-02-27 19:17:47 +01:00
Justus Klausecker
bbc77df3eb std.heap: delete ThreadSafeAllocator
We can keep ourselves safe from those threads perfectly well without you, thanks!
2026-02-26 21:20:34 +01:00
Saurabh Mishra
4e2cec265d Make std.PriorityQueue an unmanaged container (#31299)
## Summary of changes

+ Make adjustments to the `allocator` field and ensure the below tests pass:

  ```sh
  zig test lib/std/std.zig --zig-lib-dir lib
  zig build test-std -Dno-matrix --summary all
  ```

+ Rename `add` to `push` and `remove` to `pop` in methods and tests

+ Incorporate the functionality of `pop` in `popOrNull`, then rename the `popOrNull` to `pop` and update tests

+ Use `.empty` to set default field values and rename the `init` method to `initContext`

+ Improve variable types in tests: min heap uses the less than context function and max heap uses greater than context function

+ Remove the `dump` method as its not being used anywhere

+ Document methods `clearRetainingCapacity`, `clearAndFree`, `update`, and `ensureTotalCapacityPrecise`

Closes https://codeberg.org/ziglang/zig/issues/31298

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31299
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
Co-authored-by: Saurabh Mishra <saurabh.m@proton.me>
Co-committed-by: Saurabh Mishra <saurabh.m@proton.me>
2026-02-26 21:09:52 +01:00
estevesnp
0b6b65b387 fix: Allow async and concurrent from Io.Select to accept non-void error unions 2026-02-26 20:49:05 +01:00
Justus Klausecker
de41123957 std.heap.ArenaAllocator: fix reset creating undersized nodes
Previously resetting with `retain_capacity < @sizeOf(Node)` would create
an invalid node. This is now fixed, plus `Node.size` now has its own `Size`
type that provides additional safety via assertions to prevent bugs like
this in the future.
2026-02-26 15:40:48 +01:00
Justus Klausecker
2fa2300ba4 std.heap.ArenaAllocator: Get rid of cmpxchg loop in hot path
This is achieved by bumping `end_index` by a large enough amount so that
a suitably aligned region of memory can always be provided. The potential
wasted space this creates is then recovered by a single cmpxchg. This is
always successful for single-threaded arenas which means that this version
still behaves exactly the same as the old single-threaded implementation
when only being accessed by one thread at a time. It can however fail when
another thread bumps `end_index` in the meantime. The observerd failure
rates under extreme load are:

2 Threads: 4-5%
3 Threads: 13-15%
4 Threads: 15-17%
5 Threads: 17-18%
6 Threads: 19-20%
7 Threads: 18-21%

This version offers ~25% faster performance under extreme load from 7 threads,
with diminishing speedups for less threads. The performance for 1 and 2
threads is nearly identical.
2026-02-26 15:30:55 +01:00
Andrew Kelley
e0173c2ce0 Merge pull request 'rework fuzz testing to be smith based' (#31205) from gooncreeper/zig:integrated-smith into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31205
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-02-25 20:23:36 +01:00
Kendall Condon
bb304796f4 optimize flate decompression
Matches now use memcpy and memset when possible.

Block loops have been rewritten to be more optimizer friendly.

Reworks Symbol and HuffmanDecoder
* Symbol now only includes the value and number of code bits.
  decodeSymbol returns only the value.
* HuffmanDecoder now takes the regular bits instead of the reversed.
* Code table construction now uses buckets instead of sorting.
* For linked codes, the value field of Symbol is now used as the next
  index. The actual value is the element index.
* InvalidCode is now detected only once with a special linked index.

Performance is 39.7% faster than before and 1.1% faster than gzip using
a sample created from compressing a tar of the src directory.
2026-02-25 20:05:48 +01:00
Justus Klausecker
a3a9dc111d std.heap.ArenaAllocator: make it threadsafe
Modifies the `Allocator` implementation provided by `ArenaAllocator` to be
threadsafe using only atomics and no synchronization primitives locked
behind an `Io` implementation.

At its core this is a lock-free singly linked list which uses CAS loops to
exchange the head node. A nice property of `ArenaAllocator` is that the
only functions that can ever remove nodes from its linked list are `reset`
and `deinit`, both of which are not part of the `Allocator` interface and
thus aren't threadsafe, so node-related ABA problems are impossible.

There *are* some trade-offs: end index tracking is now per node instead of
per allocator instance. It's not possible to publish a head node and its
end index at the same time if the latter isn't part of the former.

Another compromise had to be made in regards to resizing existing nodes.
Annoyingly, `rawResize` of an arbitrary thread-safe child allocator can
of course never be guaranteed to be an atomic operation, so only one
`alloc` call can ever resize at the same time, other threads have to
consider any resizes they attempt during that time failed. This causes
slightly less optimal behavior than what could be achieved with a mutex.
The LSB of `Node.size` is used to signal that a node is being resized.
This means that all nodes have to have an even size.

Calls to `alloc` have to allocate new nodes optimistically as they can
only know whether any CAS on a head node will succeed after attempting it,
and to attempt the CAS they of course already need to know the address of
the freshly allocated node they are trying to make the new head.
The simplest solution to this would be to just free the new node again if
a CAS fails, however this can be expensive and would mean that in practice
arenas could only really be used with a GPA as their child allocator. To
work around this, this implementation keeps its own free list of nodes
which didn't make their CAS to be reused by a later `alloc` invocation.
To keep things simple and avoid ABA problems the free list is only ever
be accessed beyond its head by 'stealing' the head node (and thus the
entire list) with an atomic swap. This makes iteration and removal trivial
since there's only ever one thread doing it at a time which also owns all
nodes it's holding. When the thread is done it can just push its list onto
the free list again.

This implementation offers comparable performance to the previous one when
only being accessed by a single thread and a slight speedup compared to
the previous implementation wrapped into a `ThreadSafeAllocator` up to ~7
threads performing operations on it concurrently.
(measured on a base model MacBook Pro M1)
2026-02-25 19:12:35 +01:00
hemisputnik
2f8e660805 std.math.log10: handle comptime_int inputs correctly
also add a few tests for comptime types

fixes #31333
2026-02-25 18:57:58 +01:00