From 9d97015236b8f19bbf313e98065aaeb4d0322cbc Mon Sep 17 00:00:00 2001 From: mofeiZ <34200447+mofeiZ@users.noreply.github.com> Date: Wed, 5 Apr 2023 12:53:39 -0400 Subject: [PATCH] [globals] Remove global shape for Array.from Type inference currently assumes that a `FunctionSignature`'s effects have no false positives. If a `mutate` effect is observed on a read-only place, Forget currently assumes this is an user error and [throws](https://github.com/facebook/react-forget/blob/207595e04e2be08b8f62bf21dac9d846b9651e43/forget/src/Inference/InferReferenceEffects.ts#L275-L281). Array.from is polymorphic -- its effects are dependent on the type of its parameters --- compiler/forget/src/HIR/Globals.ts | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/compiler/forget/src/HIR/Globals.ts b/compiler/forget/src/HIR/Globals.ts index 8770cc0ecb..a886a041d9 100644 --- a/compiler/forget/src/HIR/Globals.ts +++ b/compiler/forget/src/HIR/Globals.ts @@ -87,16 +87,15 @@ const TYPED_GLOBALS: Array<[string, BuiltInType]> = [ calleeEffect: Effect.Read, }), ], - [ - "from", - // Array.from(arrayLike, optionalFn, optionalThis) - addFunction(DEFAULT_SHAPES, [], { - positionalParams: [Effect.Mutate], - restParam: Effect.Read, - returnType: { kind: "Object", shapeId: BuiltInArrayId }, - calleeEffect: Effect.Read, - }), - ], + // https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.from + // Array.from(arrayLike, optionalFn, optionalThis) not added because + // the Effect of `arrayLike` is polymorphic i.e. + // - Effect.read if + // - it does not have an @iterator property and is array-like + // (i.e. has a length property) + /// - it is an iterable object whose iterator does not mutate itself + // - Effect.mutate if it is a self-mutative iterator (e.g. a generator + // function) [ "of", // Array.of(element0, ..., elementN)