Move callconv, align, addrspace, and linksection annotations in declaration and field syntax #35220
Labels
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/dragonfly
os/driverkit
os/emscripten
os/freebsd
os/fuchsia
os/haiku
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
No assignees
7 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
ziglang/zig#35220
Loading…
Reference in a new issue
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?
Migrated from #22263.
There are three main syntax possibilities here:
fndeclarations.align(4) u32is itself a type (which it is not). It also improves clarity when aligned pointer types are involved, as in theptrstruct field example.align/addrspace/linksection/callconvexpressions, and this is made clear by the syntax because the annotations appear before the parameter list in source.alignannotation 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.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.
align,addrspace, andlinksection---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.callconvis a property offntypes. 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 xandfn xkeep greppably trivially (which is a strong property of Zig)fooI think that option 3, with
callconv,align,addrspace,linksectioncoming before the declaration keyword (fn,const, orvar) 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 frompub,export,inline, andnoinline- 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) u8andvar foo: *u8 align(2). Additionally, we get a very nice symmetry between declarations and pointers thereto: a pointer toalign(16) const bar: Twill 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-
alignedstructfields 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 notalignrefers to the type or the field is worth the inelegance.I agree with Fri3dNstuff, I maybe would allow to put them before pub.
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,
fnis 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:
callconvis part of the function type, whereas other annotations are not, but are properties of the location. Among them,linksectionis purely a positional attribute, whilealignandaddrspaceaffect the pointer type. Because of the current annotations' position, I once mistakenly understoodalignas part of type rather than an attribute of position.Regarding the alignment issue, I might let
pubtogether 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),alignandaddrspace(annotation location but affect pointer),callconv(part of type)