Commit Graph

985 Commits

Author SHA1 Message Date
Wooster 4585cb1e2f AstGen: fix @export with undeclared identifier crashing
This required a third `if (found_already == null)` in another place in
AstGen.zig for this special case of `@export`.

Fixes #17188
2023-09-22 12:23:57 -07:00
r00ster91 ee4ced9683 write function types consistently with a space before fn keyword
Currently, the compiler (like @typeName) writes it `fn(...) Type` but
zig fmt writes it `fn (...) Type` (notice the space after `fn`).
This inconsistency is now resolved and function types are consistently
written the zig fmt way. Before this there were more `fn (...) Type`
occurrences than `fn(...) Type` already.
2023-09-19 15:15:05 +03:00
mlugg 88f5315ddf compiler: implement destructuring syntax
This change implements the following syntax into the compiler:

```zig
const x: u32, var y, foo.bar = .{ 1, 2, 3 };
```

A destructure expression may only appear within a block (i.e. not at
comtainer scope). The LHS consists of a sequence of comma-separated var
decls and/or lvalue expressions. The RHS is a normal expression.

A new result location type, `destructure`, is used, which contains
result pointers for each component of the destructure. This means that
when the RHS is a more complicated expression, peer type resolution is
not used: each result value is individually destructured and written to
the result pointers. RLS is always used for destructure expressions,
meaning every `const` on the LHS of such an expression creates a true
stack allocation.

Aside from anonymous array literals, Sema is capable of destructuring
the following types:
* Tuples
* Arrays
* Vectors

A destructure may be prefixed with the `comptime` keyword, in which case
the entire destructure is evaluated at comptime: this means all `var`s
in the LHS are `comptime var`s, every lvalue expression is evaluated at
comptime, and the RHS is evaluated at comptime. If every LHS is a
`const`, this is not allowed: as with single declarations, the user
should instead mark the RHS as `comptime`.

There are a few subtleties in the grammar changes here. For one thing,
if every LHS is an lvalue expression (rather than a var decl), a
destructure is considered an expression. This makes, for instance,
`if (cond) x, y = .{ 1, 2 };` valid Zig code. A destructure is allowed
in almost every context where a standard assignment expression is
permitted. The exception is `switch` prongs, which cannot be
destructures as the comma is ambiguous with the end of the prong.

A follow-up commit will begin utilizing this syntax in the Zig compiler.

Resolves: #498
2023-09-15 11:33:53 -07:00
Veikka Tuominen 6484e279e5 AstGen: fix missing array type validation
Closes #17084
2023-09-07 16:56:07 +03:00
Veikka Tuominen d1a14e7b6d AstGen: fix error on missing function prototype name
Closes #17070
2023-09-05 20:00:19 +03:00
Jacob Young 7a251c4cb8 Sema: revert reference trace changes that are no longer needed 2023-08-28 17:43:37 -04:00
mlugg 8d036d1d78 Sema: allow cast builtins on vectors
The following cast builtins did not previously work on vectors, and have
been made to:

* `@floatCast`
* `@ptrFromInt`
* `@intFromPtr`
* `@floatFromInt`
* `@intFromFloat`
* `@intFromBool`

Resolves: #16267
2023-08-28 12:32:02 -07:00
Andrew Kelley ada0010471 compiler: move unions into InternPool
There are a couple concepts here worth understanding:

Key.UnionType - This type is available *before* resolving the union's
fields. The enum tag type, number of fields, and field names, field
types, and field alignments are not available with this.

InternPool.UnionType - This one can be obtained from the above type with
`InternPool.loadUnionType` which asserts that the union's enum tag type
has been resolved. This one has all the information available.

Additionally:

* ZIR: Turn an unused bit into `any_aligned_fields` flag to help
  semantic analysis know whether a union has explicit alignment on any
  fields (usually not).
* Sema: delete `resolveTypeRequiresComptime` which had the same type
  signature and near-duplicate logic to `typeRequiresComptime`.
  - Make opaque types not report comptime-only (this was inconsistent
    between the two implementations of this function).
* Implement accepted proposal #12556 which is a breaking change.
2023-08-22 13:54:14 -07:00
mlugg 6a5463951f Sema: disallow C pointer to slice coercion
Resolves: #16719
2023-08-21 11:31:22 -07:00
mlugg 82c8e45a7e Sema: check @memset operand provides length
Resolves: #16698
2023-08-21 11:30:20 -07:00
mlugg 283afb50b5 AstGen: disallow '-0' integer literal
The intent here is ambiguous: this resolves to the comptime_int '0', but
it's likely the user meant to use a floating-point literal.

