ziglang/zig
262
5.9k
Fork
You've already forked zig
763

Move callconv, align, addrspace, and linksection annotations in declaration and field syntax #35220

Open
opened 2026年05月06日 01:20:33 +02:00 by mlugg · 7 comments
Owner
Copy link

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.
mlugg added this to the 0.18.0 milestone 2026年05月06日 01:20:33 +02:00

Are annotation part of the type though ? I imagine one should not be able to convert easily between C abi compatible function pointers and other types of function pointers with different abi ? Same with alignement.
Linksection may not be part of the type, I have no idea about addrspace.
I believe a clarification on 'what is part of the type' and what is 'where the code will be put in the executable' should be adressed first: then we could separate attributes based on that.

Are annotation part of the type though ? I imagine one should not be able to convert easily between C abi compatible function pointers and other types of function pointers with different abi ? Same with alignement. Linksection may not be part of the type, I have no idea about addrspace. I believe a clarification on 'what is part of the type' and what is 'where the code will be put in the executable' should be adressed first: then we could separate attributes based on that.
Author
Owner
Copy link

align, addrspace, and linksection---which can go on any declaration---are not part of the type. Instead, they give information a declaration's location in memory and in the binary.

callconv is a property of fn types. Actually, I should edit the proposal to include a function type expression under each possibility, will do that now.

`align`, `addrspace`, and `linksection`---which can go on any declaration---are **not** part of the type. Instead, they give information a declaration's location in memory and in the binary. `callconv` *is* a property of `fn` types. Actually, I should edit the proposal to include a function type expression under each possibility, will do that now.

i would prefer having the additional attributes after the type, which keeps the following properties:

  • var x, const x and fn x keep greppably trivially (which is a strong property of Zig)
  • the important parts come first:
    1. it's a function
    2. named foo
    3. it has these parameters
    4. this return type
    5. and has these additional detailled properties
  • it keeps the names nicely aligned for declarations
  • it's consistent with struct field properties:
