928 Commits

Author SHA1 Message Date
Veikka Tuominen
cc3336c784 Sema: add source location to coerce result ptr, fix negation error 2022-06-30 09:57:38 +02:00
Veikka Tuominen
979910dc38 Sema: validate shift amounts and switch ranges 2022-06-30 09:57:38 +02:00
Veikka Tuominen
6cadac18b8 Sema: improve auto generated union enum name 2022-06-30 09:57:38 +02:00
Veikka Tuominen
2e7dc5e151 Sema: improve vector overflow errors 2022-06-30 09:57:38 +02:00
Veikka Tuominen
03b356e34a Sema: improve @call errors 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
54454fd010 std.math.big.int: breaking API changes to prevent UAF
Many of the Managed methods accepted by-val parameters which could
reference Limb slices that became invalid memory after any
ensureCapacity calls. Now, Managed methods accept `*const Managed`
parameters so that if the function allows aliasing and the
ensure-capacity call resizes the Limb slice, it also affects the
aliased parameters, avoiding use-after-free bugs.

This is a breaking change that reduces the requirement for callsites to
manually make the ensure-capacity changes prior to calling many of the
Managed methods.

Closes #11897
2022-06-29 22:06:27 -04:00
Andrew Kelley
c3ae909e93 Revert "AstGen: preserve inferred ptr result loc for breaks"
This reverts commit 8bf3e1f8d0902abd4133e2729b3625c25011c3ff, 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
Andrew Kelley
6c51c8e131 Sema: fix not propagating want_safety in zirBlock
Before this commit, `@setRuntimeSafety()` has no effect inside an if
expression.
2022-06-28 16:36:11 -07:00
Andrew Kelley
33cf6ef621
Merge pull request #11881 from Vexu/stage2
Stage2: fixes for bugs found while looking for miscompilations
2022-06-17 19:23:08 -04:00
Xavier Bouchoux
b66247c97a stage2: coerce tuple to vector 2022-06-17 19:06:17 +03:00
Veikka Tuominen
b9dcbe6b4c Sema: handle sentinels in tupleToArray 2022-06-17 18:57:02 +03:00
Veikka Tuominen
ffa6f895ff Sema: validateArrayInit detect bitcast before store 2022-06-17 18:57:02 +03:00
Andrew Kelley
13f02c30e6 stage2: fix some inline asm incompatibilities with stage1 2022-06-12 14:46:05 -07:00
Andrew Kelley
ffa700ee58
Merge pull request #11837 from Vexu/stage2
Fix (nearly) all stage2 crashes when testing stdlib
2022-06-12 17:45:57 -04:00
Veikka Tuominen
0a9d6956e7 Sema: add missing set_union_tag 2022-06-12 19:17:41 +03:00
Andrew Kelley
e64d5a0753 Sema: rework beginComptimePtrMutation
This comment is now deleted because the task is completed in this
commit:

```
// TODO: Update this to behave like `beginComptimePtrLoad` and properly check/use
// `container_ty` and `array_ty`, instead of trusting that the parent decl type
// matches the type used to derive the elem_ptr/field_ptr/etc.
//
// This is needed because the types will not match if the pointer we're mutating
// through is reinterpreting comptime memory.
```

The main strategy is to change the ComptimePtrMutationKit struct so that
instead of `val: *Value` it now returns a tagged union which can be one
of three possibilities:

 * The pointer type matches the actual comptime Value so a direct
   modification is possible. Before this commit, the implementation
   incorrectly assumed this was always the case.

 * In the case of needing to write through a reinterpreted pointer, a
   mutable base Value pointer is provided along with a byte offset
   pointing to the element value in virtual memory.

 * Otherwise, it means a compile error must be emitted because one or
   both of the types (the owner of the value, or the pointer type being
   used to write through) do not have a well-defined memory layout.

After calling beginComptimePtrMutation, the one callsite now switches on
this tagged union and does the appropriate thing. The main new logic is
for the second case, which involves pointer reinterpretation, which now
takes this strategy:

 1. write the base value to a memory buffer.
 2. perform the pointer store at the proper byte offset, thereby
    modifying a subset of the buffer.
 3. read the base value from the memory buffer, overwriting the old base
    value.
