Commit Graph

427 Commits

Author SHA1 Message Date
Veikka Tuominen d1d24b426d AstGen: check loop bodies and else branches for unused result 2022-08-03 16:45:33 +03:00
Veikka Tuominen 02112f8836 AstGen: add error for break/continue out of defer expression 2022-08-03 16:45:33 +03:00
Veikka Tuominen f1768b40b2 stage2: better source location for var decls 2022-08-01 23:37:01 +03:00
Veikka Tuominen fdaf9c40d6 stage2: handle tuple init edge cases 2022-07-29 10:12:36 +03:00
Veikka Tuominen 9e0a930ce3 stage2: add error for comptime control flow in runtime block 2022-07-29 10:08:35 +03:00
Andrew Kelley 6a4df2778e AstGen: fix ref instruction injection for functions
For the expressions regarding return type, alignment,
parameter type, etc.
2022-07-27 16:19:23 -07:00
Veikka Tuominen e4a36a4cec AstGen: add dbg_stmts for unreachable and @panic
Closes #12249
2022-07-27 17:11:46 +03:00
Veikka Tuominen a463dc7d6c AstGen: disable null bytes and empty stings in some places
Namely:
 * test names
 * identifiers
 * library names
 * import strings
2022-07-26 12:14:59 +03:00
Meghan dea437edfb stage2: implement noinline fn 2022-07-24 11:56:33 +03:00
Veikka Tuominen 2436dd2c1b Sema: validate duplicate fields in anon structs 2022-07-23 15:40:12 +03:00
Veikka Tuominen 15dddfd84d AstGen: make comptime fields in packed and extern structs compile errors 2022-07-23 15:40:12 +03:00
Veikka Tuominen ff7ec4efb5 Sema: bad union field access safety 2022-07-23 15:40:11 +03:00
Veikka Tuominen 4d20d6874c move passing safety tests to stage2 2022-07-23 15:40:11 +03:00
Veikka Tuominen 9fb8d21a01 AstGen: add error for fields in opaque types 2022-07-21 12:21:30 -07:00
Veikka Tuominen 1705a21f80 Sema: more union and enum tag type validation 2022-07-21 12:21:30 -07:00
Veikka Tuominen d729173204 stage2: better pointer source location 2022-07-21 12:21:30 -07:00
Andrew Kelley fad95741db AstGen: fix loop control flow applying to wrong loop
In the case of 'continue' or 'break' inside the 'else' block of a
'while' or 'for' loop.

Closes #12109
2022-07-13 16:10:41 -07:00
Andrew Kelley 4a28c1d5c3 stage2: lower each struct field type, align, init separately
Previously, struct types, alignment values, and initialization
expressions were all lowered into the same ZIR body, which caused false
positive "depends on itself" errors when the initialization expression
depended on the size of the struct.

This also uses ResultLoc.coerced_ty for struct field alignment and
initialization values. The resulting ZIR encoding ends up being roughly
the same, neither smaller nor larger than previously.

Closes #12029
2022-07-13 02:38:13 -04:00
Andrew Kelley 0c78ece1c9 Merge pull request #12016 from Vexu/stage2-compile-errors
Stage2 compile error improvements
2022-07-07 14:52:28 -04:00
emma 81bbefe9b8 AstGen: fix catch payoad not checking for shadowing 2022-07-07 21:38:32 +03:00
Veikka Tuominen 5007f727e5 stage2: move C pointer allowzero error to AstGen 2022-07-07 10:50:06 +03:00
Veikka Tuominen b5ac2b4330 Sema: improve array source location 2022-07-07 10:50:06 +03:00
Veikka Tuominen 2ca752ea1a Module: add .node_offset_un_op 2022-07-07 10:50:06 +03:00
Veikka Tuominen 299836dbd9 Sema: panic at comptime + misc error message improvements 2022-07-07 10:50:05 +03:00
Veikka Tuominen 252388eb28 AstGen: move error_to_int, int_to_error and select to extended 2022-07-07 10:45:45 +03:00
Veikka Tuominen 2029601cb2 AstGen: use elem_{ptr,val}_node for array access syntax 2022-07-01 10:22:26 +03:00
Veikka Tuominen ae7b32eb62 Sema: validate deref operator type and value 2022-07-01 10:22:25 +03:00
Veikka Tuominen cc3336c784 Sema: add source location to coerce result ptr, fix negation error 2022-06-30 09:57:38 +02:00
Veikka Tuominen 3204d00a5e move passing stage1 compile error tests to stage2 2022-06-30 09:57:38 +02:00
Andrew Kelley c3ae909e93 Revert "AstGen: preserve inferred ptr result loc for breaks"
This reverts commit 8bf3e1f8d0, which
introduced miscompilations for peer expressions any time they needed
coercions to runtime types.

