fix(data): convert constructor arguments to declared field types - #83
Merged
Merged
Conversation
Variant storage structs use `Any` for a field whenever its declared type
collapses -- a `Vector{Any}` field or a self-referential `Vector{Self}`
(which cannot reference the not-yet-defined wrapper `Type`). Julia's inner
constructor then performs no conversion, so the stored value keeps its
original type while `getproperty`/`variant_getfield` type-assert against the
declared annotation, raising a `TypeError`.
Convert each argument to its declared annotation type in the positional and
keyword constructors, restoring normal struct-construction semantics. The
conversion is a no-op when the value already matches, so construction stays
type stable. This also lets the explicit-brace form of self-referential
constructors promote a parametric singleton bottom (e.g. `Empty()`).
Fixes #32
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #83 +/- ##
=======================================
Coverage 91.69% 91.70%
=======================================
Files 43 43
Lines 1661 1663 +2
=======================================
+ Hits 1523 1525 +2
Misses 138 138 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
Benchmark Results (Julia v1)Time benchmarks
Memory benchmarks
|
github-actions Bot
referenced
this pull request
Jul 12, 2026
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #32 —
TypeErroringetpropertyon container-typed fields.Root cause
Variant storage structs use
Anyfor a field whenever its declared type "collapses" inguess_self_as_any(src/data/scan.jl):Anytype parameter — e.g.Vector{Any}becomes anAnystorage field.Vector{Self}must useAnybecause the wrapperTypeisn't defined yet when the storage struct is emitted (removing the collapse, as suggested in the issue thread, breaks these).Because the storage field is
Any, Julia's inner constructor performs no conversion, so the stored value keeps its original type (Vector{Int}). Butgetproperty/variant_getfieldtype-assert against the declared annotation (Vector{Any}), producing:Fix
Convert each argument to its declared annotation type in the positional (
emit_each_variant_cons) and keyword (emit_each_variant_kw_cons) constructors, restoring the normal Julia struct-construction semantics that were lost to theAnystorage. The conversion is a no-op when the value already matches, so construction stays type stable (verified viaBase.return_types).This resolves all three failure modes from the issue:
OptionVec.Some{String}("hi", [1, 2])(the report)M.V([5])(anonymous variant withVector{Any})Pattern.Row([])(self-referentialVector{Self})It also fixes a latent bug in the explicit-brace form of self-referential constructors:
Tree.Node{Int}(5, Tree.Leaf(3), Tree.Empty())now promotes the parametric singleton bottom correctly.Not changed
The strict type-inferring constructor (
Some("hi", [1,2])without{String}) still requires exact argument types — this is pre-existing behavior shared by all concrete field types (it also rejectsIntfor aFloat64field), not the reported bug, and that path is deliberately delicate for type-parameter inference (see #34).Tests
Adds
test/data/emit/container.jlcovering generic/anonymous/named/keywordVector{Any}fields, self-referentialVector{Self}conversion, and explicit-brace self-ref promotion. Full suite passes (data 285 → 304).🤖 Generated with Claude Code