Skip to content

fix(runtime): #5836 — RegExp constructor edge cases (not-a-constructor, class-range order) - #5858

Merged
proggeramlug merged 2 commits into
mainfrom
fix/5836-regexp-worklist
Jul 2, 2026
Merged

proggeramlug merged 2 commits into
mainfrom
fix/5836-regexp-worklist

Conversation

@proggeramlug

@proggeramlug proggeramlug commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • new on a RegExp instance now throws TypeError — a RegExp instance has no [[Construct]] internal method (ECMA-262 22.2.7) — instead of silently returning an empty placeholder object.
  • Character classes made out-of-order by a doubled hyphen (e.g. [a--z]) now throw SyntaxError. JS parses the class contents "a--z" as ClassAtom "a", a - range operator, then ClassAtom "-" (a literal hyphen is itself a valid ClassAtom) — i.e. the range a..-. Since a (U+0061) is greater than - (U+002D), CharacterRange (22.2.1.1) requires this to be a SyntaxError, but the Rust regex/fancy-regex crates parse the doubled hyphen differently and silently accept the pattern — Perry now catches it before delegating.
  • New has_out_of_order_double_dash_class_range lives in its own module (regex/class_range_validate.rs) rather than growing regex/grammar.rs, which is already at the 2000-line size-gate ceiling.

Fixes test262 built-ins/RegExp/S15.10.7_A2_T2.js and built-ins/RegExp/S15.10.4.1_A9_T3.js — 2 of the 91 cases tracked in #5836.

Test plan

  • cargo test --release -p perry-runtime --lib regex — 34 passed (incl. 3 new unit tests)
  • cargo fmt --all -- --check
  • bash scripts/check_file_size.sh
  • Manually verified both fixed test262 cases pass against a fresh build
  • scripts/test262_subset.py --root vendor/test262 --dir built-ins/RegExp --jobs 8 — before/after diff shows exactly the 2 targeted cases now passing with zero regressions (the run flagged 2 unrelated property-escapes/generated cases as newly failing, but both pass on an isolated re-run with more time — a transient compile-timeout flake from a heavily loaded shared CI-adjacent box, not a regression)

Refs #5836.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved RegExp syntax validation to reject out-of-order double-hyphen character ranges like [a--z] (in legacy mode), returning a consistent SyntaxError.
    • Fixed new handling so attempts to construct from RegExp-derived values no longer incorrectly fall back to object construction, and instead report “is not a constructor.”
  • Tests
    • Added coverage for the new character-range validation behavior.

…r, class-range order)

`new` on a RegExp instance now throws TypeError (RegExp has no [[Construct]]
internal method, ECMA-262 22.2.7) instead of silently producing an empty
placeholder object.

`[a--z]`-style character classes made out-of-order by a doubled hyphen now
throw SyntaxError: JS parses "a--z" as ClassAtom `a`, a `-` range operator,
then ClassAtom `-` (a literal hyphen is itself a valid ClassAtom) — i.e. the
range a..'-'. Since 'a' > '-', CharacterRange requires this to be rejected,
but the Rust `regex`/`fancy-regex` crates parse the doubled hyphen
differently and silently accept the pattern.

Fixes test262 built-ins/RegExp/S15.10.7_A2_T2.js and S15.10.4.1_A9_T3.js.
Verified zero regressions on the full built-ins/RegExp test262 subset
(1082 pass, parity unchanged at 92%).
@coderabbitai

coderabbitai Bot commented Jul 1, 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: 074b0a96-9da8-4f9a-91b7-2823cd3d3ebd

📥 Commits

Reviewing files that changed from the base of the PR and between 748f3d4 and 918ac3f.

📒 Files selected for processing (3)
  • crates/perry-runtime/src/regex.rs
  • crates/perry-runtime/src/regex/class_range_validate.rs
  • crates/perry-runtime/src/regex/compile.rs
🚧 Files skipped from review as they are similar to previous changes (3)
  • crates/perry-runtime/src/regex/compile.rs
  • crates/perry-runtime/src/regex/class_range_validate.rs
  • crates/perry-runtime/src/regex.rs

📝 Walkthrough

Walkthrough

