Commit graph

36748 commits

Author SHA1 Message Date
Matthew Lugg
fa74b80ab6
std: introduce Io.Debug proof of concept 2026-01-12 09:40:53 +00:00
Andrew Kelley
545982c029 Merge pull request 'A few Windows fixes' (#30757) from squeek502/zig:windows-misc-fixes into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30757
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-01-12 00:59:10 +01:00
GasInfinity
514f6e589c feat(libzigc): use common integer ato* and strto* implementations
* also removes their musl implementation
2026-01-12 00:55:11 +01:00
Justus Klausecker
e2338edb47 Sema: fix single-range switch prong capture
This kind of capture cannot always be comptime-known, assuming so caused
access of undefined memory during payload capture analysis!
2026-01-11 20:22:32 +01:00
mlugg
76dd39d531 Merge pull request 'frontend: rewrite switch logic' (#30776) from justusk/zig:have-you-tried-switching-it-off-and-on-again into master
Resolves: https://codeberg.org/ziglang/zig/issues/30660
Resolves: https://codeberg.org/ziglang/zig/issues/30606
Resolves: https://codeberg.org/ziglang/zig/issues/30157
Resolves: https://codeberg.org/ziglang/zig/issues/30154
Resolves: https://codeberg.org/ziglang/zig/issues/30153
Resolves: https://github.com/ziglang/zig/issues/25644
Resolves: https://github.com/ziglang/zig/issues/25632
Resolves: https://github.com/ziglang/zig/issues/24789
Resolves: https://github.com/ziglang/zig/issues/24152
Resolves: https://github.com/ziglang/zig/issues/24128
Resolves: https://github.com/ziglang/zig/issues/24126
Resolves: https://github.com/ziglang/zig/issues/23973
Resolves: https://github.com/ziglang/zig/issues/23156
Resolves: https://github.com/ziglang/zig/issues/23123
Resolves: https://github.com/ziglang/zig/issues/22138
Resolves: https://github.com/ziglang/zig/issues/21772
Resolves: https://github.com/ziglang/zig/issues/18087
Resolves: https://github.com/ziglang/zig/issues/15237

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30776
Reviewed-by: Matthew Lugg <mlugg@mlugg.co.uk>
2026-01-11 19:44:24 +01:00
Matthew Lugg
01546e68cd
compiler: handle switch rewrite review feedback 2026-01-11 14:37:28 +00:00
Matthew Lugg
3a4a7d2ca3
Sema: minor cleanup (the second) 2026-01-11 14:37:27 +00:00
Matthew Lugg
9a225456cb
Sema: minor cleanup 2026-01-11 14:37:27 +00:00
Matthew Lugg
ed1268d0e6
Zir: simplify '_' prong of 'switch' statements 2026-01-11 14:37:27 +00:00
Matthew Lugg
8ec4c5cb13
Sema: evaluate switch items at comptime 2026-01-11 14:34:19 +00:00
Justus Klausecker
078e100573
wasm: fix getting pointer type instead of error union type for is_err_ptr 2026-01-11 11:37:18 +00:00
Justus Klausecker
dbfeade221
Sema: better switch_block_err_union result location if operand has wrong type
also fixes related test case and makes it run everywhere, not just on x86_64-linux!
2026-01-11 11:37:18 +00:00
Justus Klausecker
2e99c3042e
test: add some more switch regression tests
switch evaluation order, switch lazy value resolution
2026-01-11 11:37:18 +00:00
Justus Klausecker
2479966df2
Sema: fix integration with Io.Threaded 2026-01-11 11:37:18 +00:00
Justus Klausecker
5a376d97d4
langref: document new switch features
- switch on tagged union with runtime-captured tag
- switch on errors special cases
2026-01-11 11:37:17 +00:00
Justus Klausecker
0b3b536f18
test: re-enable packed struct field type behavior test
With new code courtesy of mlugg
2026-01-11 11:37:17 +00:00
Justus Klausecker
bce7e7a52b
AstGen: Re-allow labeled break from loop else block targeting its label
This fixes a regression from a couple of commits ago; breaking from the
`else` block of a loop to the loop's tag should be allowed when explicitly
targeting the label by name.
2026-01-11 11:37:17 +00:00
Justus Klausecker
39ca03e515
test: disable packed struct field type behavior test
This test was previously masked by a bug which prevented its evaluation.
Skipping it for now.
2026-01-11 11:37:17 +00:00
Justus Klausecker
b79bd31356
Sema: rework switch_block[_ref/_err_union] logic
This commit aims to simplify and de-duplicate the logic required for
semantically analyzing `switch` expressions.

The core logic has been moved to `analyzeSwitchBlock`, `resolveSwitchBlock`
and `finishSwitchBlock` and has been rewritten around the new iterator-based
API exposed by `Zir.UnwrappedSwitchBlock`.

All validation logic and switch prong item resolution have been moved to
`validateSwitchBlock`, which produces a `ValidatedSwitchBlock` containing
all the necessary information for further analysis.

`Zir.UnwrappedSwitchBlock`, `ValidatedSwitchBlock` and `SwitchOperand`
replace `SwitchProngAnalysis` while adding more flexibility, mainly for
better integration with `switch_block_err_union`.

`analyzeSwitchBlock` has an explicit code path for OPV types which lowers
them to either a `block`-`br` or a `loop`-`repeat` construct instead of a
switch. Backends expect `switch` to actually have an operand that exists
at runtime, so this is a bug fix and avoids further special cases in the
rest of the switch logic.
`resolveSwitchBlock` and `finishSwitchBr` exclusively deal with operands
which can have more than one value, at comptime and at runtime respectively.

This commit also reworks `RangeSet` to be an unmanaged container and adds
`Air.SwitchBr.BranchHints` to offload some complexity from Sema to there
and save a few bytes of memory in the process.

Additionally, some new features have been implemented:
- decl literals and everything else requiring a result type (`@enumFromInt`!)
  may now be used as switch prong items
- union tag captures are now allowed for all prongs, not just `inline` ones
- switch prongs may contain errors which are not in the error set being
  switched on, if these prongs contain `=> comptime unreachable`

and some bugs have been fixed:
- lots of issues with switching on OPV types are now fixed
- the rules around unreachable `else` prongs when switching on errors now
  apply to *any* switch on an error, not just to `switch_block_err_union`,
  and are applied properly based on the AST
- switching on `void` no longer requires an `else` prong unconditionally
- lazy values are properly resolved before any comparisons with prong items
- evaluation order between all kinds of switch statements is now the same,
  with or without label
2026-01-11 11:37:17 +00:00
Justus Klausecker
d94137d23f
src/Type: make doc comments prettier :) 2026-01-11 11:37:17 +00:00
Justus Klausecker
6e35138901
all: prefer else => |e| return e, over else => return err,
When switching on an error, using the captured value instead of the original
one is always preferable since its error set has been narrowed to only
contain errors which haven't already been handled by other switch prongs.

The subsequent commits will disallow this form as an unreachable `else` prong.
2026-01-11 11:37:17 +00:00
Justus Klausecker
9e949f95c1
Sema: enhance comptime is_non_err resolution
This makes `is_non_err` and `unwrap_errunion_err[_ptr]` friendlier towards
being emitted into comptime blocks. Also, `analyzeIsNonErr` now actually
attempts to do something at comptime.
2026-01-11 11:37:17 +00:00
Justus Klausecker
5b00e24b6e
frontend: rework switch ZIR
Moved to a more linear layout which lends itself well to exposing an iterator.
Consumers of this iterator now just have to keep track of an index into a
homogenous sequence of bodies.

The new ZIR layout also enables giving switch prong items result locations
by storing the bodies of all items inside of the switch encoding itself.
There are some deliberate exceptions to this: enum literals and error values
are directly encoded as strings and number literals are resolved to comptime
values outside of the switch block. These special encodings exist to save
space and can easily be resolved during semantic analysis.

This commit also re-implements `AstGen` and `print_zir` for switch based on
the new layout and adds some additional information to the ZIR text repr.
Notably `switchExprErrUnion` has been merged into `switchExpr` to reduce
code duplication.

The rules around allowing an unreachable `else` prong in error switches are
also refined by this commit, and enforced properly based on the actual AST.
The special cases are listed exhaustively below:

`else => unreachable,`
`else => return,`
`else => |e| return e,` (where `e` is any identifier)

Additionally `{...} => comptime unreachable,` prongs are marked to support
future features (refer to next couple of commits).

Also fixes 'value with comptime-only type depends on runtime control flow'
error for labeled error switch statements by surrounding the entire expr
with a common block to break to (see previous commits for details).
2026-01-11 11:37:16 +00:00
Justus Klausecker
00d4f3c001
Liveness: improve logging 2026-01-11 11:37:16 +00:00
Justus Klausecker
42dea36ce9
llvm: fix jump table gen for labeled switch with single else prong
Avoids a null unwrap if there are no cases with explicit values present
while trying to construct a jump table for a labeled switch statement.
2026-01-11 11:37:16 +00:00
Justus Klausecker
d840bb5118
AstGen: improve ergonomics of Scope
Adds `Scope.Unwrapped`, a simple union of pointers to already-casted scopes
with `Scope.Tag` as its tag enum. This pairs very nicely with labeled switch
and gets rid of almost every `scope.cast(...).?`, improving developer QOL :)
2026-01-11 11:37:16 +00:00
Justus Klausecker
e0108dec54
AstGen: allow labels to provide separate break and continue targets
Enhances `GenZir` to allow labels to provide separate `break` and `continue`
target blocks and adds some more information on continue targets to
communicate whether the target is a switch block or cannot be targeted by
`continue` at all.

