85 Commits

Author SHA1 Message Date
Matthew Lugg 99b90a4315 incremental: add misssing dependency on backing/tag type source code 2026-04-23 01:13:22 +02:00
Matthew Lugg 6608b65100 incremental: fix tracking of nested container declarations
...but not in the way you'd expect. We were actually tracking them in
cases where we shouldn't have been! We cannot track a declaration if its
parent namespace has been lost, because that will cause analysis
failures immediately, but if we excluded a type from the mapping due to
a major change (such as a struct turning into a union, or a field being
added), we were still including any trackable instructions inside the
container's field expressions (e.g. struct field type expressions). This
meant we were tracking a type declaration while losing tracking on its
parent namespace, with predictably disastrous results.

Oh, also, tracking for opaque types was just totally wrong (I think this
was a typo from a while back). We could map it to things other than
opaque declarations, and we never mapped declarations inside opaques.
So, uh, I fixed that too.
2026-04-16 00:00:44 +01:00
Matthew Lugg 291addadf8 tests: move incremental target matrix out of manifests
Having the matrix of test targets for incremental compilation in the
individual test manifests has turned out to be inconvenient for a few
reasons: the tests are almost certain to accidentally get out of sync,
disabling targets entirely is annoying to do, and incr-check needs to
take care to print the target in all error messages (which currently
does not always happen).

If I recall correctly, I originally designed it this way because it
allows targets to be disabled at the granularity of individual tests,
but there's an easier approach to that: just let a test manifest that it
should be *skipped* on a certain target! As skipping is the rare case,
and also the case you want readers to notice, it makes sense for *it* to
be explicitly specified, like how unit tests use `error.SkipZigTest`.

So, `incr-check` no longer runs through a list of targets specified in
the manifest. Instead, it accepts (and, in fact, requires) a single
target on the command line, and runs the test for that specific target.
If the file contains a `#skip_target` directive for that target, then
`incr-check` exits immediately, so we can still disable targets at
individual test granularity, but you can also disable a target for all
tests by just commenting it out of the matrix in `test/tests.zig`.

As a nice bonus, this also allows the build system to run different
incremental test targets in parallel, because the targets are now
different steps.

This definitely seems like a better way to split the work between the
build system and incr-check---sorry for getting this wrong initially!
2026-04-15 00:06:18 +02:00
Alex Rønne Petersen 13839127b9 test: disable incremental tests on wasm32-wasi-selfhosted
https://codeberg.org/ziglang/zig/issues/31810
2026-04-11 04:40:16 +02:00
Alex Rønne Petersen a38c6bbcc4 test: disable incremental tests on x86_64-windows-selfhosted
https://codeberg.org/ziglang/zig/issues/31773
2026-04-06 23:04:14 +02:00
Pavel Verigo ad7a028228 stage2-wasm: pass incremental tests
This PR enables all incremental tests under the `test/incremental` directory, except one: `change_exports`, which is currently ignored as it requires a non-trivial amount of work on the linker, since we do not currently support exporting data symbols.

To enable the other tests, the following fixes were needed:

1. `src/link/Wasm.zig`: instead of chasing function type through Nav, get it directly.
2. `src/target.zig`: `.panic_fn` appears to work fine with the wasm backend.
3. `src/codegen/wasm/CodeGen.zig`: there was a liveness related bug that caused some `ArenaAllocator` code to crash the backend.

More info on (3), the liveness and local reuse code in the backend for years in unfinished state. For example there is currently no branch merging and reuse happens only when inst die in same block level. I initially considered doing a large refactor to implement everything correctly, but aborted due to its sheer size and currently! no clear idea about how to do this efficiently.

Instead, I fixed the bug with minimal changes and removed useless code, keeping the old solution otherwise intact.
2026-04-04 15:21:35 +02:00
Matthew Lugg 4a494a8cf9 tests: enable incremental tests for x86_64-linux-llvm
These all pass now! I have also removed the warning about the LLVM
backend not supporting incremental compilation; I expect it will work
sort of okay in practice by now.
2026-03-28 16:50:43 +00:00
Matthew Lugg 0978566db8 incremental: handle loss of main struct instruction
My changes to how incremental compilation handles container types mean
that, at least for now, it is possible for the ZIR `.main_struct_inst`
of a source file to be lost (this happens if the number of top-level
fields in a file changes for instance). I missed a few things which
needed changing to account for this, which could lead to crashes with
certain (trivial) changes---oops!

