16860 Commits

Author SHA1 Message Date
mlugg
3e9810266b
compiler: add some missing consts
The previous commit exposed some missing `const` qualifiers in a few
places. These mutable slices could have been used to store invalid
values into memory!
2024-12-16 14:53:54 +00:00
mlugg
f154cd1fdc
Sema: disallow unsafe in-memory coercions
The error messages here aren't amazing yet, but this is an improvement
on status quo, because the current behavior allows false negative
compile errors, so effectively miscompiles.

Resolves: #15874
2024-12-16 14:53:54 +00:00
Matthew Lugg
3a0a9aa9b8
Merge pull request #22245 from mlugg/zir-no-doc-comments
compiler: remove doc comments from Zir
2024-12-16 02:48:28 +00:00
Alex Rønne Petersen
8f330ab70e mingw: Fix CFLAGS for winpthreads.
It should not use the CRT ones, and it also needs a few flags of its own that I
forgot to add in #22156.

Follow-up fix for #10989.
2024-12-16 01:56:55 +01:00
Andrew Kelley
aa0377794d
Merge pull request #22233 from jacobly0/fix-relocs
Elf.Atom: fix truncated dyn abs relocs
2024-12-15 15:48:24 -05:00
mlugg
847c34ac66
compiler: remove doc comments from Zir
This code was left over from the legacy Autodoc implementation. No
component of the compiler pipeline actually requires doc comments, so it
is a waste of time and space to store them in ZIR.
2024-12-15 16:32:55 +00:00
mlugg
72ba7f7e98
Sema: disallow runtime stores to pointers with comptime-only element types 2024-12-15 11:09:04 +00:00
mlugg
b5d3db5fc6
Sema: do not allow coercing undefined to opaque types 2024-12-15 11:09:04 +00:00
Alex Rønne Petersen
5f34224b2b
zig cc: Remove broken CUDA C/C++ support. 2024-12-15 05:45:53 +01:00
Jacob Young
a53a8ab25e Elf: fix shdr size getting out of sync with the actual size 2024-12-14 23:20:15 -05:00
Jacob Young
6d97185bc6 Elf.Atom: fix truncated dyn abs relocs 2024-12-14 23:20:15 -05:00
Alex Rønne Petersen
af89bb05d3
Merge pull request #22224 from alexrp/cc-args
`Compilation`: Clean up `addCCArgs()` + some minor improvements
2024-12-15 04:53:24 +01:00
David Rubin
09a8fa2120 ensure InstMap capacity before remapping error code 2024-12-14 16:41:17 -05:00
Andrew Kelley
13a28345bb
Merge pull request #22222 from ianprime0509/git-sha256
zig fetch: add support for SHA-256 Git repositories
2024-12-14 16:34:47 -05:00
Alex Rønne Petersen
2c192f8882
mingw: Update sources list to dcd7fefc703fb4b12187235386900d34cc13fdc5. 2024-12-14 07:02:14 +01:00
Alex Rønne Petersen
39c4efa2a7
Compilation: Clean up addCCArgs().
The goal of this commit is to get rid of some "unused command line argument"
warnings that Clang would give for various file types previously. This cleanup
also has the side effect of making the order of flags more understandable,
especially as it pertains to include paths.

Since a lot of code was shuffled around in this commit, I recommend reviewing
the old and new versions of the function side-by-side rather than trying to make
sense of the diff.
2024-12-14 06:49:45 +01:00
Ian Johnson
5217da57fc zig fetch: support SHA-256 Git repositories
Closes #21888
2024-12-13 08:49:45 -05:00
Ian Johnson
62cc81a63a Remove copy of HashedWriter
It was added to the standard library in #18733.
2024-12-13 08:49:45 -05:00
Carl Åstholm
de3db8a1a6 Fix undefined behavior in package extraction code 2024-12-13 08:49:45 -05:00
Carl Åstholm
b352595aa2 Add compiler internals tests
There are several test decls inside `/src` that are not currently being
tested and have bitrotted as a result. This commit revives those tests
and adds the `test-compiler-internals` set of tests which tests
everything reachable from `/src/main.zig`.
2024-12-13 08:49:02 -05:00
Alex Rønne Petersen
d74e87aab1
Compilation: Use Clang dependency file for preprocessed assembly files. 2024-12-13 06:48:35 +01:00
Alex Rønne Petersen
12a289c1dd
Compilation: Use a better canonical file extension for header files. 2024-12-13 06:48:35 +01:00
Alex Rønne Petersen
88f324ebe7
Compilation: Override Clang's language type for header files.
Clang seems to treat them as linker input without this.
2024-12-13 06:48:32 +01:00
Alex Rønne Petersen
b6ece854c9
Compilation: Improve classification of various C/C++/Objective-C files. 2024-12-13 05:05:36 +01:00
Alex Rønne Petersen
130f7c2ed8
Merge pull request #22035 from alexrp/unwind-fixes
Better unwind table support + unwind protection in `_start()` and `clone()`
2024-12-13 03:09:24 +01:00
Andrew Kelley
7ff42eff91 std.Build.Cache.hit: work around macOS kernel bug
The previous commit cast doubt upon the initial report about macOS
kernel behavior, identifying another reason that ENOENT could be
returned from file creation.