Resolves: #16890
2023-08-21 11:47:31 +03:00
mlugg 321961d860 AstGen: add result location analysis pass
The main motivation for this change is eliminating the `block_ptr`
result location and corresponding `store_to_block_ptr` ZIR instruction.
This is achieved through a simple pass over the AST before AstGen which
determines, for AST nodes which have a choice on whether to provide a
result location, which choice to make, based on whether the result
pointer is consumed non-trivially.

This eliminates so much logic from AstGen that we almost break even on
line count! AstGen no longer has to worry about instruction rewriting
based on whether or not a result location was consumed: it always knows
what to do ahead of time, which simplifies a *lot* of logic. This also
incidentally fixes a few random AstGen bugs related to result location
handling, leading to the changes in `test/` and `lib/std/`.

This opens the door to future RLS improvements by making them much
easier to implement correctly, and fixes many bugs. Most ZIR is made
more compact after this commit, mainly due to not having redundant
`store_to_block_ptr` instructions lying around, but also due to a few
bugs in the old system which are implicitly fixed here.
2023-08-20 11:58:14 -07:00
mlugg 083ee8e0e2 InternPool: preserve indices of builtin types when resolved
Some builtin types have a special InternPool index (e.g.
`.type_info_type`) so that AstGen can refer to them before semantic
analysis. Unfortunately, this previously led to a second index existing
to refer to the type once it was resolved, complicating Sema by having
the concept of an "unresolved" type index.

This change makes Sema modify these InternPool indices in-place to
contain the expanded representation when resolved. The analysis of the
corresponding decls is caught in `Module.semaDecl`, and a field is set
on Sema telling it which index to place struct/union/enum types at. This
system could break if `std.builtin` contained complex decls which
evaluate multiple struct types, but this will be caught by the
assertions in `InternPool.resolveBuiltinType`.

The AstGen result types which were disabled in 6917a8c have been
re-enabled.

Resolves: #16603
2023-08-15 11:45:23 +01:00
mlugg 8f3ccbbe36 Sema: provide source location when analyzing panic handler
The panic handler decl_val was previously given a `unneeded` source
location, which was then added to the reference trace, resulting in a
crash if the source location was used in the reference trace. This
commit makes two trivial changes:

* Don't add unneeded source locations to the ref table (panic in debug, silently ignore in release)
* Pass a real source location when analyzing the panic handler
2023-08-14 11:43:21 -07:00
Jacob Young 41575fa868 AstGen: fix src loc for invalid switch expression rls coercions 2023-08-12 02:22:26 -04:00
Jacob Young ce7acf1296 AstGen: fix src loc for invalid coercion in breaks 2023-08-12 01:57:15 -04:00
Jacob Young 2b5bd56a67 AstGen: fix src loc for invalid coercions in tuple literals 2023-08-12 01:57:11 -04:00
Jacob Young ffc116de78 AstGen: fix src loc for invalid if expression rls coercions
Closes #12509
2023-08-12 01:57:07 -04:00
mlugg 2209813bae cases: modify error wording to match new errors
The changes to result locations and generic calls has caused mild
changes to some compile errors. Some are slightly better, some slightly
worse, but none of the changes are major.
2023-08-10 10:00:37 +01:00
mlugg 93e53d1e00 compiler: fix crash on invalid result type for @splat
This introduces a new ZIR instruction, `vec_elem_type`.

Co-Authored-By: Ali Chraghi <alichraghi@proton.me>
Resolves: #16567
2023-08-09 19:46:58 +01:00
Jacob Young 57470e833e Module: implement span for .call_arg of a @call
Closes #16750
2023-08-09 10:09:17 -04:00
Jacob Young 9630379a8e Sema: fix generic method argument source locations 2023-08-09 10:09:01 -04:00
Stevie Hryciw 16dcefaca5 Add compile error test for comptime slice-of-struct copy
Closes #6305. In the C++ implementation this hit a compiler assertion.
It is handled properly in the self-hosted version.
2023-08-06 18:11:49 -07:00
Jacob Young a91a8df679 Sema: fix issues passing an invalid type to a generic method
Closes #16601
2023-08-05 11:21:50 -07:00
Jacob Young 89d660c3eb Sema: improve new error messages related to naked functions
* pass a source location to all safety checks
 * add notes about what is disallowed in naked functions

