aboutsummaryrefslogtreecommitdiff
path: root/src
AgeCommit message (Collapse)Author
2024-12-15Merge pull request #22224 from alexrp/cc-argsAlex Rønne Petersen
`Compilation`: Clean up `addCCArgs()` + some minor improvements
2024-12-14ensure `InstMap` capacity before remapping error codeDavid Rubin
2024-12-14Merge pull request #22222 from ianprime0509/git-sha256Andrew Kelley
zig fetch: add support for SHA-256 Git repositories
2024-12-14mingw: Update sources list to dcd7fefc703fb4b12187235386900d34cc13fdc5.Alex Rønne Petersen
2024-12-14Compilation: Clean up addCCArgs().Alex Rønne Petersen
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-13zig fetch: support SHA-256 Git repositoriesIan Johnson
Closes #21888
2024-12-13Remove copy of `HashedWriter`Ian Johnson
It was added to the standard library in #18733.
2024-12-13Fix undefined behavior in package extraction codeCarl Åstholm
2024-12-13Add compiler internals testsCarl Åstholm
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-13Compilation: Use Clang dependency file for preprocessed assembly files.Alex Rønne Petersen
2024-12-13Compilation: Use a better canonical file extension for header files.Alex Rønne Petersen
2024-12-13Compilation: Override Clang's language type for header files.Alex Rønne Petersen
Clang seems to treat them as linker input without this.
2024-12-13Compilation: Improve classification of various C/C++/Objective-C files.Alex Rønne Petersen
2024-12-13Merge pull request #22035 from alexrp/unwind-fixesAlex Rønne Petersen
Better unwind table support + unwind protection in `_start()` and `clone()`
2024-12-11std.Build.Cache.hit: work around macOS kernel bugAndrew Kelley
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-10std.Build.Cache.hit: more discipline in error handlingAndrew Kelley
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-11compiler: Improve the handling of unwind table levels.Alex Rønne Petersen
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-11riscv64: Support the fp alias for register s0 in inline assembly.Alex Rønne Petersen
2024-12-10fix unknown file extension with rmetaTravis Lange
2024-12-09Merge pull request #22157 from mlugg/astgen-error-lazyAndrew Kelley
compiler: allow semantic analysis of files with AstGen errors
2024-12-09Merge pull request #22164 from mlugg/astgen-ref-dedupAndrew Kelley
AstGen: correctly deduplicate `ref` of `param` and `alloc_inferred`
2024-12-09Builtin: clean up formatting of generated codemlugg
This commit simply tweaks the generated `builtin.zig` source code to be more consistent and in line with current conventions.
2024-12-09Merge pull request #22191 from alexrp/wasi-libc-namesAlex Rønne Petersen
`std.zig.target`: Teach `isLibCLibName()` about emulated wasi-libc libraries
2024-12-09Merge pull request #22190 from alexrp/cc-argsAlex Rønne Petersen
`Compilation`: Don't rely on Clang defaults for options that are user-facing.
2024-12-08compiler: Link libc when we're asked to link to an emulated wasi-libc library.Alex Rønne Petersen
2024-12-08compiler: Check for wasi-libc emulated libraries before libc libraries.Alex Rønne Petersen
This will become useful when we update wasi-libc and get the emulated libdl.
2024-12-08Sema: fix use of Zcu.LazySrcLoc in error messagewooster0
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-08Compilation: Don't rely on Clang defaults for options that are user-facing.Alex Rønne Petersen
2024-12-08compiler: Update the list of targets that have a red zone.Alex Rønne Petersen
2024-12-08InternPool: fix crash in `rehashTrackedInsts`mlugg
When a shard has zero elements, we don't need to reserve any capacity.
2024-12-08cbe: prevent tautological-compare warnings in generated codeJacob Young
2024-12-08AstGen: correctly deduplicate `ref` of `param` and `alloc_inferred`mlugg
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-08compiler: Only omit frame pointers by default for ReleaseSmall.Alex Rønne Petersen
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-06Merge pull request #22167 from alexrp/compiler-rt-namesAndrew Kelley
compiler: Classify various compiler-rt and libunwind names accurately and satisfy them
2024-12-06compiler: Classify libssp as an alias for compiler-rt.Alex Rønne Petersen
This is a GCC library providing symbols with stack smashing protection. We provide (most of) these symbols in our compiler-rt.
2024-12-06compiler: Remove warning about superfluous compiler-rt libraries.Alex Rønne Petersen
* 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-06compiler: Classify libgcc as an alias for compiler-rt.Alex Rønne Petersen
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-06compiler: Classify libgcc_eh as an alias for libunwind.Alex Rønne Petersen
This is GCC's take on libunwind. We can satisfy it by way of our bundled LLVM libunwind implementation. Closes #17268.
2024-12-06compiler: Recognize libgcc_s regardless of target ABI.Alex Rønne Petersen
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-06compiler: Classify libatomic as an alias for compiler-rt.Alex Rønne Petersen
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-06Zcu: allow test declarations to be failedmlugg
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-05std.zig.Zir: improve instruction trackingmlugg
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-05compiler: incremental compilation fixesmlugg
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-05compiler: allow files with AstGen errors to undergo semantic analysismlugg
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-05Compilation: Consider *.rlib files to be static libraries.Alex Rønne Petersen
These are produced by rustc: https://rustc-dev-guide.rust-lang.org/backend/libs-and-metadata.html#rlib
2024-12-05Merge pull request #22141 from alexrp/target-remove-bridgeosAlex Rønne Petersen
`std.Target`: Remove `Os.Tag.bridgeos`
2024-12-03llvm: Remove dead targetArch() and targetOs() functions.Alex Rønne Petersen
These were leftovers from when we used the LLVM API to create modules.
2024-12-03std.Target: Remove Os.Tag.bridgeos.Alex Rønne Petersen
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-03sema: add a missing errdeferAlex Kladov
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-02macho: fix 32bit buildsJakub Konka