4896 Commits

Author SHA1 Message Date
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
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
Veikka Tuominen
5baaf90e3c Sema: implement non-special inline switch prongs 2022-09-27 18:33:23 +03:00
Veikka Tuominen
cccc4c3827 AstGen: analyze inline switch cases 2022-09-27 18:05:08 +03:00
kkHAIKE
ba5cbea0c3
Sema: fix segfault when union init with empty field 2022-09-27 13:23:51 +03:00
John Schmidt
b6bda5183e sema: load the correct AST in failWithInvalidComptimeFieldStore
The container we want to get the fields from might not be declared in the
same file as the block we are analyzing, so we should get the AST from
the decl's file instead.
2022-09-26 08:56:34 +02:00
John Schmidt
6cc2b26163 sema: load the correct AST in addFieldErrNote
The enum we want to get the fields from might not be declared in the
same file as the block we are analyzing, so we should get the AST from
the decl's file instead.

Closes #12950.
2022-09-25 10:28:48 +02:00
Veikka Tuominen
c4400e8aa5 AstGen: reset anon_name_strategy for sub expressions
Closes  #12910
2022-09-24 16:30:37 +03:00
Veikka Tuominen
8e4d0ae4f5 Sema: avoid generic parameter error in nested function type
Related to cd1833044ab7505bc101c85f59889bd3ea3fac80
Closes #12945
2022-09-24 15:15:36 +03:00
Veikka Tuominen
3a5148112d Sema: avoid using pointerDecl when dealing with slices
Closes #12885
2022-09-24 14:43:03 +03:00
Veikka Tuominen
3525b8778e Sema: properly handle generic struct as parameter type
Closes #12907
2022-09-24 14:43:03 +03:00
Veikka Tuominen
ede3798485 Sema: resolve struct layout in zirStructInit
Closes #12911
2022-09-23 17:39:21 +03:00
Veikka Tuominen
581df942e1 Sema: correct sentinel check on implicit cast from array ptr
Closes #12938
2022-09-23 17:39:06 +03:00
Veikka Tuominen
3de5c3b503 Sema: check for slices in packed and extern type validation
Closes #12930
2022-09-23 17:39:06 +03:00
Veikka Tuominen
8d1fdfc8ed Sema: preserve volatileness when constructing field pointers
Closes #12928
2022-09-23 17:39:06 +03:00
kkHAIKE
4961044ce8 AstGen: store void to ptr result loc when there is no else branch 2022-09-21 20:21:02 +03:00
kkHAIKE
183127733c AstGen: make loop body's ResultLoc .none
Fixes #12555
Fixes #12551
Fixes #12455
2022-09-21 20:20:05 +03:00
Jacob Young
14f4c73191 sema: fix typo 2022-09-21 11:29:20 +02:00
Veikka Tuominen
694fab4848 std: add return address parameter to panic fn 2022-09-20 19:05:00 -07:00