Commit Graph

371 Commits

Author SHA1 Message Date
mlugg
d11bbde5f9
compiler: remove anonymous struct types, unify all tuples
This commit reworks how anonymous struct literals and tuples work.

Previously, an untyped anonymous struct literal
(e.g. `const x = .{ .a = 123 }`) was given an "anonymous struct type",
which is a special kind of struct which coerces using structural
equivalence. This mechanism was a holdover from before we used
RLS / result types as the primary mechanism of type inference. This
commit changes the language so that the type assigned here is a "normal"
struct type. It uses a form of equivalence based on the AST node and the
type's structure, much like a reified (`@Type`) type.

Additionally, tuples have been simplified. The distinction between
"simple" and "complex" tuple types is eliminated. All tuples, even those
explicitly declared using `struct { ... }` syntax, use structural
equivalence, and do not undergo staged type resolution. Tuples are very
restricted: they cannot have non-`auto` layouts, cannot have aligned
fields, and cannot have default values with the exception of `comptime`
fields. Tuples currently do not have optimized layout, but this can be
changed in the future.

This change simplifies the language, and fixes some problematic
coercions through pointers which led to unintuitive behavior.

Resolves: #16865
2024-10-31 20:42:53 +00:00
Andrew Kelley
3929cac154
Merge pull request #21257 from mlugg/computed-goto-3
compiler: implement labeled switch/continue
2024-09-04 18:31:28 -07:00
mlugg
5e12ca9fe3
compiler: implement labeled switch/continue 2024-09-01 18:30:31 +01:00
mlugg
6e3e23a941
compiler: implement decl literals
Resolves: #9938
2024-09-01 17:34:07 +01:00
mlugg
4c0f021c2e
behavior: import unintentionally excluded file from behavior.zig
And remove the now-invalid test for the return value of `@branchHint`.
2024-08-27 00:44:35 +01:00
Dominic
1550b5b16d
astgen: fix result info for catch switch_block_err_union 2024-05-11 12:06:13 +03:00
Jacob Young
4a8121c1ab Sema: fix non-pub usingnamespace in @typeInfo 2024-04-06 12:57:51 -07:00
Jacob Young
e60d667111 Module: fix @embedFile of files containing zero bytes
If an adapted string key with embedded nulls was put in a hash map with
`std.hash_map.StringIndexAdapter`, then an incorrect hash would be
entered for that entry such that it is possible that when looking for
the exact key that matches the prefix of the original key up to the
first null would sometimes match this entry due to hash collisions and
sometimes not if performed later after a grow + rehash, causing the same
key to exist with two different indices breaking every string equality
comparison ever, for example claiming that a container type doesn't
contain a field because the field name string in the struct and the
string representing the identifier to lookup might be equal strings but
have different string indices.  This could maybe be fixed by changing
`std.hash_map.StringIndexAdapter.hash` to only hash up to the first
null, therefore ensuring that the entry's hash is correct and that all
future lookups will be consistent, but I don't trust anything so instead
I assert that there are no embedded nulls.
2024-02-22 12:33:53 -08:00
Jacob Young
2291560424 c_import: extract behavior tests that use @cImport
This introduces the new test step `test-c-import`, and removes the
ability of the behavior tests to `@cImport` paths relative to `test`.
This allows the behavior tests to be run without translate c.
2024-02-20 18:44:43 +01:00
Andrew Kelley
30688c341b LLVM: fix lowering of extern anyopaque
closes #18461
2024-01-11 01:00:49 -08:00
Veikka Tuominen
804cee3b93 categorize behavior/bugs/<issueno>.zig tests 2024-01-06 16:49:41 -08:00
Jacob Young
047d6d996e cbe: fix non-msvc externs and exports
Closes #17817
2024-01-03 02:52:25 -05:00
Tw
2fefc0b5c7
Zir: add missing extra index for linksection_or_addspace
Closes #18052
Closes #18104

Signed-off-by: Tw <tw19881113@gmail.com>
2023-11-25 11:39:37 +00:00
Andrew Kelley
24d9438bcc split export behavior test into export_keyword and export_builtin 2023-10-27 12:45:04 -07:00
Andrew Kelley
9f0fd72321 categorize nan behavior test
move it from bugs/xxx.zig to its own category
2023-10-27 12:43:34 -07:00
Andrew Kelley
3fb301b16a categorize fn ptr behavior test
move a function pointer test from bugs/xxx.zig to fn.zig
2023-10-27 12:43:34 -07:00
Andrew Kelley
dfe9cae4eb categorize globals behavior tests
moves some tests that store to global variables to their own category
instead of being named after a GitHub issue.
2023-10-27 12:43:34 -07:00
Andrew Kelley
d4911794ae rename behavior test to better describe what it does
In general, let's not lean on GitHub issue numbers as having meaning.
The goal of behavior tests is to produce a minimum set of tests that
test 100% of the language.
2023-10-23 17:28:10 -07:00
Andrew Kelley
ecfb18286a migrate make_ptr_const to new anonymous decl mechanism
Instead of creating Module.Decl objects, directly create InternPool
pointer values using the anon_decl Addr encoding.

