Repository navigation
Conversation
…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`.
|
Updated 4:20 AM PT - Oct 22nd, 2026
❌ @robobun, your commit c67363e has 3 failures in
🧪 To try this PR locally: bunx bun-pr 46458That installs a local version of the PR into your bun-46458 --bun |
|
Status: the fix is in this PR. How it was reproduced, on a build of main:
The two new tests that cover these fail on main and pass with this change. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
WalkthroughThe 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. ChangesHexadecimal Import Sorting
Checker Numeric Matching
Suggested reviewers: Priority: ➖ Normal Merge Risk: ⚪ Minimal · up to
|
|
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. |
Problem
0x,0oor0bstring in anf64. 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): withenum E { Below = 144115188075855872, Nearest = 144115188075855904 },member("0x200000000000011")infersE.Below. TypeScript infersE.Nearest.bun fmt(hexadecimal,src/format/js/sort_imports/compare.rs:216):"0x0200000000000011"sorts before"0x200000000000010". Prettier with@trivago/prettier-plugin-sort-importsprints the other order.Fix
literal_matching_text(src/sema/check/infer.rs:1246) callsbun_core::fmt::js_string_to_number.number_from_string, a second copy ofNumber(text), is deleted.hexadecimalcallsbun_core::fmt::parse_digits_of_bits, now public and generic over bytes and UTF-16 units. Both round once, inRadixInteger.test/cli/check/check.test.ts("...nearest double") andtest/cli/format/sort-imports/sort-imports.test.ts("...nearest to their value") fail on main. Alsotest/cli/check/conformance/.Background
RadixInteger(src/bun_core/fmt.rs, from Round long hexadecimal, octal and binary literals to the nearest double #45290) keeps the leading 64 bits, counts the rest, and rounds once.push_digitloop at each site. It keeps the secondNumber(text)and adds two digit loops.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 thebun fmtsorter as left out, and the notes of #42112 name both places. Bun 1.4.3 has the sum ofbun check.bun fmtis in no release yet.Reproduce.
bun check:TypeScript 7.0.2 and 6.0.2 report
Type 'E.Nearest' is not assignable to type 'never'.bun checkon main reportsE.Below.0x200000000000011is 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-importsin.prettierrc.jsonand 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 256fare in the new table for that edge.The two copies of
Number(text). A temporary build rannumber_from_stringandjs_string_to_numberon 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 ofbun check. They differ on 24,231 texts, all with a0x,0oor0bprefix, all by one double.js_string_to_numberequalsNumber(text)of Node.js 26 on all 274,972. The 16 TypeScript conformance cases that contain${number}or reachNumber(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
tsc7.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 hasbun checkwith this sum and nobun fmt, so underUSE_SYSTEM_BUN=1only 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 ofintegersThatNeedRounding(), of which main gets 65 wrong (false TS2322).check.test.ts, "a string is a number for${number}and for an enum member whenNumber(text)says so": passes on main too. It has 96 texts with whattscsays. It is there because every${number}match now goes throughjs_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.
f64digit sums for radix 2, 8 or 16 insrc/: 2 to 0.Measurements. LTO release builds of main (2d6d445) and of this change on it, linux-x64 baseline. Instructions are
instructions:ufromperf_event_open, medians of interleaved runs.perf,valgrind,straceandbloatyare not installed on the machine.bun check --threads=1, per call ofmember("<text>"), from files of 50,000 and 100,000 calls, 11 runs each:hello4212px0xff1Two 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
hellocalls: 2,435,112,440 to 2,426,028,949 instructions, spread 1.2 M. Calls ofparse_f64for 1,00012pxliterals, counted with a gdb breakpoint: 1,000 to 0.bun fmtwith the plugin named:--check --threads=1)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_asciisaves, because the longer loop is inlined into it. Withnatural_sort_unitsout 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:
.text81,929,573 to 81,928,549 bytes (-1,024).number_from_string(1,718 bytes) is gone.natural_sortandnatural_sort_asciigrow from 4,878 to 5,541 bytes.js_string_to_numberis 1,284 bytes in both builds and differs in one compare (cmp $0x10; jaeis nowcmp $0xf; ja), sobun lintandbun fmtrun the code they ran. No instance ofparse_digits_of_bitsis out of line. Allocations per call, counted in the scratch crate: 0 before and after. There is no syscall comparison:qemu-x86_64 -stracecounts differ between two runs of one binary. The changed functions make no system call.Other open PRs. #44833 makes
parse_digits_of_bitspublic with a&[u8]parameter and calls it withbitsfrom 1 to 5, forparseIntinbun 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.
Number()andparseIntof JavaScriptCore, and what the transpiler folds through them: Bump WebKit (oven-sh/WebKit#597 preview): parseInt, Number() and eval round binary, octal and hex digits to the nearest double #42112.js_to_numberinsrc/react_compiler/optimization/constant_propagation.rsis another copy ofNumber(text), with no digit sum. It reads a prefixed number as au64and the rest with Rust'strimandparse.bun build --react-compilerfoldsInfinity == "inf"and16 == "0x+10"totrue, and18446744073709551616 == "0x10000000000000000"tofalse. The notes of react-compiler: decode a UTF-8 string when constant propagation joins it to a UTF-16 string #45172 describe part of it. No issue tracks it.Bun.YAML.parsereads0xffffffffffffffffas a number and0x10000000000000000as a string (src/parsers/yaml.rs, au64). No issue tracks it.Bun.JSON5.parsefor hexadecimal numbers of 2^64 or more: json5: parse hex numbers wider than 64 bits as the nearest double instead of throwing #42136.number_from_digitsin the lexer ofbun checkkeeps its loop overRadixInteger. It skips separators, whichparse_digits_of_bitsrejects.decimal_literal_lenincompare.rsis a copy ofbun_core::fmt::js_decimal_literal_lenfor both kinds of units. The same bound on the function ofbun_corewould replace it.A smaller shape. If the deletion of
number_from_stringshould be a change of its own, its radix branch can callparse_digits_of_bitsand the rest can stay.Suites. Debug build: both sort-imports test files and
check.test.ts. Incheck.test.tsone 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 checkruns out of stack there. A debug build needs--timeoutfor these files, as CI passes. Release build:check.test.ts, all oftest/cli/check/conformance/,differential-cases-1to4,union-properties,entry-points, both sort-imports files, and the lint and format suitesconformance,rules,native-rules,lint,json-parse,format,bunfig.cargo clippyforbun_core,bun_formatandbun_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.