Skip to content

fix(codegen): argless builtins ignore extra args instead of bailing#5285

Merged
proggeramlug merged 2 commits into
mainfrom
fix/codegen-argless-builtin-args
Jun 17, 2026
Merged

fix(codegen): argless builtins ignore extra args instead of bailing#5285
proggeramlug merged 2 commits into
mainfrom
fix/codegen-argless-builtin-args

Conversation

@proggeramlug

@proggeramlug proggeramlug commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

What

Argless built-in methods (String.prototype trim/trimStart/trimEnd/toLowerCase/toUpperCase/isWellFormed/toWellFormed, Array.prototype pop/shift) bail!ed in codegen when called with extra args. But per ECMA-262 those args are simply ignored ("x".trim(1) is legal and returns the trimmed string). Minified bundles do this. Now codegen evaluates the surplus args for side effects (spec requires arg evaluation) and discards them, matching the Annex-B HTML-wrapper convention already in the same file, instead of failing the compile.

Why

Surfaced compiling a large real-world minified bundle: perry-codegen: String.trim takes no args, got 1 aborted the whole module.

Tests

New crates/perry-codegen/tests/argless_builtin_extra_args.rs (" x ".trim(1)js_string_trim; [1].pop(99) compiles). cargo test -p perry-codegen --tests green.

Summary by CodeRabbit

  • Bug Fixes

    • Updated lowering for array pop and shift and string methods toLowerCase, toUpperCase, trim, trimStart, trimEnd, and isWellFormed to ignore extra arguments while still evaluating them for side effects, matching JavaScript behavior.
  • Tests

    • Added a regression test covering argless builtin method calls with extra arguments, including verification of the expected lowering.

@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 54fb6c52-3bc7-4e2f-a87c-9a9e17d58a1a

📥 Commits

Reviewing files that changed from the base of the PR and between bf4dbfc and 69bcb0e.

📒 Files selected for processing (3)
  • crates/perry-codegen/src/lower_array_method.rs
  • crates/perry-codegen/src/lower_string_method.rs
  • crates/perry-codegen/tests/argless_builtin_extra_args.rs
🚧 Files skipped from review as they are similar to previous changes (3)
  • crates/perry-codegen/tests/argless_builtin_extra_args.rs
  • crates/perry-codegen/src/lower_array_method.rs
  • crates/perry-codegen/src/lower_string_method.rs

📝 Walkthrough

Walkthrough

Argless builtin method lowering arms for Array.pop, Array.shift, string case/trim methods, and String.isWellFormed no longer error when callers pass extra arguments. The bail! checks are replaced with side-effect-preserving evaluation loops that discard the results. A new regression test file validates this behavior for String.trim and Array.pop.

Changes

Argless Builtin Extra-Arg Tolerance

Layer / File(s) Summary
Array method lowering: pop and shift
crates/perry-codegen/src/lower_array_method.rs
pop and shift arms remove their bail! argument-count checks and instead iterate over extra args, calling lower_expr for each to preserve side effects before invoking the existing no-arg runtime helper.
String method lowering: unary and isWellFormed
crates/perry-codegen/src/lower_string_method.rs
Unary string-returning methods (toLowerCase, toUpperCase, trim, trimStart, trimEnd) and isWellFormed drop their bail! argument-count checks and instead iterate over extra args, calling lower_expr for each to preserve side effects before invoking the runtime helper.
Regression tests for extra-arg tolerance
crates/perry-codegen/tests/argless_builtin_extra_args.rs
New test file adds empty_opts() and module_with_init() helpers, plus two unit tests that compile String.trim and Array.pop calls with a surplus numeric argument, asserting compilation succeeds and verifying the expected runtime helper appears in the output IR.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Possibly related PRs

  • PerryTS/perry#5272: Complements this PR by adding an arity-aware gate in the String-method fast-path dispatch to prevent mis-dispatch on non-string receivers, while this PR handles the argument evaluation semantics for argless methods themselves.

