1261 Commits

Author SHA1 Message Date
Andrew Kelley
d12123a88c std.ArrayList: initial capacity based on cache line size
also std.MultiArrayList
2025-02-13 00:19:03 -08:00
Andrew Kelley
d789f1e5cf fuzzer: write inputs to shared memory before running
breaking change to the fuzz testing API; it now passes a type-safe
context parameter to the fuzz function.

libfuzzer is reworked to select inputs from the entire corpus.

I tested that it's roughly as good as it was before in that it can find
the panics in the simple examples, as well as achieve decent coverage on
the tokenizer fuzz test.

however I think the next step here will be figuring out why so many
points of interest are missing from the tokenizer in both Debug and
ReleaseSafe modes.

does not quite close #20803 yet since there are some more important
things to be done, such as opening the previous corpus, continuing
fuzzing after finding bugs, storing the length of the inputs, etc.
2025-02-11 13:39:20 -08:00
Andrew Kelley
1d8857bbe3
Merge pull request #22695 from alexrp/glibc-2-41
glibc 2.41
2025-02-10 17:32:55 -08:00
mlugg
0f38558435
compiler: provide result type to sentinel expression in slice operation
Resolves: #21867
2025-02-05 19:36:14 +00:00
Matthew Lugg
f01f1e33c9
Merge pull request #22754 from mlugg/files-and-stuff
ZON and incremental bits
2025-02-05 12:17:13 +00:00
Will Lillis
cf059ee087
AstGen: improve error for invalid bytes in strings and comments 2025-02-05 11:10:11 +02:00
mlugg
55a2e535fd
compiler: integrate ZON with the ZIR caching system
This came with a big cleanup to `Zcu.PerThread.updateFile` (formerly
`astGenFile`).

Also, change how the cache manifest works for files in the import table.
Instead of being added to the manifest when we call `semaFile` on them,
we iterate the import table after running the AstGen workers and add all
the files to the cache manifest then.

The downside is that this is a bit more eager to include files in the
manifest; in particular, files which are imported but not actually
referenced are now included in analysis. So, for instance, modifying any
standard library file will invalidate all Zig compilations using that
standard library, even if they don't use that file.

The original motivation here was simply that the old logic in `semaFile`
didn't translate nicely to ZON. However, it turns out to actually be
necessary for correctness. Because `@import("foo.zig")` is an
AstGen-level error if `foo.zig` does not exist, we need to invalidate
the cache when an imported but unreferenced file is removed to make sure
this error is triggered when it needs to be.

Resolves: #22746
2025-02-04 16:20:29 +00:00
Mason Remaley
13c6eb0d71
compiler,std: implement ZON support
This commit allows using ZON (Zig Object Notation) in a few ways.

* `@import` can be used to load ZON at comptime and convert it to a
  normal Zig value. In this case, `@import` must have a result type.
* `std.zon.parse` can be used to parse ZON at runtime, akin to the
  parsing logic in `std.json`.
* `std.zon.stringify` can be used to convert arbitrary data structures
  to ZON at runtime, again akin to `std.json`.
2025-02-03 09:14:37 +00:00
Andrew Kelley
963651bbf2
Merge pull request #22672 from jacobly0/x86_64-rewrite
x86_64: rewrite float conversions
2025-02-01 14:32:43 -08:00
mlugg
3924f173af compiler: do not propagate result type to try operand
This commit effectively reverts 9e683f0, and hence un-accepts #19777.
While nice in theory, this proposal turned out to have a few problems.

Firstly, supplying a result type implicitly coerces the operand to this
type -- that's the main point of result types! But for `try`, this is
actually a bad idea; we want a redundant `try` to be a compile error,
not to silently coerce the non-error value to an error union. In
practice, this didn't always happen, because the implementation was
buggy anyway; but when it did, it was really quite silly. For instance,
`try try ... try .{ ... }` was an accepted expression, with the inner
initializer being initially coerced to `E!E!...E!T`.

Secondly, the result type inference here didn't play nicely with
`return`. If you write `return try`, the operand would actually receive
a result type of `E!E!T`, since the `return` gave a result type of `E!T`
and the `try` wrapped it in *another* error union. More generally, the
problem here is that `try` doesn't know when it should or shouldn't
nest error unions. This occasionally broke code which looked like it
should work.

So, this commit prevents `try` from propagating result types through to
its operand. A key motivation for the original proposal here was decl
literals; so, as a special case, `try .foo(...)` is still an allowed
syntax form, caught by AstGen and specially lowered. This does open the
doors to allowing other special cases for decl literals in future, such
as `.foo(...) catch ...`, but those proposals are for another time.