2022-06-12 01:33:56 -07:00
Veikka Tuominen
35c7e376b8 stage2: improve anon name strategy for local variables 2022-06-11 23:49:33 +03:00
Veikka Tuominen
0333ff4476 stage2: make error{} the same size as anyerror
Having `error{}` be a zero bit type causes issues when it interracts
with empty inferred error sets which are the same size as `anyerror`.
2022-06-11 23:49:33 +03:00
Veikka Tuominen
488e1e5f51 stage2: small fixes + adjustments to std tests 2022-06-11 23:49:33 +03:00
Veikka Tuominen
95ab942184 Sema: make @src give absolute paths 2022-06-11 23:12:52 +03:00
Andrew Kelley
9360cfebc7 Sema: type safety for "runtime_index" field
This commit does not change any behavior, but changes the type of
the runtime_index field from u32 to a non-exhaustive enum. This allows
us to put `std.math.maxInt(u32)` only in the enum type definition and
give it an official meaning.
2022-06-11 12:18:04 -07:00
Veikka Tuominen
eaa6b04c3c Sema: skip decl causing namespace lookup when doing lookup 2022-06-11 11:02:56 +03:00
Veikka Tuominen
002df65b6e Sema: handle tuple and anon_struct in resolveTypeFully 2022-06-11 11:02:56 +03:00
Andrew Kelley
c1eb6c30e8
Merge pull request #11835 from ziglang/stage2-behavior
stage2: fix handling of aggregates with mixed comptime-only fields
2022-06-11 00:27:41 -04:00
Andrew Kelley
3c3bc5af29 Sema: introduce bitSizeAdvanced to recursively resolve types
Same pattern as abiSizeAdvanced.

Fixes compiler crash for nested packed structs.
2022-06-10 15:04:39 -07:00
Andrew Kelley
32c90cb553 stage2: fix handling of aggregates with mixed comptime-only fields 2022-06-09 19:23:36 -07:00
Andrew Kelley
fd32f6890d stage2: fold redundant error notes 2022-06-09 15:37:16 -07:00
Andrew Kelley
bac132bc8f introduce std.debug.Trace
And use it to debug a LazySrcLoc in stage2 that is set to a bogus value.

The actual fix in this commit is:

```diff
-        try sema.emitBackwardBranch(&child_block, call_src);
+        try sema.emitBackwardBranch(block, call_src);
```
2022-06-09 15:37:16 -07:00
Andrew Kelley
f1cff4fa4a Sema: avoid use of undefined value for generic fn calls
I saw some issues in Valgrind which are fixed after this commit.
2022-06-09 15:09:48 -07:00
Andrew Kelley
7c0614ea65 Sema: implement zirRetErrValueCode 2022-06-08 15:51:48 -07:00
Andrew Kelley
53c86febcb stage2: packed struct fixes for big-endian targets 2022-06-07 22:47:08 -07:00
Andrew Kelley
3e30ba3f20 stage2: better codegen for byte-aligned packed struct fields
* Sema: handle overaligned packed struct field pointers
 * LLVM: handle byte-aligned packed struct field pointers
2022-06-07 21:05:40 -07:00
Veikka Tuominen
fbd7e4506f stage2: implement asm with multiple outputs 2022-06-07 21:27:06 +03:00
Veikka Tuominen
e4c0b848a4 Sema: allow simple else body even when all errors handled 2022-06-07 21:27:06 +03:00
Veikka Tuominen
d5e3d5d74c Sema: make analyzeIsNonErr even lazier for inferred error sets 2022-06-07 21:27:06 +03:00
Jakub Konka
d9b0c984aa
Merge pull request #11794 from ziglang/elf-macho-alignment
elf+macho: use explicit alignment on Decl if specified
2022-06-07 01:03:15 +02: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
84000aa820 Sema: fix inline call of func using ret_ptr with comptime only type 2022-06-06 13:11:50 -07:00
Veikka Tuominen
cb5d2b691a Sema: validate equality on store to comptime field 2022-06-06 13:11:50 -07:00
Veikka Tuominen
a040ccb42f Sema: fix coerce result ptr outside of functions 2022-06-06 13:11:50 -07:00
Jakub Konka
5dade176d8 sema: set new Decl alignment to 0 for generic call instantiation 2022-06-05 17:31:22 +02:00
Andrew Kelley
6da420419d Sema: avoid emitting unused is_non_err AIR instruction 2022-06-05 10:37:08 +02:00
Andrew Kelley
f4ac37f55d Sema: extract out zirTryPtr from zirTry
This function took is_ptr: bool and then branched on it three times.
Now, instead, each implementation does no branching and the logic is
easier to follow, both for maintainers and compilers.

I also fixed a bug with TryPtr not ensuring enough capacity in the extra
array.
2022-06-05 10:37:08 +02: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
00720c52f6 Sema: implement try_inline 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
Veikka Tuominen
50a6b0f3ac Sema: fix function type callconv inference 2022-06-04 11:33:18 +03:00