11082 Commits

Author SHA1 Message Date
Veikka Tuominen
f10950526e implement writeToMemory/readFromMemory for pointers 2023-02-19 13:54:52 -05:00
Andrew Kelley
0bb178bbb2
Merge pull request #14671 from ziglang/multi-object-for
implement multi-object for loops
2023-02-19 10:10:59 -05:00
Tom Read Cutting
346ec15c50
Correctly handle carriage return characters according to the spec (#12661)
* Scan from line start when finding tag in tokenizer

This resolves a crash that can occur for invalid bytes like carriage
returns that are valid characters when not parsed from within literals.

There are potentially other edge cases this could resolve as well, as
the calling code for this function didn't account for any potential
'pending_invalid_tokens' that could be queued up by the tokenizer from
within another state.

* Fix carriage return crash in multiline string

Follow the guidance of #38:

> However CR directly before NL is interpreted as only a newline and not part of the multiline string. zig fmt will delete the CR.

Zig fmt already had code for deleting carriage returns, but would still
crash - now it no longer does so. Carriage returns encountered before
line-feeds are now appropriately removed on program compilation as well.

* Only accept carriage returns before line feeds

Previous commit was much less strict about this, this more closely
matches the desired spec of only allow CR characters in a CRLF pair, but
not otherwise.

* Fix CR being rejected when used as whitespace

Missed this comment from ziglang/zig-spec#83:

> CR used as whitespace, whether directly preceding NL or stray, is still unambiguously whitespace. It is accepted by the grammar and replaced by the canonical whitespace by zig fmt.

* Add tests for carriage return handling
2023-02-19 14:14:03 +02:00
Andrew Kelley
40c4c25e2b Sema: add missing coercion when checking for loop len 2023-02-18 19:20:19 -07:00
Andrew Kelley
12a7a0d76f omit safety check when incrementing for loop counter
Since for loops are statically analyzed to have an upper bound, and the
loop counter is a usize, it is impossible for it to overflow.
2023-02-18 19:20:19 -07:00
Andrew Kelley
8b05205bb7 implement error for unbounded for loops 2023-02-18 19:20:19 -07:00
Andrew Kelley
74db8c2e83 omit safety checks for element access in for loops
One of the main points of for loops is that you can safety check the
length once, before entering the loop, and then safely assume that every
element inside the loop is in bounds.

In master branch, the safety checks are incorrectly intact even inside
for loops. This commit fixes it. It's especially nice with multi-object
loops because the number of elided checks is N * M where N is how many
iterations and M is how many objects.
2023-02-18 19:20:19 -07:00
Andrew Kelley
4dd958d585 improve error message for byref capture of byval array 2023-02-18 19:20:19 -07:00
Andrew Kelley
601db3981c fix source location for not-indexable for loop errors 2023-02-18 19:20:19 -07:00
Andrew Kelley
bcb72401d3 AstGen: add error for discard of unbounded counter 2023-02-18 19:17:21 -07:00
Andrew Kelley
22965e6fcb Sema: improve error message for mismatched for loop lengths 2023-02-18 19:17:21 -07:00
Andrew Kelley
209e83f395 AstGen: fix ZIR for for loops accessing instruction out of block 2023-02-18 19:17:21 -07:00
Andrew Kelley
f2a6a1756b Sema: fix for loops with comptime-known int ranges 2023-02-18 19:17:21 -07:00
Andrew Kelley
aeaef8c0ff update std lib and compiler sources to new for loop syntax 2023-02-18 19:17:21 -07:00
Andrew Kelley
321ccbdc52 Sema: implement for_len
This also makes another breaking change to for loops: in order to
capture a pointer of an element, one must take the address of array
values. This simplifies a lot of things, and makes more sense than how
it was before semantically.

It is still legal to use a for loop on an array value if the
corresponding element capture is byval instead of byref.
2023-02-18 19:17:21 -07:00
Andrew Kelley
5029e5364c make zig fmt perform upgrade to new for loop syntax
The intent here is to revert this commit after Zig 0.10.0 is released.
2023-02-18 19:17:21 -07:00
Andrew Kelley
293d6bdc73 AstGen: back to index-based for loops 2023-02-18 19:17:20 -07:00
Andrew Kelley
841add6890 AstGen: finish multi-object for loops
This strategy uses pointer arithmetic to iterate through the loop. This
has a problem, however, which is tuples. AstGen does not know whether a
given indexable is a tuple or can be iterated based on contiguous
memory. Tuples unlike other indexables cannot be represented as a
many-item pointer that is incremented as the loop counter.

So, after this commit, I will modify AstGen back closer to how @vexu had
it before, using a counter and array element access.
2023-02-18 19:17:20 -07:00
Andrew Kelley
faa44e2e58 AstGen: rework multi-object for loop
* Allow unbounded looping.
* Lower by incrementing raw pointers for each iterable rather than
  incrementing a single index variable. This elides safety checks
  without any analysis required thanks to the length assertion and
  lowers to decent machine code even in debug builds.
  - An "end" value is selected, prioritizing a counter if possible,
    falling back to a runtime calculation of ptr+len on a slice input.
* Specialize on the pattern `0..`, avoiding an unnecessary subtraction
  instruction being emitted.
* Add the `for_check_lens` ZIR instruction.
2023-02-18 19:17:20 -07:00
Veikka Tuominen
6733e43d87 AstGen: work-in-progress multi-object for loops 2023-02-18 19:17:20 -07:00
Matt Knight
07630eb696
Value: implement writeToMemory for packed unions 2023-02-18 21:10:27 +02:00
Motiejus Jakštys
3f7e9ff597 [all linkers] fail hard on unsupported flags
Currently `zig cc`, when confronted with a linker argument it does
not understand, skips the flag and emits a warning.

This has been causing headaches for people that build third-party
software (including me). Zig seemingly builds and links the final
executable, only to segfault when running it.

If there are linker warnings when compiling software, the first thing we
have to do is add support for ones linker is complaining, and only then
go file issues. If zig "successfully" (i.e. status code = 0) compiles a
binary, there is instead a tendency to blaim "zig doing something
weird". (I am guilty of this.) In my experience, adding the unsupported
arguments has been quite easy; see #11679, #11875, #11874 for recent
examples.

With the current ones (+ prerequisites below) I was able to build all of
the CGo programs that I am encountering at $dayjob. CGo is a reasonable
example, because it is exercising the unusual linker args quite a bit.

Prerequisites: #11614 and #11863.
2023-02-16 19:20:53 -05:00
Andrew Kelley
d2650eb570 CLI: detect linker color diagnostics flags 2023-02-16 17:19:26 -07:00
Motiejus Jakštys
705e9cb3ca [linker] ignore --sort-common
From ld.lld(1):

     --sort-common
             This option is ignored for GNU compatibility.

Refs https://github.com/zigchroot/zig-chroot/issues/1
2023-02-16 18:46:06 -05:00
Motiejus Jakštys
b1b944a683 [elf linker] add --sort-section
Tested by: created a "hello world" C file and compiled with:

    zig cc -v main.c -Wl,--sort-section=name -o main

... and verified the `--sort-section=name` is passed to ld.lld.

Refs https://github.com/zigchroot/zig-chroot/issues/1
2023-02-16 18:44:56 -05:00
Andrew Kelley
61da9a25b9 CLI: remove option from usage text since it doesn't work 2023-02-16 16:35:06 -07:00
Motiejus Jakštys
4c7ca20da5 [linker] add --no-undefined, -z undefs
Refs https://github.com/zigchroot/zig-chroot/issues/1
2023-02-16 16:33:05 -07:00
Veikka Tuominen
7199d7c777 split @qualCast into @constCast and @volatileCast 2023-02-15 01:43:57 +02:00
Jakub Konka
4e6f21e2cb comp: reinstate -fcompiler-rt when used with build-obj as output
When the following is specified

```
$ zig build-obj -fcompiler-rt example.zig
```

the resulting relocatable object file will have the compiler-rt
unconditionally embedded inside.

```
$ nm example.o
...
0000000012345678 W __truncsfhf2
...
```
2023-02-14 12:12:22 -05:00
Tom Read Cutting
47e14b7ffb Zld: Report archive file with cpu arch mismatch
This is just a simple/hacky feature to report the source of a linking
error. I found this helpful in fixing-up some of my libs when recently
switching from an x86_64 to aarch64 device, so thought it might be
useful to others a well before zld has a fully featured error reporting
system.
2023-02-14 12:05:30 +01:00
Andrew Kelley
a9e1cf3049
Merge pull request #14571 from ziglang/more-build-zig
std.Build.ConfigHeaderStep: support sentinel-terminated strings
2023-02-13 17:23:19 -05:00
AdamGoertz
c8dc00086e
Add -ferror-tracing and -fno-error-tracing compile options 2023-02-13 16:23:13 +02:00
Andrew Kelley
9cb52ca6ce move the cache system from compiler to std lib 2023-02-13 06:42:25 -07:00
Ali Chraghi
a8edd67d3d zig fmt: additionally format .zon files 2023-02-13 02:20:02 -05:00
Andrew Kelley
d4bd1b1a60
Merge pull request #14618 from Vexu/fixes
small misc fixes
2023-02-13 01:58:01 -05:00
Veikka Tuominen
ba680aa987
Merge pull request #14588 from dweiller/test-runner-imports
fix custom test runner file import path resolution
2023-02-11 14:40:14 +02:00
Veikka Tuominen
28413ffcba Sema: fix typo in zirCUndef
Closes #14617
2023-02-11 14:36:54 +02:00
Veikka Tuominen
0328c9cbeb llvm: fix lowerDeclRefValue for extern function aliases
Same as 0577069af5f5deb859762725736537d60c324453 for extern functions.

Closes  #14610
2023-02-11 14:36:54 +02:00
Veikka Tuominen
8127a27eb1 zig fmt: do not consider tuples blocks
Closes #14056
2023-02-11 14:36:54 +02:00
Veikka Tuominen
31ed8d293d Sema: add missing peer type resolution for error unions
Closes #14077
2023-02-11 14:36:54 +02:00
Veikka Tuominen
b9c2837c1c Sema: validate inferred error set payload type
This was missed in b0a55e1b3be3a274546f9c18016e9609d546bdb0
2023-02-11 14:36:54 +02:00
John Schmidt
a5d25fabda
translate_c: fix typedeffed pointer subtraction
Closes #14560.
2023-02-10 15:46:29 +02:00
Dominic
948754c5d4
clone package table into custom test runner
Co-authored-by: Veikka Tuominen <git@vexu.eu>
2023-02-08 22:39:38 +11:00
dweiller
edc0e84270 allow custom test runners to import modules 2023-02-08 16:35:32 +11:00
Jakub Konka
94c68c1f9e macho: fix incorrect representation of encodings count per page
There can be a maximum of 256 compact encodings per page in compact
unwind info, and we were using `u8` to represent the count which is insufficient.
This commit bumps it to `u9`.
2023-02-08 05:03:14 +01:00
dweiller
1f7390f399 fix custom test runner package path resolution
Fixes #13970.

This fix makes test runners resolve package paths relative to the
directory the test runner is in. This means it is not possible to import
a file from outside the file tree root at the directory containing the
test runner.
2023-02-08 14:30:37 +11:00
Jakub Konka
4b1a883d35 macho: ensure local syms buffer is nlist_64 aligned when re-reading from file 2023-02-07 03:14:26 +01:00
Jakub Konka
f63eda3f6a macho: parse and sort data-in-code entries ahead of time 2023-02-06 16:08:42 +01:00
Jakub Konka
b32f5ee932 macho: downgrade alignment requirements for symtab in object files
Parse and sort relocations by address descending.
2023-02-06 13:23:08 +01:00
Andrew Kelley
11cc1c16fa make @embedFile support module-mapped names the same way as @import
closes #14553
2023-02-05 03:25:43 -05:00