Adds two new incremental test cases. They are currently disabled for
wasm32-wasi-selfhosted because they both trigger a crash in the WASM
backend.
2026-03-12 21:27:16 +01:00
Matthew Lugg 6d997ebe47 tests: get cases passing (and a few other bits) 2026-03-10 10:26:14 +00:00
Matthew Lugg 1826ba69d8 compiler: make dependency loop errors good 2026-03-10 10:26:09 +00:00
Andrew Kelley 80625990d5 std: different mechanism for disabling network dependency
On Windows, it is sometimes problematic to depend on ws2_32.dll. Before,
users of std.Io.Threaded would have to call ioBasic() rather than io()
in order to avoid unnecessary dependencies on ws2_32.dll. Now, the
application can disable networking with std.Options.

This change is necessary due to moving networking functionality to
be based on Io.Operation, which is a tagged union.
2026-03-08 19:20:34 -07:00
Andrew Kelley debf307594 update all incremental tests to new std API
they're still not passing on windows for some reason though
2025-12-23 22:15:11 -08:00
Andrew Kelley 77d2ad8c92 std: consolidate all instances of std.Io.Threaded into a singleton
It's better to avoid references to this global variable, but, in the
cases where it's needed, such as in std.debug.print and collecting stack
traces, better to share the same instance.
2025-12-23 22:15:11 -08:00
Andrew Kelley a91c6dc71d test: std.fs.File -> std.Io.File 2025-12-23 22:15:08 -08:00
Andrew Kelley c9a788c851 Revert "disable flaky test/incremental/add_decl"
This reverts commit d54fbc0123.

Since all incremental tests are flaky on Windows, this is reinstated and
all test-incremental tests will be skipped on Windows until the
flakiness is resolved.

Closes #26003
2025-11-22 08:09:12 -08:00
Andrew Kelley d54fbc0123 disable flaky test/incremental/add_decl
tracked by https://github.com/ziglang/zig/issues/26003
2025-11-21 19:53:33 -08:00
Jacob Young e1f3fc6ce2 Coff2: create a new linker from scratch 2025-10-02 17:44:52 -04:00
Jacob Young f58200e3f2 Elf2: create a new linker from scratch
This iteration already has significantly better incremental support.

Closes #24110
2025-09-21 14:09:14 -07:00
Jacob Young 375bc2d7b5 Dwarf: implement comptime-known extern values
Closes #24259
2025-08-15 18:29:06 -04:00
Andrew Kelley ae67c26cab disable failing incremental test cases due to dwarf linker logic
tracked by #24634
2025-07-31 22:10:11 -07:00
Andrew Kelley 38559e282b disable failing test
tracked by #24524
2025-07-21 12:32:37 -07:00
Andrew Kelley d8e26275f2 update standalone and incremental tests to new API 2025-07-07 22:43:53 -07:00
Jacob Young 857cd172d6 Dwarf: restore missing non-entry padding
Sections without padding were only supposed to omit entry padding.
Otherwise, the allocation behavior is degenerate.
2025-06-07 13:13:47 -04:00
mlugg c1a5caa454 compiler: combine @intCast safety checks
`castTruncatedData` was a poorly worded error (all shrinking casts
"truncate bits", it's just that we assume those bits to be zext/sext of
the other bits!), and `negativeToUnsigned` was a pointless distinction
which forced the compiler to emit worse code (since two separate safety
checks were required for casting e.g. 'i32' to 'u16') and wasn't even
implemented correctly. This commit combines those safety panics into one
function, `integerOutOfBounds`. The name maybe isn't perfect, but that's
not hugely important; what matters is the new default message, which is
clearer than the old ones: "integer does not fit in destination type".
2025-06-01 12:10:57 +01:00
mlugg 3d8e760552 Zcu: fix nav_ty dependency on nav_val
In the case where a declaration has no type annotation, the interaction
between resolution of `nav_ty` and `nav_val` is a little fiddly because
of the fact that resolving `nav_val` actually implicitly resolves the
type as well. This means `nav_ty` never gets an opporunity to mark its
dependency on the `nav_val`. So, `ensureNavValUpToDate` needs to be the
one to do it. It can't do it too early, though; otherwise, our marking
of dependees as out-of-date/up-to-date will go wrong.