The main motivation is enabling this:
```
const result: u32 = operand catch |err| label: switch (err) {
    else => continue :label error.MyError,
    error.MyError => break :label 1,
};
```
to be lowered to something like this:
```
%1 = block({
  %2 = is_non_err(%operand)
  %3 = condbr(%2, {
    %4 = err_union_payload_unsafe(%operand)
    %5 = break(%1, result) // targets enclosing `block`
  }, {
    %6 = err_union_code(%operand)
    %7 = switch_block(%6,
      else => {
        %8 = switch_continue(%7, "error.MyError") // targets `switch_block`
      },
      "error.MyError" => {
        %9 = break(%1, @one) // targets enclosing `block`
      },
    )
    %10 = break(%1, @void_value)
  })
})
```
which makes the non-error case and all breaks from switch prongs, but not
continues from switch prongs, peers.

This is required to avoid the problems described in gh#11957 for labeled
switches without having to introduce a fairly complex special case to the
`switch_block_err_union` logic. Since this construct is very rare in practice,
introducing this additional complexity just to save a few ZIR bytes is
likely not worth it, so the simplified lowering described above will be
used instead.

As a nice bonus, AstGen can now also detect a `continue` trying to target
a labeled block and emit an appropriate error message.
2026-01-11 11:37:16 +00:00
David Rubin
aa2b178029
disallow switch case capture discards
Previously Zig allowed you to write something like,
```zig
switch (x) {
    .y => |_| {
```