Poem

🐇 Hop hop, no more bail!
Extra args? We'll just flail
through lower_expr once or twice,
toss the results—oh how nice.
Side effects kept, errors gone,
the trim still trims—carry on! ✂️

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: fixing argless builtins to ignore extra arguments instead of failing compilation.
Description check ✅ Passed The PR description covers the what, why, and tests sections with clear explanations. However, the required checklist section is missing/incomplete, which is a non-critical omission given the substantial content provided.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/codegen-argless-builtin-args

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

@coderabbitai coderabbitai 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.

🧹 Nitpick comments (1)
crates/perry-codegen/tests/argless_builtin_extra_args.rs (1)

91-129: ⚡ Quick win

Expand regression coverage to all modified argless arms.

This file currently verifies only trim and pop, while the PR also changes shift, toLowerCase/toUpperCase/trimStart/trimEnd, isWellFormed, and toWellFormed. Adding one compile-success case per method family here would better lock the behavior and prevent partial regressions.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/perry-codegen/tests/argless_builtin_extra_args.rs` around lines 91 -
129, The test file currently only verifies compile-success cases for the argless
methods trim and pop, but the PR modifies several other argless methods
including shift, toLowerCase, toUpperCase, trimStart, trimEnd, isWellFormed, and
toWellFormed. Add a new test function for each of these remaining modified
argless methods following the same pattern as
string_trim_with_extra_arg_compiles and array_pop_with_extra_arg_compiles—create
an AST that calls each method with an extra argument, compile it, and assert
that compilation succeeds. This ensures regression coverage for all modified
argless arms and prevents partial regressions if only some of them are properly
handled.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@crates/perry-codegen/tests/argless_builtin_extra_args.rs`:
- Around line 91-129: The test file currently only verifies compile-success
cases for the argless methods trim and pop, but the PR modifies several other
argless methods including shift, toLowerCase, toUpperCase, trimStart, trimEnd,
isWellFormed, and toWellFormed. Add a new test function for each of these
remaining modified argless methods following the same pattern as
string_trim_with_extra_arg_compiles and array_pop_with_extra_arg_compiles—create
an AST that calls each method with an extra argument, compile it, and assert
that compilation succeeds. This ensures regression coverage for all modified
argless arms and prevents partial regressions if only some of them are properly
handled.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 084df4d3-60e9-4b42-9f2e-caa74338de01

📥 Commits

Reviewing files that changed from the base of the PR and between d46feff and bf4dbfc.

📒 Files selected for processing (3)
  • crates/perry-codegen/src/lower_array_method.rs
  • crates/perry-codegen/src/lower_string_method.rs
  • crates/perry-codegen/tests/argless_builtin_extra_args.rs

JS ignores surplus arguments to argless methods ("x".trim(1) is legal
and returns the trimmed string). perry's codegen bail!'d with "takes no
args, got N" for String.{toLowerCase,toUpperCase,trim,trimStart,trimEnd,
isWellFormed,toWellFormed} and Array.{pop,shift}, rejecting valid JS.

Drop the arg-count bail in all 5 sites; evaluate extra args for their
side effects (ECMA-262 evaluates arguments before the call) then discard,
matching the existing Annex B HTML-wrapper convention in the same file.

Clears the first codegen wall on the claude-code cli.js bundle.

Adds tests/argless_builtin_extra_args.rs covering "x".trim(1) and
[1].pop(99).
@proggeramlug proggeramlug force-pushed the fix/codegen-argless-builtin-args branch from bf4dbfc to 69bcb0e Compare June 17, 2026 04:57
@proggeramlug proggeramlug merged commit c0deecd into main Jun 17, 2026
15 checks passed
@proggeramlug proggeramlug deleted the fix/codegen-argless-builtin-args branch June 17, 2026 08:53
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.

1 participant