Commit Graph

4913 Commits

Author SHA1 Message Date
Luuk de Gram
1f196b9e2f
stage2: implement exporting using field access (#13136)
This implements `@export(a.b, .{..});` in semantic analysis,
allowing users to directly export a variable from a namespace.

* add test case for exporting using field access
2022-10-12 04:38:07 -04:00
Cody Tapscott
e6ebdcb82e stage2 LLVM: Use a packed aggregate for union payload init
Without the packed qualifier, the type layout that we use to
initialize doesn't match the correct layout of the underlying
storage, causing corrupted data and past-the-end writes.
2022-10-11 15:42:01 -04:00
Luuk de Gram
8635c18e40
test/link: don't strip producers section for wasm
When testing the Wasm linker for the producers section
we do not ever want to strip the binary as this will remove
the producers section in release-small.

This fixes the CI errors by d086b371f0
2022-10-11 21:27:09 +02:00
Veikka Tuominen
a72b584c76
Merge pull request #13126 from tau-dev/master
translate-c: Fix function pointers, add cast to subscripts in macros
2022-10-11 19:04:44 +02:00
Ali Chraghi
d086b371f0 Compilation: strip debug info from ReleaseSmall by default 2022-10-11 17:51:25 +02:00
Andrew Kelley
c3d67c5c4e
Merge pull request #13117 from topolarity/compiler-rt-cmul
compiler-rt: Implement complex multiply/division
2022-10-11 05:51:47 -04:00
Tau
85b105d4f9 Update translate-c tests 2022-10-11 00:43:53 +02:00
Veikka Tuominen
b316c25cc6
Merge pull request #13075 from Vexu/stage2-fixes
Stage2 misc fixes
2022-10-10 23:27:17 +02:00
Veikka Tuominen
1f8ae10a38
Merge pull request #13091 from Vexu/small-proposals
Implement some small proposals
2022-10-10 23:26:56 +02:00
Tau
2ca503ec05 translate-c: Fix #12263 2022-10-10 22:29:56 +02:00
Cody Tapscott
a06185f362 C ABI: Add tests for complex float/double support
These tests will be failing on many platforms until #8465 is resolved.

Luckily, the particular function signature used for __divXc3 and __mulXc3
seems to be OK on x86-64.
2022-10-09 23:24:29 -07:00
Cody Tapscott
05915b85dd compiler-rt: Implement mulXc3 and divXc3 functions
These are the standard complex multiplication/division functions
required by the C standard (Annex G).

Don't get me started on the standard's handling of complex-infinity...
2022-10-09 11:09:41 -07:00
Veikka Tuominen
3ccd4907fb Sema: add error for capturing a runtime value outside of function scope
Closes #13104
2022-10-08 16:58:54 +03:00
Veikka Tuominen
b5c0a797a7 Sema: inline switch capture needs to be set when switch operand is comptime known 2022-10-08 16:58:52 +03:00
Veikka Tuominen
1500b9ddc3 Sema: restore sema.src after inline call
Closes #13099
2022-10-08 16:58:26 +03:00
Luuk de Gram
fda75f53fa
test/link: Add linker test for producers section
This also turns off non-debug modes for the bss linker tests for
Wasm. This is done as it's not required to guarantee to zero out
the bss section for non-debug modes.
2022-10-08 09:43:40 +02:00
Veikka Tuominen
4a6cc1c602 Sema: allow equality comparisons between error unions and error sets
Closes #1302
2022-10-07 11:04:02 +03:00
Veikka Tuominen
2315e1b410 safety: add safety check for hitting else branch on a corrupt enum value
Closes #7053
2022-10-07 11:04:02 +03:00
Veikka Tuominen
29ae6515f3 AstGen: use 'shadows' instead of 'redeclaration' when names are in different scopes
Closes #8532
2022-10-07 11:04:02 +03:00
Ali Chraghi
6672921e32 Sema: fix error location when casting pointer to slice
Co-authored-by: Veikka Tuominen <git@vexu.eu>
2022-10-06 21:18:01 +03:00
Veikka Tuominen
dffce98045 Sema: disallow using stage1 fn ptrs in extern contexts
Closes #13022
2022-10-06 20:09:45 +03:00
Veikka Tuominen
446deb31a8 Sema: validate bitSizeOf operand type
Closes #13080
2022-10-06 20:09:45 +03:00
Veikka Tuominen
775e055b59 Sema: generic function instantiation inherits parent's branch quota
Closes #12624
2022-10-06 20:09:45 +03:00
Veikka Tuominen
94039d66ed Sema: disallow fieldParentPtr and offsetOf on comptime fields
Comptime fields are tied to the type and behave more like declarations
so these operations cannot return anything useful for them.
2022-10-06 20:09:45 +03:00
Veikka Tuominen
0b1dd845d9 stage2: add error for non-void error union payload being ignored
See https://github.com/ziglang/zig/pull/6060#discussion_r471032912
2022-10-06 15:39:06 +03:00
Veikka Tuominen
b626977f45 Sema: create sub block for inline loops
Closes #13038
2022-10-06 15:39:06 +03:00
Veikka Tuominen
ba4aa12098 Sema: use correct value when @ptrCast operand is comptime known
Closes #13034
2022-10-05 17:26:29 +03:00
Veikka Tuominen
40578656e8 Zir: handle ranges in getMultiProng
Closes #12890
2022-10-05 17:26:29 +03:00
Veikka Tuominen
c0350cf87e Sema: avoid passing undefined as reason to failWithNeededComptime
Closes #13046
2022-10-05 17:26:29 +03:00
Veikka Tuominen
e563af1329
Merge pull request #12745 from Techcable/translate-c/packed-struct-implies-align1
translate-c: Translate clang packed struct C into Zig extern struct with align(1)
2022-10-05 15:35:11 +03:00
Andrew Kelley
ff534d2267
Merge pull request #12979 from Vexu/inline-switch
Implement inline switch cases
2022-10-03 23:43:09 -04:00
Andrew Kelley
54eb0f2daa
Merge pull request #13032 from jacobly0/br-on-undef-val
stage2: fix branches on undefined values
2022-10-03 22:46:22 -04:00
Jacob Young
9cad44770a test/standalone: remove unneeded FnPtr
The behavior of this test is not affected by an extra level of
indirection.
2022-10-03 10:28:30 -04:00
Jacob Young
9d8cdb855b Sema: fix function paramater count mismatch note
expected type 'fn() void', found 'fn(i32) void'
function with 0 parameters cannot cast into a function with 0 parameters
  =>
expected type 'fn() void', found 'fn(i32) void'
function with 1 parameters cannot cast into a function with 0 parameters
2022-10-03 13:06:49 +03:00
Julian
32d755beb8
Sema: require reified packed struct fields to have zero alignment 2022-10-03 13:05:12 +03:00
Techcable
a560af9656
translate-c: Add tests for packed unions
Was missing test coverage before this (although it was suppored)
2022-10-01 15:22:26 -07:00
Techcable
5b689d389f
translate-c: packed struct implies align(1) on every field
Superceeds PR #12735 (now supporting all packed structs in GNU C)
Fixes issue #12733

This stops translating C packed struct as a Zig packed struct.
Instead use a regular `extern struct` with `align(1)`.

This is because (as @Vexu explained) Zig packed structs are really just integers (not structs).

Alignment issue is more complicated. I think @ifreund was the
first to notice it in his comment on PR #12735

Justification of my interpretion of the C(lang) behavior
comes from a careful reading of the GCC docs for type & variable attributes:

(clang emulates gnu's packed attribute here)

The final line of the documentation for __attribute__ ((aligned)) [on types] says:

> When used on a struct, or struct member, *the aligned attribute can only increase the alignment*; in order to decrease it, the packed attribute must be specified as well.

This implies that GCC uses the `packed` attribute for alignment purposes
in addition to eliminating padding.

The documentation for __attribute__((packed)) [on types], states:

> This attribute, attached to a struct, union, or C++ class type definition, specifies that each of its members (other than zero-width bit-fields) is placed to minimize the memory required. **This is equivalent to specifying the packed attribute on each of the members**.

The key is resolving this indirection, and looking at the documentation
for __attribute__((packed)) [on fields (wierdly under "variables" section)]:

> The packed attribute specifies that a **structure member should have the smallest possible alignment** — one bit for a bit-field and one byte otherwise, unless a larger value is specified with the aligned attribute. The attribute does not apply to non-member objects.

Furthermore, alignment is the only effect of the packed attribute mentioned in the GCC docs (for "common" architecture).
Based on this, it seems safe to completely substitute C 'packed' with Zig 'align(1)'.
Target-specific or undocumented behavior potentially changes this.

Unfortunately, the current implementation of `translate-c` translates as
`packed struct` without alignment info.

Because Zig packed structs are really integers (as mentioned above),
they are the wrong interpretation and we should be using 'extern struct'.

Running `translate-c` on the following code:

```c
struct foo {
    char a;
    int b;
} __attribute__((packed));

struct bar {
    char a;
    int b;
    short c;
    __attribute__((aligned(8))) long d;
} __attribute__((packed));
```

Previously used a 'packed struct' (which was not FFI-safe on stage1).

After applying this change, the translated structures have align(1)
explicitly applied to all of their fields AS EXPECTED (unless explicitly overriden).
This makes Zig behavior for `tranlsate-c` consistent with clang/GCC.

Here is the newly produced (correct) output for the above example:

```zig
pub const struct_foo = extern struct {
    a: u8 align(1),
    b: c_int align(1),
};
pub const struct_bar = extern struct {
    a: u8 align(1),
    b: c_int align(1),
    c: c_short align(1),
    d: c_long align(8),
};
```

Also note for reference: Since the last stable release (0.9.1),
there was a change in the language spec
related to the alignment of packed structures.

The docs for Zig 0.9.1 read:
> Packed structs have 1-byte alignment.

So the old behavior of translate-c (not specifying any alignment) was possibly correct back then.

However the current docs read:

> Packed structs have the same alignment as their backing integer

Suggsestive both to the change to an integer-backed representation
which is incompatible with C's notation.
2022-10-01 15:22:10 -07:00
Jacob Young
b7bd44a654 Sema: ensure builtin.StackTrace fields are analyzed
When encountering a fn type that returns an error (union), a backend
that supports error return tracing will want the StackTrace struct and
its fields to be analyzed.
2022-10-01 08:09:43 -04:00
Jacob Young
8b66443d50 llvm: avoid undefined values by ensuring the StackTrace decl is analyzed
The test builds an object file to prevent StackTrace from already having
been analyzed by other code.

Fixes #13030
2022-10-01 04:02:09 -04:00
Veikka Tuominen
2a4e89e0c9 Type: correctly handle ABI align strat for optionals and error unions
Closes #12984
2022-09-30 00:46:45 +03: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
409cf4aeb8 Sema: use correct ptr ty to check for attributes of slice field ptr
Closes #12870
Closes #13006
2022-09-30 00:09:24 +03:00
Igor Anić
9f6f460124 Sema: improve source location in errors
resolves #12793
2022-09-29 14:45:08 +03:00
kkHAIKE
317cb629fb Sema: fix resolveInferredErrorSet panic when generic inline function 2022-09-29 14:44:37 +03:00
Jacob G-W
0a064eae99 stage2: detect duplicate enum values
Closes #12805
2022-09-28 12:48:54 +03:00
Cody Tapscott
e165b8b223 stage2: Fix multiple_llvm_int parameter passing
Small iteration oopsie

We could really use some more comprehensive C ABI tests.
2022-09-28 12:48:28 +03:00
InKryption
c75e8f3616 Sema: check that reified enum field values fits tag type. 2022-09-27 18:38:37 +03:00
Veikka Tuominen
83fa216c8d Sema: implement inline else for ints 2022-09-27 18:33:23 +03:00
Veikka Tuominen
950a0e2405 Sema: implement inline else for errors enums and bools 2022-09-27 18:33:23 +03:00
Veikka Tuominen
0e77259f44 add inline switch union tag captures 2022-09-27 18:33:23 +03:00