Resolves: #21991
Resolves: #22633
2025-02-01 15:48:45 +00:00
Jacob Young
b9531f5de6 x86_64: rewrite float vector conversions 2025-01-31 23:00:34 -05:00
Alex Rønne Petersen
6fcf8e6809
std.zig.target: Fix glibc runtime triple for x86-linux-gnu. 2025-01-31 14:00:32 +01:00
Alex Rønne Petersen
7d699be772 std.zig.target: Remove some library names from isLibCLibName() for MinGW.
These are system DLLs, most of which MinGW provides .def files for. It just so
happens that MinGW also has some static libraries by the same name which link in
some GUID definitions.

The remaining non-MinGW library names represent libraries that are always
statically linked, so if those are requested by the user, it makes sense to
error if libc is not linked. A future enhancement could be to compile those
independent of mingw32.lib, however.

Closes #22560.
2025-01-29 21:38:34 +01:00
Alex Rønne Petersen
7843deb16b std.zig.system.darwin: Add driverkit handling in getSdk(). 2025-01-29 09:25:06 +01:00
Meghan Denny
92b20e4216 stf: do not call xcode-select or xcrun by absolute path in darwin sdk detection 2025-01-26 09:07:39 +01:00
Jacob Young
c7433212d1 x86_64: rewrite scalar and vector int @min and @max 2025-01-24 21:02:32 -05:00
Jacob Young
b1fa89439a x86_64: rewrite float vector @abs and equality comparisons 2025-01-24 20:56:11 -05:00
Andrew Kelley
0bacb79c09 Revert "Merge pull request #21540 from BratishkaErik/search-env-in-path"
It caused an assertion failure when building Zig from source.

This reverts commit 0595feb34128db22fbebea843af929de3ede8190, reversing
changes made to 744771d3303e122474a72c8a94b14fe1e9fb480c.

closes #22566
closes #22568
2025-01-21 11:22:28 -08:00
Andrew Kelley
0595feb341
Merge pull request #21540 from BratishkaErik/search-env-in-path
std.zig.system: use both PATH and hardcoded locations to find `env`
2025-01-21 00:16:19 -05:00
mlugg
0ec6b2dd88 compiler: simplify generic functions, fix issues with inline calls
The original motivation here was to fix regressions caused by #22414.
However, while working on this, I ended up discussing a language
simplification with Andrew, which changes things a little from how they
worked before #22414.

The main user-facing change here is that any reference to a prior
function parameter, even if potentially comptime-known at the usage
site or even not analyzed, now makes a function generic. This applies
even if the parameter being referenced is not a `comptime` parameter,
since it could still be populated when performing an inline call. This
is a breaking language change.

The detection of this is done in AstGen; when evaluating a parameter
type or return type, we track whether it referenced any prior parameter,
and if so, we mark this type as being "generic" in ZIR. This will cause
Sema to not evaluate it until the time of instantiation or inline call.

A lovely consequence of this from an implementation perspective is that
it eliminates the need for most of the "generic poison" system. In
particular, `error.GenericPoison` is now completely unnecessary, because
we identify generic expressions earlier in the pipeline; this simplifies
the compiler and avoids redundant work. This also entirely eliminates
the concept of the "generic poison value". The only remnant of this
system is the "generic poison type" (`Type.generic_poison` and
`InternPool.Index.generic_poison_type`). This type is used in two
places:

* During semantic analysis, to represent an unknown result type.
* When storing generic function types, to represent a generic parameter/return type.

It's possible that these use cases should instead use `.none`, but I
leave that investigation to a future adventurer.

One last thing. Prior to #22414, inline calls were a little inefficient,
because they re-evaluated even non-generic parameter types whenever they
were called. Changing this behavior is what ultimately led to #22538.
Well, because the new logic will mark a type expression as generic if
there is any change its resolved type could differ in an inline call,
this redundant work is unnecessary! So, this is another way in which the
new design reduces redundant work and complexity.

Resolves: #22494
Resolves: #22532
Resolves: #22538
2025-01-21 02:41:42 +00:00
Eric Joldasov
a79e421e59
std.zig.system: use both PATH and hardcoded locations to find env
Should help systems that have main `env` binary in different location
than hardcoded `/usr/bin/env` **during build** (not neccessarily always),
like Nix/Guix, Termux, Gentoo Prefix etc.

Related:
https://www.github.com/ziglang/zig/issues/12156
https://www.github.com/ziglang/zig/issues/14146
https://www.github.com/ziglang/zig/issues/14577
https://www.github.com/ziglang/zig/issues/15898

