Skip to content

Remove duplicated helpers across the compiler, analysis and tools - #9292

Merged
cknitt merged 14 commits into
masterfrom
dedupe-helpers
Nov 12, 2026
Merged

cknitt merged 14 commits into
masterfrom
dedupe-helpers

Conversation

@cknitt

@cknitt cknitt commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Removes duplicated helpers found in the cleanup survey, one commit each. Two of them also fix small inconsistencies.

  • Package namespaces in one place. Ext_namespace, rewatch-ocaml's Config_decode and genType each derived a namespace from a package name. genType's copy dropped _, unlike the build system, so it didn't recognize the namespace module of a scoped dependency like @my_org/lib (My_orgLib). All three now call Ext_namespace.namespace_of_package_name; a new gentype ounit test covers the underscore case (fix fragment).
  • One type-declaration printer. print_type_declaration (for with type constraints) was a copy of print_type_declaration2 with the name and =/:= as parameters; it is now the only implementation. Output is unchanged.
  • One type walker in analysis. instantiate_type and instantiate_type2 were 55-line copies that differed only in how a variable is substituted; both now use map_type_vars. The by-name version returned the whole type being instantiated for a variable that isn't a parameter; it now keeps the variable.
  • walk_around in the comments table. "Attach the comments before a node, walk it with those inside, attach those after" was spelled out 50 times; the two identical functor-parameter walkers are now one. The syntax snapshots are unchanged.
  • Standard library instead of copies: Ext_pervasives.min_int/max_int, Ext_int.compare/equal, Ext_bytes, Ext_ref, five Ext_string and three Ext_list helpers, and analysis's Utils.starts_with/ends_with/find/filter_map. Kept on purpose, because they aren't exact copies:
    • Ext_pervasives.finally (re-raises a cleanup exception unchanged);
    • Ext_string.starts_with (String.starts_with allocates a closure per call);
    • Ext_list.nth_opt (no exception on a negative index);
    • Ext_list.assoc_by_string (default or assert);
    • Ext_list.is_empty (List.is_empty needs OCaml 5.1).
  • @deprecated read as the compiler does. Analysis parsed the attribute itself and disagreed with the compiler on malformed payloads, e.g. a record without reason showed as deprecated although the compiler doesn't warn. It now uses Builtin_attributes.deprecated_of_attrs_with_migrate (polish fragment).
  • One local value-completion lookup, with ~constructors.
  • Printtyp.string_of_out_ident replaces the copies in createInterface and rescript-tools.

Allocations and instructions. The stdlib's String.starts_with allocates a closure for its local loop on every call (no flambda), and is_package_relative_path calls it for every external: the benchmarks showed +163 words on Stdlib_Symbol. Ext_string.starts_with loops without allocating, so it stays, with a comment saying why. The built-in PPX also checked every applied identifier against ["->"] with List.mem, which goes through the generic compare; view_as_pipe now matches "->" in the pattern. Stdlib_Symbol is back to master within 7 words (Fun.protect's closures, once per file).

