Skip to content

bun check, bun fmt: round hexadecimal, octal and binary strings to the nearest double - #46458

Closed
robobun wants to merge 1 commit into
mainfrom
robobun/970b7adb/radix-integer-sorter-check
Closed

robobun wants to merge 1 commit into
mainfrom
robobun/970b7adb/radix-integer-sorter-check

Conversation

@robobun

@robobun robobun commented Oct 22, 2026

Copy link
Copy Markdown
Collaborator

Problem

  • Two places sum the digits of a 0x, 0o or 0b string in an f64. From 2^57, 2^56 and 2^54, the sum can be one double too low. No user reported it.
  • bun check (number_from_string, src/sema/check/relate.rs:8237): with enum E { Below = 144115188075855872, Nearest = 144115188075855904 }, member("0x200000000000011") infers E.Below. TypeScript infers E.Nearest.
  • bun fmt (hexadecimal, src/format/js/sort_imports/compare.rs:216): "0x0200000000000011" sorts before "0x200000000000010". Prettier with @trivago/prettier-plugin-sort-imports prints the other order.

Fix

  • literal_matching_text (src/sema/check/infer.rs:1246) calls bun_core::fmt::js_string_to_number. number_from_string, a second copy of Number(text), is deleted.
  • hexadecimal calls bun_core::fmt::parse_digits_of_bits, now public and generic over bytes and UTF-16 units. Both round once, in RadixInteger.
  • Verified: test/cli/check/check.test.ts ("...nearest double") and test/cli/format/sort-imports/sort-imports.test.ts ("...nearest to their value") fail on main. Also test/cli/check/conformance/.
  • Self-reviewed: 9 concerns raised, 8 addressed. Rejected: new issues for the two other defects in Notes.

Background

Notes

Where it comes from. Nobody reported either symptom. #45290 fixed this sum in the transpiler and in Bun.JSONC.parse. A review comment there named the bun fmt sorter as left out, and the notes of #42112 name both places. Bun 1.4.3 has the sum of bun check. bun fmt is in no release yet.

Reproduce. bun check:

enum E { Below = 144115188075855872, Nearest = 144115188075855904 }
declare function member<T extends E>(text: `${T}` | (string & {})): T;
const hex: never = member("0x200000000000011");

TypeScript 7.0.2 and 6.0.2 report Type 'E.Nearest' is not assignable to type 'never'. bun check on main reports E.Below. 0x200000000000011 is 2^57 + 17, and the nearest double is 2^57 + 32. "0o10000000000000000021" and the binary spelling give the same. bun fmt: name @trivago/prettier-plugin-sort-imports in .prettierrc.json and import from "0x0200000000000011", "0x200000000000010", "0x200000000000011" and "0x0200000000000020".

Where the sum is wrong. The step that crosses 2^53 rounds once, and a sum that ends there is the nearest double. Each later digit is dropped. The smallest wrong integers are 2^57 + 17 (hexadecimal), 2^56 + 9 (octal) and 2^54 + 3 (binary). Above those, 3% to 25% of random values are wrong, each one double low, none high.

Which line changes a result. literal_matching_text (inferToTemplateLiteralType) compares the number with the values of enum members, so the wrong double picked the wrong member or none. is_valid_number_string (src/sema/check/relate.rs:8208) calls the same function now, and no ${number} match changes: it only asks if the number is finite, and the sum is finite for the same texts. The largest double has an odd significand, so a tie next to 2^1024 rounds up in the sum too. "0x" with 255 and with 256 f are in the new table for that edge.

The two copies of Number(text). A temporary build ran number_from_string and js_string_to_number on every call and stopped on a difference. Input: 274,972 generated texts of up to 160 bits (decimal and radix shapes, every white space of JavaScript, lone surrogates, edges of the double range), through the lexer of bun check. They differ on 24,231 texts, all with a 0x, 0o or 0b prefix, all by one double. js_string_to_number equals Number(text) of Node.js 26 on all 274,972. The 16 TypeScript conformance cases that contain ${number} or reach Number(text) (96 distinct strings) stay byte for byte. A second check, on a copy of the functions in a scratch crate, converted every integer from 0 to 0xFFFFF and from 2^53 - 70,000 to 2^53 + 70,000, in three radices, as bytes and as UTF-16: 10,697,193 conversions, 0 wrong, before and after.