Source for logic: https://www.github.com/ziglang/zig/issues/14146#issuecomment-2308984936

Signed-off-by: Eric Joldasov <bratishkaerik@landless-city.net>
2025-01-20 14:29:04 +05:00
Alex Rønne Petersen
b074fb7dda std.zig.system.x86: Update Intel/AMD model detection. 2025-01-19 23:42:39 +01:00
Jacob Young
8ee80d61f6 x86_64: add a bunch of instruction encodings
Closes #19773
2025-01-18 23:30:45 -05:00
Alex Rønne Petersen
9758371419
std.zig.system: Move CPU feature hacks after ABI detection.
This was accidentally broken in #22434.
2025-01-18 12:59:38 +01:00
Jacob Young
e5d5a8bc4e x86_64: implement switch jump tables 2025-01-16 20:42:08 -05:00
Jacob Young
ac1a975f9b x86_64: implement clz and not 2025-01-16 20:42:08 -05:00
mlugg
9804cc8bc6
all: update to std.builtin.Type.{Pointer,Array,StructField} field renames 2025-01-16 12:49:58 +00:00
mlugg
d00e05f186
all: update to std.builtin.Type.Pointer.Size field renames
This was done by regex substitution with `sed`. I then manually went
over the entire diff and fixed any incorrect changes.

This diff also changes a lot of `callconv(.C)` to `callconv(.c)`, since
my regex happened to also trigger here. I opted to leave these changes
in, since they *are* a correct migration, even if they're not the one I
was trying to do!
2025-01-16 12:46:29 +00:00
Andrew Kelley
4fccb5ae7a wasm linker: improve error messages by making source locations more lazy 2025-01-15 15:11:36 -08:00
mlugg
04c9f50aec compiler: improve "... contains reference to comptime var" errors
`Sema.explainWhyValueContainsReferenceToComptimeVar` (concise name!)
adds notes to an error explaining how to get from a given `Value` to a
pointer to some `comptime var` (or a comptime field). Previously, this
error could be very opaque in any case where it wasn't obvious where the
comptime var pointer came from; particularly for type captures. Now, the
error notes explain this to the user.
2025-01-11 08:54:47 +00:00
mlugg
e9bd2d45d4
Sema: rewrite semantic analysis of function calls
This rewrite improves some error messages, hugely simplifies the logic,
and fixes several bugs. One of these bugs is technically a new rule
which Andrew and I agreed on: if a parameter has a comptime-only type
but is not declared `comptime`, then the corresponding call argument
should not be *evaluated* at comptime; only resolved. Implementing this
required changing how function types work a little, which in turn
required allowing a new kind of function coercion for some generic use
cases: function coercions are now allowed to implicitly *remove*
`comptime` annotations from parameters with comptime-only types. This is
okay because removing the annotation affects only the call site.

Resolves: #22262
2025-01-09 06:46:47 +00:00
Des-Nerger
6a21d18adf -fwine: allow 32-bit .exe's be run on backward-compatible 64-bit arches
Closes #22361.
2025-01-07 19:03:40 +01:00
David Rubin
fc28a71d9f Target: update the extra features before resolving the dynamic linker 2025-01-07 02:58:10 +01:00
Alex Rønne Petersen
f54f58b60f
std.zig.system: Handle thumb/thumbeb for QEMU in getExternalExecutor(). 2025-01-06 15:37:25 +01:00
Alex Rønne Petersen
749e67dee8
std.zig.system: Reject x32 for QEMU in getExternalExecutor(). 2025-01-06 15:37:23 +01:00
mlugg
f818098971
incremental: correctly return error.AnalysisFail when type structure changes
`Zcu.PerThead.ensureTypeUpToDate` is set up in such a way that it only
returns the updated type the first time it is called. In general, that's
okay; however, the exception is that we want the function to continue
returning `error.AnalysisFail` when the type has been lost, or its
number of captures changed.

Therefore, the check for this case now happens before the up-to-date
success return.

