From a1c7a26fc62c4ef0fc2a5845eaba69b12ee8824c Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Mon, 11 Dec 2023 11:34:29 -0800 Subject: [PATCH] Put type-annotation-based inference behind feature flag --- .../src/HIR/Environment.ts | 8 ++++++ .../src/TypeInference/InferTypes.ts | 28 +++++++++++++------ .../todo_type-annotations-props.expect.md | 3 +- .../todo_type-annotations-props.ts | 1 + .../type-annotation-as-array.expect.md | 3 +- .../type-annotation-as-array.ts | 1 + .../type-annotation-as-array_.flow.expect.md | 2 +- .../type-annotation-as-array_.flow.js | 2 +- .../type-annotation-as-number.expect.md | 2 ++ .../type-annotation-as-number.ts | 1 + .../type-annotation-as-number_.flow.expect.md | 2 +- .../type-annotation-as-number_.flow.js | 2 +- .../type-annotation-var-array.expect.md | 3 +- .../type-annotation-var-array.ts | 1 + .../type-annotation-var-array_.flow.expect.md | 2 +- .../type-annotation-var-array_.flow.js | 2 +- 16 files changed, 46 insertions(+), 17 deletions(-) diff --git a/compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts b/compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts index aa3c523985..af3d326124 100644 --- a/compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts +++ b/compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts @@ -114,6 +114,14 @@ const EnvironmentConfigSchema = z.object({ */ memoizeJsxElements: z.boolean().default(true), + /** + * Enable use of type annotations in the source to drive type inference. By default + * Forget attemps to infer types using only information that is guaranteed correct + * given the source, and does not trust user-supplied type annotations. This mode + * enables trusting user type annotations. + */ + enableUseTypeAnnotations: z.boolean().default(false), + /* * Enable validation of hooks to partially check that the component honors the rules of hooks. * When disabled, the component is assumed to follow the rules (though the Babel plugin looks diff --git a/compiler/packages/babel-plugin-react-forget/src/TypeInference/InferTypes.ts b/compiler/packages/babel-plugin-react-forget/src/TypeInference/InferTypes.ts index 0864d45cbe..9b677e4e95 100644 --- a/compiler/packages/babel-plugin-react-forget/src/TypeInference/InferTypes.ts +++ b/compiler/packages/babel-plugin-react-forget/src/TypeInference/InferTypes.ts @@ -137,12 +137,20 @@ function* generateInstructionTypes( } case "StoreLocal": { - yield equation( - value.lvalue.place.identifier.type, - value.value.identifier.type - ); - yield equation(value.type, value.lvalue.place.identifier.type); - yield equation(left, value.type); + if (env.config.enableUseTypeAnnotations) { + yield equation( + value.lvalue.place.identifier.type, + value.value.identifier.type + ); + yield equation(value.type, value.lvalue.place.identifier.type); + yield equation(left, value.type); + } else { + yield equation(left, value.value.identifier.type); + yield equation( + value.lvalue.place.identifier.type, + value.value.identifier.type + ); + } break; } @@ -263,8 +271,12 @@ function* generateInstructionTypes( } case "TypeCastExpression": { - yield equation(value.type, value.value.identifier.type); - yield equation(left, value.type); + if (env.config.enableUseTypeAnnotations) { + yield equation(value.type, value.value.identifier.type); + yield equation(left, value.type); + } else { + yield equation(left, value.value.identifier.type); + } break; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/todo_type-annotations-props.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/todo_type-annotations-props.expect.md index 0077afb722..8bd9db8a26 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/todo_type-annotations-props.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/todo_type-annotations-props.expect.md @@ -2,6 +2,7 @@ ## Input ```javascript +// @enableUseTypeAnnotations function useArray(items: Array) { // With type information we know that the callback cannot escape // and does not need to be memoized, only the result needs to be @@ -19,7 +20,7 @@ export const FIXTURE_ENTRYPOINT = { ## Code ```javascript -import { unstable_useMemoCache as useMemoCache } from "react"; +import { unstable_useMemoCache as useMemoCache } from "react"; // @enableUseTypeAnnotations function useArray(items) { const $ = useMemoCache(3); let t1; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/todo_type-annotations-props.ts b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/todo_type-annotations-props.ts index c4f73b774f..1f0d177c60 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/todo_type-annotations-props.ts +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/todo_type-annotations-props.ts @@ -1,3 +1,4 @@ +// @enableUseTypeAnnotations function useArray(items: Array) { // With type information we know that the callback cannot escape // and does not need to be memoized, only the result needs to be diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-array.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-array.expect.md index d871aa4856..713725507f 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-array.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-array.expect.md @@ -2,6 +2,7 @@ ## Input ```javascript +// @enableUseTypeAnnotations function Component(props: { id: number }) { const x = makeArray(props.id) as number[]; const y = x.at(0); @@ -22,7 +23,7 @@ export const FIXTURE_ENTRYPOINT = { ## Code ```javascript -import { unstable_useMemoCache as useMemoCache } from "react"; +import { unstable_useMemoCache as useMemoCache } from "react"; // @enableUseTypeAnnotations function Component(props) { const $ = useMemoCache(4); let t0; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-array.ts b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-array.ts index ec4b6f7904..6284ea3bd9 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-array.ts +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-array.ts @@ -1,3 +1,4 @@ +// @enableUseTypeAnnotations function Component(props: { id: number }) { const x = makeArray(props.id) as number[]; const y = x.at(0); diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-array_.flow.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-array_.flow.expect.md index 3175f6d8d7..7ecbe7bca5 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-array_.flow.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-array_.flow.expect.md @@ -2,7 +2,7 @@ ## Input ```javascript -// @flow +// @flow @enableUseTypeAnnotations import { identity, makeArray } from "shared-runtime"; function Component(props: { id: number }) { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-array_.flow.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-array_.flow.js index 1efc8b7bbe..fc892f9f64 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-array_.flow.js +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-array_.flow.js @@ -1,4 +1,4 @@ -// @flow +// @flow @enableUseTypeAnnotations import { identity, makeArray } from "shared-runtime"; function Component(props: { id: number }) { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-number.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-number.expect.md index f4e07ae4f1..d3831b9803 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-number.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-number.expect.md @@ -2,6 +2,7 @@ ## Input ```javascript +// @enableUseTypeAnnotations import { identity } from "shared-runtime"; function Component(props: { id: number }) { @@ -20,6 +21,7 @@ export const FIXTURE_ENTRYPOINT = { ## Code ```javascript +// @enableUseTypeAnnotations import { identity } from "shared-runtime"; function Component(props) { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-number.ts b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-number.ts index 0237789afc..f22fa8c7ca 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-number.ts +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-number.ts @@ -1,3 +1,4 @@ +// @enableUseTypeAnnotations import { identity } from "shared-runtime"; function Component(props: { id: number }) { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-number_.flow.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-number_.flow.expect.md index 7b5a821764..ae10044507 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-number_.flow.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-number_.flow.expect.md @@ -2,7 +2,7 @@ ## Input ```javascript -// @flow +// @flow @enableUseTypeAnnotations import { identity } from "shared-runtime"; function Component(props: {id: number}) { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-number_.flow.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-number_.flow.js index ab31be6cde..1eb336c60b 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-number_.flow.js +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-as-number_.flow.js @@ -1,4 +1,4 @@ -// @flow +// @flow @enableUseTypeAnnotations import { identity } from "shared-runtime"; function Component(props: {id: number}) { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-var-array.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-var-array.expect.md index e000afe888..ca0ee323cf 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-var-array.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-var-array.expect.md @@ -2,6 +2,7 @@ ## Input ```javascript +// @enableUseTypeAnnotations function Component(props: { id: number }) { const x: number[] = makeArray(props.id); const y = x.at(0); @@ -22,7 +23,7 @@ export const FIXTURE_ENTRYPOINT = { ## Code ```javascript -import { unstable_useMemoCache as useMemoCache } from "react"; +import { unstable_useMemoCache as useMemoCache } from "react"; // @enableUseTypeAnnotations function Component(props) { const $ = useMemoCache(4); let t0; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-var-array.ts b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-var-array.ts index 936a280352..efc7eb7be0 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-var-array.ts +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-var-array.ts @@ -1,3 +1,4 @@ +// @enableUseTypeAnnotations function Component(props: { id: number }) { const x: number[] = makeArray(props.id); const y = x.at(0); diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-var-array_.flow.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-var-array_.flow.expect.md index 84f63011a1..ea032442fd 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-var-array_.flow.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-var-array_.flow.expect.md @@ -2,7 +2,7 @@ ## Input ```javascript -// @flow +// @flow @enableUseTypeAnnotations import { identity } from "shared-runtime"; function Component(props: { id: number }) { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-var-array_.flow.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-var-array_.flow.js index 044f9fb379..6651181f71 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-var-array_.flow.js +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/type-annotations/type-annotation-var-array_.flow.js @@ -1,4 +1,4 @@ -// @flow +// @flow @enableUseTypeAnnotations import { identity } from "shared-runtime"; function Component(props: { id: number }) {