However, it is demonstrable that ENOENT can be returned for both cases:
1. create file race
2. handle refers to deleted directory

This commit re-introduces the workaround for the file creation race on
macOS however it does not unconditionally retry - it first tries again
with O_EXCL to disambiguate the error condition that has occurred.
2024-12-11 11:56:44 -08:00
Andrew Kelley
d37ee79535 std.Build.Cache.hit: more discipline in error handling
Previous commits

2b0929929d67e222ca6a9523a3a594ed456c4a51
4ea2f441df36cec61e1017f4d795d4037326c98c

had this text:

> There are no dir components, so you would think that this was
> unreachable, however we have observed on macOS two processes racing to
> do openat() with O_CREAT manifest in ENOENT.

This appears to have been a misunderstanding based on the issue
report #12138 and corresponding PR #12139 in which the steps to
reproduce removed the cache directory in a loop which also executed
detached Zig compiler processes.

There is no evidence for the macOS kernel bug however the ENOENT is
easily explained by the removal of the cache directory.

This commit reverts those commits, ultimately reporting the ENOENT as an
error rather than repeating the create file operation. However this
commit also adds an explicit error set to `std.Build.Cache.hit` as well
as changing the `failed_file_index` to a proper diagnostic field that
fully communicates what failed, leading to more informative error
messages on failure to check the cache.

The equivalent failure when occuring for AstGen performs a fatal process
kill, reasoning being that the compiler has an invariant of the cache
directory not being yanked out from underneath it while executing. This
could be made a more granular error in the future but I suspect such
thing is not valuable to pursue.

Related to #18340 but does not solve it.
2024-12-10 18:11:12 -08:00
Alex Rønne Petersen
8af82621d7
compiler: Improve the handling of unwind table levels.
The goal here is to support both levels of unwind tables (sync and async) in
zig cc and zig build. Previously, the LLVM backend always used async tables
while zig cc was partially influenced by whatever was Clang's default.
2024-12-11 00:10:15 +01:00
Alex Rønne Petersen
0b67463b92
riscv64: Support the fp alias for register s0 in inline assembly. 2024-12-11 00:09:22 +01:00
Travis Lange
414fc3b207 fix unknown file extension with rmeta 2024-12-10 21:12:55 +01:00
Andrew Kelley
7575f21212
Merge pull request #22157 from mlugg/astgen-error-lazy
compiler: allow semantic analysis of files with AstGen errors
2024-12-09 18:32:23 -05:00
Andrew Kelley
8245d7fac0
Merge pull request #22164 from mlugg/astgen-ref-dedup
AstGen: correctly deduplicate `ref` of `param` and `alloc_inferred`
2024-12-09 18:04:26 -05:00
mlugg
3aab81b0ab Builtin: clean up formatting of generated code
This commit simply tweaks the generated `builtin.zig` source code to be
more consistent and in line with current conventions.
2024-12-09 17:35:10 -05:00
Alex Rønne Petersen
7cd2c1ce87
Merge pull request #22191 from alexrp/wasi-libc-names
`std.zig.target`: Teach `isLibCLibName()` about emulated wasi-libc libraries
2024-12-09 10:46:56 +01:00
Alex Rønne Petersen
589f4e9170
Merge pull request #22190 from alexrp/cc-args
`Compilation`: Don't rely on Clang defaults for options that are user-facing.
2024-12-09 10:46:31 +01:00
Alex Rønne Petersen
f283791a5e
compiler: Link libc when we're asked to link to an emulated wasi-libc library. 2024-12-08 20:22:13 +01:00
Alex Rønne Petersen
1e095024a7
compiler: Check for wasi-libc emulated libraries before libc libraries.
This will become useful when we update wasi-libc and get the emulated libdl.
2024-12-08 20:20:54 +01:00
wooster0
a221b2fbf2 Sema: fix use of Zcu.LazySrcLoc in error message
It currently prints as:

:3:18: error: untagged union 'Zcu.LazySrcLoc{ .base_node_inst = InternPool.TrackedInst.Index(104), .offset = Zcu.LazySrcLoc.Offset{ .node_offset = Zcu.LazySrcLoc.Offset.TracedOffset{ .x = -2, .trace = (value tracing disabled) } } }' cannot be converted to integer
2024-12-08 17:23:57 +00:00
Alex Rønne Petersen
6951ee43bf
Compilation: Don't rely on Clang defaults for options that are user-facing. 2024-12-08 16:53:55 +01:00
Alex Rønne Petersen
8d9ff8353d
compiler: Update the list of targets that have a red zone. 2024-12-08 16:52:44 +01:00
mlugg
135c733eef
InternPool: fix crash in rehashTrackedInsts
When a shard has zero elements, we don't need to reserve any capacity.
2024-12-08 10:53:51 +00:00
Jacob Young
bd0ace5c4e
cbe: prevent tautological-compare warnings in generated code 2024-12-08 10:53:50 +00:00
mlugg
03f5b967f0
AstGen: correctly deduplicate ref of param and alloc_inferred
Both of these instructions were previously under a special case in
`rvalue` which resulted in every reference to such an instruction adding
a new `ref` instruction. This had the effect that, for instance,
`&a != &a` for parameters. Deduplicating these `ref` instructions was
problematic for different reasons.

