Commit graph

73 commits

Author SHA1 Message Date
Jacob Young
c11b6adf13 Ast: fix comptime destructure
A preceding `comptime` keyword was being ignored if the first
destructure variable was an expression.
2024-03-17 15:23:16 -07:00
Jacob Young
aa7d16aba1 grammar: remove gratuitous ambiguity
Previously, the following matched both ContainerField alternatives:
 * [IDENTIFIER]
 * [IDENTIFIER][COLON][TypeExpr]
2024-03-06 13:59:46 -08:00
Andrew Kelley
d661f0f35b compiler: JIT zig fmt
See #19063
2024-02-26 22:26:19 -07:00
Andrew Kelley
a2e87aba66 rearrange std.zig
This frees up std.zig.fmt to be used for the implementation of `zig
fmt`.
2024-02-26 21:35:33 -07:00
fn ⌃ ⌥
75b3feee05 std.zig.Ast: Reorder fields to reflect position in source 2023-12-29 08:07:37 -08:00
fn ⌃ ⌥
04dad64dbe std.zig.Ast: Fix docs for FnProto and FnProtoOne 2023-12-29 07:52:11 -08:00
frmdstryr
f258a391da
Speed up ast.tokenLocation 2023-11-09 01:45:25 +00:00
Andrew Kelley
9a1094c00f fix compilation regression 2023-11-03 20:05:32 -07:00
Andrew Kelley
3263d6e6ab std.zig: move render state to struct; add fixups 2023-11-03 20:05:32 -07:00
frmdstryr
a4cffd80bd Update slice_sentinel comment
Slice end can be omitted
2023-10-27 18:45:59 +03:00
frmdstryr
8dccd77277 Update comment on while
The @"while" is still used if cont expr is missing.
2023-10-27 18:45:24 +03:00
frmdstryr
5c5d1f93c4 Update comment on for_range in Ast
The rhs can be omitted eg `0..`
2023-10-25 14:09:34 -04:00
frmdstryr
f30ab46306 Update comment on Tag value assign_mod in Ast.zig 2023-10-25 09:31:27 +03:00
Tobias Simetsreiter
7a9500fd80
Fix rendering ast in zon mode (#17547)
Co-authored-by: Tobias Simetsreiter <tobias.simetsreiter@wabtec.com>
2023-10-17 19:04:01 -04:00
Andrew Kelley
027aabf497 drop for loop syntax upgrade mechanisms 2023-10-13 03:43:54 -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
techatrix
7e25fb4a43 add bound check on for and while nodes 2023-07-28 00:20:53 -07:00
mlugg
f26dda2117 all: migrate code to new cast builtin syntax
Most of this migration was performed automatically with `zig fmt`. There
were a few exceptions which I had to manually fix:

* `@alignCast` and `@addrSpaceCast` cannot be automatically rewritten
* `@truncate`'s fixup is incorrect for vectors
* Test cases are not formatted, and their error locations change
2023-06-24 16:56:39 -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
Jacob Young
bac3a28214 fmt: avoid canonicalizing enum fields named @"_" to _
This can be used to escape the usual meaning of `_` to indicate a
non-exhaustive enum and create an enum tag that is a literal underscore,
so zig fmt should allow this syntax.

Before, zig fmt changes

    const E = enum { @"_" };

to the semantically different

    const E = enum { _ };

After, it remains the same.
2023-05-08 10:58:31 +03:00
r00ster91
e0d3904638 Ast: properly handle sentinel-terminated slices in tuple
Co-authored-by: Veikka Tuominen <git@vexu.eu>
2023-03-04 01:08:03 +02: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
aeaef8c0ff update std lib and compiler sources to new for loop syntax 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
Veikka Tuominen
1b7055b514 parse and render new for loop syntax 2023-02-18 19:17:20 -07:00
Ali Chraghi
c32171991b Ast: fix expected_block error message
Fixes #14579
2023-02-17 20:54:50 +02:00
Andrew Kelley
81c27c74bc use build.zig.zon instead of build.zig.ini for the manifest file
* improve error message when build manifest file is missing
 * update std.zig.Ast to support ZON
 * Compilation.AllErrors.Message: make the notes field a const slice
 * move build manifest parsing logic into src/Manifest.zig and add more
   checks, and make the checks integrate into the standard error
   reporting code so that reported errors look sexy

closes #14290
2023-02-03 00:06:11 -07:00
Andrew Kelley
873bb29c98 introduce ZON: Zig Object Notation
* std.zig.parse is moved to std.zig.Ast.parse
 * the new function has an additional parameter that requires passing
   Mode.zig or Mode.zon
 * moved parser.zig code to Parse.zig
 * added parseZon function next to parseRoot function
2023-02-03 00:06:11 -07:00
Veikka Tuominen
e2adf3b61a parser: add helpful note for missing const/var before container level decl
Closes #13888
2023-01-11 21:11:21 +02:00
Techatrix
1f8f79cd53
std: add helper functions to std.zig.Ast for extracting data out of nodes 2023-01-09 16:59:19 +02:00
Veikka Tuominen
af9a9a1374 zig fmt: improve handling of comptime tuple fields 2022-12-27 13:52:37 +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
travisstaloch
4aa8462cc9
std.zig: fix integer overflows during parsing
these were found while fuzzing zls.

this patch prevents overflow for the following file contents and adds
tests for them.
	* `enum(u32)` - causes overflow in std.zig.Ast.fullContainerDecl()
	* `*x` - causes overflow in std.zig.Ast.fullPtrType()
	* `**x` - causes overflow in std.zig.Ast.firstToken()
2022-12-20 17:33:40 +02:00
Veikka Tuominen
40ed6ae846
Merge pull request #13930 from r00ster91/renamings
std.builtin: renamings
2022-12-18 19:33:15 +02:00
Techatrix
4809e0ea7f
fix potential integer underflow in std.zig.Ast.fullCall 2022-12-18 04:59:43 +00:00
r00ster91
aac2d6b56f std.builtin: rename Type.UnionField and Type.StructField's field_type to type 2022-12-17 14:11:33 +01:00
Andrew Kelley
50eb7983cd remove most conditional compilation based on stage1
There are still a few occurrences of "stage1" in the standard library
and self-hosted compiler source, however, these instances need a bit
more careful inspection to ensure no breakage.
2022-12-06 20:38:54 -07:00
Veikka Tuominen
4cea15f12b std.zig.Ast: simplify usage of tuple_like container fields 2022-11-23 12:13:39 +02:00
Veikka Tuominen
6fb689e97a parser: allow unnamed fields in structs 2022-11-23 12:13:39 +02:00
Veikka Tuominen
278c32976e parser: add helpful error for extra = in variable initializer
Closes #12768
2022-10-29 14:55:43 +03:00
Veikka Tuominen
9607bd90e6 parser: improve error message for missing var/const before local variable
Closes #12721
2022-10-29 14:55:43 +03:00
Veikka Tuominen
b4d81857f3 stage1+2: parse inline switch cases 2022-09-27 18:05:08 +03:00
Veikka Tuominen
349d78a443 validate number literals in AstGen 2022-09-13 20:26:04 -04:00
Isaac Freund
0d32b73078
stage2: Implement explicit backing integers for packed structs
Now the backing integer of a packed struct type may be explicitly
specified with e.g. `packed struct(u32) { ... }`.
2022-08-10 19:54:45 +02:00
Veikka Tuominen
2f54129087 parser: add error for doc comment attached to comptime or test blocks 2022-07-26 12:14:59 +03:00
Veikka Tuominen
5d22204d2d parser: add helpful error for C style container declarations
```zig
// a.zig
struct Foo {
    a: u32,
};
```

before:
```
a.zig:1:1: error: expected test, comptime, var decl, or container field, found 'struct'
struct Foo {
^
```
after:
```
a.zig:1:8: error: 'struct Foo' is invalid
struct Foo {
       ^
a.zig:1:8: note: to declare a container do 'const Foo = struct'
struct Foo {
       ^
```
2022-07-12 12:50:59 +03:00
Andrew Kelley
d227f76afb std.zig.Ast: fix escaped capture of by-value parameters 2022-03-30 11:52:10 -07:00
Daniel Hooper
911c839e97
add error when binary ops don't have matching whitespace on both sides
This change also moves the warning about "&&" from the AstGen into the parser so that the "&&" warning can supersede the whitespace warning.
2022-03-20 12:55:04 +02:00