Commit Graph

6075 Commits

Author SHA1 Message Date
mlugg
6df78c3bc1 Sema: mark pointers to inline functions as comptime-only
This is supposed to be the case, similar to how pointers to generic
functions are comptime-only (several pieces of logic already assumed
this). These types being considered runtime was causing `dbg_var_val`
AIR instructions to be wrongly emitted for such values, causing codegen
backends to create a runtime reference to the inline function, which (at
least on the LLVM backend) triggers an error.

Resolves: #38
2023-09-15 21:46:38 -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
mlugg
cba7e8a4e9 AstGen: do not forward result pointers through @as
The `coerce_result_ptr` instruction is highly problematic and leads to
unintentional memory reinterpretation in some cases. It is more correct
to simply not forward result pointers through this builtin.

`coerce_result_ptr` is still used for struct and array initializations,
where it can still cause issues. Eliminating this usage will be a future
change.

Resolves: #16991
2023-09-15 01:05:02 -07:00
Jakub Konka
975cb235cc
Merge pull request #17081 from Techatrix/wasm-float-op
wasm: implement more float operations on f80 and f128
2023-09-11 07:59:30 +02:00
Techatrix
ab970094ab wasm: enable successful behavior tests 2023-09-10 15:59:02 +02:00
Veikka Tuominen
6484e279e5 AstGen: fix missing array type validation
Closes #17084
2023-09-07 16:56:07 +03:00
Carl Åstholm
e980bd0aee translate-c: fix panic when translating long double literals 2023-09-06 11:12:32 +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
e2ff486de5 Sema: cleanup coerceExtra
* remove unreachable code
 * remove already handled cases
 * avoid `InternPool.getCoerced`
 * add some undef checks
 * error when converting undef int to float

Closes #16987
2023-08-30 16:50:30 -04:00
Andrew Kelley
49075d2055
Merge pull request #16969 from jacobly0/no-clear-ref-trace
Sema: refactor to use fewer catch expressions
2023-08-29 16:13:39 -04:00
Jakub Konka
3b2b9fcbc5 darwin: move inference of SDK version into the linker
`std.zig.system.darwin.getSdk` now pulls only the SDK path
so we execute a child process only once and not twice as it was
until now since we parse the SDK version directly from the pulled path.
This is actually how `ld64` does it too.
2023-08-29 06:43:41 +02:00
Jacob Young
c429bb5d2f llvm/cbe: support slice in @prefetch
Closes #16967
2023-08-28 19:04:53 -07:00
Jacob Young
7a251c4cb8 Sema: revert reference trace changes that are no longer needed 2023-08-28 17:43:37 -04:00
mlugg
b8e6c42688 compiler: provide result type for @memset value
Resolves: #16986
2023-08-28 12:33:36 -07: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
Jay Petacat
ff61c42879 std: Rename TailQueue to DoublyLinkedList
`TailQueue` was implemented as a doubly-linked list, but named after an
abstract data type. This was inconsistent with `SinglyLinkedList`, which
can be used to implement an abstract data type, but is still named after
the implementation. Renaming `TailQueue` to `DoublyLinkedList` improves
consistency between the two type names, and should help discoverability.

`TailQueue` is now a deprecated alias of `DoublyLinkedList`.

Related to issues #1629 and #8233.
2023-08-27 20:57:46 -07:00
riverbl
87557b37c6 Replace @panic with unreachable, add test
Replace `@panic` with `unreachable` in stage2 wasm `@divFloor` implementation