Resolves: #23959
2025-05-25 05:50:26 +01:00
mlugg 37a9a4e0f1 compiler: refactor Zcu.File and path representation
This commit makes some big changes to how we track state for Zig source
files. In particular, it changes:

* How `File` tracks its path on-disk
* How AstGen discovers files
* How file-level errors are tracked
* How `builtin.zig` files and modules are created

The original motivation here was to address incremental compilation bugs
with the handling of files, such as #22696. To fix this, a few changes
are necessary.

Just like declarations may become unreferenced on an incremental update,
meaning we suppress analysis errors associated with them, it is also
possible for all imports of a file to be removed on an incremental
update, in which case file-level errors for that file should be
suppressed. As such, after AstGen, the compiler must traverse files
(starting from analysis roots) and discover the set of "live files" for
this update.

Additionally, the compiler's previous handling of retryable file errors
was not very good; the source location the error was reported as was
based only on the first discovered import of that file. This source
location also disappeared on future incremental updates. So, as a part
of the file traversal above, we also need to figure out the source
locations of imports which errors should be reported against.

Another observation I made is that the "file exists in multiple modules"
error was not implemented in a particularly good way (I get to say that
because I wrote it!). It was subject to races, where the order in which
different imports of a file were discovered affects both how errors are
printed, and which module the file is arbitrarily assigned, with the
latter in turn affecting which other files are considered for import.
The thing I realised here is that while the AstGen worker pool is
running, we cannot know for sure which module(s) a file is in; we could
always discover an import later which changes the answer.

So, here's how the AstGen workers have changed. We initially ensure that
`zcu.import_table` contains the root files for all modules in this Zcu,
even if we don't know any imports for them yet. Then, the AstGen
workers do not need to be aware of modules. Instead, they simply ignore
module imports, and only spin off more workers when they see a by-path
import.

During AstGen, we can't use module-root-relative paths, since we don't
know which modules files are in; but we don't want to unnecessarily use
absolute files either, because those are non-portable and can make
`error.NameTooLong` more likely. As such, I have introduced a new
abstraction, `Compilation.Path`. This type is a way of representing a
filesystem path which has a *canonical form*. The path is represented
relative to one of a few special directories: the lib directory, the
global cache directory, or the local cache directory. As a fallback, we
use absolute (or cwd-relative on WASI) paths. This is kind of similar to
`std.Build.Cache.Path` with a pre-defined list of possible
`std.Build.Cache.Directory`, but has stricter canonicalization rules
based on path resolution to make sure deduplicating files works
properly. A `Compilation.Path` can be trivially converted to a
`std.Build.Cache.Path` from a `Compilation`, but is smaller, has a
canonical form, and has a digest which will be consistent across
different compiler processes with the same lib and cache directories
(important when we serialize incremental compilation state in the
future). `Zcu.File` and `Zcu.EmbedFile` both contain a
`Compilation.Path`, which is used to access the file on-disk;
module-relative sub paths are used quite rarely (`EmbedFile` doesn't
even have one now for simplicity).

After the AstGen workers all complete, we know that any file which might
be imported is definitely in `import_table` and up-to-date. So, we
perform a single-threaded graph traversal; similar to what
`resolveReferences` plays for `AnalUnit`s, but for files instead. We
figure out which files are alive, and which module each file is in. If a
file turns out to be in multiple modules, we set a field on `Zcu` to
indicate this error. If a file is in a different module to a prior
update, we set a flag instructing `updateZirRefs` to invalidate all
dependencies on the file. This traversal also discovers "import errors";
these are errors associated with a specific `@import`. With Zig's
current design, there is only one possible error here: "import outside
of module root". This must be identified during this traversal instead
of during AstGen, because it depends on which module the file is in. I
tried also representing "module not found" errors in this same way, but
it turns out to be much more useful to report those in Sema, because of
use cases like optional dependencies where a module import is behind a
comptime-known build option.

