Skip to content

feat(rust): integer-type option with safe conservative i32 inference#2964

Merged
schani merged 5 commits into
masterfrom
rust-integer-type-fixes-2791
Jul 20, 2026
Merged

feat(rust): integer-type option with safe conservative i32 inference#2964
schani merged 5 commits into
masterfrom
rust-integer-type-fixes-2791

Conversation

@schani

@schani schani commented Jul 20, 2026

Copy link
Copy Markdown
Member

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 (conservative default | force-i32 | force-i64). In conservative mode, integers whose JSON Schema minimum/maximum bounds both fit in i32 render as i32; everything else stays i64 (the previous unconditional behavior). Fixes #2790.

Fixes on top of the original

  • Safe conservative mode: the original treated a one-sided bound ({"minimum": 0}) as fitting i32 — an overflow hazard. Now both bounds must be present and fit.
  • Integer-range integration: bounds are read via minMaxValueForType, and force-i32 overrides getSupportedIntegerRange (mirroring cJSON's integer-size precedent 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.
  • Harness fix: pinned quickTestRendererOptions tuples naming .schema files now actually run in the JSON Schema fixture (they were silently ignored before).
  • Schema redesign: integer-type.schema covers 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 overflowed int64_t constants); 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, minMaxIntersection in minmax-integer.schema.

Testing

rust 223/223, schema-rust 71/71 (including all three integer-type option runs), schema-cplusplus green on the new schema, schema-golang 67/67; biome clean.

🤖 Generated with Claude Code

jonashao and others added 4 commits June 24, 2025 10:21
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>
@schani
schani merged commit 2b26cc7 into master Jul 20, 2026
35 of 56 checks passed
@schani
schani deleted the rust-integer-type-fixes-2791 branch July 20, 2026 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE]: Smart Integer Type Inference for Rust Target

2 participants