@FieldType Semantics #35826
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
5 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
ziglang/zig#35826
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?
Zig Version
0.17.0-dev.831+431092fa6
Steps to Reproduce, Observed Behavior, and Expected Behavior
@FieldTypeSemanticsCurrent Semantics
The section of the manual on
@FieldTypesays:However, the current implementation supports some additional usages not described here. I believe this is an unintentional consequence of
@FieldTypebeing built on top of an existing internal feature:Additionally,
@FieldTypedoesn't support some usages you might expect it to. I suspect that these omissions weren't intentional, but I could be mistaken:Should
@FieldTypebe 1:1 with@field?It's tempting to suggest that
@FieldType(foo, "bar")should always be equivalent to@TypeOf(@field(foo, "bar")). However, that would require@FieldTypeto support operating on pointers, which currently does not:It makes sense that
@fieldsupports this since it's supposed to mimic the.operator.It would seem odd to me for
@FieldTypeto support operating on pointers, but I don't have a strong argument as to why and could see the value in these being 1:1, so I'm open to having my mind changed.Pointers To Fields
@fieldhas one additional trick up its sleeve that@FieldTypedoesn't: you can take the address of the result of@field.On the flip side, it's not currently possible to reconstruct the type of a pointer to a field using just
@FieldType.A naive attempt would be
*@FieldType(MyStruct, "my_field"), but this drops all the pointer attributes.You could manually reify a new pointer type to add them back, but this would still fail if it was a pointer into a packed struct. Not only is it not currently possile to reify bit pointers, even if it was, short of reimplementing part of the compiler in comptime using reflection info you wouldn't know what offset and byte size to use.
Currently your best try (assuming you don't have an actual pointer to the parent) is to create a dummy parent pointer:
Note to mlugg: I think we concluded this doesn't work yesterday due to an issue relating to alignment. I plugged it back into my use case just now to remind myself why exactly it doesn't work, and, well, it works. I suspect that I either misremember this part of our conversation and it was an earlier try, or we were lead astray by my half finished refactor.
Proposal
I'm interested in others feedback on this topic before making a concrete proposal. Here are my thoughts so far:
@FieldTypeor document that it exists. I believe it's unintentional and should be removed.@FieldTypeshould operate on pointers the way@fielddoes. Having@FieldTypeand@fieldbe 1:1 could provide utility, but having@FieldTypeoperate on pointers seems odd.FieldPtrimplementation to the standard library, or adding a builtin that achieves the same thing.CC @jacobly @mlugg
This does work okay (I believe you're misremembering that part of the conversation), it's just kind of awkward and "feels" hacky, so I prefer to avoid it where possible :P
I agree---this behavior definitely seems accidental, to the extent that I think changing that is purely a bugfix rather than a proposal.
It's unclear to me what the supposed utility is to having
@fieldand@FieldTypematch in this way. To my mind, the answer to this question is straightforward:@FieldTypeshould not support pointers, because there is no reason for it to do so. The main reason@fieldsupports pointers is that it simplifies that builtin's definition to be precisely equivalent to the expressiona.b. But@FieldTypeis not a substitute for any other expression, so it needs its own definition---and the simplest possible one is just "the type of the field with the given name on the given struct/union type".(Using the more restrictive definition also means that if there actually does turn out to be a good use case, we can extend the builtin in the future without breakage, whereas changing the builtin in future to restrict its usage would be a breaking change.)
I'd be down for having a builtin for this. However, I think this should be covered by a separate proposal (let's have this issue track the
@FieldTypebehavior). I'll let you open a proposal or a Zulip thread to hash this out if you're interested, but just while I'm here, my initial thoughts are that I might suggest the following 3 builtins:@FieldTypeto@Field@FieldType, the next one logically ought to be@FieldPtrType, which is a bit of a mouthful. The capitalization already disambiguates that it returns a type!@FieldPtr(*struct { a: u32 }, "a") == *u32@ElemPtr([]T) == *T[]align(1) u32is*align(1) u32, but an element pointer of[]align(8) u32is*u32(assuming@sizeOf(u32) == 4), because the stride between elements is only 4 bytes.Don't you mean
*u32instead of[]u32?Yep, good catch, edited to fix.
Hello,
It's seems that dropping alignment also affect
@TypeOfandanytype, see below. Maybeconstcould be a thing to consider too. I guess if we address this issue, it will also address the issue of@FieldType.I do not know if I should create a dedicated bug report.
@gschwind That is currently by design.
Alignment and modifiability in Zig aren't part of the value types, but of locations (local/global variable/const, field declaration).
Given
const c: u8 align(8)andvar v: u8,@TypeOf(c) == @TypeOf(v)will hold since(u8) == (u8).The address-of operator
&applies those location attributes to the resulting pointer type, so@TypeOf(&c) != @TypeOf(&v)since(*align(8) const u8) != (*u8).@FieldType(T, name)yields the declared field type, which also doesn't contains the field's alignment requirement.Suggested above is a proposal for another builtin
@FieldPtr(T, name)/@FieldPtrType(T, name), which would return a pointer type, so contain modifiability and alignment attributes.@fieldto@memberfor clarity #32029