For simplicity, `failed_files` now just maps to `?[]u8`, since the
source location is always the whole file. In fact, this allows removing
`LazySrcLoc.Offset.entire_file` completely, slightly simplifying some
error reporting logic. File-level errors are now directly built in the
`std.zig.ErrorBundle.Wip`. If the payload is not `null`, it is the
message for a retryable error (i.e. an error loading the source file),
and will be reported with a "file imported here" note pointing to the
import site discovered during the single-threaded file traversal.

The last piece of fallout here is how `Builtin` works. Rather than
constructing "builtin" modules when creating `Package.Module`s, they are
now constructed on-the-fly by `Zcu`. The map `Zcu.builtin_modules` maps
from digests to `*Package.Module`s. These digests are abstract hashes of
the `Builtin` value; i.e. all of the options which are placed into
"builtin.zig". During the file traversal, we populate `builtin_modules`
as needed, so that when we see this imports in Sema, we just grab the
relevant entry from this map. This eliminates a bunch of awkward state
tracking during construction of the module graph. It's also now clearer
exactly what options the builtin module has, since previously it
inherited some options arbitrarily from the first-created module with
that "builtin" module!

The user-visible effects of this commit are:
* retryable file errors are now consistently reported against the whole
  file, with a note pointing to a live import of that file
* some theoretical bugs where imports are wrongly considered distinct
  (when the import path moves out of the cwd and then back in) are fixed
* some consistency issues with how file-level errors are reported are
  fixed; these errors will now always be printed in the same order
  regardless of how the AstGen pass assigns file indices
* incremental updates do not print retryable file errors differently
  between updates or depending on file structure/contents
* incremental updates support files changing modules
* incremental updates support files becoming unreferenced

Resolves: #22696
2025-05-18 17:37:02 +01:00
Andrew Kelley 7bd3207921 make @memcpy and @memmove share panic handlers 2025-04-27 23:30:00 -07:00
dweiller 898ca82458 compiler: add @memmove builtin 2025-04-26 13:34:16 +10:00
mlugg 8c9c24e09b compiler: integrate @compileLog with incremental compilation
Compile log output is now separated based on the `AnalUnit` which
perfomred the `@compileLog` call, so that we can omit the output for
unreferenced ("dead") units. The units are also sorted when collecting
the `ErrorBundle`, so that compile logs are always printed in a
consistent order, like compile errors are. This is important not only
for incremental compilation, but also for parallel analysis.

Resolves: #23609
2025-04-20 18:11:53 +01:00
mlugg 6561a98a61 incremental: correctly handle dead exporters
Resolves: #23604
2025-04-20 18:11:53 +01:00
mlugg dd3f01eadf incremental: add test for ZON file without result type 2025-04-02 05:58:29 +01:00
Jacob Young b431e9af97 Elf: fix incrementally reallocating the last atom in a section 2025-03-31 19:06:25 -04:00
mlugg 501e84a96a incremental: invalidate namespace dependencies when a name changes visibility
We could have more fine-grained dependencies here, but I think this is
fine for now.
2025-03-03 22:18:02 +00:00
mlugg 5e20e9b449 Sema: allow @ptrCast of slices changing the length
Also, refactor `Sema.ptrCastFull` to not be a horrifying hellscape.
2025-02-23 08:28:58 +00:00
mlugg b21becb2a6 incremental: fix crash when introducing syntax error
Clearing the analysis roots was very clever and all, but not actually
valid. We need to avoid *any* reference to the analysis errors if there
were any fatal files, and that includes sorting the errors!

