feat(rust): integer-type option with safe conservative i32 inference#2964
Merged
Conversation
1. Added IntegerType enum 2. Introduced integerType configuration option - Supports forced i32/i64 usage - Enables automatic selection based on numerical range
Re-fits PR #2791 (Rust integer-type option) onto the reworked Rust backend: .js import suffixes, new derive-debug/derive-clone defaults, and the updated quick-test renderer option combinations. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The conservative integer-type mode treated a one-sided schema bound as
fitting i32, so `{"type": "integer", "minimum": 0}` — unbounded above —
rendered as `i32`, an overflow hazard. Conservative now picks i32 only
when the schema bounds the integer on both sides and both bounds fit
the i32 range.
Also override `getSupportedIntegerRange` so that with `force-i32`,
whole numbers in input JSON outside the i32 range are inferred as
`double` instead of an integer type that cannot round-trip them,
matching how cJSON's `integer-size` option hooks into the integer
range machinery from #2931.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
- Support pinned-input quick-test entries that name a `.schema` file: they now run in the JSON Schema fixture with their renderer options, while the JSON fixtures ignore them. - Pin the Rust integer-type quick tests: conservative and force-i64 run against integer-type.schema; force-i32 runs against minmax-integer.schema, whose sample values all fit in i32. - Redesign integer-type.schema to cover one-sided bounds (which must stay i64), exact i32 boundaries, bounds one past them, and large bounds, using only integers that are exactly representable as IEEE doubles. The previous +/-2^63 bounds are lossy as JS doubles - the int64 max rounds up to 2^63, which overflowed the int64_t constraint constants in generated C++ - and that was why the schema had to be skipped for C++. With exact bounds the skip is no longer needed. Co-Authored-By: Claude Fable 5 <noreply@anthropic.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.
Carries #2791 by @jonashao to completion — the contributor's original commit is the base of this branch, with fixes on top. The original PR disallows maintainer edits, so this successor PR replaces it.
What the feature does
New Rust option
--integer-type(conservativedefault |force-i32|force-i64). In conservative mode, integers whose JSON Schemaminimum/maximumbounds both fit in i32 render asi32; everything else staysi64(the previous unconditional behavior). Fixes #2790.Fixes on top of the original
{"minimum": 0}) as fitting i32 — an overflow hazard. Now both bounds must be present and fit.minMaxValueForType, andforce-i32overridesgetSupportedIntegerRange(mirroring cJSON'sinteger-sizeprecedent from [BUG]: Numbers beyond int64 range are typed as int64/long — generated Go can't unmarshal the sample it was generated from #2931) so out-of-range whole numbers infer as double.quickTestRendererOptionstuples naming.schemafiles now actually run in the JSON Schema fixture (they were silently ignored before).integer-type.schemacovers exact i32 boundaries, one-past values, one-sided and unbounded cases, with every constant exactly representable as an IEEE double — which resolves the original's undiagnosed C++ skip (its ±2^63 bounds rounded to 2^63 and overflowedint64_tconstants); the skip is removed.Output changes (sanctioned: next release is a major)
Under the new conservative default, bounded-integer schema properties flip i64→i32 where both bounds fit, e.g.
minmax,union,intersection,minMaxIntersectioninminmax-integer.schema.Testing
rust223/223,schema-rust71/71 (including all threeinteger-typeoption runs),schema-cplusplusgreen on the new schema,schema-golang67/67; biome clean.🤖 Generated with Claude Code