Commit graph

498 commits

Author SHA1 Message Date
Techatrix
8b9425c248 AstGen: add error message for capture error by ref in switch on error 2024-01-16 05:55:26 +01:00
Andrew Kelley
32e88251e4 update test case for new const/var compile error
commit 8afafa717f was created when this
error did not exist yet.
2024-01-15 01:53:41 -07:00
dweiller
8afafa717f sema: allow slicing *T with comptime known [0..1] 2024-01-14 17:26:45 -08:00
Andrew Kelley
bd46410419 Revert "Merge pull request #18410 from dweiller/by-length-slice-bug"
This reverts commit d9d840a33a, reversing
changes made to a04d433094.

This is not an adequate implementation of the missing safety check, as
evidenced by the changes to std.json that are reverted in this commit.

Reopens #18382
Closes #18510
2024-01-13 23:21:44 -07:00
Meghan Denny
3d6c26525f sema: forbid asm output to const locals 2024-01-12 16:23:42 -08:00
Bogdan Romanyuk
4a1a5ee47b
AstGen: add error for redundant comptime var in comptime scope (#18242) 2024-01-09 20:09:39 -05:00
dweiller
69ab687156 test: add tests for switch_block_err_union 2024-01-09 14:42:12 +11:00
Veikka Tuominen
804cee3b93 categorize behavior/bugs/<issueno>.zig tests 2024-01-06 16:49:41 -08:00
Carl Åstholm
501a2350ab sema: Prevent reifying non-empty union with empty tag type 2024-01-04 22:38:31 +02:00
dweiller
1748511058 sema: add compile error for OOB by-length slice of array 2023-12-31 15:36:58 +11:00
Andrew Kelley
3d23ba9c35 Revert "Sema: forbid @breakpoint from being called at comptime"
This reverts commit f88b523065.

Let's please go through the language proposal process for this change. I
don't see any justification for this breaking change even in the commit
message.
2023-12-11 12:24:15 -07:00
Bogdan Romanyuk
f88b523065
Sema: forbid @breakpoint from being called at comptime 2023-12-11 17:52:19 +02:00
Veikka Tuominen
69195d0cd4 AstGen: add error for using inline loops in comptime only scopes 2023-12-08 16:54:32 -08:00
Veikka Tuominen
39a966b0a4 Sema: improve error location for array cat/mul 2023-11-30 13:15:40 +02:00
Bogdan Romanyuk
2252dcc508
Compiler: move checking function-scope-only builtins to AstGen 2023-11-25 17:29:07 +00:00
Bogdan Romanyuk
aa31096cbd
Sema: disallow call to undefined function 2023-11-23 02:08:15 +00:00
Andrew Kelley
d5e21a4f1a std: remove meta.trait
In general, I don't like the idea of std.meta.trait, and so I am
providing some guidance by deleting the entire namespace from the
standard library and compiler codebase.

My main criticism is that it's overcomplicated machinery that bloats
compile times and is ultimately unnecessary given the existence of Zig's
strong type system and reference traces.

Users who want this can create a third party package that provides this
functionality.

closes #18051
2023-11-22 13:24:27 -05:00
Andrew Kelley
54d196bb30
Merge pull request #18057 from Vexu/fixes
Fix bad error location on field init with field access
2023-11-21 15:12:03 -05:00
Veikka Tuominen
a947f97331 Sema: fix bad error location on field init with field access
Closes #14753
2023-11-21 13:59:14 +02:00
Bogdan Romanyuk
583afd6f0c
value: update isDeclRef for anonymous declarations 2023-11-21 13:21:32 +02:00
mlugg
38b373bf0b
cases: add compile error test for never-mutated local variable 2023-11-19 11:11:50 +00:00
mlugg
21fa187abc
test: update cases to silence 'var is never mutated' errors 2023-11-19 09:56:51 +00:00
dweiller
325e0f5f0e test: check compile errors when compilation has no errors 2023-11-19 00:12:43 +02:00
Curtis Tate Wilkinson
7b99189f19
Add struct declaration location to empty root struct field member error 2023-11-18 12:03:10 +00:00
David
941090d94f
Move duplicate field detection for struct init expressions into AstGen
Partially addresses #17916.
2023-11-16 14:38:16 +00:00
Krzysztof Wolicki
acf9de376d
Sema: Add error for non-power of 2 field alignment when reifying Unions, Structs, Pointers 2023-11-16 10:19:54 +02:00
Andrew Kelley
70d8baaec1 Revert "Sema: fix comparison with undefined"
This reverts commit 547481c31c.

There is a comment that did not get addressed with this patch, and the
required test cases are not added.

Reopens #17798.
2023-11-12 14:33:17 -07:00
Bogdan Romanyuk
547481c31c
Sema: fix comparison with undefined 2023-11-12 11:03:23 +02:00
Bogdan Romanyuk
ec934c6d32 disallow calling @trap at comptime 2023-11-10 18:39:42 +02:00
mlugg
3f10b3ee1e Sema: do not allow comptime-only pointer fields in packed structs 2023-11-10 06:51:48 +00:00
mlugg
4504e03a18 Sema: fix source location for untyped array init with result type
Resolves: #17923
2023-11-08 06:56:52 +00:00
kcbanner
f10499be0a
sema: analyze field init bodies in a second pass
This change allows struct field inits to use layout information
of their own struct without causing a circular dependency.

`semaStructFields` caches the ranges of the init bodies in the `StructType`
trailing data. The init bodies are then resolved by `resolveStructFieldInits`,
which is called before the inits are actually required.

Within the init bodies, the struct decl's instruction is repurposed to refer
to the field type itself. This is to allow us to easily rebuild the inst_map
mapping required for the init body instructions to refer to the field type.

Thanks to @mlugg for the guidance on this one!
2023-11-07 00:49:35 +00:00
Bogdan Romanyuk
62e67a2b56
Sema: emit error when pointer to extern function is called 2023-11-06 15:26:28 +00:00
Bogdan Romanyuk
e9a6197a8e
Sema: fix error notes with wrong extern type 2023-11-05 12:36:06 +02:00
kcbanner
825da34769 Add compile error test case for union layout depending on pointer alignment 2023-10-31 01:35:58 +00:00
Andrew Kelley
46a6d50fdf Sema: make @src().line comptime-known
Reverts 89cef9f5f7.

Closes #13315
2023-10-23 18:35:10 -07:00
Andrew Kelley
149f2f8adb
Merge pull request #17524 from Vexu/aro-translate-c
Add ability to test Aro based `translate-c`
2023-10-18 00:32:59 -04:00
Bogdan Romanyuk
ad168db727
Sema: disallow @intFromPtr for comptime-only types 2023-10-17 20:05:55 +00:00
Veikka Tuominen
e765495b11 tests: translate-c and run-translated-c to the test harness 2023-10-17 11:55:17 +03:00
Veikka Tuominen
14efbf5ed1 Sema: fix missing check for tuple default initializers
Closes #17525
2023-10-16 14:00:18 +03:00
Bogdan Romanyuk
ebde525cce
Sema: fix @extern error on function pointer 2023-10-16 01:30:39 +00:00
Andrew Kelley
027aabf497 drop for loop syntax upgrade mechanisms 2023-10-13 03:43:54 -07:00
Andrew Kelley
5eb5d523b5 give modules friendly names for error reporting 2023-10-08 20:58:04 -07:00
Veikka Tuominen
63bd2bff12 Sema: add @errorCast which works for both error sets and error unions
Closes #17343
2023-10-01 17:00:01 +03:00
mlugg
09a57583a4
compiler: preserve result type information through address-of operator
This commit introduces the new `ref_coerced_ty` result type into AstGen.
This represents a expression which we want to treat as an lvalue, and
the pointer will be coerced to a given type.

This change gives known result types to many expressions, in particular
struct and array initializations. This allows certain casts to work
which previously required explicitly specifying types via `@as`. It also
eliminates our dependence on anonymous struct types for expressions of
the form `&.{ ... }` - this paves the way for #16865, and also results
in less Sema magic happening for such initializations, also leading to
potentially better runtime code.

As part of these changes, this commit also implements #17194 by
disallowing RLS on explicitly-typed struct and array initializations.
Apologies for linking these changes - it seemed rather pointless to try
and separate them, since they both make big changes to struct and array
initializations in AstGen. The rationale for this change can be found in
the proposal - in essence, performing RLS whilst maintaining the
semantics of the intermediary type is a very difficult problem to solve.

This allowed the problematic `coerce_result_ptr` ZIR instruction to be
completely eliminated, which in turn also simplified the logic for
inferred allocations in Sema - thanks to this, we almost break even on
line count!

In doing this, the ZIR instructions surrounding these initializations
have been restructured - some have been added and removed, and others
renamed for clarity (and their semantics changed slightly). In order to
optimize ZIR tag count, the `struct_init_anon_ref` and
`array_init_anon_ref` instructions have been removed in favour of using
`ref` on a standard anonymous value initialization, since these
instructions are now virtually never used.

Lastly, it's worth noting that this commit introduces a slightly strange
source of generic poison types: in the expression `@as(*anyopaque, &x)`,
the sub-expression `x` has a generic poison result type, despite no
generic code being involved. This turns out to be a logical choice,
because we don't know the result type for `x`, and the generic poison
type represents precisely this case, providing the semantics we need.

Resolves: #16512
Resolves: #17194
2023-09-23 22:01:08 +01:00
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