688 Commits

Author SHA1 Message Date
mlugg
38b83d9d93 Zir: eliminate field_call_bind and field_call_bind_named
This commit removes the `field_call_bind` and `field_call_bind_named` ZIR
instructions, replacing them with a `field_call` instruction which does the bind
and call in one.

`field_call_bind` is an unfortunate instruction. It's tied into one very
specific usage pattern - its result can only be used as a callee. This means
that it creates a value of a "pseudo-type" of sorts, `bound_fn` - this type used
to exist in Zig, but now we just hide it from the user and have AstGen ensure
it's only used in one way. This is quite silly - `Type` and `Value` should, as
much as possible, reflect real Zig types and values.

It makes sense to instead encode the `a.b()` syntax as its own ZIR instruction,
so that's what we do here. This commit introduces a new instruction,
`field_call`. It's like `call`, but rather than a callee ref, it contains a ref
to the object pointer (`&a` in `a.b()`) and the string field name (`b`). This
eliminates `bound_fn` from the language, and slightly decreases the size of
generated ZIR - stats below.

This commit does remove a few usages which used to be allowed:
- `@field(a, "b")()`
- `@call(.auto, a.b, .{})`
- `@call(.auto, @field(a, "b"), .{})`

These forms used to work just like `a.b()`, but are no longer allowed. I believe
this is the correct choice for a few reasons:
- `a.b()` is a purely *syntactic* form; for instance, `(a.b)()` is not valid.
  This means it is *not* inconsistent to not allow it in these cases; the
  special case here isn't "a field access as a callee", but rather this exact
  syntactic form.
- The second argument to `@call` looks much more visually distinct from the
  callee in standard call syntax. To me, this makes it seem strange for that
  argument to not work like a normal expression in this context.
- A more practical argument: it's confusing! `@field` and `@call` are used in
  very different contexts to standard function calls: the former normally hints
  at some comptime machinery, and the latter that you want more precise control
  over parts of a function call. In these contexts, you don't want implicit
  arguments adding extra confusion: you want to be very explicit about what
  you're doing.

Lastly, some stats. I mentioned before that this change slightly reduces the
size of ZIR - this is due to two instructions (`field_call_bind` then `call`)
being replaced with one (`field_call`). Here are some numbers:

+--------------+----------+----------+--------+
| File         | Before   | After    | Change |
+--------------+----------+----------+--------+
| Sema.zig     | 4.72M    | 4.53M    | -4%    |
| AstGen.zig   | 1.52M    | 1.48M    | -3%    |
| hash_map.zig | 283.9K   | 276.2K   | -3%    |
| math.zig     | 312.6K   | 305.3K   | -2%    |
+--------------+----------+----------+--------+
2023-05-20 12:27:48 -07:00
Andrew Kelley
503302ceef Sema: simplify "duplicate test name" error message
* Avoid redundant words ("found")
   - All compile errors are found by the compiler
 * Avoid unnecessary prepositions ("with")
   - There is a grammatically correct alternate word order without the
     preposition.
