Repository navigation
fix(runtime): #5836 — RegExp constructor edge cases (not-a-constructor, class-range order) - #5858
Conversation
…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%).
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (3)
📝 WalkthroughWalkthroughThis PR adds a TypeError guard rejecting ChangesRegExp construction and validation fixes
Estimated code review effort: 2 (Simple) | ~15 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (4)
crates/perry-runtime/src/object/class_registry/construct.rscrates/perry-runtime/src/regex.rscrates/perry-runtime/src/regex/class_range_validate.rscrates/perry-runtime/src/regex/compile.rs
| if has_out_of_order_double_dash_class_range(pattern_str) { | ||
| throw_regexp_syntax_error(&format!( | ||
| "Invalid regular expression: /{}/: invalid pattern", | ||
| pattern_str | ||
| )); | ||
| } |
There was a problem hiding this comment.
🎯 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' || trueRepository: 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 -nRepository: 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 -nRepository: 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.
|
Addressed in 918ac3f:
|
Summary
newon a RegExp instance now throwsTypeError— a RegExp instance has no[[Construct]]internal method (ECMA-262 22.2.7) — instead of silently returning an empty placeholder object.[a--z]) now throwSyntaxError. JS parses the class contents"a--z"asClassAtom "a", a-range operator, thenClassAtom "-"(a literal hyphen is itself a validClassAtom) — i.e. the rangea..-. Sincea(U+0061) is greater than-(U+002D),CharacterRange(22.2.1.1) requires this to be aSyntaxError, but the Rustregex/fancy-regexcrates parse the doubled hyphen differently and silently accept the pattern — Perry now catches it before delegating.has_out_of_order_double_dash_class_rangelives in its own module (regex/class_range_validate.rs) rather than growingregex/grammar.rs, which is already at the 2000-line size-gate ceiling.Fixes test262
built-ins/RegExp/S15.10.7_A2_T2.jsandbuilt-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 -- --checkbash scripts/check_file_size.shscripts/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 unrelatedproperty-escapes/generatedcases 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
RegExpsyntax validation to reject out-of-order double-hyphen character ranges like[a--z](in legacy mode), returning a consistentSyntaxError.newhandling so attempts to construct from RegExp-derived values no longer incorrectly fall back to object construction, and instead report “is not a constructor.”