Closes #16651
2023-08-02 16:12:30 -07:00
AdamGoertz 796927b900 Allow zero-sized fields in extern structs (#16404)
This change allows the following types to appear in extern structs:
* Zero-bit integers
* void
* zero-sized structs and packed structs
* enums with zero-bit backing integers
* arrays of any length with zero-size elements
2023-07-29 12:45:01 -04:00
xdBronch 2826f78a61 suggest using else when '_' is used for exhaustive enums (#16583) 2023-07-28 22:28:55 -04:00
Andrew Kelley 6cee98eb30 frontend: forbid packed and extern tuples 2023-07-25 21:45:33 -07:00
r00ster91 72ac37952e fix @embedFile("") not giving a proper error
Currently, in a debug build of the compiler, `@embedFile("")` is a crash;
in a release build the compiler, `@embedFile("")` is "error: unable to open '': OutOfMemory".
2023-07-21 23:39:42 +02:00
Andrew Kelley 3145ae561d Sema: fix compile error source location regressions 2023-07-18 19:02:06 -07:00
Andrew Kelley 8daa8d255b Sema: fix fn_proto_param LazySrcLoc resolution
to match source code span from merge-base.
2023-07-18 19:02:06 -07:00
Andrew Kelley 0153f3a8f9 Sema: fix crash: array_in_c_exported_function
Fuck it, we're storing decl indexes in LazySrcLoc now.
2023-07-18 19:02:06 -07:00
Andrew Kelley 47499bf47b Sema: enhance generic call error message
when the type of an anytype parameter is a comptime-only type but the
argument at the callsite is runtime-known.
2023-07-18 19:02:06 -07:00
antlilja a0ec2266fe Update tests to new splat syntax 2023-07-12 15:35:57 -07:00
Anton Lilja 711b4e93e2 Fixes crash when a struct is given as the first parameter to the unionInit builtin (#16385) 2023-07-11 23:37:42 -07:00
Anton Lilja ff0e2ab398 Fixes wrong error location for unionInit when first parameter is not a type (#16384) 2023-07-11 23:35:50 -07:00
r00ster91 0b1e8690da AstGen: make sure for range start and end are usizes
Fixes #16311

The actual cause of #16311 is the `start_is_zero` special case:
```zig
                const range_len = if (end_val == .none or start_is_zero)
                    end_val
                else
                    try parent_gz.addPlNode(.sub, input, Zir.Inst.Bin{
                        .lhs = end_val,
                        .rhs = start_val,
                    });
```
It only happens if the range start is 0. In that case we would not perform any type checking.
Only in the other cases coincidentally `.sub` performs type checking in Sema, but the errors are still rather poor:
```
$ zig test x.zig
x.zig:9:15: error: invalid operands to binary expression: 'Pointer' and 'Pointer'
    for ("abc".."def") |val| {
         ~~~~~^~~~~~~
```
Note how it's the same as if I use `-`:
```
x.zig:9:11: error: invalid operands to binary expression: 'Pointer' and 'Pointer'
    "abc" - "def";
    ~~~~~~^~~~~~~
```
Now after this PR, the errors are much clearer for both range start and end:
```
x.zig:9:10: error: expected type 'usize', found '*const [3:0]u8'
    for ("abc".."def") |val| {
         ^~~~~
```
This is why I decided to use `.ty` instead of `.coerced_ty` for both range start and end rather than
just perform type checking in that `end_val == .none or start_is_zero` case.
2023-07-10 10:51:55 -07:00
Andrew Kelley 2b8c1f0d46 Merge pull request #16339 from r00ster91/ueficc
std.os.uefi: use std.os.uefi.cc instead of .C as calling convention
2023-07-10 10:41:19 -07:00
Niles Salter 27a66191c2 Change math.Order order (#16356)
This speeds up algorithms like binary search
2023-07-09 01:22:52 -04:00
r00ster91 026c63d8fe Sema: infrastructure for supporting more than .C callconv for variadic functions
Now you can add new calling conventions that you confirmed to work with
variadic functions simply in a single place and the rest will work
automatically.
2023-07-08 18:05:03 -04:00
r00ster91 adf0718316 behavior: boolean vector with 2 or more elements
Closes #12169
2023-06-27 19:57:23 -04:00
r00ster91 d01641f6be test cases: fix typo in filename 2023-06-27 19:57:23 -04:00
r00ster91 c647799e5e test cases: expected optional type in for loop
Closes #10674
2023-06-27 19:57:23 -04:00
r00ster91 5e9fe84d24 test cases: array inside of anonymous struct
Closes #7525
2023-06-27 19:57:23 -04:00
r00ster91 10218dd096 test cases: never-inline call of inline function with comptime parameter
Closes #5995
2023-06-27 19:57:23 -04:00
r00ster91 18fe951e24 test cases: returning undefined [:0]const u8
Closes #5947
2023-06-27 19:57:23 -04:00
r00ster91 78da0e40a8 test cases: taking pointer of global tagged union
Closes #11619
2023-06-27 19:57:23 -04:00
r00ster91 c040c0f45a test cases: @intCast on vector
Closes #11770
2023-06-27 19:57:19 -04:00
r00ster91 7213234f0c test cases: taking address of extern var as constant
Closes #5344
2023-06-27 19:08:27 -04:00
r00ster91 d10456eb8d test cases: maximum sized integer literal
Closes #12116
2023-06-27 19:08:27 -04:00