I opened #11957 as a proposal to accomplish the goal of the reverted
commit.

Closes #11898
2022-06-28 18:38:25 -07:00
Veikka Tuominen 35c7e376b8 stage2: improve anon name strategy for local variables 2022-06-11 23:49:33 +03:00
Andrew Kelley f4d5fcde72 AstGen: avoid redundant "ref" instructions
Whenever a `ref` instruction is needed, it is created and saved in
`AstGen.ref_table` instead of being immediately appended to the current
block body. Then, when the referenced instruction is being added to the
parent block (e.g. from setBlockBody), if it has a ref_table entry, then
the ref instruction is added directly after the instruction being referenced.
This makes sure two properties are upheld:
1. All pointers to the same locals return the same address. This is required
   to be compliant with the language specification.
2. `ref` instructions will dominate their uses. This is a required property
   of ZIR.

A complication arises when a ref instruction refs another ref
instruction. The logic in appendBodyWithFixups must take this into
account, recursively handling ref refs.
2022-06-08 20:40:16 -07:00
Veikka Tuominen fbd7e4506f stage2: implement asm with multiple outputs 2022-06-07 21:27:06 +03:00
Andrew Kelley d1bfc83774 Merge pull request #11783 from ziglang/stage2-try
introduce a "try" ZIR and AIR instruction
2022-06-06 19:01:39 -04:00
Veikka Tuominen 14685e59b2 stage2: use correct type (u29) for alignment 2022-06-06 13:11:54 -07:00
Veikka Tuominen 8fa88c88c2 AstGen: fix coercion scope type when stores are eliminated 2022-06-06 13:11:50 -07:00
Andrew Kelley 6d3586e0ed explicit "_ptr" variants of ZIR try instruction
* Introduce "_ptr" variants of ZIR try instruction to disallow constructs
   such as `try` on a pointer value instead of an error union value.
 * Disable the "_inline" variants of the ZIR try instruction for now because
   we are out of ZIR tags. I will free up some space in an independent commit.
 * AstGen: fix tryExpr calling rvalue() on ResultLoc.ref
2022-06-05 10:37:08 +02:00
Andrew Kelley ef885a78d6 stage2: implement the new "try" ZIR/AIR instruction
Implements semantic analysis for the new try/try_inline ZIR
instruction. Adds the new try/try_ptr AIR instructions and implements
them for the LLVM backend.

Fixes not calling rvalue() for tryExpr in AstGen.

This is part of an effort to implement #11772.
2022-06-05 10:37:08 +02:00
Andrew Kelley 0224ad19b8 AstGen: introduce try instruction
This introduces two ZIR instructions:
 * `try`
 * `try_inline`

This is part of an effort to implement #11772.
2022-06-05 10:37:08 +02:00
Andrew Kelley 356a865b87 stage2: introduce support for noalias
Not implemented yet is enhancements to coerceInMemory to account for
noalias parameters.

