35913 Commits

Author SHA1 Message Date
Ryan Liptak
822f412424 fs.path: Fix on big-endian architectures, make PathType.isSep assume WTF-16 is LE
This commit flips usage of PathType.isSep from requiring the caller to convert to native to assuming the input is LE encoded, which is a breaking change. This makes usage a bit nicer, though, and moves the endian conversion work from runtime to comptime.
2025-11-21 22:26:58 -08:00
Ryan Liptak
59b8bed222 Teach fs.path about the wonderful world of Windows paths
Previously, fs.path handled a few of the Windows path types, but not all of them, and only a few of them correctly/consistently. This commit aims to make `std.fs.path` correct and consistent in handling all possible Win32 path types.

This commit also slightly nudges the codebase towards a separation of Win32 paths and NT paths, as NT paths are not actually distinguishable from Win32 paths from looking at their contents alone (i.e. `\Device\Foo` could be an NT path or a Win32 rooted path, no way to tell without external context). This commit formalizes `std.fs.path` being fully concerned with Win32 paths, and having no special detection/handling of NT paths.

Resources on Windows path types, and Win32 vs NT paths:

- https://googleprojectzero.blogspot.com/2016/02/the-definitive-guide-on-win32-to-nt.html
- https://chrisdenton.github.io/omnipath/Overview.html
- https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file

API additions/changes/deprecations

- `std.os.windows.getWin32PathType` was added (it is analogous to `RtlDetermineDosPathNameType_U`), while `std.os.windows.getNamespacePrefix` and `std.os.windows.getUnprefixedPathType` were deleted. `getWin32PathType` forms the basis on which the updated `std.fs.path` functions operate.
- `std.fs.path.parsePath`, `std.fs.path.parsePathPosix`, and `std.fs.path.parsePathWindows` were added, while `std.fs.path.windowsParsePath` was deprecated. The new `parsePath` functions provide the "root" and the "kind" of a path, which is platform-specific. The now-deprecated `windowsParsePath` did not handle all possible path types, while the new `parsePathWindows` does.
- `std.fs.path.diskDesignator` has been deprecated in favor of `std.fs.path.parsePath`, and same deal with `diskDesignatorWindows` -> `parsePathWindows`
- `relativeWindows` is now a compile error when *not* targeting Windows, while `relativePosix` is now a compile error when targeting Windows. This is because those functions read/use the CWD path which will behave improperly when used from a system with different path semantics (e.g. calling `relativePosix` from a Windows system with a CWD like `C:\foo\bar` will give you a bogus result since that'd be treated as a single relative component when using POSIX semantics). This also allows `relativeWindows` to use Windows-specific APIs for getting the CWD and environment variables to cut down on allocations.
- `componentIterator`/`ComponentIterator.init` have been made infallible. These functions used to be able to error on UNC paths with an empty server component, and on paths that were assumed to be NT paths, but now:
  + We follow the lead of `RtlDetermineDosPathNameType_U`/`RtlGetFullPathName_U` in how it treats a UNC path with an empty server name (e.g. `\\\share`) and allow it, even if it'll be invalid at the time of usage
  + Now that `std.fs.path` assumes paths are Win32 paths and not NT paths, we don't have to worry about NT paths

Behavior changes

- `std.fs.path` generally: any combinations of mixed path separators for UNC paths are universally supported, e.g. `\/server/share`, `/\server\share`, `/\server/\\//share` are all seen as equivalent UNC paths
- `resolveWindows` handles all path types more appropriately/consistently.
  + `//` and `//foo` used to be treated as a relative path, but are now seen as UNC paths
  + If a rooted/drive-relative path cannot be resolved against anything more definite, the result will remain a rooted/drive-relative path.
  + I've created [a script to generate the results of a huge number of permutations of different path types](https://gist.github.com/squeek502/9eba7f19cad0d0d970ccafbc30f463bf) (the result of running the script is also included for anyone that'd like to vet the behavior).