For `alloc_inferred`, the problem was that it's not valid to `ref` the
alloc until the allocation has been resolved (`resolve_inferred_alloc`),
but `AstGen.appendBodyWithFixups` would place the `ref` directly after
the `alloc_inferred`. This is solved by bringing
`resolve_inferred_alloc` in line with `make_ptr_const` by having it
*return* the final pointer, rather than modifying `sema.inst_map` of the
original `alloc_inferred`. That way, the `ref` refers to the
`resolve_inferred_alloc` instruction, so is placed immediately after it,
avoiding this issue.

For `param`, the problem is a bit trickier: `param` instructions live in
a body which must contain only `param` instructions, then a
`func{,_inferred,_fancy}`, then a `break_inline`. Moreover, `param`
instructions may be referenced not only by the function body, but also
by other parameters, the return type expression, etc. Each of these
bodies requires separate `ref` instructions. This is solved by pulling
entries out of `ref_table` after evaluating each component of the
function declaration, and appending the refs later on when actually
putting the bodies together. This gives way to another issue: if you
write `fn f(x: T) @TypeOf(x.foo())`, then since `x.foo()` takes a
reference to `x`, this `ref` instruction is now in a comptime context
(outside of the `@TypeOf` ZIR body), so emits a compile error. This is
solved by loosening the rules around `ref` instructions; because they
are not side-effecting, it is okay to allow `ref` of runtime values at
comptime, resulting in a runtime-known value in a comptime scope. We
already apply this mechanism in some cases; for instance, it's why
`runtime_array.len` works in a `comptime` context. In future, we will
want to give similar treatment to many operations in Sema: in general,
it's fine to apply runtime operations at comptime provided they don't
have side effects!

Resolves: #22140
2024-12-08 10:53:50 +00:00
Alex Rønne Petersen
e62aac3ec4 compiler: Only omit frame pointers by default for ReleaseSmall.
Frame pointers make both debugging and profiling work better, and the overhead
is reportedly 1% or less for typical programs [0]. I think the pros outweigh the
cons here. People who *really* care about that 1% can simply use the
-fomit-frame-pointer option to reclaim it. For ReleaseSmall, though, it makes
sense to omit frame pointers by default for the sake of code size, as we already
strip the binary in this case anyway.

Closes #22161.

[0] https://www.brendangregg.com/blog/2024-03-17/the-return-of-the-frame-pointers.html
2024-12-08 04:18:12 -05:00
Andrew Kelley
eb9c9a38f2
Merge pull request #22167 from alexrp/compiler-rt-names
compiler: Classify various compiler-rt and libunwind names accurately and satisfy them
2024-12-06 19:04:39 -05:00
Alex Rønne Petersen
8f27fc6c07
compiler: Classify libssp as an alias for compiler-rt.
This is a GCC library providing symbols with stack smashing protection. We
provide (most of) these symbols in our compiler-rt.
2024-12-06 15:10:37 +01:00
Alex Rønne Petersen
da794ec7a3
compiler: Remove warning about superfluous compiler-rt libraries.
* This warning's wording is actually inaccurate when using the -fno-compiler-rt
  or -rtlib=none options.
* It's not all that helpful; it's already understood that these libraries are
  part of the compiler, so printing a warning is just noise. In practice, this
  warning would always happen when building upstream musl, for example.
* We don't warn when we satisfy -lunwind using our bundled libunwind either, or
  various libc libraries using our bundled libc, or when providing libc++, etc.
  So I really don't think we should be warning here either.
2024-12-06 14:57:22 +01:00
Alex Rønne Petersen
a879c3ea0b
compiler: Classify libgcc as an alias for compiler-rt.
This is GCC's equivalent to compiler-rt. The two libraries have a huge overlap
in exported symbols, so we may as well satisfy it this way to increase
compatibility with build systems in the wild.
2024-12-06 14:57:22 +01:00
Alex Rønne Petersen
e7169e9d4d
compiler: Classify libgcc_eh as an alias for libunwind.
This is GCC's take on libunwind. We can satisfy it by way of our bundled LLVM
libunwind implementation.

Closes #17268.
2024-12-06 14:57:22 +01:00
Alex Rønne Petersen
37a34b54af
compiler: Recognize libgcc_s regardless of target ABI.
The real libgcc_s is a compiler-provided library; it works just fine with both
glibc and musl. There's no reason that I can see for this check to be limited to
glibc-based targets.
2024-12-06 14:57:19 +01:00