This seems a bit strange because in other cases, such as when
capturing the tag in a switch case,
```zig
switch (x) {
    .y => |_, _| {
```
this produces an error.

The only usecase I can think of for the previous behaviour is
if you wanted to assert that all union payloads are able
to coerce,
```zig
const X = union(enum) { y: u8, z: f32 };

switch (x) {
    .y, .z => |_| {
```

This will compile-error with the `|_|` and pass without it.

I don't believe this usecase is strong enough to keep the current
behaviour; it was never used in the Zig codebase and I cannot
find a single usage of this behaviour in the real world, searching
through Sourcegraph.
2026-01-11 11:37:16 +00:00
Ryan Liptak
4cf7dc22fa Expand the errors that act as "sym links can't be created" on Windows
Previously, the errors that are now mapped to AccessDenied, PermissionDenied, and FileSystem were all mapped to AccessDenied.
2026-01-11 02:08:18 -08:00
Ryan Liptak
b4831403c9 fileRead functions: handle INVALID_FUNCTION on Windows and map it to error.IsDir
INVALID_FUNCTION may be possible in other scenarios as well, but it is verifiably returned when the handle refers to a directory.
2026-01-11 02:08:18 -08:00
Ryan Liptak
d08098861f Fix RENAME_INFORMATION.toBuffer returning sizes below the minimum recognized size
Passing a length below 24 to NtSetInformationFile results in INFO_LENGTH_MISMATCH, so always return at least 24 for the buffer length.
2026-01-11 02:07:49 -08:00
Alex Rønne Petersen
c434599134 Merge pull request 'std.process: add PermissionDenied to ProtectMemoryError (for OpenBSD)' (#30781) from alexrp/zig:openbsd-mprotect-immutable into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30781
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-01-11 09:43:01 +01:00
Alex Rønne Petersen
4e806f2521
build: bump test-libc max_rss to 2_496_066_355 2026-01-11 05:55:46 +01:00
Alex Rønne Petersen
e8a6e58f9d
std.process: fix some page size assumptions in lockMemory/protectMemory tests
Makes the tests work on hexagon and loongarch.
2026-01-11 03:07:45 +01:00
Alex Rønne Petersen
c002b96f17
Revert "ci: bump loongarch64-linux timeouts by 1 hour"
This reverts commit 867501d9d2.

It seems that the increased run time was a result of the regressed
test-incremental step and that we are now back to normal.
2026-01-11 00:51:37 +01:00
Alex Rønne Petersen
fe0b2ab9df
ci: temporarily remove riscv64-linux while I investigate its performance issues 2026-01-11 00:13:28 +01:00
Jay Petacat
484cc15366 Sema: Allow small integer types to coerce to floats
If the float can store all possible values of the integer without
rounding, coercion is allowed. The integer's precision must be less than
or equal to the float's significand precision.

Closes #18614
2026-01-10 22:19:20 +01:00
Andrew Kelley
5082e85de9 Merge pull request 'libc: use compiler_rt/libzigc for some math functions and add some libc-tests' (#30767) from rpkak/zig:libc-compiler_rt-math into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30767
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-01-10 22:15:32 +01:00
nash1111
9a7f14354e crypto: correct checkHostName docs about wildcard matching 2026-01-10 22:11:52 +01:00
Jakub Konka
f3db3a087e std: ignore -fmacro-prefix-map flag when parsing NIX_CFLAGS_COMPILE
Since we ignore this flag in `clang_options_data.zig`, it makes
sense to ignore it for Nix as well. One thing I've been thinking about
is if it would make sense to somehow use `clang_options_data.zig` as
source of truth for handling Nix cflags too rather than slap more
hard-coded escape hatches.
2026-01-10 22:04:00 +01:00
Alex Rønne Petersen
9acfd167fa
std.process: add PermissionDenied to ProtectMemoryError (for OpenBSD)
See EPERM notes on https://man.openbsd.org/mprotect.2.
2026-01-10 21:04:20 +01:00
InKryption
707823abdb
fix semantic UAFs in std.Io.Threaded 2026-01-10 17:43:33 +01:00
Andrew Kelley
ec25b13848 Merge pull request 'std: move memory locking and memory protection to process' (#30758) from mmap into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30758
2026-01-10 04:14:48 +01:00
rpkak
08d37d6e14
compiler_rt: fix sqrtf implementation; libc: remove sqrt implementations 2026-01-10 00:09:54 +01:00
rpkak
813ae89208
libc -> libzigc: copysign 2026-01-10 00:09:54 +01:00
rpkak
aa7dac9739
libc: remove fmin/fmax implementations 2026-01-10 00:09:54 +01:00
rpkak
a2861a6c8d
libc: remove fmod implementations 2026-01-10 00:09:54 +01:00
rpkak
9dd32d5e65
libc: remove some more log implementations 2026-01-10 00:09:54 +01:00
rpkak
b94a65ed40
test-libc: run some math tests 2026-01-10 00:09:54 +01:00
Andrew Kelley
4d6d2922b8 std: move memory locking and memory protection to process
and introduce type safety for posix.PROT (mmap, mprotect)

progress towards #6600
2026-01-09 13:52:00 -08:00