Skip to content

Commit 90d251d

Browse files
author
Ralph Küpper
committed
fix(codegen): spread method call dispatches on an imported-value receiver (#7191)
1 parent b9415d7 commit 90d251d

4 files changed

Lines changed: 110 additions & 1 deletion

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
**A spread method call on an imported receiver now dispatches the receiver's method** (#7191).
2+
3+
```ts
4+
import { arr, nums } from "./lib.ts";
5+
arr.map(...[cb]) // was: TypeError
6+
arr.slice(...[1, 3]) // was: [3,1,2] — the whole array
7+
nums.join(...["-"]) // was: TypeError
8+
nums.includes(...[20]) // was: false
9+
```
10+
11+
Four cases, four wrong, and two of them wrong *silently* — which is why it lasted. The spread argument list was arriving as a single array in the builtin's first slot, so `slice` saw one non-numeric argument and `includes` compared against an array.
12+
13+
`Expr::ExternFuncRef` is not one shape. It covers an imported **function**, where a method-apply dispatch would be wrong — which is why the generic `CallSpread` tail skips it — and an imported **value**, where method-apply is exactly what is needed. The skip did not distinguish the two, so a spread call on any imported receiver declined to dispatch. `ctx.imported_vars` already records precisely this distinction ("names of imports that are exported variables, not functions"), so the arm consults it instead of declining the whole family.
14+
15+
The imported-array fold that used to cover part of this was papering over the same gap, and papering badly — `slice` and `includes` were already returning wrong values through it.
16+
17+
`test_gap_spread_call_imported_receiver_7191.ts` asserts both halves, because the risk here is fixing one by breaking the other: the imported-value receivers that were broken (arrays via `map`/`slice`/`join`/`includes`/`indexOf`/`concat`/`at`, including a spread argument that is itself an array so "the args collapsed into one array" cannot pass by accident), and the imported-function/object/class receivers the skip exists to protect (`fn.call(...)`, `fn.apply(...)`, a direct `fn(...)`, an imported object's method, an imported class's static and instance methods). A local receiver is asserted unchanged.
18+
19+
The existing `test_gap_6718_spread_call_native_method` covers inline literals, local receivers and `queueMicrotask` — not imported receivers — which is why this shape had never been under test. It stays green, as do `test_gap_import` and the class/static families.

‎crates/perry-codegen/src/expr/call_spread.rs‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,24 @@ pub(crate) fn lower(ctx: &mut FnCtx<'_>, expr: &Expr) -> Result<String> {
278278
object, property, ..
279279
} = callee.as_ref()
280280
{
281+
// #7191: `ExternFuncRef` is not one shape. It covers an
282+
// imported FUNCTION (`import { fn }` — a method-apply dispatch
283+
// on it would be wrong, which is why this skip exists) and an
284+
// imported VALUE (`import { arr }; arr.map(...args)` — where
285+
// method-apply is exactly right). Skipping both meant a spread
286+
// method call on any imported receiver never dispatched the
287+
// receiver's method: `arr.slice(...[1,3])` returned the whole
288+
// array and `nums.includes(...[20])` returned false, because
289+
// the spread list arrived as a single array in the builtin's
290+
// first slot. `ctx.imported_vars` is the existing discriminator
291+
// — "names of imports that are exported variables, not
292+
// functions" — so consult it rather than declining the family.
281293
let mut skip = matches!(
282294
object.as_ref(),
283-
Expr::GlobalGet(_) | Expr::NativeModuleRef(_) | Expr::ExternFuncRef { .. }
295+
Expr::GlobalGet(_) | Expr::NativeModuleRef(_)
296+
) || matches!(
297+
object.as_ref(),
298+
Expr::ExternFuncRef { name, .. } if !ctx.imported_vars.contains(name.as_str())
284299
);
285300
// `recv.prop(...args)` where `prop` is an instance ACCESSOR
286301
// (`get prop()`) is NOT a method call: it must READ the accessor
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Support module for test_gap_spread_call_imported_receiver_7191.ts (#7191).
2+
// The receivers must be IMPORTED — that is the whole subject of the test.
3+
export const arr = [3, 1, 2];
4+
export const nums: number[] = [10, 20, 30];
5+
export function fn(a: number, b: number) {
6+
return a + b;
7+
}
8+
export const obj = {
9+
m(x: number) {
10+
return x * 3;
11+
},
12+
v: 7,
13+
};
14+
export class K {
15+
static s(x: number) {
16+
return x + 100;
17+
}
18+
m(x: number) {
19+
return x - 1;
20+
}
21+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Gap: a spread method call whose RECEIVER is an imported binding (#7191).
2+
//
3+
// `arr.map(...[cb])` where `arr` comes from another module was wrong four ways
4+
// out of four — and two of them were silently wrong rather than throwing, which
5+
// is why it survived: `arr.slice(...[1,3])` returned the whole array and
6+
// `nums.includes(...[20])` returned false, because the spread argument list
7+
// arrived as a single array sitting in the builtin's first slot.
8+
//
9+
// The cause is that `Expr::ExternFuncRef` is not one shape. It is both an
10+
// imported FUNCTION — where a method-apply dispatch would be wrong, which is
11+
// why the generic `CallSpread` tail skips it — and an imported VALUE, where
12+
// method-apply is exactly what is needed. The skip did not distinguish them, so
13+
// a spread call on any imported receiver never dispatched the receiver's
14+
// method. `ctx.imported_vars` already records which imports are variables
15+
// rather than functions.
16+
//
17+
// `test_gap_6718_spread_call_native_method` covers inline literals, local
18+
// receivers and `queueMicrotask` — not imported receivers — so this shape had
19+
// never been under test. Both halves are asserted here: the imported-value
20+
// receivers that were broken, and the imported-function/class/object receivers
21+
// the skip exists to protect, which must keep working.
22+
23+
import { arr, nums, fn, obj, K } from "./spread_call_imported_receiver_7191_helper.ts";
24+
25+
const cb = (x: number) => x * 2;
26+
27+
// The four cases from the report, in their original order.
28+
console.log(JSON.stringify(arr.map(...([cb] as [any]))));
29+
console.log(JSON.stringify(arr.slice(...([1, 3] as [any, any]))));
30+
console.log(JSON.stringify(nums.join(...(["-"] as [any]))));
31+
console.log(JSON.stringify(nums.includes(...([20] as [any]))));
32+
33+
// More imported-array receivers, including one whose spread arg is itself an
34+
// array (so "the args became one array" cannot pass by accident).
35+
console.log("indexOf:", arr.indexOf(...([2] as [any])));
36+
console.log("concat:", JSON.stringify(arr.concat(...([[9]] as [any]))));
37+
console.log("at:", nums.at(...([-1] as [any])));
38+
39+
// The shapes the skip protects: an imported FUNCTION reached through
40+
// `.call` / `.apply`, and a direct spread call on it.
41+
console.log("fn.call:", fn.call(...([null, 1, 2] as [any, any, any])));
42+
console.log("fn.apply:", fn.apply(...([null, [3, 4]] as [any, any])));
43+
console.log("fn direct:", fn(...([5, 6] as [any, any])));
44+
45+
// An imported object's method, and an imported class's static and instance
46+
// methods — all reached with a spread argument list.
47+
console.log("obj.m:", obj.m(...([2] as [any])));
48+
console.log("K.s:", K.s(...([1] as [any])));
49+
console.log("K#m:", new K().m(...([10] as [any])));
50+
51+
// A local receiver must be unaffected by any of this.
52+
const local = [3, 1, 2];
53+
console.log("local-map:", JSON.stringify(local.map(...([cb] as [any]))));
54+
console.log("local-slice:", JSON.stringify(local.slice(...([1, 3] as [any, any]))));

0 commit comments

Comments
 (0)