534 Commits

Author SHA1 Message Date
Cody Tapscott
c36a2c27a5 Change how Block propagates (error return) trace index
Instead of adding 3 fields to every `Block`, this adds just one. The
function-level information is saved in the `Sema` struct instead,
which is created/copied more rarely.
2022-10-21 12:46:46 -07:00
Cody Tapscott
d060cbbec7 stage2: Keep error return traces alive when storing to const
This change extends the "lifetime" of the error return trace associated
with an error to continue throughout the block of a `const` variable
that it is assigned to.

This is necessary to support patterns like this one in test_runner.zig:
```zig
const result = foo();
if (result) |_| {
    // ... success logic
} else |err| {
    // `foo()` should be included in the error trace here
    return error.TestFailed;
}
```

To make this happen, the majority of the error return trace popping logic
needed to move into Sema, since `const x = foo();` cannot be examined
syntactically to determine whether it modifies the error return trace. We
also have to make sure not to delete pertinent block information before it
makes it to Sema, so that Sema can pop/restore around blocks correctly.

* Why do this only for `const` and not `var`? *

There is room to relax things for `var`, but only a little bit. We could
do the same thing we do for const and keep the error trace alive for the
remainder of the block where the *assignment* happens. Any wider scope
would violate the stack discipline for traces, so it's not viable.

In the end, I decided the most consistent behavior for the user is just
to kill all error return traces assigned to a mutable `var`.
2022-10-21 12:40:29 -07:00
Veikka Tuominen
972c39e2c0
Merge pull request #13219 from Vexu/stage2-fixes
Stage2 bug fixes
2022-10-21 12:11:49 +02:00
Veikka Tuominen
c95a34b68f stage2: improve source location of assignment 2022-10-20 20:11:00 +03:00
Veikka Tuominen
6582896ee0 Sema: remove unresolved inferred allocs
Closes #2557
2022-10-19 01:38:19 +03:00
Ali Chraghi
ca27055cda all: rename @maximum to @max and @minimum to @min 2022-10-18 14:15:16 +03:00
jacobly0
bd0dd225e8
Sema: implement linksection on functions
* Sema: implement linksection on functions

 * Implement function linksection in Sema.
 * Don't clobber function linksection/align/addrspace in Sema.
 * Fix copy-paste typo in tests.
 * Add a bunch of missing test_step.dependOn.
 * Fix checkInSymtab match.

Closes #12546
2022-10-18 14:02:10 +03:00
Jacob Young
c7f9833238 Module: fix early exit conditions during compilation
* `--verbose-llvm-ir` should trigger analysis and codegen
 * `-femit-asm` etc should trigger codegen even with `-fno-emit-bin`

Closes #12588
2022-10-15 14:18:35 -04:00
Guillaume Wenzek
aad983cf40 sanitize qualified name for nvptx backend 2022-10-15 10:39:19 -07:00
Robin Voetter
5d429b03e3
stage2: add @addrSpaceCast builtin 2022-10-12 20:36:12 +02:00
Andrew Kelley
7ce1ee1bce
Merge pull request #13081 from r00ster91/docs
fix(text): hyphenation and other fixes
2022-10-12 05:26:11 -04:00
r00ster91
8e2aaf6aed fix(text): hyphenate "runtime" adjectives 2022-10-05 21:33:42 +02:00
r00ster91
51d9db8569 fix(text): hyphenate "comptime" adjectives 2022-10-05 21:19:30 +02:00
Andrew Kelley
3234e8de3a
Merge pull request #13071 from ziglang/resolve-cache-files
stage2: resolve file before putting them into cache
2022-10-05 09:41:03 -04:00
Ryan Liptak
6ac0d2d9d6 Fix all std lib tests being run for any file within the std package
Before this commit:

```
$ zig test lib/std/fs/test.zig --main-pkg-path lib/std --zig-lib-dir lib
2170 passed; 37 skipped; 0 failed.
```

After this commit:

```
$ zig test lib/std/fs/test.zig --main-pkg-path lib/std --zig-lib-dir lib
All 45 tests passed.
```

This matches stage1 behavior:

```
$ zig test -fstage1 lib/std/fs/test.zig --main-pkg-path lib/std --zig-lib-dir lib
All 45 tests passed.
```

All tests are still run if `zig test` is run directly on `lib/std/std.zig`:

```
$ zig test lib/std/std.zig --main-pkg-path lib/std --zig-lib-dir lib
2170 passed; 37 skipped; 0 failed.
```

`zig build test-std` is unaffected by this change.

Closes #12926
2022-10-05 04:21:16 -04:00
Andrew Kelley
6152f043c0 stage2: resolve file before putting them into cache
This was an accidental misuse of the Cache API which intends to call
resolve on all file paths going into it. This one callsite was failing
to do that; fixed now.

Fixes relative file paths from making it into the global cache manifest.

See #13050
2022-10-05 00:42:08 -07:00
Andrew Kelley
ff534d2267
Merge pull request #12979 from Vexu/inline-switch
Implement inline switch cases
2022-10-03 23:43:09 -04:00
Jacob Young
d9490a4340 Sema: avoid undefined fields in file struct decls
Fixes original comment of #12399
2022-10-01 04:02:00 -04:00
Veikka Tuominen
b3c6d774d2 stage2: improve error message for missing member in file root struct
* the root struct decl name is fully qualified
  this prevents error messages containing 'main.main'
* avoid declared here note when file struct is missing a member
  It always points at the start of the file which might contain another
  container misleading the user.
2022-09-30 00:09:24 +03:00
Veikka Tuominen
5baaf90e3c Sema: implement non-special inline switch prongs 2022-09-27 18:33:23 +03:00
Veikka Tuominen
31daea74d2 stage2: implement referenced by trace for error messages
Closes #7668
Closes #12141
2022-09-15 00:50:18 +03:00
Veikka Tuominen
99826a2ba8 Sema: fix UAF in zirClosureGet
Previously if a decl failed its capture scope would be deallocated and
set to undefined which would then lead to invalid dereference in
`zirClosureGet`. To avoid this set the capture scope to a special
failed state and fail the current decl with dependency failure if
the failed state is encountered in `zirClosureGet`.

Closes #12433
Closes #12530
Closes #12593
2022-09-08 00:37:11 +03:00
bfredl
e02b9f458d build-exe: allow combination of -fno-emit-bin and --verbose-air
Currently, `zig build-exe -fno-emit-bin --verbose-air src/main.zig`
results in no output at all. With this refactor, it dumps AIR
and then exits without invoking LLVM, as expected
2022-09-07 14:53:31 +03:00
Veikka Tuominen
6aee07c144 Sema: remove unused src param from typeRequiresComptime 2022-09-02 19:49:11 +03:00
Andrew Kelley
56cfa8f22f Sema: prevent access of undefined fields
When instantiating a generic function, there is a period of time where
the function is inserted into monomorphed_funcs map, but is not yet
initialized. Despite semantic analysis being single-threaded, generic
function instantiation can happen recursively, meaning that the hash
and equality functions for monomorphed_funcs entries are potentially
invoked for an uninitialized function.

This problem was mitigated by pre-setting the hash field on the newly
allocated function, however it did not solve the problem for hash
collisions in which case the equality function would be invoked. That it
was solved for hash() but not eql() explains why the problem was
difficult to observe. I tested this patch by temporarily sabotaging the
hash and making it always return 0.

This fix is centered on adding a new field to Module.Fn which is the one
checked by eql() and is populated pre-initialization.

closes #12643
2022-08-30 18:34:08 -07:00
Jakub Konka
30baba899c coff: add missing bits required for minimal PE example 2022-08-30 10:42:21 +02:00
Jakub Konka
90b3599c68 coff: reorganize the linker 2022-08-30 10:42:21 +02:00
Andrew Kelley
7453f56e67 stage2: explicitly tagged enums no longer have one possible value
Previously, Zig had inconsistent semantics for an enum like this:

`enum(u8){zero = 0}`

Although in theory this can only hold one possible value, the tag
`zero`, Zig no longer will treat the type this way. It will do loads and
stores, as if the type has runtime bits.

Closes #12619

Tests passed locally:
 * test-behavior
 * test-cases
2022-08-24 22:20:31 -07:00
Andrew Kelley
af19909b9c stage2: fix generic function cleanup
When removing generic function instantiations from monomorphed_funcs, we
need to first make sure the function is generic, otherwise the hash map
tries to access the `hash` field of the function which is undefined.

closes #12614
2022-08-24 16:47:58 -07:00
Veikka Tuominen
b55a5007fa Sema: fix parameter of type 'T' must be comptime error
Closes #12519
Closes #12505
2022-08-22 11:16:36 +03:00
Andrew Kelley
437311756d LLVM: add DLL export attribute
This was present in stage1 but missing from self-hosted.
2022-08-19 16:45:16 -07:00
Andrew Kelley
cee82c7ce4 improved ABI alignment/size for >= 128-bit integers
* riscv64: adjust alignment and size of 128-bit integers.
 * take ofmt=c into account for ABI alignment of 128-bit integers and
   structs.
 * Type: make packed struct support intInfo
 * fix f80 alignment for i386-windows-msvc
2022-08-18 20:34:36 -07:00
Andrew Kelley
6e313eb110 stage2: agree with LLVM that @alignOf(u128) is 8
on x86_64 and similar targets.
2022-08-18 17:11:32 -07:00
Andrew Kelley
933436dc52 stage2: remove destroyed functions from maps
This is likely the cause of the flaky test failures in master branch.
Since we have some test coverage for incremental compilation, it's not
OK to leave proper memory management of Fn objects as "TODO".
2022-08-10 23:23:30 -07: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
c76b5c1a31 stage2: correct node offset of nested declarations 2022-08-09 11:59:10 +03:00
Veikka Tuominen
42ade6a114
Merge pull request #12300 from antlilja/getParamName
Replace param_names and anytype_args fields inside of Fn with functions
2022-08-05 15:29:59 +03:00
Veikka Tuominen
f1768b40b2 stage2: better source location for var decls 2022-08-01 23:37:01 +03:00
antlilja
ab3b614a33
Removed anytype_args field from Fn
anytype_args field was replaced with isAnytypeParam function.
2022-08-01 14:51:54 +02:00
antlilja
cd8070f94f
Removed param_names from Fn inside Module.zig
Removed the copy of param_names inside of Fn and changed to
implementation of getParamName to fetch to parameter name from the ZIR.
The signature of getParamName was also changed to take an additional
*Module argument.
2022-08-01 14:51:50 +02: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
Meghan
02acde99a1 stage2: ensure 'std', 'builtin', and 'root' is always available to @import 2022-07-28 15:19:17 -07:00
Andrew Kelley
dfc7493dcb
Merge pull request #12256 from Vexu/stage2
stage2 typeInfo UAF fix + more
2022-07-27 16:11:07 -07:00
Veikka Tuominen
793db63746 Sema: copy fn param ty in zirTypeInfo
Closes #12247
2022-07-27 18:27:17 +03:00
LordMZTE
c6f5832bb6 Module: fix inverted condition 2022-07-27 18:17:07 +03:00
LordMZTE
8c4896fb3a Module: use path.isSep
Co-authored-by: Veikka Tuominen <git@vexu.eu>
2022-07-27 18:17:07 +03:00
LordMZTE
1110eafe9b Module: fix error message importing file starting with root path 2022-07-27 18:17:07 +03:00
Veikka Tuominen
825fc654b6 Sema: better source location for builtin options 2022-07-26 12:14:59 +03:00
Veikka Tuominen
28478a4bac Module: improve handling of errors in @call arguments 2022-07-26 12:14:59 +03:00