From 6dfd20a72898ce1897f72ee905d6c544e51f2ca3 Mon Sep 17 00:00:00 2001 From: Mofei Zhang Date: Wed, 2 Oct 2024 12:53:57 -0400 Subject: [PATCH] Update (base update) [ghstack-poisoned] --- .../src/HIR/CollectHoistablePropertyLoads.ts | 10 ++++---- .../HIR/CollectOptionalChainDependencies.ts | 16 ++++++------- .../src/Utils/utils.ts | 6 ----- ...equential-optional-chain-nonnull.expect.md | 24 +++++++++++++++++-- ...infer-sequential-optional-chain-nonnull.ts | 10 +++++++- ...equential-optional-chain-nonnull.expect.md | 24 +++++++++++++++++-- ...infer-sequential-optional-chain-nonnull.ts | 10 +++++++- 7 files changed, 74 insertions(+), 26 deletions(-) diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/CollectHoistablePropertyLoads.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/CollectHoistablePropertyLoads.ts index 0aabc856ff..3603416ee6 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/CollectHoistablePropertyLoads.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/CollectHoistablePropertyLoads.ts @@ -279,16 +279,11 @@ function collectNonNullsInBlocks( knownNonNullIdentifiers, ); - nodes.set(block.id, { - block, - assumedNonNullObjects, - }); const maybeOptionalChain = hoistableFromOptionals.get(block.id); if (maybeOptionalChain != null) { assumedNonNullObjects.add( registry.getOrCreateProperty(maybeOptionalChain), ); - continue; } for (const instr of block.instructions) { if (instr.value.kind === 'PropertyLoad') { @@ -329,6 +324,11 @@ function collectNonNullsInBlocks( } } } + + nodes.set(block.id, { + block, + assumedNonNullObjects, + }); } return nodes; } diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/CollectOptionalChainDependencies.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/CollectOptionalChainDependencies.ts index 171455ff48..4532947842 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/CollectOptionalChainDependencies.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/CollectOptionalChainDependencies.ts @@ -1,5 +1,4 @@ import {CompilerError} from '..'; -import {arrayNonNulls} from '../Utils/utils'; import {assertNonNull} from './CollectHoistablePropertyLoads'; import { BlockId, @@ -15,6 +14,7 @@ import { TBasicBlock, OptionalTerminal, HIRFunction, + DependencyPathEntry, } from './HIR'; import {printIdentifier} from './PrintHIR'; @@ -237,20 +237,18 @@ function traverseOptionalBlock( ) { return null; } - const path = maybeTest.instructions.slice(1).map((entry, i) => { - const instrVal = entry.value; - const prevEntry = maybeTest.instructions[i]; + const path: Array = []; + for (let i = 1; i < maybeTest.instructions.length; i++) { + const instrVal = maybeTest.instructions[i].value; + const prevInstr = maybeTest.instructions[i - 1]; if ( instrVal.kind === 'PropertyLoad' && - instrVal.object.identifier.id === prevEntry.lvalue.identifier.id + instrVal.object.identifier.id === prevInstr.lvalue.identifier.id ) { - return {property: instrVal.property, optional: false}; + path.push({property: instrVal.property, optional: false}); } else { return null; } - }); - if (!arrayNonNulls(path)) { - return null; } CompilerError.invariant( maybeTest.terminal.test.identifier.id === diff --git a/compiler/packages/babel-plugin-react-compiler/src/Utils/utils.ts b/compiler/packages/babel-plugin-react-compiler/src/Utils/utils.ts index 0836f84fdb..aa91c48b1b 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Utils/utils.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Utils/utils.ts @@ -139,12 +139,6 @@ export function nonNull, U>( return value != null; } -export function arrayNonNulls, U>( - arr: Array, -): arr is Array { - return arr.every(e => e != null); -} - export function Set_filter( source: ReadonlySet, fn: (arg: T) => boolean, diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-sequential-optional-chain-nonnull.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-sequential-optional-chain-nonnull.expect.md index 9bce09be6c..31e2cadf9f 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-sequential-optional-chain-nonnull.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-sequential-optional-chain-nonnull.expect.md @@ -12,7 +12,15 @@ function useFoo({a}) { export const FIXTURE_ENTRYPOINT = { fn: useFoo, params: [{a: null}], - sequentialRenders: [{a: null}, {a: null}, {a: {}}], + sequentialRenders: [ + {a: null}, + {a: null}, + {a: {}}, + {a: {b: {c: {d: {e: 42}}}}}, + {a: {b: {c: {d: {e: 43}}}}}, + {a: {b: {c: {d: {e: undefined}}}}}, + {a: {b: undefined}}, + ], }; ``` @@ -40,7 +48,15 @@ function useFoo(t0) { export const FIXTURE_ENTRYPOINT = { fn: useFoo, params: [{ a: null }], - sequentialRenders: [{ a: null }, { a: null }, { a: {} }], + sequentialRenders: [ + { a: null }, + { a: null }, + { a: {} }, + { a: { b: { c: { d: { e: 42 } } } } }, + { a: { b: { c: { d: { e: 43 } } } } }, + { a: { b: { c: { d: { e: undefined } } } } }, + { a: { b: undefined } }, + ], }; ``` @@ -48,4 +64,8 @@ export const FIXTURE_ENTRYPOINT = { ### Eval output (kind: ok) [[ (exception in render) TypeError: Cannot read properties of null (reading 'b') ]] [[ (exception in render) TypeError: Cannot read properties of null (reading 'b') ]] +[[ (exception in render) TypeError: Cannot read properties of undefined (reading 'c') ]] +[42,42] +[43,43] +[null,null] [[ (exception in render) TypeError: Cannot read properties of undefined (reading 'c') ]] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-sequential-optional-chain-nonnull.ts b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-sequential-optional-chain-nonnull.ts index 910ecd0a2d..479048085e 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-sequential-optional-chain-nonnull.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-sequential-optional-chain-nonnull.ts @@ -8,5 +8,13 @@ function useFoo({a}) { export const FIXTURE_ENTRYPOINT = { fn: useFoo, params: [{a: null}], - sequentialRenders: [{a: null}, {a: null}, {a: {}}], + sequentialRenders: [ + {a: null}, + {a: null}, + {a: {}}, + {a: {b: {c: {d: {e: 42}}}}}, + {a: {b: {c: {d: {e: 43}}}}}, + {a: {b: {c: {d: {e: undefined}}}}}, + {a: {b: undefined}}, + ], }; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/infer-sequential-optional-chain-nonnull.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/infer-sequential-optional-chain-nonnull.expect.md index b87453e287..757ad2666d 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/infer-sequential-optional-chain-nonnull.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/infer-sequential-optional-chain-nonnull.expect.md @@ -14,7 +14,15 @@ function useFoo({a}) { export const FIXTURE_ENTRYPOINT = { fn: useFoo, params: [{a: null}], - sequentialRenders: [{a: null}, {a: null}, {a: {}}], + sequentialRenders: [ + {a: null}, + {a: null}, + {a: {}}, + {a: {b: {c: {d: {e: 42}}}}}, + {a: {b: {c: {d: {e: 43}}}}}, + {a: {b: {c: {d: {e: undefined}}}}}, + {a: {b: undefined}}, + ], }; ``` @@ -43,7 +51,15 @@ function useFoo(t0) { export const FIXTURE_ENTRYPOINT = { fn: useFoo, params: [{ a: null }], - sequentialRenders: [{ a: null }, { a: null }, { a: {} }], + sequentialRenders: [ + { a: null }, + { a: null }, + { a: {} }, + { a: { b: { c: { d: { e: 42 } } } } }, + { a: { b: { c: { d: { e: 43 } } } } }, + { a: { b: { c: { d: { e: undefined } } } } }, + { a: { b: undefined } }, + ], }; ``` @@ -51,4 +67,8 @@ export const FIXTURE_ENTRYPOINT = { ### Eval output (kind: ok) [[ (exception in render) TypeError: Cannot read properties of null (reading 'b') ]] [[ (exception in render) TypeError: Cannot read properties of null (reading 'b') ]] +[[ (exception in render) TypeError: Cannot read properties of undefined (reading 'c') ]] +[42,42] +[43,43] +[null,null] [[ (exception in render) TypeError: Cannot read properties of undefined (reading 'c') ]] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/infer-sequential-optional-chain-nonnull.ts b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/infer-sequential-optional-chain-nonnull.ts index 81a4a33c61..750e422861 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/infer-sequential-optional-chain-nonnull.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/infer-sequential-optional-chain-nonnull.ts @@ -10,5 +10,13 @@ function useFoo({a}) { export const FIXTURE_ENTRYPOINT = { fn: useFoo, params: [{a: null}], - sequentialRenders: [{a: null}, {a: null}, {a: {}}], + sequentialRenders: [ + {a: null}, + {a: null}, + {a: {}}, + {a: {b: {c: {d: {e: 42}}}}}, + {a: {b: {c: {d: {e: 43}}}}}, + {a: {b: {c: {d: {e: undefined}}}}}, + {a: {b: undefined}}, + ], };