The LLVM backend needed code to notice the alignment of the pointer and
lower accordingly. The other backends likely need a similar change.
2023-10-21 21:38:41 -04:00
antlilja
fcdb7d9e47 Add behavior tests for @abs builtin 2023-09-27 11:24:36 -07: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
Jacob Young
8d1805f81c behavior: add coverage for no longer reproducing issue
Closes #14305
2023-07-29 09:00:23 -07:00
Andrew Kelley
87961237cf add behavior test for tail calls
closes #9703
2023-07-26 19:02:02 -07:00
Jacob G-W
3c08fe931a make @typeInfo not return private decls
fixes #10731
Thanks @nektro for previous work in #14878

This change creates a small breaking change:
It removes the `is_pub` field of a decl in `@typeInfo`
2023-07-25 16:19:08 -07:00
Andrew Kelley
ddd27db362 add comment to discourage using GitHub issue numbers
in behavior tests.

closes #16506
2023-07-24 10:54:08 -07:00
Evan Haas
fb9d6b8bd9
codegen: Set c_char signedness based on the target 2023-06-20 00:26:42 -07:00
Eric Joldasov
50339f595a all: zig fmt and rename "@XToY" to "@YFromX"
Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>
2023-06-19 12:34:42 -07:00
Luuk de Gram
e3db210cf1
wasm: support calling alias'd function pointers
When lowering a decl value we verify whether its owner decl index
equals to the decl index of the decl being lowered. When this is not
the case, we are lowering an alias. So instead, we will now lower
the owner decl instead and call its symbol to ensure its type
is being correctly generated.
2023-06-16 17:16:56 +02:00
Evin Yulo
235b776d61 fix #15778: Binary operations on empty vectors crash 2023-05-29 13:01:11 +03:00
Ali Chraghi
ccc490ef68
setup spirv backend in behavior tests 2023-05-11 20:31:52 +02:00
Dominic
5a3eca5d4c
Disallow named test decls with duplicate names 2023-05-08 10:59:06 +03:00
Andrew Kelley
73d3fb9883 C backend: fix ptr comparison of array ptrs when one is null-terminated 2023-04-28 13:24:43 -07:00
Andrew Kelley
00b690540e llvm backend: fix lowering of memset
The bitcast of ABI size 1 elements was problematic for some types.
2023-04-28 13:24:42 -07:00
mlugg
71e873703f Sema: make @returnAddress return 0 at comptime
See also #14938.

Resolves: #14931
2023-03-17 15:55:02 -04:00
Ian Johnson
adc6dec26b Sema: avoid panic on callconv(.C) generic return type
Fixes #14854
2023-03-13 13:17:13 +02:00
Veikka Tuominen
d284c00fda Sema: handle lazy values in more places
* resolve lazy values in anon structs being passed to anytype params
* use `resolveMaybeUndefValIntable` where appropriate

Closes #14356
2023-01-22 00:12:36 +02:00
Michael Dusan
e0fb4c29cb llvm codegen: fix f16,f32,f64 nan bitcasts
@bitCast from integer NaN representation to float NaN resulted in
changed bits in float. This only happened with signaled NaN.

- added test for signaled NaN
- added tests for quiet NaN (for completeness)

closes #14198
2023-01-05 02:22:30 -07:00
Michael Dusan
ed23615638 behavior: add test for #8277
Test `@sizeOf` reified union with zero-size payload fields.

closes #8277
2023-01-04 15:48:00 -05:00
Veikka Tuominen
81443fcde8 Sema: add error for recursive inline call
Closes #12973
2022-12-26 16:36:30 +02:00
travisstaloch
581d292381
fix overflow found while fuzzing
* allow file level `union {}` to parse as tuple field

this was found while fuzzing zls.

* before this patch the input `union {}` crashed the parser.  after
  this, it parses correctly just like `struct {}`.
* adds behavior tests for both inputs `struct {}` and `union {}`,
  checking that each becomes a file level tuple field.
2022-12-23 23:10:04 +02:00
Andrew Kelley
f211c1559a
Merge pull request #13960 from r00ster91/stage1cruft
Close more old stage1 issues
2022-12-22 16:36:55 -05:00
r00ster91
5ecc2b99af behavior: more test coverage
Closes #12450
Closes #13113
Closes #12051
Closes #12092
Closes #12119
Closes #12142
Closes #12450
Closes #13113
Closes #11995
Closes #12000
2022-12-21 23:34:22 +01:00
Veikka Tuominen
901c3e9636
Merge pull request #13552 from hryx/comparus-tautologicus
Sema: elide integer comparisons with guaranteed outcomes
2022-12-18 01:57:49 +02:00
Evin Yulo
8c8f6bfa64 Add test for #13366
Closes #13366
2022-12-16 18:21:56 +02:00
Evin Yulo
b5d1df091b Add test for #12571
Closes #12571
2022-12-16 18:19:32 +02:00
Travis Staloch
1ebb761244 codegen - lower str_lit to vector 2022-12-16 06:08:10 -05:00
Stevie Hryciw
e57e835904 Sema: elide integer comparisons with guaranteed outcomes 2022-12-15 00:56:27 -08:00
IntegratedQuantum
0b4461d97b
Fix tautological big_int tests. 2022-12-14 00:29:25 +00:00
r00ster91
654e30069d behavior and cases: more test coverage for old issues
Closes #2622
Closes #2727
Closes #6047
Closes #6947
Closes #6656
2022-12-10 12:34:42 +01:00
r00ster91
bf863878ac behavior: add test coverage for slice and array-related issues
Closes #10684
Closes #6905
Closes #8646
2022-12-10 12:34:42 +01:00