Not done: reanalyze's and genType's find_source_file look alike but differ (genType resolves paths against the compiler request's directory and returns absolute paths), so merging them would change reanalyze's output.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gKyiYMf54wPRxFdUWe

cknitt and others added 10 commits November 10, 2026 19:33
Ext_namespace, rewatch-ocaml's Config_decode and genType each had a copy
of the package-name-to-namespace rule. genType's dropped `_`, unlike the
build system, so it didn't recognize the namespace module of a scoped
dependency like `@my_org/lib` (My_orgLib). All three now use
Ext_namespace.namespace_of_package_name; a gentype ounit test covers the
underscore case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gKyiYMf54wPRxFdUWe
Signed-off-by: Christoph Knittel <ck@cca.io>
print_type_declaration, used for `with type` constraints, was a copy of
print_type_declaration2 that took the name and the `=`/`:=` sign as
parameters and lacked the empty-record case. The copy is now the only
implementation, and print_type_declaration2 computes the name and passes
`=`. Output is unchanged: `with type t = {}` parses as an object type.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gKyiYMf54wPRxFdUWe
Signed-off-by: Christoph Knittel <ck@cca.io>
instantiate_type and instantiate_type2 were 55-line copies of the same
type walker that differed only in how a variable is substituted (by
identity or by name). Both now use map_type_vars with their own
substitution. The by-name one returned the whole type being instantiated
for a variable that isn't a parameter; it now keeps the variable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gKyiYMf54wPRxFdUWe
Signed-off-by: Christoph Knittel <ck@cca.io>
The comments table spelled out "attach the comments before a node,
walk it with those inside, attach those after" 50 times; walk_around
now does it. walk_mod_expr_parameter and walk_mod_type_parameter were
identical and are now walk_functor_parameter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gKyiYMf54wPRxFdUWe
Signed-off-by: Christoph Knittel <ck@cca.io>
Ext_pervasives.min_int/max_int, Ext_int.compare/equal,
Ext_bytes.unsafe_blit_string and Ext_ref.protect copied Int.min/max,
Int.compare/equal, Bytes.unsafe_blit_string and Fun.protect. Their
callers use the standard library now; Ext_bytes and Ext_ref are gone.
Ext_pervasives.finally stays: unlike Fun.protect it re-raises an
exception from the cleanup unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gKyiYMf54wPRxFdUWe
Signed-off-by: Christoph Knittel <ck@cca.io>
Ext_string.starts_with, for_all, equal, is_empty, single_colon and
uncapitalize_ascii, and Ext_list.has_string, init and find_opt (a
find_map) copied String and List functions; callers use those now. Three
ounit cases only tested the removed copies. Ext_list.nth_opt (no raise
on a negative index), assoc_by_string (default or assert) and is_empty
(List.is_empty needs OCaml 5.1) stay.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gKyiYMf54wPRxFdUWe
Signed-off-by: Christoph Knittel <ck@cca.io>
Utils.starts_with, ends_with, find and filter_map copied
String.starts_with, String.ends_with, List.find_map and List.filter_map;
their 37 callers use the standard library now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gKyiYMf54wPRxFdUWe
Signed-off-by: Christoph Knittel <ck@cca.io>
Process_attributes.find_deprecated_attribute parsed the attribute itself
and disagreed with the compiler on malformed payloads: a record without
`reason` showed as deprecated with an empty message although the
compiler doesn't warn. It now takes the message from
Builtin_attributes.deprecated_of_attrs_with_migrate, which the
interface exports for this.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gKyiYMf54wPRxFdUWe
Signed-off-by: Christoph Knittel <ck@cca.io>
find_local_completions_for_values_and_constructors and
find_local_completions_for_values differed only in the constructor
steps; find_local_completions_for_values now takes ~constructors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gKyiYMf54wPRxFdUWe
Signed-off-by: Christoph Knittel <ck@cca.io>
createInterface and rescript-tools each printed an out_ident with their
own copy of Printtyp.string_of_out_ident; they use Printtyp's now, which
the interface exports for this.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gKyiYMf54wPRxFdUWe
Signed-off-by: Christoph Knittel <ck@cca.io>
@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • genType now recognizes the namespace module of a scoped dependency whose name contains _ (such as @my_org/lib), which it derived differently from the build system.

💅 Polish

  • The editor now shows a value as deprecated exactly when the compiler warns about it, also for @deprecated payloads it read differently before (such as a record without reason).

Semantic Diff

14 commits · Full page

Derive a package's namespace in one place

7d9ed1b · cknitt + Claude · Nov 10, 2026, 19:33 UTC

Ext_namespace, rewatch-ocaml's Config_decode and genType each had a copy of the package-name-to-namespace rule. genType's dropped _, unlike the build system, so it didn't recognize the namespace module of a scoped dependency like @my_org/lib (My_orgLib). All three now use Ext_namespace.namespace_of_package_name; a gentype ounit test covers the underscore case.

Print type declarations with one function

aca4019 · cknitt + Claude · Nov 10, 2026, 20:03 UTC

print_type_declaration, used for with type constraints, was a copy of print_type_declaration2 that took the name and the =/:= sign as parameters and lacked the empty-record case. The copy is now the only implementation, and print_type_declaration2 computes the name and passes =. Output is unchanged: with type t = {} parses as an object type.

Instantiate types in analysis with one type walker

12a61fe · cknitt + Claude · Nov 10, 2026, 20:03 UTC

instantiate_type and instantiate_type2 were 55-line copies of the same type walker that differed only in how a variable is substituted (by identity or by name). Both now use map_type_vars with their own substitution. The by-name one returned the whole type being instantiated for a variable that isn't a parameter; it now keeps the variable.

Attach comments around a node with one helper

7af08bd · cknitt + Claude · Nov 10, 2026, 20:05 UTC

The comments table spelled out "attach the comments before a node, walk it with those inside, attach those after" 50 times; walk_around now does it. walk_mod_expr_parameter and walk_mod_type_parameter were identical and are now walk_functor_parameter.

Use the standard library for int, bytes and ref helpers

d764697 · cknitt + Claude · Nov 10, 2026, 20:25 UTC

Ext_pervasives.min_int/max_int, Ext_int.compare/equal, Ext_bytes.unsafe_blit_string and Ext_ref.protect copied Int.min/max, Int.compare/equal, Bytes.unsafe_blit_string and Fun.protect. Their callers use the standard library now; Ext_bytes and Ext_ref are gone. Ext_pervasives.finally stays: unlike Fun.protect it re-raises an exception from the cleanup unchanged.

Use the standard library for string and list helpers

192746c · cknitt + Claude · Nov 10, 2026, 20:29 UTC

Ext_string.starts_with, for_all, equal, is_empty, single_colon and uncapitalize_ascii, and Ext_list.has_string, init and find_opt (a find_map) copied String and List functions; callers use those now. Three ounit cases only tested the removed copies. Ext_list.nth_opt (no raise on a negative index), assoc_by_string (default or assert) and is_empty (List.is_empty needs OCaml 5.1) stay.

Use the standard library for analysis's string and list helpers

7bdef7a · cknitt + Claude · Nov 10, 2026, 20:29 UTC

Utils.starts_with, ends_with, find and filter_map copied String.starts_with, String.ends_with, List.find_map and List.filter_map; their 37 callers use the standard library now.

Read @deprecated in analysis as the compiler does

b2f9dad · cknitt + Claude · Nov 10, 2026, 20:30 UTC

Process_attributes.find_deprecated_attribute parsed the attribute itself and disagreed with the compiler on malformed payloads: a record without reason showed as deprecated with an empty message although the compiler doesn't warn. It now takes the message from Builtin_attributes.deprecated_of_attrs_with_migrate, which the interface exports for this.

Look up local value completions with one function

abc9755 · cknitt + Claude · Nov 10, 2026, 20:31 UTC

find_local_completions_for_values_and_constructors and find_local_completions_for_values differed only in the constructor steps; find_local_completions_for_values now takes ~constructors.

Print module paths with Printtyp in analysis and tools

2d20578 · cknitt + Claude · Nov 10, 2026, 20:32 UTC

createInterface and rescript-tools each printed an out_ident with their own copy of Printtyp.string_of_out_ident; they use Printtyp's now, which the interface exports for this.

Keep Ext_string.starts_with, which doesn't allocate

d37461f · cknitt + Claude · Nov 10, 2026, 21:04 UTC

String.starts_with allocates a closure for its local loop on every call, and is_package_relative_path calls it twice for every external: the benchmarks showed +163 words on Stdlib_Symbol. Ext_string.starts_with loops without allocating; it is back, with a comment saying why, for the compiler's two callers and its ounit case.

Match the pipe operator by its name in the built-in PPX

21c0a90 · cknitt + Claude · Nov 10, 2026, 21:04 UTC

view_as_app checked every applied identifier against a one-element list with List.mem, which compares through the generic structural compare; its only caller passed ["->"]. view_as_pipe matches the constant "->" in the pattern, a direct string comparison.

Stop exporting Printtyp.tree_of_path

3aa41e0 · cknitt + Claude · Nov 11, 2026, 14:54 UTC

createInterface was its last user outside Printtyp and prints the path with Printtyp.string_of_path now, so the dead-code check reported the export.

✨ Test analysis's by-name instantiation and @deprecated reading

2a7d515 · cknitt + Claude · Nov 12, 2026, 20:45 UTC · new

Codex review: the two analysis behavior fixes had no tests. ounit_analysis_types_tests checks that instantiate_type2 substitutes a parameter and keeps a variable that isn't one, and that find_deprecated_attribute reads a string, a record with a reason, and a record without one (not deprecated) as the compiler does. Both fail against the previous behavior.

@codecov

codecov Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.02062% with 31 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.07%. Comparing base (00e3f40) to head (2a7d515).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
analysis/src/type_utils.ml 55.81% 19 Missing ⚠️
compiler/core/js_stmt_make.ml 0.00% 4 Missing ⚠️
tests/ounit_tests/ounit_analysis_types_tests.ml 91.89% 3 Missing ⚠️
analysis/src/process_cmt.ml 50.00% 1 Missing ⚠️
analysis/src/process_extra.ml 50.00% 1 Missing ⚠️
compiler/core/js_implementation.ml 0.00% 1 Missing ⚠️
compiler/ml/path.ml 0.00% 1 Missing ⚠️
compiler/syntax/src/res_comments_table.ml 97.67% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9292      +/-   ##
==========================================
+ Coverage   81.97%   82.07%   +0.09%     
==========================================
  Files         456      455       -1     
  Lines       65671    65360     -311     
==========================================
- Hits        53836    53642     -194     
+ Misses      11835    11718     -117     
Files with missing lines Coverage Δ
analysis/src/commands.ml 62.50% <ø> (ø)
analysis/src/completion_back_end.ml 81.39% <100.00%> (-0.10%) ⬇️
analysis/src/create_interface.ml 85.52% <100.00%> (+0.56%) ⬆️
analysis/src/dot_completion_utils.ml 91.30% <100.00%> (ø)
analysis/src/find_files.ml 78.07% <100.00%> (ø)
analysis/src/process_attributes.ml 87.50% <100.00%> (+0.65%) ⬆️
analysis/src/references.ml 70.51% <100.00%> (+2.28%) ⬆️
analysis/src/scope.ml 100.00% <ø> (ø)
analysis/src/shared_types.ml 83.87% <100.00%> (ø)
analysis/src/utils.ml 42.65% <100.00%> (-4.85%) ⬇️
... and 36 more

... and 4 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 640,764,338 → 640,780,622 (±0.00%).

Generated projects: 920,848,686 → 920,879,594 (±0.00%).

None of the 265 benchmarks changed by 0.1% or more.

Instructions

In total: 9,211,262,577 → 9,209,653,365 (-0.02%).

Benchmark Base Head Change
packages/@rescript/runtime/Stdlib_Math.resi 21,162,193 21,220,383 +0.27%
packages/@rescript/runtime/Stdlib_Temporal_PlainDate.res 23,970,346 24,021,701 +0.21%
packages/@rescript/runtime/Stdlib_Temporal_PlainDateTime.res 25,405,293 25,457,482 +0.21%
packages/@rescript/runtime/Stdlib_List.resi 24,505,647 24,552,812 +0.19%
packages/@rescript/runtime/Stdlib_Temporal_ZonedDateTime.res 31,034,420 31,091,148 +0.18%
parse/Napkinscript.res 333,735,550 333,144,715 -0.18%
packages/@rescript/belt/src/Belt_Array.resi 33,678,840 33,738,013 +0.18%
packages/@rescript/belt/src/Belt_List.resi 38,949,937 39,009,717 +0.15%
packages/@rescript/runtime/Stdlib_String.resi 29,337,552 29,382,400 +0.15%
packages/@rescript/runtime/Stdlib_Date.resi 34,108,597 34,158,328 +0.15%
7 more changes
Benchmark Base Head Change
packages/@rescript/runtime/Stdlib_TypedArray.res 41,082,721 41,140,652 +0.14%
parse/RedBlackTreeNoComments.res 14,535,303 14,516,267 -0.13%
stress/NestedClosures.res 529,253,256 528,585,248 -0.13%
packages/@rescript/runtime/Stdlib_Array.resi 39,945,142 39,991,379 +0.12%
format/RedBlackTree.res 24,408,980 24,436,745 +0.11%
format/RedBlackTreeNoComments.res 24,064,543 24,091,686 +0.11%
parse/RedBlackTree.res 14,935,881 14,920,398 -0.10%

Output size

In total: 25,842,868 → 25,838,530 (-0.02%).

Benchmark Base Head Change
output/playground/compiler.js 1,894,041 1,889,703 -0.23%

Pull request 2a7d515 compared with base 469d677 in this run.

cknitt and others added 2 commits November 10, 2026 21:04
String.starts_with allocates a closure for its local loop on every call,
and is_package_relative_path calls it twice for every external: the
benchmarks showed +163 words on Stdlib_Symbol. Ext_string.starts_with
loops without allocating; it is back, with a comment saying why, for the
compiler's two callers and its ounit case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gKyiYMf54wPRxFdUWe
Signed-off-by: Christoph Knittel <ck@cca.io>
view_as_app checked every applied identifier against a one-element list
with List.mem, which compares through the generic structural compare;
its only caller passed ["->"]. view_as_pipe matches the constant "->" in
the pattern, a direct string comparison.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gKyiYMf54wPRxFdUWe
Signed-off-by: Christoph Knittel <ck@cca.io>
createInterface was its last user outside Printtyp and prints the path
with Printtyp.string_of_path now, so the dead-code check reported the
export.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gKyiYMf54wPRxFdUWe
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt marked this pull request as ready for review November 11, 2026 14:59
@pkg-pr-new

pkg-pr-new Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9292

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9292

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9292

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9292

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9292

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9292

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9292

commit: 2a7d515

@github-actions

Copy link
Copy Markdown

@cknitt

cknitt commented Nov 12, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T20:48:11.456352Z 2a7d515 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3aa41e0f53

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread analysis/src/type_utils.ml Resolved
Codex review: the two analysis behavior fixes had no tests.
ounit_analysis_types_tests checks that instantiate_type2 substitutes a
parameter and keeps a variable that isn't one, and that
find_deprecated_attribute reads a string, a record with a reason, and a
record without one (not deprecated) as the compiler does. Both fail
against the previous behavior.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gKyiYMf54wPRxFdUWe
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt

cknitt commented Nov 12, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 2a7d515d81

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@cknitt
cknitt enabled auto-merge (squash) November 12, 2026 20:53
@cknitt
cknitt merged commit 622016f into master Nov 12, 2026
22 checks passed
@cknitt
cknitt deleted the dedupe-helpers branch November 12, 2026 20:59
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.

2 participants