This PR adds a TypeError guard rejecting new on registered regex instances, and introduces a validator for out-of-order doubled-hyphen character-class ranges that is enforced during js_regexp_new and js_regexp_compile_value.

Changes

RegExp construction and validation fixes

Layer / File(s) Summary
Reject new on regex instances
crates/perry-runtime/src/object/class_registry/construct.rs
js_new_function_construct throws TypeError when func_value is a registered regex pointer, instead of falling through to empty-object construction.
Doubled-hyphen class range validator
crates/perry-runtime/src/regex/class_range_validate.rs
New module adds has_out_of_order_double_dash_class_range, scanning patterns for descending doubled-hyphen ranges while tracking class boundaries and skipping escapes; includes unit tests.
Wire validator into js_regexp_new
crates/perry-runtime/src/regex.rs
Adds the internal module declaration, imports the helper, and rejects invalid patterns on cache-miss validation when v is absent.
Wire validator into js_regexp_compile_value
crates/perry-runtime/src/regex/compile.rs
Imports the helper and adds the same invalid-pattern SyntaxError path during Annex B compile validation.

Estimated code review effort: 2 (Simple) | ~15 minutes

Possibly related PRs

  • PerryTS/perry#5777: Both PRs modify crates/perry-runtime/src/regex.rs validation behavior around REGEX_CACHE hits and misses.
  • PerryTS/perry#5804: Both PRs adjust the new-with-RegExp “not a constructor” TypeError path.
  • PerryTS/perry#5808: Both PRs change js_regexp_new validation flow around REGEX_CACHE misses.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main runtime fix: RegExp constructor edge cases and doubled-hyphen class-range validation.
Description check ✅ Passed The description covers the summary, tests, and issue reference, though it skips the template's explicit Changes and Checklist sections.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ 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/5836-regexp-worklist

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.

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

Actionable comments posted: 3

🤖 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.

Inline comments:
In `@crates/perry-runtime/src/regex.rs`:
- Around line 594-599: The doubled-dash validator in the regex pattern check is
running unconditionally and incorrectly rejects valid unicodeSets `/v` patterns.
Update the logic around has_out_of_order_double_dash_class_range in regex.rs so
it only throws the syntax error when flags_str does not contain v, allowing the
parser to handle set subtraction for `/v` regexes.

In `@crates/perry-runtime/src/regex/class_range_validate.rs`:
- Around line 21-33: Update class_range_validate’s class-scanning logic to be
position-aware inside bracket expressions instead of blindly toggling on every
`[`/`]` and skipping all `^` characters. In the validator loop, use the existing
class-state handling around the regex scan to treat `]` as a terminator only
when it is not the first class character, and treat `^` as special only at the
start of a class so inputs like []a--z] and [x^--z] still reach the double-dash
check. Keep the range-detection branch in class_range_validate aligned with this
adjusted state tracking so malformed class contents cannot bypass the `--`
validation.

In `@crates/perry-runtime/src/regex/compile.rs`:
- Around line 96-101: The double-dash validator is being applied to all regex
compilations in compile_regex, which can incorrectly reject valid `/v` class-set
syntax. Update `compile_regex` to skip
has_out_of_order_double_dash_class_range() when the pattern is using `/v`, and
apply the same gate in js_regexp_new so both entry points treat `/v` patterns
consistently. Keep the check only for non-`/v` regexes and preserve the existing
SyntaxError path for invalid legacy patterns.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 4ac9a99c-8770-4490-a474-90fb51a61ee7

📥 Commits

Reviewing files that changed from the base of the PR and between b566ea7 and 748f3d4.

📒 Files selected for processing (4)
  • crates/perry-runtime/src/object/class_registry/construct.rs
  • crates/perry-runtime/src/regex.rs
  • crates/perry-runtime/src/regex/class_range_validate.rs
  • crates/perry-runtime/src/regex/compile.rs

