16860 Commits

Author SHA1 Message Date
Alex Rønne Petersen
f8372d030e
compiler: Classify libatomic as an alias for compiler-rt.
This is a library that ships with GCC and provides fallback implementations of
atomic intrinsics where necessary. Since we do the same in our compiler-rt
implementation, and since some build systems insist on passing -latomic even
for Clang (which zig cc masquerades as), just satisfy this dependency by way of
compiler-rt.

Closes #22165.
2024-12-06 14:51:22 +01:00
mlugg
9f086f84f5
Zcu: allow test declarations to be failed
The introduction of the `extended(astgen_error())` instruction allows a
`test` declaration to be unresolved, i.e. the declaration doesn't even
contain a `func`. I could modify AstGen to not do this, but it makes
more sense to just handle this case when collecting test functions.

Note that tests under incremental compilation are currently broken if
you ever remove all references to a test; this is tracked as a subtask
of #21165.
2024-12-06 06:22:32 +00:00
mlugg
8f849684f4
std.zig.Zir: improve instruction tracking
The main change here is to partition tracked instructions found within a
declaration. It's very unlikely that, for instance, a `struct { ... }`
type declaration was intentionally turned into a reification or an
anonymous initialization, so it makes sense to track things in a few
different arrays.

In particular, this fixes an issue where a `func` instruction could
wrongly be mapped to something else if the types of function parameters
changed. This would cause huge problems further down the pipeline; we
expect that if a `declaration` is tracked, and it previously contained a
`func`/`func_inferred`/`func_fancy`, then this instruction is either
tracked to another `func`/`func_inferred`/`func_fancy` instruction, or
is lost.

Also, this commit takes the opportunity to rename the functions actually
doing this logic. `Zir.findDecls` was a name that might have made sense
at some point, but nowadays, it's definitely not finding declarations,
and it's not *exclusively* finding type declarations. Instead, the point
is to find instructions which we want to track; hence the new name,
`Zir.findTrackable`.

Lastly, a nice side effect of partitioning the output of `findTrackable`
is that `Zir.declIterator` no longer needs to accept input instructions
which aren't type declarations (e.g. `reify`, `func`).
2024-12-05 19:58:42 +00:00
mlugg
7f3211a101
compiler: incremental compilation fixes
The previous commit exposed some bugs in incremental compilation. This
commit fixes those, and adds a little more logging for debugging
incremental compilation.

Also, allow `ast-check -t` to dump ZIR when there are non-fatal AstGen
errors.
2024-12-05 19:58:42 +00:00
mlugg
4d7818a76a
compiler: allow files with AstGen errors to undergo semantic analysis
This commit enhances AstGen to introduce a form of error resilience
which allows valid ZIR to be emitted even when AstGen errors occur.

When a non-fatal AstGen error (e.g. `appendErrorNode`) occurs, ZIR
generation is not affected; the error is added to `astgen.errors` and
ultimately to the errors stored in `extra`, but that doesn't stop us
getting valid ZIR. Fatal AstGen errors (e.g. `failNode`) are a bit
trickier. These errors return `error.AnalysisFail`, which is propagated
up the stack. In theory, any parent expression can catch this error and
handle it, continuing ZIR generation whilst throwing away whatever was
lost. For now, we only do this in one place: when creating declarations.
If a call to `fnDecl`, `comptimeDecl`, `globalVarDecl`, etc, returns
`error.AnalysisFail`, the `declaration` instruction is still created,
but its body simply contains the new `extended(astgen_error())`
instruction, which instructs Sema to terminate semantic analysis with a
transitive error. This means that a fatal AstGen error causes the
innermost declaration containing the error to fail, but the rest of the
file remains intact.

If a source file contains parse errors, or an `error.AnalysisFail`
happens when lowering the top-level struct (e.g. there is an error in
one of its fields, or a name has multiple declarations), then lowering
for the entire file fails. Alongside the existing `Zir.hasCompileErrors`
query, this commit introduces `Zir.loweringFailed`, which returns `true`
only in this case.