- `dirnameWindows` now treats the drive-relative root as the dirname of a drive-relative path with a component, e.g. `dirname("C:foo")` is now `C:`, whereas before it would return null. `dirnameWindows` also handles local device paths appropriately now.
- `basenameWindows` now handles all path types more appropriately. The most notable change here is `//a` being treated as a partial UNC path now and therefore `basename` will return `""` for it, whereas before it would return `"a"`
- `relativeWindows` will now do its best to resolve against the most appropriate CWD for each path, e.g. relative for `D:foo` will look at the CWD to check if the drive letter matches, and if not, look at the special environment variable `=D:` to get the shell-defined CWD for that drive, and if that doesn't exist, then it'll resolve against `D:\`.

Implementation details

- `resolveWindows` previously looped through the paths twice to build up the relevant info before doing the actual resolution. Now, `resolveWindows` iterates backwards once and keeps track of which paths are actually relevant using a bit set, which also allows it to break from the loop when it's no longer possible for earlier paths to matter.
- A standalone test was added to test parts of `relativeWindows` since the CWD resolution logic depends on CWD information from the PEB and environment variables

Edge cases worth noting

- A strange piece of trivia that I found out while working on this is that it's technically possible to have a drive letter that it outside the intended A-Z range, or even outside the ASCII range entirely. Since we deal with both WTF-8 and WTF-16 paths, `path[0]`/`path[1]`/`path[2]` will not always refer to the same bits of information, so to get consistent behavior, some decision about how to deal with this edge case had to be made. I've made the choice to conform with how `RtlDetermineDosPathNameType_U` works, i.e. treat the first WTF-16 code unit as the drive letter. This means that when working with WTF-8, checking for drive-relative/drive-absolute paths is a bit more complicated. For more details, see the lengthy comment in `std.os.windows.getWin32PathType`
- `relativeWindows` will now almost always be able to return either a fully-qualified absolute path or a relative path, but there's one scenario where it may return a rooted path: when the CWD gotten from the PEB is not a drive-absolute or UNC path (if that's actually feasible/possible?). An alternative approach to this scenario might be to resolve against the `HOMEDRIVE` env var if available, and/or default to `C:\` as a last resort in order to guarantee the result of `relative` is never a rooted path.
- Partial UNC paths (e.g. `\\server` instead of `\\server\share`) are a bit awkward to handle, generally. Not entirely sure how best to handle them, so there may need to be another pass in the future to iron out any issues that arise. As of now the behavior is:
  + For `relative`, any part of a UNC disk designator is treated as the "root" and therefore isn't applicable for relative paths, e.g. calling `relative` with `\\server` and `\\server\share` will result in `\\server\share` rather than just `share` and if `relative` is called with `\\server\foo` and `\\server\bar` the result will be `\\server\bar` rather than `..\bar`
  + For `resolve`, any part of a UNC disk designator is also treated as the "root", but relative and rooted paths are still elligable for filling in missing portions of the disk designator, e.g. `resolve` with `\\server` and `foo` or `\foo` will result in `\\server\foo`

Fixes #25703
Closes #25702
2025-11-21 00:03:44 -08:00
Ryan Liptak
26afcdb7fe std.process: Actually use explicit GetCwdError/GetCwdAllocError sets
Also fix GetCwdAllocError to include only the set of possible errors.
2025-11-19 04:10:11 -08:00
Ryan Liptak
fb1bd78908 process.getenvW: Document that returned memory points to the PEB 2025-11-18 20:07:39 -08:00
Ryan Liptak
adf74ba4fb windows.eqlIgnoreCaseWTF16 -> eqlIgnoreCaseWtf16
Consistent with naming of other, similar functions
2025-11-16 04:03:52 -08:00
Ryan Liptak
aa4332fb0e
Merge pull request #25539 from squeek502/windows-readlinkw
windows: Make readLinkW APIs output WTF-16, reduce stack usage of callers
2025-11-15 23:36:34 -08:00
xdBronch
f6fecfdc00 improve assembly error test coverage 2025-11-16 06:30:51 +00:00
Meghan Denny
d07360f999 std.c: implement rusage for freebsd
Reference: e0c41af925/sys/sys/resource.h (L70)
2025-11-16 06:15:54 +01:00
Ryan Liptak
6aa3570cb0 windows: Make readLinkW APIs output WTF-16, reduce stack usage of callers
- Affects the following functions:
  + `std.fs.Dir.readLinkW`
  + `std.os.windows.ReadLink`
  + `std.os.windows.ntToWin32Namespace`
  + `std.posix.readlinkW`
  + `std.posix.readlinkatW`

Each of these functions (except `ntToWin32Namespace`) took WTF-16 as input and would output WTF-8, which makes optimal buffer re-use difficult at callsites and could force unnecessary WTF-16 <-> WTF-8 conversion during an intermediate step.

The functions have been updated to output WTF-16, and also allow for the path and the output to re-use the same buffer (i.e. in-place modification), which can reduce the stack usage at callsites. For example, all of `std.fs.Dir.readLink`/`readLinkZ`/`std.posix.readlink`/`readlinkZ`/`readlinkat`/`readlinkatZ` have had their stack usage reduced by one PathSpace struct (64 KiB) when targeting Windows.

The new `ntToWin32Namespace` takes an output buffer and returns a slice from that instead of returning a PathSpace, which is necessary to make the above possible.
2025-11-15 18:16:03 -08:00
Ryan Liptak
06a7597ea8 windows.ReadLink: Use OpenFile now that .filter = .any exists
The reasoning in the comment deleted by this commit no longer applies, since that same benefit can be obtained by using OpenFile with `.filter = .any`.

Also removes a stray debug.print
2025-11-15 18:07:25 -08:00
Andrew Kelley
6ecf497714
Merge pull request #25936 from Traxar/master
Add some missing features to `std.Io.Timestamp` and `spirv/CodeGen.zig`
2025-11-15 17:14:12 -08:00
Alex Rønne Petersen
83d9a5968f
std.Thread: disable thread local storage test on 32-bit targets
https://github.com/ziglang/zig/issues/25498
2025-11-16 00:08:20 +01:00
Matthew Lugg
bc589c271a
Merge pull request #25924 from mlugg/legalize-soft-float
Legalize: implement soft-float legalizations
2025-11-15 16:18:40 +00:00
Sam Bossley
1ebbdf8eef fix: add specific error set for SelectiveWalker iterator function 2025-11-15 05:00:14 -08:00
Matthew Lugg
cd8fdd252d
build.zig: use Step.UpdateSourceFiles for zig1.wasm
We were already using this for `stage1/zig.h`, but `stage1/zig1.wasm`
was being modified directly by the `wasm-opt` command. That's a bad idea
because it forces the build system to assume that `wasm-opt` has side
effects, so it is re-run every time you run `zig build update-zig1`,
i.e. it does not interact with the cache system correctly. It is much
better to create non-side-effecting `Run` steps (using `addOutput*Arg`)
where possible so that the build system has a more correct understanding
of the step graph.
2025-11-15 09:49:02 +00:00
Matthew Lugg
bc78d8efdb
Legalize: implement soft-float legalizations
A new `Legalize.Feature` tag is introduced for each float bit width
(16/32/64/80/128). When e.g. `soft_f16` is enabled, all arithmetic and
comparison operations on `f16` are converted to calls to the appropriate
compiler_rt function using the new AIR tag `.legalize_compiler_rt_call`.
This includes casts where the source *or* target type is `f16`, or
integer<=>float conversions to or from `f16`. Occasionally, operations
are legalized to blocks because there is extra code required; for
instance, legalizing `@floatFromInt` where the integer type is larger
than 64 bits requires calling an arbitrary-width integer conversion
function which accepts a pointer to the integer, so we need to use
`alloc` to create such a pointer, and store the integer there (after
possibly zero-extending or sign-extending it).

No backend currently uses these new legalizations (and as such, no
backend currently needs to implement `.legalize_compiler_rt_call`).
However, for testing purposes, I tried modifying the self-hosted x86_64
backend to enable all of the soft-float features (and implement the AIR
instruction). This modified backend was able to pass all of the behavior
tests (except for one `@mod` test where the LLVM backend has a bug
resulting in incorrect compiler-rt behavior!), including the tests
specific to the self-hosted x86_64 backend.

`f16` and `f80` legalizations are likely of particular interest to
backend developers, because most architectures do not have instructions
to operate on these types. However, enabling *all* of these legalization
passes can be useful when developing a new backend to hit the ground
running and pass a good amount of tests more easily.
2025-11-15 09:49:01 +00:00
Matthew Lugg
9c45a87490
compiler_rt: fix and simplify additional Windows exports
Simplifies the logic, clarifies the comment, and fixes a minor bug,
which is that we exported the Windows ABI name *instead* of the standard
compiler-rt name, but it's meant to be exported *in addition* to the
standard name (this is LLVM's behavior and it is more useful).
2025-11-15 09:49:01 +00:00
Justus Klausecker
4187d0e8fe MemoryPool: add unmanaged variants and make them the default 2025-11-15 09:30:57 +00:00
traxar
868413007f src/codegen/spirv/CodeGen.zig: add missing unary Ops 2025-11-15 16:39:39 +09:00
traxar
19c9062840 std/Io.zig Timestamp: add toMilliseconds() 2025-11-15 16:38:33 +09:00
Alex Rønne Petersen
250803661c
Merge pull request #25927 from lalinsky/netbsd-madv
Fix madvice/msync flags for BSDs
2025-11-15 01:28:05 +01:00
Justus Klausecker
06d08dabab Sema: fix illegal multi level pointer coercions
Moves a premature check that allowed pointers to mutable pointers to coerce
to any other pointer to a mutable pointer.
2025-11-14 23:25:54 +00:00
Matthew Lugg
c6b5945356 std.Build: don't force all children to inherit color option
The build runner was previously forcing child processes to have their
stderr colorization match the build runner by setting `CLICOLOR_FORCE`
or `NO_COLOR`. This is a nice idea in some cases---for instance a simple
`Run` step which we just expect to exit with code 0 and whose stderr is
not being programmatically inspected---but is a bad idea in others, for
instance if there is a check on stderr or if stderr is captured, in
which case forcing color on the child could cause checks to fail.

Instead, this commit adds a field to `std.Build.Step.Run` which
specifies a behavior for the build runner to employ in terms of
assigning the `CLICOLOR_FORCE` and `NO_COLOR` environment variables. The
default behavior is to set `CLICOLOR_FORCE` if the build runner's output
is colorized and the step's stderr is not captured, and to set
`NO_COLOR` otherwise. Alternatively, colors can be always enabled,
always disabled, always match the build runner, or the environment
variables can be left untouched so they can be manually controlled
through `env_map`.

Notably, this fixes a failure when running `zig build test-cli` in a
TTY (or with colors explicitly enabled). GitHub CI hadn't caught this
because it does not request color, but Codeberg CI now does, and we were
seeing a failure in the `zig init` test because the actual output had
color escape codes in it due to 6d280dc.
2025-11-14 21:50:24 +01:00
Lukas Lalinsky
6bdea35ce5 Fix std.c.MSF.SYNC for freebsd, openbsd, dragonfly 2025-11-14 18:02:53 +01:00
Lukas Lalinsky
6fc5923a54 Define std.c.MADV for openbsd 2025-11-14 18:01:57 +01:00
Lukas Lalinsky
3a08d2f162 Define std.c.MADV for NetBSD
The `.netbsd` branch was completely missing. Validated against the
actual system headers.
2025-11-14 17:48:19 +01:00
Alex Rønne Petersen
b38fb4bff3
Merge pull request #25917 from alexrp/target-features
`std.Target`: add CPU features and models for alpha and hppa
2025-11-14 12:23:09 +01:00
Alex Rønne Petersen
94538d8dd2 std.pie: add missing clobbers on alpha and sparc
Also format all the assembly code in the file.
2025-11-14 12:19:38 +01:00
Alex Rønne Petersen
9ab7eec23e represent Mac Catalyst as aarch64-maccatalyst-none rather than aarch64-ios-macabi
Apple's own headers and tbd files prefer to think of Mac Catalyst as a distinct
OS target. Earlier, when DriverKit support was added to LLVM, it was represented
a distinct OS. So why Apple decided to only represent Mac Catalyst as an ABI in
the target triple is beyond me. But this isn't the first time they've ignored
established target triple norms (see: armv7k and aarch64_32) and it probably
won't be the last.

While doing this, I also audited all Darwin OS prongs throughout the codebase
and made sure they cover all the tags.
2025-11-14 11:33:35 +01:00
0x4a61636f62
2e6f7d36b9 std.Io.net: fix off-by-one in HostName.expand
`HostName.expand` was including the null terminator in the slice passed to `HostName.init`, which caused `HostName.validate` to fail.
2025-11-13 21:04:21 -08:00
Andrew Kelley
813e8614bc
Merge pull request #25817 from castholm/windows-fetch
Fix TLS, `io.async()` and package fetching on Windows
2025-11-13 19:20:13 -08:00
Alex Rønne Petersen
375d873e1e libcxx: use compiler's _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION when set
closes #25911
2025-11-14 02:02:59 +01:00
Alex Rønne Petersen
18a131f3da
ci: enable Forgejo Actions on master pushes 2025-11-13 23:04:40 +01:00
Alex Rønne Petersen
cca2da16bd
ci: only cancel previous PR runs on Forgejo Actions 2025-11-13 23:00:40 +01:00
Alex Rønne Petersen
65b0a2342b
std.Target.x86: refresh from update_cpu_features.zig
Just to get rid of this ordering diff.
2025-11-13 22:29:28 +01:00
Alex Rønne Petersen
39e91deb77
std.Target: add CPU features and models for hppa 2025-11-13 22:29:12 +01:00
Alex Rønne Petersen
2b23f98d2c
std.Target: add CPU features and models for alpha 2025-11-13 22:28:50 +01:00
xdBronch
071453d5b9 fix 'redundant comptime keyword' error source location and add tests 2025-11-13 19:47:36 +00:00
Matthew Lugg
181b25ce4f
Merge pull request #25772 from mlugg/kill-dead-code
compiler: rewrite some legalizations, and remove a bunch of dead code
2025-11-12 23:14:02 +00:00
Alex Rønne Petersen
dfd7b7f233 std.Target: remove Abi.cygnus
There is approximately zero chance of the Zig team ever spending any effort on
supporting Cygwin; the MSVC and MinGW-w64 ABIs are superior in every way that
matters, and not least because they lead to binaries that just run natively on
Windows without needing a POSIX emulation environment installed.
2025-11-12 22:39:04 +01:00
Matthew Lugg
92bc619c49 std.debug: allow fp unwind from context
It's easy to do FP unwinding from a CPU context: you just report the
captured ip/pc value first, and then unwind from the captured fp value.
All this really needed was a couple of new functions on the
`std.debug.cpu_context` implementations so that we don't need to rely on
`std.debug.Dwarf` to access the captured registers.

Resolves: #25576
2025-11-12 21:02:38 +00:00
So1aric
49e19fc94f
Sema: fix inline fn compiler crash (#25586)
Resolves: https://github.com/ziglang/zig/issues/25581
2025-11-12 19:21:28 +00:00
Alex Rønne Petersen
be2a902784
cmake: update ZIG_LLVM_REQUIRED_TARGETS to contain LoongArch and SPIRV 2025-11-12 20:16:58 +01:00
Matthew Lugg
532aa3c575
cbe: work around some miscompilations
The changes to `codegen.c` are blatant hacks, but the problem they work
around isn't a regression: it's an existing miscompilation. This branch
happened to *expose* that miscompilation in more cases by changing how
an incorrect result is *used*.
2025-11-12 16:00:16 +00:00
Matthew Lugg
5df5e2ed26
zig.h: drop dependency on deleted compiler_rt functions
It turns out we did use these in the C backend. However, it's really
just as easy, if not easier, to replicate the logic directly in C.

Synchronizes stage1/zig.h to make sure the bootstrap doesn't depend on
these functions either. The actual zig1 tarball is unmodified because
regenerating it is unnecessary in this instance.
2025-11-12 16:00:16 +00:00
Matthew Lugg
69f39868b4
Air.Legalize: revert to loops for scalarizations
I had tried unrolling the loops to avoid requiring the
`vector_store_elem` instruction, but it's arguably a problem to generate
O(N) code for an operation on `@Vector(N, T)`. In addition, that
lowering emitted a lot of `.aggregate_init` instructions, which is
itself a quite difficult operation to codegen.

This requires reintroducing runtime vector indexing internally. However,
I've put it in a couple of instructions which are intended only for use
by `Air.Legalize`, named `legalize_vec_elem_val` (like `array_elem_val`,
but for indexing a vector with a runtime-known index) and
`legalize_vec_store_elem` (like the old `vector_store_elem`
instruction). These are explicitly documented as *not* being emitted by
Sema, so need only be implemented by backends if they actually use an
`Air.Legalize.Feature` which emits them (otherwise they can be marked as
`unreachable`).
2025-11-12 16:00:16 +00:00
Matthew Lugg
99a7884308
behavior: disable test on cbe
This isn't so much a regression as it is foreshadowing of accepted
proposal https://github.com/ziglang/zig/issues/24657.
2025-11-12 16:00:16 +00:00
Matthew Lugg
6576c3b898
x86_64: spill eflags when initializing bool vector 2025-11-12 16:00:16 +00:00
Matthew Lugg
20bd5e8018
compiler-rt: remove dead code
`__addosi4`, `__addodi4`, `__addoti4`, `__subosi4`, `__subodi4`, and
`__suboti4` were all functions which we invented for no apparent reason.
Neither LLVM, nor GCC, nor the Zig compiler use these functions. It
appears the functions were created in a kind of misunderstanding of an
old language proposal; see https://github.com/ziglang/zig/pull/10824.

There is no benefit to these functions existing; if a Zig compiler
backend needs this operation, it is trivial to implement, and *far*
simpler than calling a compiler-rt routine. Therefore, this commit
deletes them. A small amount of that code was used by other parts of
compiler-rt; the logic is trivial so has just been inlined where needed.
I also chose to quickly implement `__addvdi3` (a standard function)
because it is trivial and we already implement the `sub` parallel.
2025-11-12 16:00:16 +00:00
Matthew Lugg
c091e27aac
compiler: spring cleaning
I started this diff trying to remove a little dead code from the C
backend, but ended up finding a bunch of dead code sprinkled all over
the place:

* `packed` handling in the C backend which was made dead by `Legalize`
* Representation of pointers to runtime-known vector indices
* Handling for the `vector_store_elem` AIR instruction (now removed)
* Old tuple handling from when they used the InternPool repr of structs
* Straightforward unused functions
* TODOs in the LLVM backend for features which Zig just does not support
2025-11-12 16:00:15 +00:00