Add test for division and remainder operations for stage2 wasm
2023-08-23 20:34:59 +01: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
Carl Åstholm
60fc18bd1c compiler_rt: fix f80 comparisons
This corrects comparisons between negative numbers.
2023-08-21 11:26:25 -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
Andrew Kelley
4c97919e8e
Merge pull request #16885 from squeek502/windows-selfExePath-symlink
`fs.selfExePath`: Make the Windows implementation follow symlinks
2023-08-19 21:24:36 -07:00
Ryan Liptak
47343f9bcf Add self_exe_symlink standalone test
Tests that fs.selfExePath follows symlinks by comparing it to the path of the File returned from fs.openSelfExe
2023-08-19 17:48:19 -07:00
Andrew Kelley
84b4b6bffa
Merge pull request #16818 from ziglang/damn-sysroot
build: do not emit `-iwithsysroot`/`-iframeworkwithsysroot` implicitly
2023-08-18 18:06:20 -07:00
Jakub Konka
f4afc6525b standalone: add iOS smoke test - test ability to target iOS with Zig 2023-08-18 11:58:11 +02:00
Jakub Konka
517a2c7caf build: add build test check for availability of IOS SDK on the host 2023-08-18 11:57:12 +02:00
Lewis Gaul
387b0ac4f1
Make NaNs quiet by default and other NaN tidy-up (#16826)
* Generalise NaN handling and make std.math.nan() give quiet NaNs

* Address uses of std.math.qnan_* and std.math.nan_* consts

* Comment out failing test due to issues with signalling NaN

* Fix issue in c_builtins.zig where we need qnan_u32
2023-08-18 02:07:49 -04:00
Jakub Konka
fd830b146d
Merge pull request #16852 from ziglang/issue-16751
macho: tie FDEs and unwind records to all symbol aliases, not just the first global
2023-08-16 15:21:46 +02:00
Jakub Konka
573bb77ab6 macho: add smoke test for re-exports in static libs 2023-08-16 12:19:34 +02:00
Evan Haas
49cb1683ff translate-c: Use canonical type of field for flexible array definition
This prevents incorrectly casting an ElaboratedType to ArrayType
if the flexible field is a typedef'ed zero-length array

Fixes #16838
2023-08-15 23:59:07 -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
Jacob Young
8b9161179d Sema: avoid deleting runtime side-effects in comptime initializers
Closes #16744
2023-08-11 11:01:47 -07:00
Xavier Bouchoux
77dd64b5f4 Sema: fix coerceArrayLike() for vectors with padding
as explainded at https://llvm.org/docs/LangRef.html#vector-type :

"In general vector elements are laid out in memory in the same way as array types.
Such an analogy works fine as long as the vector elements are byte sized.
However, when the elements of the vector aren’t byte sized it gets a bit more complicated.
One way to describe the layout is by describing what happens when a vector such
as <N x iM> is bitcasted to an integer type with N*M bits, and then following the
rules for storing such an integer to memory."

"When <N*M> isn’t evenly divisible by the byte size the exact memory layout
is unspecified (just like it is for an integral type of the same size)."
2023-08-10 16:10:59 -07:00
Andrew Kelley
b820d5df79
Merge pull request #16747 from jacobly0/llvm-wo-libllvm
llvm: enable the backend even when not linked to llvm
2023-08-10 12:02:57 -07: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
f2c8fa769a
Sema: refactor generic calls to interleave argument analysis and parameter type resolution
AstGen provides all function call arguments with a result location,
referenced through the call instruction index. The idea is that this
should be the parameter type, but for `anytype` parameters, we use
generic poison, which is required to be handled correctly.

Previously, generic instantiations and inline calls worked by evaluating
all args in advance, before resolving generic parameter types. This
means any generic parameter (not just `anytype` ones) had generic poison
result types. This caused missing result locations in some cases.

Additionally, the generic instantiation logic caused `zirParam` to
analyze the argument types a second time before coercion. This meant
that for nominal types (struct/enum/etc), a *new* type was created,
distinct to the result type which was previously forwarded to the
argument expression.

This commit fixes both of these issues. Generic parameter type
resolution is now interleaved with argument analysis, so that we don't
have unnecessary generic poison types, and generic instantiation logic
now handles parameters itself rather than falling through to the
standard zirParam logic, so avoids duplicating the types.

Resolves: #16566
Resolves: #16258
Resolves: #16753
2023-08-10 10:00:26 +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
mlugg
6917a8c258
AstGen: handle ty result location for struct and array init correctly
Well, this was a journey!

The original issue I was trying to fix is covered by the new behavior
test in array.zig: in essence, `ty` and `coerced_ty` result locations
were not correctly propagated.

While fixing this, I noticed a similar bug in struct inits: the type was
propagated to *fields* fine, but the actual struct init was
unnecessarily anonymous, which could lead to unnecessary copies. Note
that the behavior test added in struct.zig was already passing - the bug
here didn't change any easy-to-test behavior - but I figured I'd add it
anyway.

This is a little harder than it seems, because the result type may not
itself be an array/struct type: it could be an optional / error union
wrapper. A new ZIR instruction is introduced to unwrap these.

This is also made a little tricky by the fact that it's possible for
result types to be unknown at the time of semantic analysis (due to
`anytype` parameters), leading to generic poison. In these cases, we
must essentially downgrade to an anonymous initialization.

Fixing these issues exposed *another* bug, related to type resolution in
Sema. That issue is now tracked by #16603. As a temporary workaround for
this bug, a few result locations for builtin function operands have been
disabled in AstGen. This is technically a breaking change, but it's very
minor: I doubt it'll cause any breakage in the wild.
2023-08-09 19:46:55 +01:00
Zachary Raineri
0461a64a93
change uses of std.builtin.Mode to OptimizeMode (#16745)
std.builtin.Mode is deprecated.
2023-08-09 14:39:34 -04: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
736df27663 Sema: use the correct decl for generic argument source locations
Closes #16746
2023-08-09 10:09:01 -04:00
Jacob Young
9630379a8e Sema: fix generic method argument source locations 2023-08-09 10:09:01 -04:00
Jacob Young
66084b6c3f Sema: remove validateRunTimeType
This function does not seem to differ in any interesting way from
`!typeRequiresComptime`, other than the `is_extern` param which is only
used in one place, and some differences did not seem correct anyway.

My reasoning for changing opaque types to be comptime-only is that
`explainWhyTypeIsComptime` is quite happy to explain why they are. :D
2023-08-09 05:46:44 -04:00