The end result here is that files with AstGen failures will almost
always still emit valid ZIR, and hence can undergo semantic analysis on
the parts of the file which are (from AstGen's perspective) valid. This
is a noteworthy improvement to UX, but the main motivation here is
actually incremental compilation. Previously, AstGen failures caused
lots of semantic analysis work to be thrown out, because all `AnalUnit`s
in the file required re-analysis so as to trigger necessary transitive
failures and remove stored compile errors which would no longer make
sense (because a fresh compilation of this code would not emit those
errors, as the units those errors applied to would fail sooner due to
referencing a failed file). Now, this case only applies when a file has
severe top-level errors, which is far less common than something like
having an unused variable.

Lastly, this commit changes a few errors in `AstGen` to become fatal
when they were previously non-fatal and vice versa. If there is still a
reasonable way to continue AstGen and lower to ZIR after an error, it is
non-fatal; otherwise, it is fatal. For instance, `comptime const`, while
redundant syntax, has a clear meaning we can lower; on the other hand,
using an undeclared identifer has no sane lowering, so must trigger a
fatal error.
2024-12-05 19:58:38 +00:00
Alex Rønne Petersen
cbc05e0b1d Compilation: Consider *.rlib files to be static libraries.
These are produced by rustc: https://rustc-dev-guide.rust-lang.org/backend/libs-and-metadata.html#rlib
2024-12-05 15:05:55 +01:00
Alex Rønne Petersen
d70be200d8
Merge pull request #22141 from alexrp/target-remove-bridgeos
`std.Target`: Remove `Os.Tag.bridgeos`
2024-12-05 15:05:25 +01:00
Alex Rønne Petersen
4e29c67eed
llvm: Remove dead targetArch() and targetOs() functions.
These were leftovers from when we used the LLVM API to create modules.
2024-12-03 20:43:20 +01:00
Alex Rønne Petersen
09b39f77b7
std.Target: Remove Os.Tag.bridgeos.
It doesn't appear that targeting bridgeOS is meaningfully supported by Apple.
Even LLVM/Clang appear to have incomplete support for it, suggesting that Apple
never bothered to upstream that support. So there's really no sense in us
pretending to support this.
2024-12-03 20:43:15 +01:00
Alex Kladov
6188cb8e50 sema: add a missing errdefer
This fix doesn't matter at all in the grand scheme of things, but I
think the story behind it is perhaps curious, as it might point at a
design flaw in the Sema's error reporting API. So, a story:

On lobsters, there's a rather heated discussion on the merits on RAII vs
defer. I don't really like participating in heating discussions, but
also sort of can't stop thinking about this.

My own personal experience with Zig's defer and errdefer is that they
are fiddly to get right consistency --- if a program has a lot of
resource management to do, I _always_ mess up at least one
defer/errdefer. I've found my internal peace by just avoiding
spread-out, "pox" resource management, and instead centralizing resource
ownership under one of the following patterns:

* Either the thing is acquired and released in main
* Or main allocates N instances of thing, and then the rest of the code
  explicitly juggles this finite pool of N. Notably, this juggling
  typically doesn't involve defer/errdefer at all, as, at this level of
  precision, there are no `try`s left, so you only code the happy path
* Or there's some sort of arena thing, where a bunch of resources have a
  single owner, the user's don' bother cleaning up their resources, and
  instead the owner does it once at the end.

So I wanted to make a lobster.rs comment in the vein of "yeah, if your
program is mostly about resource management, then Zig could be kinda a
pain, but that's friction tells you something: perhaps your program
shouldn't be about resource management, and instead it should be doing
what it is supposed to do?". And, as an evidence for my claim, I wanted
to point out some large body of Zig code which doesn't have a lot of
errdefers.

So, I cracked opened Sema.zig, `ctrl+f` for `defer`, saw whopping 400
something occupancies, and my heart skipped a bit. Looking at the
occurrences, _some_ of them were non-resource-related usages of defer.
But a lot of them were the following pattern:

```zig
const msg = try sema.errMsg(src, "comptime control flow inside runtime block", .{});
errdefer msg.destroy(sema.gpa);
```

This is exactly the thing that I know _I_ can't get right consistently!
So, at this point, I made a prediction that at least one of `errdefer`s
is missing. So, I looked at the first few `const msg = try` and of
course found one without `errdefer`.

I am at 0.8 that, even with this PR applied, the claim will still stand
--- there will be `errdefer` missing. So it feels like some API
re-design is in order, to make sure individual error messages are not
resources.

Could Sema just own all partially-constructed error messages, and, at a
few known safe-points:

* if the control flow is normal, assert that there are no in-progress
  error messages
* if we are throwing an error, go and release messages immediately?

I am unlikely to do the actual refactor here, but I think it's worth
highlighting the overall pattern here.

PS: I am only 0.9 sure that what I've found is indeed a bug! I don't
understand the code, I did a dumb text search, so I _could_ have made a
fool of myself here :P
2024-12-03 14:18:26 -05:00
Jakub Konka
29c7f6810f macho: fix 32bit builds 2024-12-02 22:05:21 -05:00
Jakub Konka
0769afbb0f macho: refactors errors from parsing DWARF
Currently we don't report any errors to the user due to a bug in
self-hosted x86_64-macos backend.
2024-12-02 22:05:21 -05:00
Jakub Konka
c824b35051 macho: move things around in MachO/Object.zig and refactor 2024-12-02 22:05:21 -05:00
Jakub Konka
8e81500051 macho: handle DWARFv5 when parsing debug info in objects 2024-12-02 22:05:21 -05:00
Jakub Konka
808306f49a macho: rename dwarf.zig to Dwarf.zig
Separate commit since macOS is case-insensitive by default and so
I had to do it from Linux.
2024-12-02 22:05:21 -05:00
Jacob Young
c013f45ad0 coff: fix memory leak 2024-12-02 16:01:58 -05:00
Tw
aa7d138462 zig fetch: add missing path separator in error message
Signed-off-by: Tw <tw19881113@gmail.com>
2024-11-29 18:50:21 -05:00
Andrew Kelley
a47aa9dd9d
Merge pull request #22095 from alexrp/test-llvm-emit
Change `llvm_targets` tests to actually emit objects, and fix bugs found as a result
2024-11-29 15:28:54 -05:00
mlugg
c3821fe4ca compiler: use @Type instead of @TypeOf to print enum literal type 2024-11-29 15:26:58 -05:00
Justin Braben
07cd488d42
Add build option to set tracy-callstack-depth in build.zig (#21990) 2024-11-29 12:13:06 -08:00
Andrew Kelley
cfdb001a8f
Merge pull request #22099 from Rexicon226/fix-cat-mul
change `++` and `**` to not return mutable pointers
2024-11-29 15:05:49 -05:00
David Rubin
1d78d4f8c1
sema: hotpath ++ and ** for array-pointers 2024-11-28 18:05:36 -08:00
Alex Rønne Petersen
7361f0bafa
link.MachO: Don't try to get a semver value for bridgeos. 2024-11-28 22:04:00 +01:00
Alex Rønne Petersen
f0f2dc52cc
llvm: Lower ohoseabi to ohos instead of verbatim.
LLVM doesn't recognize ohoseabi.
2024-11-28 22:04:00 +01:00
Alex Rønne Petersen
8594f179f9
Merge pull request #22067 from alexrp/pie-tests
Add PIC/PIE tests and fix some bugs + some improvements to the test harness
2024-11-28 14:07:28 +01:00
David Rubin
bc3094b278
sema: make ++ and ** return immutable pointers 2024-11-27 20:39:23 -08:00
Justin Braben
d16a9b0acb
std.os.windows: Map PIPE_NOT_AVAILABLE from OpenFile() to error.NoDevice (#21938) 2024-11-27 22:33:29 +01:00
Andrew Kelley
11bf2d92de diversify "unable to spawn" failure messages
to help understand where a spurious failure is occurring
2024-11-26 13:56:40 -08:00
David Rubin
a6af55cc6e ip: cleanup @constCast usages 2024-11-25 18:41:36 -05:00
Jacob Young
c894ac09a3 dwarf: fix stepping through an inline loop containing one statement
Previously, stepping from the single statement within the loop would
always exit the loop because all of the code unrolled from the loop is
associated with the same line and treated by the debugger as one line.
2024-11-24 17:28:12 -05:00
Alex Rønne Petersen
24ecf45569
std.Target: Add Os.HurdVersionRange for Os.Tag.hurd.
This is necessary since isGnuLibC() is true for hurd, so we need to be able to
represent a glibc version for it.

Also add an Os.TaggedVersionRange.gnuLibCVersion() convenience function.
2024-11-24 22:11:16 +01:00
Andrew Kelley
ed8955cea6
Merge pull request #22049 from alexrp/musl-gen-stubs
`gen_stubs`: No compiler-rt hacks, more compact output, and more arch support
2024-11-23 23:50:36 -05:00
Alex Rønne Petersen
aef5c75602 compiler: Disallow align(0) everywhere in the language.
Thus leaving the design space for this alignment value open, e.g. for packing.
2024-11-23 18:44:07 -05:00
Alex Rønne Petersen
57e4fa14bb
musl: Define TIME32 and FAMILY_* macros for libc.S as appropriate.
Also adjust ARCH_* logic for the updated gen_stubs.zig tool.
2024-11-23 03:10:58 +01:00
David Rubin
66a7c09def link: use target to determine risc-v eflag validity 2024-11-22 19:34:47 -05:00
Alex Rønne Petersen
8b2b9aa019
Compilation: Consider *.lo files to be object files.
Fixes musl libc.so compilation with zig cc.
2024-11-23 01:22:20 +01:00
Shawn Gao
dafe1a910d Append disabled LLVM CPU features after enabled ones 2024-11-20 10:09:03 +01:00
xdBronch
5f3a70ed5f Fix peer type resolution with allowzero pointers 2024-11-20 02:09:50 +02:00
Alex Rønne Petersen
94be75a94f Compilation: Re-enable LTO for RISC-V. 2024-11-19 12:04:42 +01:00
Ian Johnson
dceab4502a zig fetch: handle redirects for Git packages
Closes #21976
2024-11-19 00:35:00 -08:00
Andrew Kelley
fbcb00fbb3
Merge pull request #22004 from jacobly0/fix-self-llvm
fix llvm-enabled compiler builds with the self-hosted backend
2024-11-18 16:44:12 -08:00
Alex Rønne Petersen
e6d2e16413 Compilation: Disable LTO for all ILP32-on-LP64 ABIs.
Extension of 3a6a8b8aa540413d099a6f41a0d8f882f22acb45 to all similar ABIs. The
LLD issue affects them all.
2024-11-18 19:10:14 +01:00
Alex Rønne Petersen
3a6a8b8aa5 Compilation: Disable LTO for mips n32.
See: https://github.com/llvm/llvm-project/pull/116537
2024-11-17 15:15:59 +01:00
Jacob Young
5be8a5fe5f link: fix memory bugs 2024-11-16 21:29:17 -05:00
Jacob Young
a8ec306b49 Sema: fix peer resolution alignment between slice and empty struct
An empty struct that coerces to an empty array should not force
`align(1)` on the resulting slice type.
2024-11-16 21:22:57 -05:00
Jacob Young
7266d4497e
Merge pull request #21999 from jacobly0/incr-cases
link: fix failing incremental test cases
2024-11-16 20:32:02 -05:00
Benjamin
7cc7ae1fd3
Use options debug format if it is provided (#21995)
Co-authored-by: Alex Rønne Petersen <alex@alexrp.com>
2024-11-16 19:32:11 +00:00
Jacob Young
11e54a3559 link: fix failing incremental test cases 2024-11-16 14:03:31 -05:00
Jacob Young
96552638ae dev: support incremental for x86_64-linux env 2024-11-16 11:49:49 -05:00
Carl Åstholm
4fc295dc02 Take eagerness into account when deduplicating dependencies
If the same dependency is first found as lazy and then later as eager,
the existing entry needs to be updated to eager in order for
`b.dependency()` to work.
2024-11-13 14:59:46 -08:00