Skip to content

Commit 26cd0f3

Browse files
enghitaloclaude
andcommitted
encoding.cbor: build option payloads via comptime $zero(field.typ.payload_type)
The generic struct decoder recovered an option field's payload type through a helper whose only job was to abuse generic inference: fn create_value_from_optional[T](_val ?T) T { return T{} } #27048 makes `$zero(TypeExpr)` resolve per-field comptime metadata, so the payload type can be read directly at the `$for field` site. Replace the call with `$zero(field.typ.payload_type)` and drop the helper. No behavior change: the helper unconditionally returned `T{}` (zero of the payload), exactly what `$zero(field.typ.payload_type)` yields; `inner` is then filled by `unpack_into`. The full `vlib/encoding/cbor` suite (10 files) still passes. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 8520724 commit 26cd0f3

1 file changed

Lines changed: 1 addition & 7 deletions

File tree

vlib/encoding/cbor/generic.v

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ fn (mut u Unpacker) unpack_struct_into[T](mut result T) ! {
416416
u.pos++
417417
result.$(field.name) = none
418418
} else {
419-
mut inner := create_value_from_optional(result.$(field.name))
419+
mut inner := $zero(field.typ.payload_type)
420420
u.unpack_into(mut inner)!
421421
result.$(field.name) = inner
422422
}
@@ -570,12 +570,6 @@ fn utf8_validate_slice(data []u8, start int, size int) bool {
570570
return true
571571
}
572572

573-
// create_value_from_optional returns a zero value of an Option's inner T.
574-
// Exists so the comptime call site can infer T from a struct field.
575-
fn create_value_from_optional[T](_val ?T) T {
576-
return T{}
577-
}
578-
579573
// unpack_into fills the target through a mutable reference. The mut
580574
// parameter exists so V's generic inferer picks up T from the
581575
// `u.unpack_into(mut result.$(field.name))!` call site.

0 commit comments

Comments
 (0)