struct{foo:Talign(4),bar:*constfn()voidalign(4),// aligns field by 4}
i would prefer having the additional attributes after the type, which keeps the following properties: - `var x`, `const x` and `fn x` keep greppably trivially (which is a **strong** property of Zig) - the important parts come first: 1. it's a function 2. named `foo` 3. it has these parameters 4. this return type 5. and has these additional detailled properties - it keeps the names nicely aligned for declarations - it's consistent with struct field properties: ```zig struct { foo: T align(4), bar: *const fn() void align(4), // aligns field by 4 } ```
Contributor
Copy link

I think that option 3, with callconv, align, addrspace, linksection coming before the declaration keyword (fn, const, or var) makes the most sense. As mentioned in this comment of the GitHub issue, the four can be viewed as modifiers of the binding, no different from pub, export, inline, and noinline - and if the latter group all come before the declaration keyword, the former should match.

A nice consequence of making the modifiers precede the declaration keyword is that type annotations, I believe, will always followed by a punctuation, thus eliminating the possible confusion of var foo: *align(2) u8 and var foo: *u8 align(2). Additionally, we get a very nice symmetry between declarations and pointers thereto: a pointer to align(16) const bar: T will have type *align(16) const T.

Having the modifiers first does mean that the necleus does not appear at the beginning, but I doubt that in practice we'll find many modifier chains long enough so as to confuse the reader. Explicitly-aligned struct fields also become slightly annoying, as they're not horizontally aligned with the rest of the fields, but I think eliminating the confusion of whether or not align refers to the type or the field is worth the inelegance.

I think that option 3, with `callconv`, `align`, `addrspace`, `linksection` coming before the declaration keyword (`fn`, `const`, or `var`) makes the most sense. As mentioned in [this comment](https://github.com/ziglang/zig/issues/22263#issuecomment-2558146794) of the GitHub issue, the four can be viewed as modifiers of the binding, no different from `pub`, `export`, `inline`, and `noinline` - and if the latter group all come before the declaration keyword, the former should match. A nice consequence of making the modifiers precede the declaration keyword is that type annotations, I believe, will always followed by a punctuation, thus eliminating the possible confusion of `var foo: *align(2) u8` and `var foo: *u8 align(2)`. Additionally, we get a very nice symmetry between declarations and pointers thereto: a pointer to `align(16) const bar: T` will have type `*align(16) const T`. Having the modifiers first does mean that [the necleus](https://en.wikipedia.org/wiki/Head_(linguistics)) does not appear at the beginning, but I doubt that in practice we'll find many modifier chains long enough so as to confuse the reader. Explicitly-`align`ed `struct` fields also become slightly annoying, as they're not horizontally aligned with the rest of the fields, but I think eliminating the confusion of whether or not `align` refers to the type or the field is worth the inelegance.

I agree with Fri3dNstuff, I maybe would allow to put them before pub.

// move the annotations before the `fn`/`var`/`const` and before field namesalign(4)addrspace(.something)linksection(".foo")callconv(.c)pubfnfoo()void{...}align(128)addrspace(.foo)linksection(".bar")pubvarx:u32=123;struct{foo:u32,align(8)ptr:*align(4)T,}callconv(.c)fn()void// function type expression
I agree with Fri3dNstuff, I maybe would allow to put them before pub. ```zig // move the annotations before the `fn`/`var`/`const` and before field names align(4) addrspace(.something) linksection(".foo") callconv(.c) pub fn foo() void { ... } align(128) addrspace(.foo) linksection(".bar") pub var x: u32 = 123; struct { foo: u32, align(8) ptr: *align(4) T, } callconv(.c) fn () void // function type expression ```
Member
Copy link

I kind of like the way it is right now. The properties most people care about most when reading a function declaration is its name and its return type. With the current order the name is always all the way on the left and the type is always all the way on the right (or at the beginning/end if args are split into muliple lines), which means that both are very easy to find by just starting to read from either end.

That's not the case with either of the alternatives, which means that a reader of a function would have to spend more time searching for the properties they are most interested in.

(granted, fn is to the left of the name but it's always the same so it's very easy to skip over)

I kind of like the way it is right now. The properties most people care about most when reading a function declaration is its name and its return type. With the current order the name is *always* all the way on the left and the type is *always* all the way on the right (or at the beginning/end if args are split into muliple lines), which means that both are very easy to find by just starting to read from either end. That's not the case with either of the alternatives, which means that a reader of a function would have to spend more time searching for the properties they are most interested in. (granted, `fn` is to the left of the name but it's always the same so it's very easy to skip over)

Support option 3
Reason 1: #4630
Reason 2: The current placement of these annotations may lead people to mistakenly think that they are part of the type. To be precise: callconv is part of the function type, whereas other annotations are not, but are properties of the location. Among them, linksection is purely a positional attribute, while align and addrspace affect the pointer type. Because of the current annotations' position, I once mistakenly understood align as part of type rather than an attribute of position.

Regarding the alignment issue, I might let pub together with the location annotations to place on the line before the declaration along with other annotations by zig fmt. Their order is: pub (annotation symbol), linksection (annotation location), align and addrspace (annotation location but affect pointer), callconv (part of type)

publinksection(".foo")align(4)addrspace(.something)callconv(.c)fnfoo()void{...}publinksection(".bar")align(128)addrspace(.foo)varx:u32=123;struct{foo:u32,align(8)ptr:*align(4)T,}callconv(.c)fn()void// function type expression
Support option 3 Reason 1: [#4630](https://github.com/ziglang/zig/issues/4630) Reason 2: The current placement of these annotations may lead people to mistakenly think that they are part of the type. To be precise: `callconv` is part of the function type, whereas other annotations are not, but are properties of the location. Among them, `linksection` is purely a positional attribute, while `align` and `addrspace` affect the pointer type. Because of the current annotations' position, I once mistakenly understood `align` as part of type rather than an attribute of position. Regarding the alignment issue, I might let `pub` together with the location annotations to place on the line before the declaration along with other annotations by zig fmt. Their order is: `pub` (annotation symbol), `linksection` (annotation location), `align` and `addrspace` (annotation location but affect pointer), `callconv` (part of type) ``` zig pub linksection(".foo") align(4) addrspace(.something) callconv(.c) fn foo() void { ... } pub linksection(".bar") align(128) addrspace(.foo) var x: u32 = 123; struct { foo: u32, align(8) ptr: *align(4) T, } callconv(.c) fn () void // function type expression ```
Sign in to join this conversation.
No Branch/Tag specified
master
elfv2
spork8
restricted
0.16.x
panic-rewrite
parking-futex-lockfree
windows-Io-cleanup
Io-watch
ParseCommandLineOptions
windows-async-files
poll-ring
debug-file-leaks-differently
debug-file-leaks
ProcessPrng
elfv2-dyn
jobserver
threadtheft
io-threaded-no-queue
0.15.x
Io.net
comptime-allocator
restricted-function-pointers
cli
wasm-linker-writer
wrangle-writer-buffering
sha1-stream
async-await-demo
fixes
0.14.x
ast-node-methods
macos-debug-info
make-vs-configure
fuzz-macos
sans-aro
ArrayList-reserve
incr-bug
llvm-ir-nosanitize-metadata
ci-tarballs
ci-scripts
threadpool
0.12.x
new-pkg-hash
json-diagnostics
more-doctests
rework-comptime-mutation
0.11.x
ci-perf-comment
stage2-async
0.10.x
autofix
0.9.x
aro
hcs
0.8.x
0.7.x
0.16.0
0.15.2
0.15.1
0.15.0
0.14.1
0.14.0
0.12.1
0.13.0
0.12.0
0.11.0
0.10.1
0.10.0
0.9.1
0.9.0
0.8.1
0.8.0
0.7.1
0.7.0
0.6.0
0.5.0
0.4.0
0.3.0
0.2.0
0.1.1
0.1.0
Labels
Clear labels
abi/f32
abi/ilp32
abi/sf
accepted
This proposal is planned.
arch/21k
arch/6502
arch/aarch64
arch/alpha
arch/amdgcn
arch/arc
arch/arc32
arch/arc64
arch/arm
arch/avr
arch/bfin
arch/bpf
arch/colossus
arch/cris
arch/csky
arch/dlx
arch/epiphany
arch/fr30
arch/frv
arch/hexagon
arch/hppa
arch/hppa64
arch/ia64
arch/kalimba
arch/kvx
arch/lanai
arch/lm32
arch/loongarch32
arch/loongarch64
arch/m32r
arch/m68k
arch/m88k
arch/mcore
arch/microblaze
arch/mips
arch/mips64
arch/mmix
arch/moxie
arch/mrisc32
arch/msp430
arch/nds32
arch/ns32k
arch/nvptx
arch/or1k
arch/powerpc
arch/powerpc64
arch/propeller
arch/riscv32
arch/riscv64
arch/rl78
arch/rx
arch/s390x
arch/sh
arch/sparc
arch/sparc64
arch/spirv
arch/spu
arch/tricore
arch/v850
arch/vax
arch/vc4
arch/ve
arch/wasm
arch/x86
arch/x86_64
arch/xcore
arch/xtensa
autodoc
The web application for interactive documentation and generation of its assets.
backend/c
The C backend outputs C source code.
backend/llvm
The LLVM backend outputs an LLVM bitcode module.
backend/self-hosted
The self-hosted backends produce machine code directly.
binutils
Zig's included binary utilities: zig ar, zig dlltool, zig lib, zig ranlib, zig objcopy, and zig rc.
breaking
Implementing this issue could cause existing code to no longer compile or have different behavior.
build system
The Zig build system - zig build, std.Build, the build runner, and package management.
debug info
An issue related to debug information (e.g. DWARF) produced by the Zig compiler.
docs
An issue with documentation, e.g. the language reference or standard library doc comments.
error message
This issue points out an error message that is unhelpful and should be improved.
frontend
Tokenization, parsing, AstGen, ZonGen, Sema, Legalize, and Liveness.
fuzzing
An issue related to Zig's integrated fuzz testing.
incremental
Reuse of internal compiler state for faster compilation.
lib/c
This issue relates to Zig's libc implementation and/or vendored libcs.
lib/compiler-rt
This issue relates to Zig's compiler-rt library.
lib/cxx
This issue relates to Zig's vendored libc++ and/or libc++abi.
lib/std
This issue relates to Zig's standard library.
lib/tsan
This issue relates to Zig's vendored libtsan.
lib/ubsan-rt
This issue relates to Zig's ubsan-rt library.
lib/unwind
This issue relates to Zig's vendored libunwind.
linking
Zig's integrated object file and incremental linker.
miscompilation
The compiler reports success but produces semantically incorrect code.
os/android
os/contiki
os/dragonfly
os/driverkit
os/emscripten
os/freebsd
os/fuchsia
os/haiku
os/hermit
os/hurd
os/illumos
os/ios
os/linux
os/maccatalyst
os/macos
os/managarm
os/netbsd
os/ohos
os/openbsd
os/plan9
os/redox
os/rtems
os/serenity
os/tvos
os/uefi
os/visionos
os/wasi
os/watchos
os/windows
proposal
This issue suggests language modifications. If it also has the "accepted" label then it is planned.
release notes
This issue or pull request should be mentioned in the release notes.
testing
This issue is related to testing the compiler, standard library, or other parts of Zig.
zig cc
Zig as a drop-in C-family compiler.
zig fmt
The Zig source code formatter.
zig reduce
The Zig source code reduction tool.
bounty
https://ziglang.org/news/announcing-donor-bounties
bug
Observed behavior contradicts documented or intended behavior.
contributor-friendly
This issue is limited in scope and/or knowledge of project internals.
downstream
An issue with a third-party project that uses this project.
enhancement
Solving this issue will likely involve adding new logic or components to the codebase.
infra
An issue related to project infrastructure, e.g. continuous integration.
optimization
A task to improve performance and/or resource usage.
question
No questions on the issue tracker; use a community space instead.
regression
Something that used to work in a previous version stopped working
upstream
An issue with a third-party project that this project uses.
use case
Describes a real use case that is difficult or impossible, but does not propose a solution.
No labels
abi/f32
abi/ilp32
abi/sf
accepted
arch/21k
arch/6502
arch/aarch64
arch/alpha
arch/amdgcn
arch/arc
arch/arc32
arch/arc64
arch/arm
arch/avr
arch/bfin
arch/bpf
arch/colossus
arch/cris
arch/csky
arch/dlx
arch/epiphany
arch/fr30
arch/frv
arch/hexagon
arch/hppa
arch/hppa64
arch/ia64
arch/kalimba
arch/kvx
arch/lanai
arch/lm32
arch/loongarch32
arch/loongarch64
arch/m32r
arch/m68k
arch/m88k
arch/mcore
arch/microblaze
arch/mips
arch/mips64
arch/mmix
arch/moxie
arch/mrisc32
arch/msp430
arch/nds32
arch/ns32k
arch/nvptx
arch/or1k
arch/powerpc
arch/powerpc64
arch/propeller
arch/riscv32
arch/riscv64
arch/rl78
arch/rx
arch/s390x
arch/sh
arch/sparc
arch/sparc64
arch/spirv
arch/spu
arch/tricore
arch/v850
arch/vax
arch/vc4
arch/ve
arch/wasm
arch/x86
arch/x86_64
arch/xcore
arch/xtensa
autodoc
backend/c
backend/llvm
backend/self-hosted
binutils
breaking
build system
debug info
docs
error message
frontend
fuzzing
incremental
lib/c
lib/compiler-rt
lib/cxx
lib/std
lib/tsan
lib/ubsan-rt
lib/unwind
linking
miscompilation
os/android
os/contiki
os/dragonfly
os/driverkit
os/emscripten
os/freebsd
os/fuchsia
os/haiku
os/hermit
os/hurd
os/illumos
os/ios
os/linux
os/maccatalyst
os/macos
os/managarm
os/netbsd
os/ohos
os/openbsd
os/plan9
os/redox
os/rtems
os/serenity
os/tvos
os/uefi
os/visionos
os/wasi
os/watchos
os/windows
proposal
release notes
testing
zig cc
zig fmt
zig reduce
bounty
bug
contributor-friendly
downstream
enhancement
infra
optimization
question
regression
upstream
use case
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
7 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ziglang/zig#35220
Reference in a new issue
ziglang/zig
No description provided.
Delete branch "%!s()"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?