-
-
Notifications
You must be signed in to change notification settings - Fork 158
fix(hir): boxed primitive wrappers are not arrays — stop the array fold from hijacking their methods (#5902) #7470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+90
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| fix(hir): a boxed primitive wrapper receiver is not an array, so the array fast path no longer hijacks its methods (#5902). `try_local_array_methods` gates the `Expr::Array*` fold on `is_known_not_string`, which reads "definitely not a string" as positive evidence of *array*-ness — so a local typed `Named("Object")` / `Named("Number")` / `Named("Boolean")` (`new Object(true)`, `new Number(1)`, `new Boolean`) walked straight in and lowered `inst.indexOf(x)` to `Expr::ArrayIndexOf`, which reads the wrapper's `ObjectHeader` as an `ArrayHeader` and answers `-1` — so a borrowed `String.prototype.indexOf` stored on the receiver (or on `Number.prototype`) never ran. `Named("String")` was already excluded one arm earlier by `is_boxed_string_wrapper`, which routes a boxed String to the `ToString`-coercing *string* dispatch; the new `receiver_is_non_array_builtin_wrapper` predicate extends that reasoning to the remaining boxed wrappers and to `Object`, routing them to the generic runtime dispatch, which resolves the own/inherited property first and still reaches the array engine for a genuine array. Real arrays are structurally unaffected — `Expr::New { class_name: "Array" }` already infers `Type::Array`, not `Type::Named("Array")` — and `--print-hir` confirms the `Array*` folds are still emitted, so the optimization is preserved rather than disabled. test262 `built-ins/String`: pass 916→922, runtime-fail 16→10 (6 flips, zero regressions), parity 98.3%→98.9%; the flipped cases are `indexOf/S15.5.4.7_{A1_T1,A1_T2,A4_T4}` and `lastIndexOf/S15.5.4.8_{A1_T1,A1_T2,A4_T4}`. New per-PR-visible coverage: three `perry-hir` unit tests on the extracted predicate, one of which asserts `Named("String")` is deliberately *not* claimed here. Still open in this cluster and left for its own PR: `concat/S15.5.4.6_A4_T1` has a different root — `classify_own_slot` (`perry-runtime/src/array/generic.rs`) labels *any* borrowed builtin an *Array* builtin, so `obj.concat = String.prototype.concat` runs the array engine. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
Handle union-typed wrapper receivers before enabling the array fast path.
receiver_is_non_array_builtin_wrappermatches only a top-levelType::Named. Iflookup_local_typereturns aType::UnioncontainingType::Named("Object"),Type::Named("Number"),Type::Named("Boolean"),Type::Named("Symbol"), orType::Named("BigInt"),is_union_with_stringremains false andis_known_not_stringbecomes true. The gate can then emit anExpr::Array*operation for a possible wrapper receiver and recreate theObjectHeader/ArrayHeadermismatch.Make the predicate inspect union variants and decline the fold when any non-array wrapper is possible. Add a union case to the unit tests.
Proposed fix
fn receiver_is_non_array_builtin_wrapper(recv_ty: Option<&Type>) -> bool { - matches!( - recv_ty, - Some(Type::Named(n)) - if matches!( - n.as_str(), - "Object" | "Number" | "Boolean" | "Symbol" | "BigInt" - ) - ) + match recv_ty { + Some(Type::Named(n)) => matches!( + n.as_str(), + "Object" | "Number" | "Boolean" | "Symbol" | "BigInt" + ), + Some(Type::Union(variants)) => variants.iter().any(|ty| { + receiver_is_non_array_builtin_wrapper(Some(ty)) + }), + _ => false, + } } fn real_array_receivers_keep_the_fold() { + assert!(receiver_is_non_array_builtin_wrapper(Some(&Type::Union(vec![ + Type::Named("Object".to_string()), + Type::Named("Number".to_string()), + ]))));Also applies to: 141-141, 280-281, 1131-1174
🤖 Prompt for AI Agents