Resolves: #22774
2025-02-05 22:00:55 +00:00
mlugg d60910c9d0 incremental: add ZON test 2025-02-04 16:20:30 +00:00
Matthew Lugg 3767b08039 Merge pull request #22602 from mlugg/incr-embedfile
incremental: handle `@embedFile`
2025-01-26 01:41:56 +00:00
mlugg fcf8d5ada2 incr-check: check compile errors against expected
Also modifies all incremental cases using `#expect_error` to include the
errors and notes which are expected.
2025-01-25 09:32:40 +00:00
mlugg f47b8de2ad incremental: handle @embedFile
Uses of `@embedFile` register dependencies on the corresponding
`Zcu.EmbedFile`. At the start of every update, we iterate all embedded
files and update them if necessary, and invalidate the dependencies if
they changed.

In order to properly integrate with the lazy analysis model, failed
embed files are now reported by the `AnalUnit` which actually used
`@embedFile`; the filesystem error is stored in the `Zcu.EmbedFile`.

An incremental test is added covering incremental updates to embedded
files, and I have verified locally that dependency invalidation is
working correctly.
2025-01-25 06:07:08 +00:00
mlugg 5a6666db55 all: update for panic.unwrapError and panic.call signature changes 2025-01-24 22:33:23 +00:00
mlugg 83991efe10 compiler: yet more panic handler changes
* `std.builtin.Panic` -> `std.builtin.panic`, because it is a namespace.
* `root.Panic` -> `root.panic` for the same reason. There are type
  checks so that we still allow the legacy `pub fn panic` strategy in
  the 0.14.0 release.
* `std.debug.SimplePanic` -> `std.debug.simple_panic`, same reason.
* `std.debug.NoPanic` -> `std.debug.no_panic`, same reason.
* `std.debug.FormattedPanic` is now a function `std.debug.FullPanic`
  which takes as input a `panicFn` and returns a namespace with all the
  panic functions. This handles the incredibly common case of just
  wanting to override how the message is printed, whilst keeping nice
  formatted panics.
* Remove `std.builtin.panic.messages`; now, every safety panic has its
  own function. This reduces binary bloat, as calls to these functions
  no longer need to prepare any arguments (aside from the error return
  trace).
* Remove some legacy declarations, since a zig1.wasm update has
  happened. Most of these were related to the panic handler, but a quick
  grep for "zig1" brought up a couple more results too.

Also, add some missing type checks to Sema.

Resolves: #22584

formatted -> full
2025-01-24 19:29:51 +00:00
mlugg f7b9f84df2 incremental: fix enum resolution bugs 2025-01-18 14:30:06 +00:00
Jacob Young 257054a146 Revert "disable flaky incremental compilation tests"
This reverts commit 133abdeda2 but keeps
the tests disabled for the wasm target, which is the only configuration
that seems to fail, even though the error looks like a frontend error.
2025-01-16 20:02:49 -05:00
Andrew Kelley 133abdeda2 disable flaky incremental compilation tests
tracking issue #22510
2025-01-16 13:13:11 -08:00
Andrew Kelley 9285f91ccc wasm linker: incremental test coverage 2025-01-15 15:11:36 -08:00
Jacob Young dde3116e50 Dwarf: implement new incremental line number update API 2025-01-05 02:20:56 +00:00
mlugg 6cc848e9f6 incremental: add new test cases
These cover the fixes from the last few commits.
2025-01-04 07:51:19 +00:00
mlugg f0d5e0df4d incremental: fix errors not being deleted upon re-analysis
Previously, logic in `Compilation.getAllErrorsAlloc` was corrupting the
`failed_analysis` hashmap. This meant that on updates after the initial
update, attempts to remove entries from this map (because the `AnalUnit`
in question is being re-analyzed) silently failed. This resulted in
compile errors from earlier updates wrongly getting "stuck", i.e. never
being removed.

This commit also adds a few log calls which helped me to find this bug.
2025-01-01 15:49:37 +00:00
Jacob Young 11e54a3559 link: fix failing incremental test cases 2024-11-16 14:03:31 -05:00
mlugg 6e56bc1096 test: add new incremental case
This is similar to the old `llvm/shift_right_plus_left` case, which was
disabled by 1b1c78c. The case is not enabled on the LLVM backend, since
incremental compilation support for this backend is a work in progress
and is tracked by #21165. It passes on the x86_64-linux target with the
self-hosted backend.

Resolves: #12288
2024-11-11 12:22:55 +00:00