Related to #11498.
2022-05-31 17:38:42 -07:00
Andrew Kelley c3ef4ac15f Merge pull request #11752 from ziglang/zir-fancy-fns
stage2: add missing data to ZIR encoding of functions
2022-05-30 21:18:10 -04:00
Andrew Kelley 7e98b047dd AstGen: simplify function return type expressions
This check for primitives is already handled by the generic logic that
checks if the body ends up being empty. I kept this commit in the git
history in case we ever want that nodePrimitive function again in the
future, it might be useful.
2022-05-30 12:03:53 -07:00
Andrew Kelley 9da3a058d8 stage2: add missing data to ZIR encoding of functions
The main purpose of this commit is to prepare to implement support for
callconv(), align(), linksection(), and addrspace() annotations on
generic functions where the provided expression depends on comptime
parameters (making the function generic).

It's a rather involved change, so this commit only makes the necessary
changes to AstGen without regressing any behavior, and a follow-up
commit can finish the task by making the enhancements to Sema.

By my quick estimation, the new encoding for functions is a negligible
improvement - along the lines of 0.005% fewer total ZIR bytes on
average. Still, it's nice that this commit, while adding more
data into ZIR, actually ends up reducing the storage size thanks to a
slightly more sophisticated encoding.

Zir.Inst.ExtendedFunc is renamed to Zir.Inst.FuncFancy to eliminate
confusion about it being an extended instruction (it used to be but is
no longer). The encoding for this instruction is completely reworked.

The encoding for Zir.Inst.Func is also changed slightly - when the
return type body length is 1, then only a Zir.Inst.Ref is provided; not
a full body.

linksection() and addrspace() are now communicated via func_fancy ZIR
instruction rather than as part of the corresponding decl. This allows
their expressions to observe comptime parameters.
2022-05-30 12:03:53 -07:00
Veikka Tuominen 0e8307789a AstGen: add tuple aware elem_type_index 2022-05-29 13:19:03 +03:00
Veikka Tuominen c7b778992e AstGen: improve generated Zir for array init exprs 2022-05-29 13:19:03 +03:00
Veikka Tuominen 8bf3e1f8d0 AstGen: preserve inferred ptr result loc for breaks 2022-05-27 10:29:24 +03:00
Andrew Kelley cd04b49041 stage2: fix @call when used in a comptime or nosuspend block
`@call` allows specifying the modifier explicitly, however it can still
appear in a context that overrides the modifier. This commit adds flags
to the BuiltinCall ZIR encoding. Since we have unused bits I also threw
in the ensure_result_used mechanism.

I also deleted a behavior test that was checking for bound function
behavior where I think stage2 behavior is correct and stage1 behavior
is incorrect.
2022-05-20 02:47:20 -07:00
Veikka Tuominen ab4ec35b8b stage2: add runtime safety for unwrapping error 2022-05-16 17:42:51 -07:00
Andrew Kelley 5b1c0d922c stage2: improve semantics of atomic operations
ZIR instructions updated: atomic_load, atomic_rmw, atomic_store, cmpxchg
These no longer construct a pointer type as the result location. This
solves a TODO that was preventing the pointer from possibly being
volatile, as well as properly handling allowzero and addrspace.
It also allows the pointer to be over-aligned, which may be needed
depending on the target. As a consequence, the element type needs to be
communicated in the ZIR. This is done by strategically making one of the
operands be ResultLoc.ty instead of ResultLoc.coerced_ty if possible, or
otherwise explicitly adding elem_type into the ZIR encoding, such as in
the case of atomic_load.

The pointer type of atomic operations is now checked in Sema by coercing
it to an expected pointer type, that maybe over-aligned according to
target requirements.

Together with the previous commit, Zig now has smaller alignment for
large integers, depending on the target, and yet still has type safety
for atomic operations that specially require higher alignment.
2022-05-04 17:34:16 -07:00
Marc Tiehuis bbfe2234c8 update AstGen float literal parsing 2022-05-03 16:46:40 +12:00