2648 Commits

Author SHA1 Message Date
Justus Klausecker 36069a2a7d Sema: make dereferences of comptime-known null C pointers runtime-known
This allows `@TypeOf(@as([*c]T, null).*.x)` to (continue to) work.
In the long term these are probably not the semantics we want, unwrapping
a comptime-known `null` pointer should always result in `unreachable`.

Also adds missing runtime safety checks for loading null C pointers.
2026-06-20 09:45:38 +02:00
David Rubin d5446652fa Sema: correctly copy pointer attributes for optional pointer payload 2026-06-18 22:42:42 +02:00
David Rubin e6686ae00d Sema: copy alignment flags to pointer of slice field 2026-06-18 22:40:04 +02:00
Ali Cheraghi 65e74dfbda @extern: add flat decoration 2026-06-18 13:38:58 +02:00
Ali Cheraghi 508cbec694 spirv: implement tuple types 2026-06-18 13:38:58 +02:00
Ali Cheraghi 4653794852 spirv: implement switch with ranges and loop_switch_br switch_dispatch 2026-06-18 13:38:58 +02:00
Ali Cheraghi a5fbbb8305 spirv: composite integers
composite integers (wider than usize) are represented in as `[N]u32` arrays.
however, no operations were implemented before.
following operations are implemented:

- bitwise (and, or, xor, not)
- comparison (eq/neq)
- add/sub/mul/shl/shr
- `@abs`, `@intCast`

this removes 36 SPIR-V behavior test skips.

also fixed `derivePtr` to use `OpAccessChain` instead of `OpBitcast`
for array pointer casts (e.g. `*[100]u8` to `*u8`) in logical addressing
mode, where pointer bitcasts are not valid.
2026-06-18 13:38:58 +02:00
Ali Cheraghi 7140d08334 spirv: int/bool fixes
- use logical ops for boolean bitwise instructions
- normalize strange-int results in airArithOp, airReduce, and airIntCast

Co-authored-by: Quint Daenen <quint@daenen.email>
2026-06-18 13:38:58 +02:00
Ali Cheraghi c6d178f93d spirv: make codegen multi-threaded
The SPIR-V backend previously ran codegen single-threaded in the linker thread.
Now each codegen job creates an `Mir` like other self-hosted backends.

Other changes:
- Bring back `dedup_types.zig` and `prune_unused.zig` **ISel**s which were originaly
  removed because codegen was single-threaded at that time and therefor had no use
- Clean up `BinaryModule.zig`
- Remove `checkLogicalPtrOperation` from `elemPtrOneLayerOnly` in `Sema.zig`.
  Element access uses `OpAccessChain`, which works on all logical address spaces
  without `VariablePointers`. the check is only needed for pointer arithmetic.