Tests. The expectations come from tsc 7.0.2 (6.0.2 agrees) and from Prettier 3.9.10 with the plugin 6.0.2 on Node.js, not from Bun. Bun 1.4.3 has bun check with this sum and no bun fmt, so under USE_SYSTEM_BUN=1 only the first test fails for this reason. Results on a build of main:

  • check.test.ts, "a string that spells a hexadecimal, octal or binary number infers the enum member of the nearest double": fails. It has the program above, plus the 640 finite numbers of integersThatNeedRounding(), of which main gets 65 wrong (false TS2322).
  • check.test.ts, "a string is a number for ${number} and for an enum member when Number(text) says so": passes on main too. It has 96 texts with what tsc says. It is there because every ${number} match now goes through js_string_to_number, and the sample of TypeScript's cases that a debug build runs has none of the 16 that cover it.
  • sort-imports.test.ts, "hexadecimal module names sort by the double nearest to their value": fails. It has 16 names in the order of the plugin. The first 8 are doubles themselves and are in the right order on main.

Wrong results removed. False TS2322 on the 640 numbers: 65 to 0. Input orders of the four names above that come out in the plugin's order: 0 to 24 of 24. Generated files of 10 hexadecimal names that differ from Prettier with the plugin: 290 to 0 of 800. f64 digit sums for radix 2, 8 or 16 in src/: 2 to 0.

Measurements. LTO release builds of main (2d6d445) and of this change on it, linux-x64 baseline. Instructions are instructions:u from perf_event_open, medians of interleaved runs. perf, valgrind, strace and bloaty are not installed on the machine.

bun check --threads=1, per call of member("<text>"), from files of 50,000 and 100,000 calls, 11 runs each:

text main PR difference
no string is read as a number 9,868 9,867 -1
hello 24,049 23,963 -86
42 24,211 24,162 -48
12px 24,199 24,000 -199
17 decimal digits 25,213 25,162 -51
0xff 24,124 24,104 -20
50 binary digits 25,574 25,810 +236
a lone surrogate and 1 24,619 24,638 +19

Two more runs of the table moved a row by up to 15 instructions, and the last row between +1 and +46. The file of 100,000 hello calls: 2,435,112,440 to 2,426,028,949 instructions, spread 1.2 M. Calls of parse_f64 for 1,000 12px literals, counted with a gdb breakpoint: 1,000 to 0.

bun fmt with the plugin named:

input main PR difference
one file, 2,000 ordinary names 27,898,801 27,937,016 +0.14%
one file, 2,000 names that start with a digit 43,949,773 43,814,701 -0.31%
one file, 2,000 non-ASCII names 58,980,495 58,827,157 -0.26%
one file, 2,000 hexadecimal names 35,039,208 37,586,560 +7.3%
1,122 real files (--check --threads=1) 868,770,844 868,838,760 +0.008%

The spread of a one-file row is about 1,000 instructions. The real files are the sources of svelte, mongodb, msw and happy-dom. Their spread is 200,000 to 280,000, more than the difference, and the wall time is the same (176.6 and 173.4 ms). The 2 instructions are one more register that natural_sort_ascii saves, because the longer loop is inlined into it. With natural_sort_units out of line the four one-file rows are -0.42%, +0.18%, +0.26% and +7.9%. That trade is not in this PR.

Size: .text 81,929,573 to 81,928,549 bytes (-1,024). number_from_string (1,718 bytes) is gone. natural_sort and natural_sort_ascii grow from 4,878 to 5,541 bytes. js_string_to_number is 1,284 bytes in both builds and differs in one compare (cmp $0x10; jae is now cmp $0xf; ja), so bun lint and bun fmt run the code they ran. No instance of parse_digits_of_bits is out of line. Allocations per call, counted in the scratch crate: 0 before and after. There is no syscall comparison: qemu-x86_64 -strace counts differ between two runs of one binary. The changed functions make no system call.

Other open PRs. #44833 makes parse_digits_of_bits public with a &[u8] parameter and calls it with bits from 1 to 5, for parseInt in bun lint. The generic signature here takes that call as it is. The two PRs conflict on the signature line and on the sentence above it.

Not in this PR.

A smaller shape. If the deletion of number_from_string should be a change of its own, its radix branch can call parse_digits_of_bits and the rest can stay.

Suites. Debug build: both sort-imports test files and check.test.ts. In check.test.ts one test that this change does not reach, "reports TS2589 for a conditional type that recurs through a type node that isn't deferred", fails on a debug build only: bun check runs out of stack there. A debug build needs --timeout for these files, as CI passes. Release build: check.test.ts, all of test/cli/check/conformance/, differential-cases-1 to 4, union-properties, entry-points, both sort-imports files, and the lint and format suites conformance, rules, native-rules, lint, json-parse, format, bunfig. cargo clippy for bun_core, bun_format and bun_sema.