Comment thread crates/perry-runtime/src/regex.rs Resolved
Comment on lines +96 to +101
if has_out_of_order_double_dash_class_range(pattern_str) {
throw_regexp_syntax_error(&format!(
"Invalid regular expression: /{}/: invalid pattern",
pattern_str
));
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '\n== outline compile.rs ==\n'
ast-grep outline crates/perry-runtime/src/regex/compile.rs --view expanded || true

printf '\n== outline for regex constructor files ==\n'
fd -a 'regex' crates/perry-runtime/src | sed -n '1,120p' || true

printf '\n== search for relevant symbols ==\n'
rg -n "has_out_of_order_double_dash_class_range|js_regexp_new|flags_str.contains\\('v'\\)|compile\\(" crates/perry-runtime/src/regex crates/perry-runtime/src -g '*.rs' || true

Repository: PerryTS/perry

Length of output: 12034


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '\n== compile.rs relevant slice ==\n'
sed -n '70,115p' crates/perry-runtime/src/regex/compile.rs | cat -n

printf '\n== regex.rs constructor slice ==\n'
sed -n '560,610p' crates/perry-runtime/src/regex.rs | cat -n

Repository: PerryTS/perry

Length of output: 5584


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '\n== validator implementation ==\n'
sed -n '1,220p' crates/perry-runtime/src/regex/class_range_validate.rs | cat -n

printf '\n== search for double-dash / v tests or comments ==\n'
rg -n "double dash|class-set|\\[a--z\\]|/v|flags_str.contains\\('v'\\)" crates/perry-runtime/src/regex crates/perry-runtime/src -g '*.rs'

Repository: PerryTS/perry

Length of output: 12756


🏁 Script executed:

#!/bin/bash
set -euo pipefail
sed -n '590,625p' crates/perry-runtime/src/regex.rs | cat -n

Repository: PerryTS/perry

Length of output: 2442


Gate the double-dash validator for /v patterns here, and in js_regexp_new too.
has_out_of_order_double_dash_class_range() still runs on every compile, so valid /v class-set syntax can be rejected as SyntaxError.

🤖 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-runtime/src/regex/compile.rs` around lines 96 - 101, The
double-dash validator is being applied to all regex compilations in
compile_regex, which can incorrectly reject valid `/v` class-set syntax. Update
`compile_regex` to skip has_out_of_order_double_dash_class_range() when the
pattern is using `/v`, and apply the same gate in js_regexp_new so both entry
points treat `/v` patterns consistently. Keep the check only for non-`/v`
regexes and preserve the existing SyntaxError path for invalid legacy patterns.

- Skip the doubled-hyphen class-range check under the `v` flag: `--` is
  the real ClassSetExpression subtraction operator there (UTS #51), so
  `[a--z]` is valid set-difference syntax, not a malformed range.
  Verified against Node: `/[a--z]/v` doesn't throw, `/[a--z]/u` and
  `/[a--z]/` do.
- Fix `^` handling to be position-aware: it's only a negation marker as
  the class's first ClassAtom (`[^a]`); anywhere else (`[x^--z]`) it's
  an ordinary ClassAtom that must still be range-order-checked (`^` is
  U+005E, greater than `-`, so Node throws for `[x^--z]` too).

The `]`-position claim in the same review comment doesn't hold up:
verified against both Node (`new RegExp("[]a-z]")` matches nothing —
consistent with `[]` closing as an empty, never-matching class, not a
POSIX-style literal `]`) and the existing `js_regex_to_rust` translator,
which already treats a `]` immediately after `[` as closing an empty
class. Left unchanged.
@proggeramlug

Copy link
Copy Markdown
Contributor Author

Addressed in 918ac3f:

  1. /v flag gate — confirmed. Fixed: the check now skips whenever flags_str.contains('v'), since -- is the real ClassSetExpression subtraction operator under UTS perf: inline shape-stable PropertyGet/Set for JSON-parsed records + object literals #51.
  2. ^-position bug — confirmed. Fixed: ^ is now only treated as a non-atom negation marker when it's the class's first ClassAtom; [x^--z] is correctly rejected again.
  3. ]-position claim — verified against Node and doesn't hold up: new RegExp("[]a-z]") matches nothing for any input (including the literal substring "a-z]"), which is only consistent with [] closing immediately as an empty, never-matching class — not with ] being a POSIX-style literal first member. This also matches the existing js_regex_to_rust translator's handling of []. Left unchanged.

@proggeramlug
proggeramlug merged commit 1ae4797 into main Jul 2, 2026
15 checks passed
@proggeramlug
proggeramlug deleted the fix/5836-regexp-worklist branch July 2, 2026 05:03
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