For simplicity, the number of captures is now handled by intentionally
losing the instruction in `Zcu.mapOldZirToNew`, since there is nothing
to gain from tracking a type when old instances of it can never be
reused.
2025-01-04 05:44:29 +00:00
mlugg
b4da8eef2a Zir: split up start and end of range in for_len
The old lowering was kind of neat, but it unintentionally allowed the
syntax `for (123) |_| { ... }`, and there wasn't really a way to fix
that. So, instead, we include both the start and the end of the range in
the `for_len` instruction (each operand to `for` now has *two* entries
in this multi-op instruction). This slightly increases the size of ZIR
for loops of predominantly indexables, but the difference is small
enough that it's not worth complicating ZIR to try and fix it.
2025-01-03 22:28:37 +00:00
Tangtang Zhou
fed8df9217 fix: windows libc lib paths for x86-windows-msvc 2025-01-01 23:47:39 +01:00
mlugg
ba78d79228
Zir: fix instruction tracking when function signatures are given 2025-01-01 12:19:14 +00:00
mlugg
d6120cf6a9
AstGen: better block_comptime elision 2024-12-31 09:55:03 +00:00
mlugg
71bcbd5993
AstGen: add missing comptimeExpr calls
Some sub-expressions should always be evaluated at comptime -- in
particular, type expressions, e.g. `E` in `E!T`. However, bugs in this
logic are easy to miss, because the parent scope is usually comptime
anyway!
2024-12-31 09:55:03 +00:00
mlugg
9a70eeeac5
compiler: ensure local consts in comptime scope are comptime-known
This fixes a bug which exposed a compiler implementation detail (ZIR
alloc elision). Previously, `const` declarations with a runtime-known
value in a comptime scope were permitted only if AstGen was able to
elide the alloc in ZIR, since the error was reported by storing to the
comptime alloc.

This just adds a new instruction to also emit this error when the alloc
is elided.
2024-12-31 09:55:03 +00:00
mlugg
6026a5f217
compiler: ensure result of block_comptime is comptime-known
To avoid this PR regressing error messages, most of the work here has
gone towards improving error notes for why code was comptime-evaluated.
ZIR `block_comptime` now stores a "comptime reason", the enum for which
is also used by Sema. There are two types in Sema:

* `ComptimeReason` represents the reason we started evaluating something
  at comptime.
* `BlockComptimeReason` represents the reason a given block is evaluated
  at comptime; it's either a `ComptimeReason` with an attached source
  location, or it's because we're in a function which was called at
  comptime (and that function's `Block` should be consulted for the
  "parent" reason).

Every `Block` stores a `?BlockComptimeReason`. The old `is_comptime`
field is replaced with a trivial `isComptime()` method which returns
whether that reason is non-`null`.

Lastly, the handling for `block_comptime` has been simplified. It was
previously going through an unnecessary runtime-handling path; now, it
is a trivial sub block exited through a `break_inline` instruction.

Resolves: #22296
2024-12-31 09:55:03 +00:00
Techatrix
5b6326ec65
fix slice of slice with sentinel on the lhs slice
example:
```zig
test {
    var foo: [2:0]u8 = .{ 1, 2 };
    _ = foo[0.. :1][0..2];
}
```

A `.slice_open` ast node will not have a end index nor sentinel.
2024-12-29 07:00:39 +01:00
Techatrix
5d51d4474a
fix slice of slice with sentinel but no end index
example:
```zig
test {
	var foo = "";
	_ = foo[0..][0.. :0];
}
```

A `.slice_sentinel` ast node may not have an end index.
2024-12-29 07:00:35 +01:00
Techatrix
2e40a1d22e
simplify AstGen handling of slicing syntax 2024-12-29 00:47:24 +01:00
mlugg
01081cc8e8 AstGen: lower function addrspace expression correctly
Also, add some basic behavior tests for addrspace and linksection which
would have caught this bug in CI.
2024-12-28 02:15:00 +00:00
mlugg
18362ebe13
Zir: refactor declaration instruction representation
The new representation is often more compact. It is also more
straightforward to understand: for instance, `extern` is represented on
the `declaration` instruction itself rather than using a special
instruction. The same applies to `var`, making both of these far more
compact.

This commit also separates the type and value bodies of a `declaration`
instruction. This is a prerequisite for #131.

In general, `declaration` now directly encodes details of the syntax
form used, and the embedded ZIR bodies are for actual expressions. The
only exception to this is functions, where ZIR is effectively designed
as if we had #1717. `extern fn` declarations are modeled as
`extern const` with a function type, and normal `fn` definitions are
modeled as `const` with a `func{,_fancy,_inferred}` instruction. This
may change in the future, but improving on this was out of scope for
this commit.
2024-12-23 21:09:17 +00:00
Alex Rønne Petersen
f06ca14cb5
Merge pull request #22225 from alexrp/libc-linux-os-version
Attach minimum Linux versions to provided libcs + incorporate ABI in `VersionRange.default()`
2024-12-23 19:42:53 +01:00
mlugg
7408679234
compiler: disallow callconv etc from depending on function parameters
Resolves: #22261
2024-12-18 23:06:35 +00:00