@FieldType() differs semantically from @TypeOf(@field()) #35323
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 milestone
No assignees
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
ziglang/zig#35323
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.16.0
Steps to Reproduce and Observed Behavior
Expected Behavior
The names of @FieldType and @TypeOf(@field()) imply they are logically equivalent. This relates somewhat to #32029 (changing @field to @member), which would fix the issue of these reading as effectively the same despite being different. However, this would also leave a gap for something like an @MemberType builtin to be used instead of requiring two builtins.
yep, the reason I wrote #32029 (comment)
@nektro wrote in #32029 (comment):
Suppose a type
Tthat have field ax, and declaration ac, and supposeban instance ofT.@FieldTypeexist because even thought you can get the type of a field by@TypeOf(b.x)and this is resolved at comptime, it's not possible to do so without creating an instance ofTfirst. Another method would involve getting the@typeInfoofTthen the type ofxfrom it.Where as declarations can be referenced directly once you have the type/namespace ->
@TypeOf(T.c). While the@field/@memberis still useful in both cases (due to the ability to programmatically generate or inspect types at comptime), it's not the same for@FieldType.@TypeOf(T.c)or@TypeOf(@member(T, "c"))can be directly used for declarations, for fields you can ONLY use@FieldType(unless you have on hand an INSTANCE of T, or resolve to@TypeOf(@as(T, undefined).x)which was considered an "ugly hack"f83bb94c).Further more, this distinction already exists in other buitins such as
@hasFieldand@hasDecl, the later only consider public declarations. As of 0.17 even private declaration from the same file are not considered by@hasDeclwhileT.cand@field(T, "c")only fail for different files; this is in itself somewhat inconsistent but--- that is even the case for struct info'sdecl_names. It's not clear if@FieldTypeor@MemberTypewhen supporting declarations too, should also "error: no fieldcinT" ifcwhere to be private.In short, because of their differences (in nature and usages), and also because of the ability to directly
T.cor@member(T, "c"), I think, there is no need for a@FieldTypeor@MemberTypethat would need to work on both fields and declarations. The rationals for the existence of@FieldTypein itself was 1: avoid the mentioned ugly hack, 2: avoid performance implications of a@typeInfobased approach. Thus #35323 alone should fix this issues.@fieldto@memberfor clarity #32029