Downsides

  • bun fmt: comparing two ordinary module names costs 2 more instructions. A file of 2,000 imports costs 0.14% more, 1,122 real files 0.008%. Hexadecimal names cost 7.3% more.
  • bun check: a string of 50 binary digits costs about 240 more instructions. Ordinary text costs 50 to 200 fewer.

…o the nearest double

`bun check` had its own copy of `Number(text)`, and the import sorter of
`bun fmt` had its own loop for a hexadecimal module name. Each added the
digits up in a double. For a hexadecimal number of 2^57 or more, an octal
number of 2^56 or more and a binary number of 2^54 or more, that sum can
be one double below the nearest one, which TypeScript and `parseInt` give.

`bun check` now calls `bun_core::fmt::js_string_to_number`, and its copy
is deleted. The sorter calls `bun_core::fmt::parse_digits_of_bits`, which
now takes bytes or UTF-16 code units. Both round once, in `RadixInteger`.
@robobun

robobun commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 4:20 AM PT - Oct 22nd, 2026

❌ @robobun, your commit c67363e has 3 failures in Build #130704 (All Failures):


🧪   To try this PR locally:

bunx bun-pr 46458

That installs a local version of the PR into your bun-46458 executable, so you can run:

bun-46458 --bun

@robobun

robobun commented Oct 22, 2026

Copy link
Copy Markdown
Collaborator Author

Status: the fix is in this PR.

How it was reproduced, on a build of main:

  • bun check: for the program in the Notes of the description, member("0x200000000000011") reports E.Below. TypeScript 7.0.2 and 6.0.2 report E.Nearest.
  • bun fmt: with @trivago/prettier-plugin-sort-imports named in .prettierrc.json, the four module names "0x0200000000000011", "0x200000000000010", "0x200000000000011" and "0x0200000000000020" come out in an order that Prettier 3.9.10 with the plugin prints for none of the 24 input orders.

The two new tests that cover these fail on main and pass with this change.

@coderabbitai

coderabbitai Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: oven-sh/bun/.coderabbit.yaml
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 304bcda4-7130-44f2-bf95-f20a8e868ea6
📥 Commits

Reviewing files that changed from the base of the PR and between 398f1e3 and c67363e.

📒 Files selected for processing: 6
  • src/bun_core/fmt.rs
  • src/format/js/sort_imports/compare.rs
  • src/sema/check/infer.rs
  • src/sema/check/relate.rs
  • test/cli/check/check.test.ts
  • test/cli/format/sort-imports/sort-imports.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 2 remain after this review.

Walkthrough

The change makes digit parsing public and generic, uses shared numeric conversion in the import sorter and checker, and adds regression tests for hexadecimal ordering and checker numeric matching.

Changes

Hexadecimal Import Sorting

Layer / File(s) Summary
Hexadecimal comparison and ordering tests
src/bun_core/fmt.rs, src/format/js/sort_imports/compare.rs, test/cli/format/sort-imports/sort-imports.test.ts
parse_digits_of_bits accepts elements convertible to u32. The import sorter uses it to parse hexadecimal names. The test checks ordering across double-precision values, including values longer than 64 bits.

Checker Numeric Matching

Layer / File(s) Summary
Checker conversion and inference tests
src/sema/check/relate.rs, src/sema/check/infer.rs, test/cli/check/check.test.ts
is_valid_number_string and literal_matching_text use js_string_to_number. Tests cover numeric formats, whitespace, rounding, invalid strings, and non-finite values.

Suggested reviewers: jarred-sumner

Priority: ➖ Normal

Merge Risk: ⚪ Minimal · up to c6736

The new numeric-string tests can run sequentially without a known workflow issue. No change is required before merging.

Pre-merge checks: Passed 4
Check name Status Explanation
Title check Passed The title clearly and concisely identifies the main change: correcting rounding for hexadecimal, octal, and binary strings in bun check and bun fmt.
Description check Passed The description is comprehensive. It explains the problem, fix, verification results, tests, performance impact, and scope. It does not use the template headings exactly, but it provides the required …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description is comprehensive. It explains the problem, fix, verification results, tests, performance impact, and scope. It does not use the template headings exactly, but it provides the required information in equivalent sections.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review found no issues

No high-confidence issues detected in this change.

@robobun

robobun commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

Closing as stale: this PR conflicts with main, and its last commit is from 2026-10-22.

The branch stays, so the work is not lost. To pick it up again, rebase the branch on main and reopen this PR.

@robobun robobun closed this Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant