Migrated from #22263.
There are three main syntax possibilities here:
// status quopubfnfoo()align(4)addrspace(.something)linksection(".foo")callconv(.c)void{...}pubvarx:u32align(128)addrspace(.foo)linksection(".bar")=123;struct{foo:u32,ptr:*align(4)Talign(8),}fn()callconv(.c)void// function type expression// in functions, move annotations after the return type (fields and non-`fn` declarations unchanged)pubfnfoo()voidalign(4)addrspace(.something)linksection(".foo")callconv(.c){...}pubvarx:u32align(128)addrspace(.foo)linksection(".bar")=123;struct{foo:u32,ptr:*align(4)Talign(8),}fn()voidcallconv(.c)// function type expression// move the annotations before the `fn`/`var`/`const` and before field namespubalign(4)addrspace(.something)linksection(".foo")callconv(.c)fnfoo()void{...}pubalign(128)addrspace(.foo)linksection(".bar")varx:u32=123;struct{foo:u32,align(8)ptr:*align(4)T,}callconv(.c)fn()void// function type expression
- The second option seems like a clear improvement on the first option (status quo): right now, annotations arbitrarily "break up" a function type, which is confusing and not consistent with where they are placed on fields and non-
fn declarations.
- The last option has a clear separation between the annotations and the declaration/return type, which is good for clarity. It avoids confusion where people think that (e.g.)
align(4) u32 is itself a type (which it is not). It also improves clarity when aligned pointer types are involved, as in the ptr struct field example.
- The last option models with syntax whether function parameters are in scope for expressions. Function parameters can be referenced in the function return type expression, but cannot be referenced in
align/addrspace/linksection/callconv expressions, and this is made clear by the syntax because the annotations appear before the parameter list in source.
- The last option has the downside that the
align annotation on struct/union fields cannot be neatly formatted (the field name becomes unaligned with other field names). The first two options avoid this by placing the annotation after the field name and type, so those can be visually aligned.
Migrated from [#22263](https://github.com/ziglang/zig/issues/22263).
---
There are three main syntax possibilities here:
```zig
// status quo
pub fn foo() align(4) addrspace(.something) linksection(".foo") callconv(.c) void { ... }
pub var x: u32 align(128) addrspace(.foo) linksection(".bar") = 123;
struct {
foo: u32,
ptr: *align(4) T align(8),
}
fn () callconv(.c) void // function type expression
// in functions, move annotations after the return type (fields and non-`fn` declarations unchanged)
pub fn foo() void align(4) addrspace(.something) linksection(".foo") callconv(.c) { ... }
pub var x: u32 align(128) addrspace(.foo) linksection(".bar") = 123;
struct {
foo: u32,
ptr: *align(4) T align(8),
}
fn () void callconv(.c) // function type expression
// move the annotations before the `fn`/`var`/`const` and before field names
pub align(4) addrspace(.something) linksection(".foo") callconv(.c) fn foo() void { ... }
pub align(128) addrspace(.foo) linksection(".bar") var x: u32 = 123;
struct {
foo: u32,
align(8) ptr: *align(4) T,
}
callconv(.c) fn () void // function type expression
```
* The second option seems like a clear improvement on the first option (status quo): right now, annotations arbitrarily "break up" a function type, which is confusing and not consistent with where they are placed on fields and non-`fn` declarations.
* The last option has a clear separation between the annotations and the declaration/return type, which is good for clarity. It avoids confusion where people think that (e.g.) `align(4) u32` is itself a type (which it is **not**). It also improves clarity when aligned pointer types are involved, as in the `ptr` struct field example.
* The last option models with syntax whether function parameters are in scope for expressions. Function parameters *can* be referenced in the function return type expression, but *cannot* be referenced in `align`/`addrspace`/`linksection`/`callconv` expressions, and this is made clear by the syntax because the annotations appear *before* the parameter list in source.
* The last option has the downside that the `align` annotation on struct/union fields cannot be neatly formatted (the field name becomes unaligned with other field names). The first two options avoid this by placing the annotation after the field name and type, so those can be visually aligned.