Commit Graph

956 Commits

Author SHA1 Message Date
Andrew Kelley
3145ae561d Sema: fix compile error source location regressions 2023-07-18 19:02:06 -07:00
Andrew Kelley
8daa8d255b Sema: fix fn_proto_param LazySrcLoc resolution
to match source code span from merge-base.
2023-07-18 19:02:06 -07:00
Andrew Kelley
0153f3a8f9 Sema: fix crash: array_in_c_exported_function
Fuck it, we're storing decl indexes in LazySrcLoc now.
2023-07-18 19:02:06 -07:00
Andrew Kelley
47499bf47b Sema: enhance generic call error message
when the type of an anytype parameter is a comptime-only type but the
argument at the callsite is runtime-known.
2023-07-18 19:02:06 -07:00
antlilja
a0ec2266fe Update tests to new splat syntax 2023-07-12 15:35:57 -07:00
Anton Lilja
711b4e93e2
Fixes crash when a struct is given as the first parameter to the unionInit builtin (#16385) 2023-07-11 23:37:42 -07:00
Anton Lilja
ff0e2ab398
Fixes wrong error location for unionInit when first parameter is not a type (#16384) 2023-07-11 23:35:50 -07:00
r00ster91
0b1e8690da AstGen: make sure for range start and end are usizes
Fixes #16311

The actual cause of #16311 is the `start_is_zero` special case:
```zig
                const range_len = if (end_val == .none or start_is_zero)
                    end_val
                else
                    try parent_gz.addPlNode(.sub, input, Zir.Inst.Bin{
                        .lhs = end_val,
                        .rhs = start_val,
                    });
```
It only happens if the range start is 0. In that case we would not perform any type checking.
Only in the other cases coincidentally `.sub` performs type checking in Sema, but the errors are still rather poor:
```
$ zig test x.zig
x.zig:9:15: error: invalid operands to binary expression: 'Pointer' and 'Pointer'
    for ("abc".."def") |val| {
         ~~~~~^~~~~~~
```
Note how it's the same as if I use `-`:
```
x.zig:9:11: error: invalid operands to binary expression: 'Pointer' and 'Pointer'
    "abc" - "def";
    ~~~~~~^~~~~~~
```
Now after this PR, the errors are much clearer for both range start and end:
```
x.zig:9:10: error: expected type 'usize', found '*const [3:0]u8'
    for ("abc".."def") |val| {
         ^~~~~
```
This is why I decided to use `.ty` instead of `.coerced_ty` for both range start and end rather than
just perform type checking in that `end_val == .none or start_is_zero` case.
2023-07-10 10:51:55 -07:00
Andrew Kelley
2b8c1f0d46
Merge pull request #16339 from r00ster91/ueficc
std.os.uefi: use std.os.uefi.cc instead of .C as calling convention
2023-07-10 10:41:19 -07:00
Niles Salter
27a66191c2
Change math.Order order (#16356)
This speeds up algorithms like binary search
2023-07-09 01:22:52 -04:00
r00ster91
026c63d8fe Sema: infrastructure for supporting more than .C callconv for variadic functions
Now you can add new calling conventions that you confirmed to work with
variadic functions simply in a single place and the rest will work
automatically.
2023-07-08 18:05:03 -04:00
r00ster91
adf0718316 behavior: boolean vector with 2 or more elements
Closes #12169
2023-06-27 19:57:23 -04:00
r00ster91
d01641f6be test cases: fix typo in filename 2023-06-27 19:57:23 -04:00
r00ster91
c647799e5e test cases: expected optional type in for loop
Closes #10674
2023-06-27 19:57:23 -04:00
r00ster91
5e9fe84d24 test cases: array inside of anonymous struct
Closes #7525
2023-06-27 19:57:23 -04:00
r00ster91
10218dd096 test cases: never-inline call of inline function with comptime parameter
Closes #5995
2023-06-27 19:57:23 -04:00
r00ster91
18fe951e24 test cases: returning undefined [:0]const u8
Closes #5947
2023-06-27 19:57:23 -04:00
r00ster91
78da0e40a8 test cases: taking pointer of global tagged union
Closes #11619
2023-06-27 19:57:23 -04:00
r00ster91
c040c0f45a test cases: @intCast on vector
Closes #11770
2023-06-27 19:57:19 -04:00
r00ster91
7213234f0c test cases: taking address of extern var as constant
Closes #5344
2023-06-27 19:08:27 -04:00
r00ster91
d10456eb8d test cases: maximum sized integer literal
Closes #12116
2023-06-27 19:08:27 -04:00
mlugg
88284c124a AstGen: fix result locations for elements of typed array init
Resolves: #16226
2023-06-26 16:20:33 -07:00
Andrew Kelley
df389b62de
Merge pull request #16192 from mlugg/builtins-infer-dest-ty-fixes
Follow-up to cast builtin result type inference
2023-06-25 12:38:56 -07:00
Jacob Young
b9c4857ed6 AstGen: add source location to certain const initializers
Before:

    assign_local_bad_coercion.zig:5:1: error: expected type 'u32', found 'u64'
    export fn constEntry() u32 {
    ^~~~~~
    assign_local_bad_coercion.zig:11:19: error: expected type 'u32', found 'u64'
        var x: u32 = g();
                     ~^~

After:

    assign_local_bad_coercion.zig:6:21: error: expected type 'u32', found 'u64'
        const x: u32 = g();
                       ~^~
    assign_local_bad_coercion.zig:11:19: error: expected type 'u32', found 'u64'
        var x: u32 = g();
                     ~^~
2023-06-25 12:00:48 -07:00
mlugg
d249629ef1
cases: add tests for errors introduced by cast builtin result type inference 2023-06-25 13:28:32 +01: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
kcbanner
9d66481e3d llvm: fixup elem_count argument of ZigLLVMCreateDebugArrayType to be i64
The signature is `getOrCreateSubrange(int64_t  Lo, int64_t  Count)`, so this updates the bindings to match.

This fixes a crash in `lowerDebugTypeImpl` when analyzing slices that have a length of 2^32 or
larger (up to `2^64 >> 3`, which still crashes, because above that the array size in bits overflows u64).
2023-06-23 14:53:17 -07:00
Jacob Young
a257e33fff Type: remove arbitrary restrictions on param and return types
Opaque and `noreturn` makes sense since they don't represent real
values, but `null` and `undefined` are perfectly normal
comptime-only values.

Closes #16088
2023-06-20 21:51:01 -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
Andrew Kelley
496320d935
Merge pull request #15726 from mlugg/feat/peer-type-resolution-but-better
Sema: rewrite peer type resolution
2023-06-14 07:18:28 -07:00
Andrew Kelley
38fc826a5a
Merge pull request #15957 from BratishkaErik/deprecated-
std.*: remove stuff that was deprecated in older versions
2023-06-13 14:53:41 -07:00
mlugg
c9531eb833
Sema: rewrite peer type resolution
The existing logic for peer type resolution was quite convoluted and
buggy. This rewrite makes it much more resilient, readable, and
extensible. The algorithm works by first iterating over the types to
select a "strategy", then applying that strategy, possibly applying peer
resolution recursively.

Several new tests have been added to cover cases which the old logic did
not correctly handle.

Resolves: #15138
Resolves: #15644
Resolves: #15693
Resolves: #15709
Resolves: #15752
2023-06-13 21:48:18 +01:00
IntegratedQuantum
02df9aa976 Add a compiler error for @mulAdd with int vectors. 2023-06-13 10:45:57 -07:00
Eric Joldasov
eb4439f1e4
std.meta: remove Vector (deprecated in 0.10)
Followup to d42d31f72f.
Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>
2023-06-13 23:45:08 +06:00
mlugg
42dc7539c5
Fix bad source locations in switch capture errors
To do this, I expanded SwitchProngSrc a bit. Several of the tags there
aren't actually used by any current errors, but they're there for
consistency and if we ever need them.

Also delete a now-redundant test and fix another.
2023-06-13 12:55:27 +01:00
mlugg
bcb673d94a
Sema: resolve union payload switch captures with peer type resolution
This is a bit harder than it seems at first glance. Actually resolving
the type is the easy part: the interesting thing is actually getting the
capture value. We split this into three cases:

* If all payload types are the same (as is required in status quo), we
  can just do what we already do: get the first field value.
* If all payloads are in-memory coercible to the resolved type, we still
  fetch the first field, but we also emit a `bitcast` to convert to the
  resolved type.
* Otherwise, we need to handle each case separately. We emit a nested
  `switch_br` which, for each possible case, gets the corresponding
  union field, and coerces it to the resolved type. As an optimization,
  the inner switch's 'else' prong is used for any peer which is
  in-memory coercible to the target type, and the bitcast approach
  described above is used.

Pointer captures have the additional constraint that all payload types
must be in-memory coercible to the resolved type.

Resolves: #2812
2023-06-13 12:55:22 +01:00
mlugg
2a6b91874a stage2: pass most test cases under InternPool
All but 2 test cases now pass (tested on x86_64 Linux, native only). The
remaining two signify an issue requiring a larger refactor, which I will
do in a separate commit.

Notable changes:
* Fix uninitialized memory when allocating objects from free lists
* Implement TypedValue printing for pointers
* Fix some TypedValue printing logic
* Work around non-existence of InternPool.remove implementation
2023-06-10 20:51:10 -07:00
mlugg
34d44e0c1c Sema: emit error on @intToPtr with slice dest type
Resolves: #15967
2023-06-10 11:12:35 +03:00
Evin Yulo
3085e2af41 add missing note "operation is runtime due to this operand" 2023-05-31 19:18:36 +03:00
yujiri8
cd1417dbdf
don't crash when can't evaluate comptime expression with inferred type
Closes #15911.
2023-05-31 11:15:52 +00:00
Bogdan Romanyuk
32e719e070
sema: add compile error for incorrect extern type 2023-05-31 04:38:32 +00:00
mlugg
4976b58ab1
Prevent analysis of functions only referenced at comptime
The idea here is that there are two ways we can reference a function at runtime:

* Through a direct call, i.e. where the function is comptime-known
* Through a function pointer

This means we can easily perform a form of rudimentary escape analysis
on functions. If we ever see a `decl_ref` or `ref` of a function, we
have a function pointer, which could "leak" into runtime code, so we
emit the function; but for a plain `decl_val`, there's no need to.

This change means that `comptime { _ = f; }` no longer forces a function
to be emitted, which was used for some things (mainly tests). These use
sites have been replaced with `_ = &f;`, which still triggers analysis
of the function body, since you're taking a pointer to the function.

Resolves: #6256
Resolves: #15353
2023-05-29 23:06:08 +01:00
Veikka Tuominen
4ce1ae71a5
Merge pull request #15235 from Vexu/safety
add runtime safety for noreturn function returning
2023-05-23 13:34:52 +03:00
Veikka Tuominen
eef92753c7 Sema: improve error message when calling optional function
Co-authored-by: wrongnull <wrongnull@gmail.com>
2023-05-22 19:11:38 +03:00
mlugg
38b83d9d93 Zir: eliminate field_call_bind and field_call_bind_named
This commit removes the `field_call_bind` and `field_call_bind_named` ZIR
instructions, replacing them with a `field_call` instruction which does the bind
and call in one.

`field_call_bind` is an unfortunate instruction. It's tied into one very
specific usage pattern - its result can only be used as a callee. This means
that it creates a value of a "pseudo-type" of sorts, `bound_fn` - this type used
to exist in Zig, but now we just hide it from the user and have AstGen ensure
it's only used in one way. This is quite silly - `Type` and `Value` should, as
much as possible, reflect real Zig types and values.

It makes sense to instead encode the `a.b()` syntax as its own ZIR instruction,
so that's what we do here. This commit introduces a new instruction,
`field_call`. It's like `call`, but rather than a callee ref, it contains a ref
to the object pointer (`&a` in `a.b()`) and the string field name (`b`). This
eliminates `bound_fn` from the language, and slightly decreases the size of
generated ZIR - stats below.

This commit does remove a few usages which used to be allowed:
- `@field(a, "b")()`
- `@call(.auto, a.b, .{})`
- `@call(.auto, @field(a, "b"), .{})`

These forms used to work just like `a.b()`, but are no longer allowed. I believe
this is the correct choice for a few reasons:
- `a.b()` is a purely *syntactic* form; for instance, `(a.b)()` is not valid.
  This means it is *not* inconsistent to not allow it in these cases; the
  special case here isn't "a field access as a callee", but rather this exact
  syntactic form.
- The second argument to `@call` looks much more visually distinct from the
  callee in standard call syntax. To me, this makes it seem strange for that
  argument to not work like a normal expression in this context.
- A more practical argument: it's confusing! `@field` and `@call` are used in
  very different contexts to standard function calls: the former normally hints
  at some comptime machinery, and the latter that you want more precise control
  over parts of a function call. In these contexts, you don't want implicit
  arguments adding extra confusion: you want to be very explicit about what
  you're doing.

Lastly, some stats. I mentioned before that this change slightly reduces the
size of ZIR - this is due to two instructions (`field_call_bind` then `call`)
being replaced with one (`field_call`). Here are some numbers:

+--------------+----------+----------+--------+
| File         | Before   | After    | Change |
+--------------+----------+----------+--------+
| Sema.zig     | 4.72M    | 4.53M    | -4%    |
| AstGen.zig   | 1.52M    | 1.48M    | -3%    |
| hash_map.zig | 283.9K   | 276.2K   | -3%    |
| math.zig     | 312.6K   | 305.3K   | -2%    |
+--------------+----------+----------+--------+
2023-05-20 12:27:48 -07:00
Andrew Kelley
503302ceef Sema: simplify "duplicate test name" error message
* Avoid redundant words ("found")
   - All compile errors are found by the compiler
 * Avoid unnecessary prepositions ("with")
   - There is a grammatically correct alternate word order without the
     preposition.
2023-05-18 19:17:04 -07:00
zooster
3d64ed0353
make @trap return unreachable/noreturn (#15749)
`@trap` is a special function that we know never returns so it should
behave just like `@panic` and `@compileError` do currently and cause the
"unreachable code" + "control flow is diverted here" compile error.
Currently, `@trap(); @trap();` does not cause this error. Now it does.
2023-05-18 17:00:35 -04:00
Veikka Tuominen
b91d6ff9e8 add runtime safety for noreturn function returning
Closes #15221
2023-05-16 11:33:38 +03:00
Andrew Kelley
4ba61a2191
Merge pull request #15704 from Vexu/fix-memcpyset
`@mem{cpy,set}` fixes
2023-05-15 22:39:45 -07:00
Veikka Tuominen
2703db3b40 Sema: add more type checks to @mem{cpy,set}
Closes #15634
Co-authored-by: Dima Afanasyev <dimaafanasev@example.com>
2023-05-15 10:31:24 +03:00