2026-06-18 13:38:58 +02:00
Alex Rønne Petersen cfeab92587 Merge pull request 'add full support for libc-less x86_64-linux-x32 and mips64(el)-linux-abin32' (#35828) from alexrp/zig:x32-n32 into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/35828
2026-06-18 12:45:27 +02:00
Ryan Mehri 14bc6f5917 Sema: check for undefined values in zirArrayCat
Right now, when an undefined tuple value is passed to zirArrayCat, it
will call `elemValue`, which will incorrectly try to produce a undef
value of the child type (of which there is none) and then panics.

This adds a check for an undef value first to produce an undef elem
value of the correct resolved type.
2026-06-18 11:41:36 +02:00
Alex Rønne Petersen 6aaea5af41 std.Target: add abin32 and x32 Abi tags
We only had gnu/musl variants of these previously.
2026-06-18 02:37:52 +02:00
Justus Klausecker e6be5cfe3e Sema: allow overwriting previous analysis results when using switch inst to store temporary information
Essentially `inst_map.putAssumeCapacityNoClobber` is always wrong here
because we *want* to clobber previous results when analyzing the same
switch expression multiple times.
2026-06-16 23:28:03 +02:00
Justus Klausecker 9ff926bb1e frontend: guarantee references to dereference expressions to be single-item pointers
AstGen doesn't emit a `ref` ZIR inst if it encounters a dereference `.*`
with a reference result location (`ref`/`ref_coerced_ty`/`ref_const`),
since the operand of the dereference expression already is a reference.

This caused some problems as Zig allows `.*` on both single-item *and* C
pointers, but most logic in Sema assumes that everything with a reference
rl is always a single-item pointer. This largely didn't cause any issues
since C pointers can do everything single-item pointers can, but it caused
Sema to mistake `&p.*[0]` with `@TypeOf(p) == [*c][n]T` for an index into
a many-item pointer `[*][n]T` instead of a single-item pointer `*[n]T`.

To avoid such problems in the future, a pointer obtained from `&p.*` is
now always turned into a proper single-item pointer. This is achieved by
introducing two new ZIR instructions, `deref` and `ref_deref`:

For `ptr.*`:
```
%1 = validate_deref(%ptr)
%2 = load(%ptr)
```
vvv
```
%1 = deref(%ptr)
```

For `&ptr.*`:
```
%1 = validate_deref(%ptr)
// use %ptr directly
```
vvv
```
%1 = ref_deref(%ptr)
// use %1
```

This makes `validate_deref` superfluous, so it's been removed.

Also fixes `node_offset_deref_ptr` source location to actually point to
the pointer being dereferenced and uses it to provide better source locs
for `deref` and `ref_deref`.
2026-06-16 12:58:34 +02:00
Justus Klausecker 0bcaad394d Merge pull request 'Sema: make struct fields referencing comptime vars not comptime' (#31462) from rmehri01/zig:struct_comptime_var_ref into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31462
Reviewed-by: Justus Klausecker <justusk@noreply.codeberg.org>
2026-06-16 11:51:54 +02:00
Ryan Mehri ec5dc55cd1 fix: make struct fields referencing comptime vars not comptime
Similar to 2c0aa1c, just applies the same logic to structs.
2026-06-15 13:39:55 -04:00
Alex Rønne Petersen daf3cca1a4 compiler: fix struct field alignment for big ints on s390x
contributes to https://codeberg.org/ziglang/zig/issues/35523
2026-06-15 14:36:41 +02:00
Ali Cheraghi 9115f88386 test: skip failing/crashing behavior tests for spirv backend
tested with `-target spirv64-vulkan` and `-mcpu vulkan_v1_2+float16+variable_pointers+float64+int64`
2026-06-14 09:11:59 +02:00
Jacob Young 3deb86bafd AstGen: make local align expression implicitly comptime
Closes #35730
2026-06-13 17:58:37 +02:00
Justus Klausecker ffac200e66 AstGen: move check for primitive integer type with leading zero
Identifiers like `u0_` should be legal but were previously rejected because
they were spuriously interpreted as primitive integer type identifiers.
Now the integer is first parsed and only if that succeeds it's checked for
a leading zero.
2026-06-11 12:08:45 +02:00
Devin J. Pohly 41d08843ff x86_64 backend: fix signed enum switch
Once the switch condition is been shifted up so the minimum value is 0,
it makes sense to treat its type as the unsigned version of its original
type.

Fixes #35651
2026-06-10 20:52:33 -07:00
Ali Cheraghi d7d131c050 add @SpirvType builtin
Closes #35240
Fixes #35238
Fixes #35259

Supported types are:
- `OpTypeSampler`
- `OpTypeImage`
- `OpTypeSampledImage`
- `OpTypeRuntimeArray` with indexing and `.len` field

The SPIR-V backend is bit-rotted so behavior tests no longer pass (compiler crashes).
However I've verified the new added tests are passing.
2026-06-07 04:58:53 +02:00
Jacob Young 3391ad7a90 x86_64: implement and test @intFromBool
Also fixes, for the x86_64 backend, #31963
2026-06-03 22:28:39 +02:00
Alex Rønne Petersen 6384ac6601 behavior: disable tail call tests on powerpc and mips with the C backend
Since LLVM doesn't support tail calls on these targets, these tests shouldn't
run with the C backend either.

closes https://codeberg.org/ziglang/zig/issues/35522
2026-06-01 14:46:00 +02:00
Andrew Kelley d6386772da Merge pull request 'Sema: simplify switch capture logic' (#35207) from justusk/zig:switch-improve-captures into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/35207
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-05-31 09:40:52 +02:00
Andrew Kelley 9ad29fbc6c disable failing tests revealed by this branch
See #35517
See #35518
See #35519
See #35520
See #35521
See #35522
See #35523
See #35537
2026-05-29 15:32:27 -07:00
Justus Klausecker 73b760e033 Sema: simplify switch capture logic
Previously all switch capture logic was in a single gigantic function and
had lots of weird rules around its args depending on each other which were
only encoded in doc comments and not actually enforced by the compiler.

That's odd since the 'happy path' of a switch capture is actually super
simple; it's literally just loading the switch operand inside of the scope
of the prong. Error sets introduce a little complexity since the type of
an error capture must be narrowed to the set of values that can possibly
be captured by their prong, but the real source of complexity here are
tagged union captures. First of, they support *two* captures - a payload
and a tag capture. The captured payload has to be set up correctly, and
payload captures are not guaranteed to have the same type so Sema has to
make sure that all possible payloads of a capture are compatible.

Instead of throwing all of this logic into one large function with lots of
special casing interspersed it is now split into three main paths:

- tagged unions (+ a potential tag capture)
- errors (including error set narrowing)
- everything else

This should greatly simplify reasoning about this logic in the future if
there's a bug or a change to be made. There's also no more dependencies of
args on each other; instead everything is a bit more modular, dependencies
are now encoded either as control flow or in the type system. This has
additionally lead to the OPV path being able to reuse some more of the
capture logic.

This commit also fixes inline prongs not narrowing error sets.
2026-05-28 19:35:48 +02:00
Krzysztof Wolicki 9e80795623 all: update to use new std.lang.Type definitions 2026-05-27 10:03:51 +01:00
Kendall Condon 65819004f3 workaround surfaced backend bugs
self-hosted wasm workaround provided by pavelverigo in #31991

powerpc workarounds provided by alexrp in #31991
2026-05-25 18:38:01 -07:00
Kendall Condon e1ce81eb54 link.Elf: fix large abi aligned globals 2026-05-25 18:37:23 -07:00
Alex Rønne Petersen 0d4f3cc675 re-enable some tests on RISC-V that no longer fail
Miscompilations appear to have been fixed with LLVM 22.

closes https://github.com/ziglang/zig/issues/24299
closes https://github.com/ziglang/zig/issues/24300
closes https://github.com/ziglang/zig/issues/24301
closes https://github.com/ziglang/zig/issues/25083
2026-05-21 20:37:11 +02:00
Alex Rønne Petersen 91bcdbec34 behavior: re-enable vector reduce operation on sparc64
closes https://github.com/ziglang/zig/issues/23719
2026-05-18 21:39:32 +02:00
Alex Rønne Petersen 72966e2a7b disable some tests that fail on x86_64-macos
These might be Rosetta 2 bugs, but I have no way to actually check since we no
longer have any native x86_64-macos CI machines.
2026-05-09 08:49:34 +02:00
Matthew Lugg fecd28371d Sema: fix crash bitcasting undefined to bitpack type
Resolves: https://codeberg.org/ziglang/zig/issues/31944
2026-05-07 06:22:47 +02:00
Matthew Lugg fc1c83a363 Air: fix legalization of packed struct init with OPV field
I have verified that this fixes *both* of the reproductions given in
https://codeberg.org/ziglang/zig/issues/31837 (they were the same bug).

Resolves: https://codeberg.org/ziglang/zig/issues/31837
2026-05-07 06:21:59 +02:00
Matthew Lugg 0bcf29aff6 compiler: correct ABI size of comptime-only optional type
Resolves: https://codeberg.org/ziglang/zig/issues/31603
2026-05-07 06:21:30 +02:00
Andrew Kelley cd23f7a814 Merge pull request 'std.meta: Remove Int/Tuple in favor of @Int/@Tuple' (#35188) from linus/zig:deprecated-std-meta into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/35188
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-05-06 19:35:40 +02:00
Pavel Verigo 3d1fb4fac8 stage2-wasm: disable std tests, add failing behavior tests 2026-05-04 07:40:02 +02:00
Linus Groh bf953c4d6a std.meta: Remove Tuple in favor of @Tuple 2026-05-03 21:42:06 +01:00
Linus Groh 991f56fd6b std.meta: Remove Int in favor of @Int 2026-05-03 21:42:06 +01:00
Justus Klausecker 1f22b2cbb2 LowerZon: fix packed containers
Since `packed` containers are now internally represented by a `bitpack`,
they need special handling on initialization: they need to be either
bitpacked or bitcasted to their backing integer. `Sema` already did this,
but `LowerZon` didn't yet.
2026-05-02 20:05:33 +02:00
Matthew Lugg fdac89d6cd remove uses of array multiplication
In preparation for its removal as accepted in
https://github.com/ziglang/zig/issues/24738.
2026-04-30 08:57:51 +01:00
Matthew Lugg 213c4fc25f lib,test: remove uses of i0
In preparation for its removal, as accepted in
https://github.com/ziglang/zig/issues/1593.
2026-04-30 08:57:51 +01:00
Matthew Lugg e67c344fc0 compiler,tests,tools: remove uses of capturing errdefer
In preparation for its removal, as accepted in
https://github.com/ziglang/zig/issues/23734.
2026-04-29 23:27:58 +01:00
Matthew Lugg 106850fd4c tests: remove uses of void{}
In preparation for this syntax to be removed from the language per
https://github.com/ziglang/zig/issues/15213.
2026-04-29 23:27:58 +01:00
Alex Rønne Petersen 1af476d5d8 behavior: re-enable a bunch of behavior tests that now pass
closes https://github.com/ziglang/zig/issues/9660
closes https://github.com/ziglang/zig/issues/21050
closes https://github.com/ziglang/zig/issues/21090
closes https://github.com/ziglang/zig/issues/21091
2026-04-25 21:54:48 +02:00
Alex Rønne Petersen 4eb8640213 Revert "test: skip alternative constraints behavior test on LoongArch"
This reverts commit f90548e740.

The bug was fixed in LLVM 22.
2026-04-25 21:54:48 +02:00
Alex Rønne Petersen a0ad35d0ba Revert "test: disable some vector ctz/clz behavior tests on LoongArch with LSX"
This reverts commit beb25b0430.

The bug was fixed in LLVM 22.
2026-04-25 21:54:48 +02:00
Alex Rønne Petersen 67fa822300 test: disable switch on pointer type behavior test with LLVM
See: https://github.com/llvm/llvm-project/issues/176634

ref https://github.com/ziglang/zig/issues/23509
2026-04-25 21:54:48 +02:00
Pavel Verigo 22945fbbdc stage2-wasm: vector, std tests 2026-04-22 00:19:46 +02:00