2023-05-18 19:17:04 -07:00
zooster
3d64ed0353
make @trap return unreachable/noreturn (#15749)
`@trap` is a special function that we know never returns so it should
behave just like `@panic` and `@compileError` do currently and cause the
"unreachable code" + "control flow is diverted here" compile error.
Currently, `@trap(); @trap();` does not cause this error. Now it does.
2023-05-18 17:00:35 -04:00
Andrew Kelley
4ba61a2191
Merge pull request #15704 from Vexu/fix-memcpyset
`@mem{cpy,set}` fixes
2023-05-15 22:39:45 -07:00
Veikka Tuominen
2703db3b40 Sema: add more type checks to @mem{cpy,set}
Closes #15634
Co-authored-by: Dima Afanasyev <dimaafanasev@example.com>
2023-05-15 10:31:24 +03:00
wrongnull
2516d8671f correct error note and check type of extern variables 2023-05-15 04:52:02 +03:00
Veikka Tuominen
0958d5d7db Sema: fix crash when generating anon name on invalid code
Closes #15615
2023-05-11 17:23:06 +03:00
Veikka Tuominen
c0102ac1c8 Sema: add error for resolving inferred error set of generic function
Closes #14991
2023-05-11 17:23:06 +03:00
Veikka Tuominen
67afd2a470 Sema: make @call compile errors match regular calls
Closes #15642
2023-05-11 12:23:57 +03:00
John Schmidt
2606498409 module: return null if no candidate src
Closes #15572.
2023-05-11 11:21:44 +03:00
Veikka Tuominen
ae69fb87eb
Merge pull request #15508 from r00ster91/semathings
Sema: fixes to error messages
2023-05-10 16:12:46 +03:00
Dominic
5a3eca5d4c
Disallow named test decls with duplicate names 2023-05-08 10:59:06 +03:00
Dominic
e1f5ad3cc8
Fix parsing of hexadecimal literals 2023-05-07 08:05:53 +00:00
r00ster91
3485a0e7fb Sema: fix and improve errors for for loop objects and non-indexables
Operands to @memcpy and @memset were being called "for loop operands" in
the error.
2023-05-06 09:32:34 +02:00
r00ster91
743976ef48 Sema: add some missing apostrophes to error messages 2023-05-06 09:32:34 +02:00
kcbanner
ec6ffaa1e4 sema: improve the error message when coercing to []anyopaque 2023-04-30 16:40:06 -07:00
Andrew Kelley
c83ab7cc6a
Merge pull request #15503 from r00ster91/noinline
Sema: emit error for always_inline call of noinline function
2023-04-29 11:13:51 -07:00
r00ster91
dff6efe369 Sema: disallow indexing non-tuple struct
Fixes #15497
2023-04-29 14:28:36 +03:00
r00ster91
bd8b5c25ec Sema: emit error for always_inline call of noinline function
Fixes #15489

This also lays the groundwork for exposing the whether or not a function is
noinline in std.builtin.Fn as an `is_noinline: bool` field if we ever want to do that.
2023-04-29 04:19:58 +02:00
Andrew Kelley
3c66850e42
Merge pull request #15278 from ziglang/memcpy-memset
change semantics of `@memcpy` and `@memset`
2023-04-26 10:01:54 -07:00
kcbanner
295b8ca467 sema: add error for coercing a slice to an anyopaque pointer 2023-04-26 00:53:09 +03:00
Andrew Kelley
1ba72bcf9a update test cases for new memcpy/memset semantics 2023-04-25 11:23:41 -07:00
Andrew Kelley
83a7303bbf Sema: implement comptime @memset 2023-04-25 11:23:41 -07:00
zooster
bc8e1e1de4
Improvements to docs and text
* docs(std.math): elaborate on difference between absCast and absInt

* docs(std.rand.Random.weightedIndex): elaborate on likelihood

I think this makes it easier to understand.

* langref: add small reminder

* docs(std.fs.path.extension): brevity

* docs(std.bit_set.StaticBitSet): mention the specific types

* std.debug.TTY: explain what purpose this struct serves

This should also make it clearer that this struct is not supposed to provide unrelated terminal manipulation functionality such as setting the cursor position or something because terminals are complicated and we should keep this struct simple and focused on debugging.

* langref(package listing): brevity

* langref: explain what exactly `threadlocal` causes to happen

* std.array_list: link between swapRemove and orderedRemove

Maybe this can serve as a TLDR and make it easier to decide.

* PrefetchOptions.locality: clarify docs that this is a range

This confused me previously and I thought I can only use either 0 or 3.

* fix typos and more

* std.builtin.CallingConvention: document some CCs

* langref: explain possibly cryptic names

I think it helps knowing what exactly these acronyms (@clz and @ctz) and
abbreviations (@popCount) mean.

* variadic function error: add missing preposition

* std.fmt.format docs: nicely hyphenate

* help menu: say what to optimize for

I think this is slightly more specific than just calling it
"optimizations". These are speed optimizations. I used the word
"performance" here.
2023-04-23 21:06:21 +03:00
Jacob Young
a1ed4bd796 cbe: fix remaining aarch64 issues 2023-04-21 16:36:31 -04:00
mlugg
d5f1a8823e Sema: allow ptr field access on pointer-to-array
Also remove an incorrect piece of logic which allowed fetching the 'len'
property on non-single-ptrs (e.g. many-ptrs) and add a corresponding
compile error test case.

Resolves: #4765
2023-04-20 09:05:22 -07:00
mlugg
ccf670c2b0 Zir: implement explicit block_comptime instruction
Resolves: #7056
2023-04-12 12:06:19 -04:00
Veikka Tuominen
66520c8342 Sema: validate array element types
Fixes the compiler crash part of #15175
2023-04-05 14:45:56 +03:00
Veikka Tuominen
4a5628e730 Module: fix lazy srcloc resolution for new for loop syntax
Closes #15081
2023-03-26 15:14:03 +03:00
Veikka Tuominen
87e07d8671 fix broken test cases exposed by ec445fb6b8bb3f3d423cafa4f3a7860da65ca233
shoulda rebased
2023-03-21 20:57:14 +02:00
John Schmidt
ec445fb6b8 Improve error messages for break type coercion 2023-03-21 15:09:42 +02:00
Veikka Tuominen
f7204c7f37
Merge pull request #15028 from Vexu/compile-errors
Sema: improve error message of field access of wrapped type
2023-03-21 14:55:36 +02:00
mlugg
3a25f6a22e Port some stage1 test cases to stage2
There are now very few stage1 cases remaining:
* `cases/compile_errors/stage1/obj/*` currently don't work correctly on
  stage2. There are 6 of these, and most of them are probably fairly
  simple to fix.
* `cases/compile_errors/async/*` and all remaining `safety/*` depend on
  async; see #6025.

Resolves: #14849
2023-03-20 19:55:50 -04:00
Veikka Tuominen
82133cd992 Sema: improve error message of field access of wrapped type
Closes #15027
2023-03-21 00:34:12 +02:00
InKryption
9964f1c160 Add error for bad cast from *T to *[n]T
Casting `*T` to `*[1]T` should still work, but every other length
will now be a compiler error instead of a potential OOB access.
2023-03-16 13:00:36 +02:00
Andrew Kelley
7177b39946 fix test-case copy-paste typo from earlier commit
commit 3204d00a5e7fe119b690e921138a439fb84dff5b intended to move this
passing test case from stage1 folder but it was accidentally changed to
have identical contents as a different test case instead.

Fortunately, the test case has not regressed, so I simply replaced it
with the intended test from before.
2023-03-15 12:32:17 -07:00
Andrew Kelley
21b544a90a fix compile log test case expected output 2023-03-15 10:48:15 -07:00
Andrew Kelley
6664d2418d test-cases: add missing compile log output
The new testing harness is not bound by previous limitations; it can now
test compile log output as well.
2023-03-15 10:48:15 -07:00
Andrew Kelley
29cfd47d65 re-enable test-cases and get them all passing
Instead of using `zig test` to build a special version of the compiler
that runs all the test-cases, the zig build system is now used as much
as possible - all with the basic steps found in the standard library.

For incremental compilation tests (the ones that look like foo.0.zig,
foo.1.zig, foo.2.zig, etc.), a special version of the compiler is
compiled into a utility executable called "check-case" which checks
exactly one sequence of incremental updates in an independent
subprocess. Previously, all incremental and non-incremental test cases
were done in the same test runner process.

The compile error checking code is now simpler, but also a bit
rudimentary, and so it additionally makes sure that the actual compile
errors do not include *extra* messages, and it makes sure that the
actual compile errors output in the same order as expected. It is also
based on the "ends-with" property of each line rather than the previous
logic, which frankly I didn't want to touch with a ten-meter pole. The
compile error test cases have been updated to pass in light of these
differences.

Previously, 'error' mode with 0 compile errors was used to shoehorn in a
different kind of test-case - one that only checks if a piece of code
compiles without errors. Now there is a 'compile' mode of test-cases,
and 'error' must be only used when there are greater than 0 errors.

link test cases are updated to omit the target object format argument
when calling checkObject since that is no longer needed.

The test/stage2 directory is removed; the 2 files within are moved to be
directly in the test/ directory.
2023-03-15 10:48:14 -07:00
mlugg
948926c513 Sema: improve error message when calling non-member function as method
Resolves: #14880
2023-03-12 18:47:02 +02:00
mlugg
023753b469 Sema: correctly detect use of undefined within slices in @Type
Resolves: #14712
2023-03-10 12:18:06 +02:00
mlugg
14590e956e Fix test case added in 6d7fb8f 2023-03-09 23:25:38 +02:00
mlugg
6d7fb8f19c Sema: check type of comptime try operand
Resolves: #14693
2023-03-09 02:02:19 +02:00
John Schmidt
ecc0108cea astgen: fill result location with void value if no other value
With this change, `break` and `break :blk` will fill the result location
with `.void_value`, ensuring that the value will be type checked.
The same will happen for a for loop that contains no `break`s in it's body.

Closes https://github.com/ziglang/zig/issues/14686.
2023-03-08 16:35:53 +02:00
r00ster91
010596c930 AstGen: compile-error on primitive value export
Fixes #14778

Co-authored-by: Veikka Tuominen <git@vexu.eu>
2023-03-04 22:52:57 +02:00
John Schmidt
e41bc640c6 astgen: do not discard result location in for/while loops
If we use the discard result location any break with a value will be
ignored and not checked for usage.

Closes https://github.com/ziglang/zig/issues/14684.
2023-03-01 02:51:59 -05:00
Isaac Freund
05da5b32a8 Sema: implement @fieldParentPtr for unions 2023-02-21 15:57:13 +02:00
Andrew Kelley
53104b9165 add test coverage for fixed bug. closes #5410 2023-02-19 21:18:27 -07:00
Veikka Tuominen
f10950526e implement writeToMemory/readFromMemory for pointers 2023-02-19 13:54:52 -05:00
Andrew Kelley
8b05205bb7 implement error for unbounded for loops 2023-02-18 19:20:19 -07:00