Skip to content

Commit ea7b2ec

Browse files
committed
Remove wrong return pointer warning
I'm about to refactor part of the commit phase to use recursion instead of iteration. As part of that change, we will no longer assign the `return` pointer when traversing into a subtree. So I'm disabling the internal warning that fires if the return pointer is not consistent with the parent during the commit phase. I had originally added this warning to help prevent mistakes when traversing the tree iteratively, but since we're intentionally switching to recursion instead, we don't need it.
1 parent 8dcedba commit ea7b2ec

File tree

3 files changed

+24
-103
lines changed

3 files changed

+24
-103
lines changed

packages/react-dom/src/__tests__/ReactWrongReturnPointer-test.js

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -153,51 +153,6 @@ function resolveMostRecentTextCache(text) {
153153

154154
const resolveText = resolveMostRecentTextCache;
155155

156-
// Don't feel too guilty if you have to delete this test.
157-
// @gate dfsEffectsRefactor
158-
// @gate __DEV__
159-
test('warns in DEV if return pointer is inconsistent', async () => {
160-
const {useRef, useLayoutEffect} = React;
161-
162-
let ref = null;
163-
function App({text}) {
164-
ref = useRef(null);
165-
return (
166-
<>
167-
<Sibling text={text} />
168-
<div ref={ref}>{text}</div>
169-
</>
170-
);
171-
}
172-
173-
function Sibling({text}) {
174-
useLayoutEffect(() => {
175-
if (text === 'B') {
176-
// Mutate the return pointer of the div to point to the wrong alternate.
177-
// This simulates the most common type of return pointer inconsistency.
178-
const current = ref.current.fiber;
179-
const workInProgress = current.alternate;
180-
workInProgress.return = current.return;
181-
}
182-
}, [text]);
183-
return null;
184-
}
185-
186-
const root = ReactNoop.createRoot();
187-
await act(async () => {
188-
root.render(<App text="A" />);
189-
});
190-
191-
spyOnDev(console, 'error');
192-
await act(async () => {
193-
root.render(<App text="B" />);
194-
});
195-
expect(console.error.calls.count()).toBe(1);
196-
expect(console.error.calls.argsFor(0)[0]).toMatch(
197-
'Internal React error: Return pointer is inconsistent with parent.',
198-
);
199-
});
200-
201156
// @gate enableCache
202157
// @gate enableSuspenseList
203158
test('regression (#20932): return pointer is correct before entering deleted tree', async () => {

packages/react-reconciler/src/ReactFiberCommitWork.new.js

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ function commitBeforeMutationEffects_begin() {
360360
(fiber.subtreeFlags & BeforeMutationMask) !== NoFlags &&
361361
child !== null
362362
) {
363-
ensureCorrectReturnPointer(child, fiber);
363+
child.return = fiber;
364364
nextEffect = child;
365365
} else {
366366
commitBeforeMutationEffects_complete();
@@ -382,7 +382,7 @@ function commitBeforeMutationEffects_complete() {
382382

383383
const sibling = fiber.sibling;
384384
if (sibling !== null) {
385-
ensureCorrectReturnPointer(sibling, fiber.return);
385+
sibling.return = fiber.return;
386386
nextEffect = sibling;
387387
return;
388388
}
@@ -2185,7 +2185,7 @@ function commitMutationEffects_begin(root: FiberRoot, lanes: Lanes) {
21852185

21862186
const child = fiber.child;
21872187
if ((fiber.subtreeFlags & MutationMask) !== NoFlags && child !== null) {
2188-
ensureCorrectReturnPointer(child, fiber);
2188+
child.return = fiber;
21892189
nextEffect = child;
21902190
} else {
21912191
commitMutationEffects_complete(root, lanes);
@@ -2207,7 +2207,7 @@ function commitMutationEffects_complete(root: FiberRoot, lanes: Lanes) {
22072207

22082208
const sibling = fiber.sibling;
22092209
if (sibling !== null) {
2210-
ensureCorrectReturnPointer(sibling, fiber.return);
2210+
sibling.return = fiber.return;
22112211
nextEffect = sibling;
22122212
return;
22132213
}
@@ -2425,7 +2425,7 @@ function commitLayoutEffects_begin(
24252425
}
24262426

24272427
if ((fiber.subtreeFlags & LayoutMask) !== NoFlags && firstChild !== null) {
2428-
ensureCorrectReturnPointer(firstChild, fiber);
2428+
firstChild.return = fiber;
24292429
nextEffect = firstChild;
24302430
} else {
24312431
commitLayoutMountEffects_complete(subtreeRoot, root, committedLanes);
@@ -2459,7 +2459,7 @@ function commitLayoutMountEffects_complete(
24592459

24602460
const sibling = fiber.sibling;
24612461
if (sibling !== null) {
2462-
ensureCorrectReturnPointer(sibling, fiber.return);
2462+
sibling.return = fiber.return;
24632463
nextEffect = sibling;
24642464
return;
24652465
}
@@ -2628,7 +2628,7 @@ function commitPassiveMountEffects_begin(
26282628
const fiber = nextEffect;
26292629
const firstChild = fiber.child;
26302630
if ((fiber.subtreeFlags & PassiveMask) !== NoFlags && firstChild !== null) {
2631-
ensureCorrectReturnPointer(firstChild, fiber);
2631+
firstChild.return = fiber;
26322632
nextEffect = firstChild;
26332633
} else {
26342634
commitPassiveMountEffects_complete(subtreeRoot, root, committedLanes);
@@ -2662,7 +2662,7 @@ function commitPassiveMountEffects_complete(
26622662

26632663
const sibling = fiber.sibling;
26642664
if (sibling !== null) {
2665-
ensureCorrectReturnPointer(sibling, fiber.return);
2665+
sibling.return = fiber.return;
26662666
nextEffect = sibling;
26672667
return;
26682668
}
@@ -2849,7 +2849,7 @@ function commitPassiveUnmountEffects_begin() {
28492849
}
28502850

28512851
if ((fiber.subtreeFlags & PassiveMask) !== NoFlags && child !== null) {
2852-
ensureCorrectReturnPointer(child, fiber);
2852+
child.return = fiber;
28532853
nextEffect = child;
28542854
} else {
28552855
commitPassiveUnmountEffects_complete();
@@ -2868,7 +2868,7 @@ function commitPassiveUnmountEffects_complete() {
28682868

28692869
const sibling = fiber.sibling;
28702870
if (sibling !== null) {
2871-
ensureCorrectReturnPointer(sibling, fiber.return);
2871+
sibling.return = fiber.return;
28722872
nextEffect = sibling;
28732873
return;
28742874
}
@@ -2923,7 +2923,7 @@ function commitPassiveUnmountEffectsInsideOfDeletedTree_begin(
29232923
// TODO: Only traverse subtree if it has a PassiveStatic flag. (But, if we
29242924
// do this, still need to handle `deletedTreeCleanUpLevel` correctly.)
29252925
if (child !== null) {
2926-
ensureCorrectReturnPointer(child, fiber);
2926+
child.return = fiber;
29272927
nextEffect = child;
29282928
} else {
29292929
commitPassiveUnmountEffectsInsideOfDeletedTree_complete(
@@ -2961,7 +2961,7 @@ function commitPassiveUnmountEffectsInsideOfDeletedTree_complete(
29612961
}
29622962

29632963
if (sibling !== null) {
2964-
ensureCorrectReturnPointer(sibling, returnFiber);
2964+
sibling.return = returnFiber;
29652965
nextEffect = sibling;
29662966
return;
29672967
}
@@ -3039,23 +3039,6 @@ function commitPassiveUnmountInsideDeletedTreeOnFiber(
30393039
}
30403040
}
30413041

3042-
let didWarnWrongReturnPointer = false;
3043-
function ensureCorrectReturnPointer(fiber, expectedReturnFiber) {
3044-
if (__DEV__) {
3045-
if (!didWarnWrongReturnPointer && fiber.return !== expectedReturnFiber) {
3046-
didWarnWrongReturnPointer = true;
3047-
console.error(
3048-
'Internal React error: Return pointer is inconsistent ' +
3049-
'with parent.',
3050-
);
3051-
}
3052-
}
3053-
3054-
// TODO: Remove this assignment once we're confident that it won't break
3055-
// anything, by checking the warning logs for the above invariant
3056-
fiber.return = expectedReturnFiber;
3057-
}
3058-
30593042
// TODO: Reuse reappearLayoutEffects traversal here?
30603043
function invokeLayoutEffectMountInDEV(fiber: Fiber): void {
30613044
if (__DEV__ && enableStrictEffects) {

packages/react-reconciler/src/ReactFiberCommitWork.old.js

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ function commitBeforeMutationEffects_begin() {
360360
(fiber.subtreeFlags & BeforeMutationMask) !== NoFlags &&
361361
child !== null
362362
) {
363-
ensureCorrectReturnPointer(child, fiber);
363+
child.return = fiber;
364364
nextEffect = child;
365365
} else {
366366
commitBeforeMutationEffects_complete();
@@ -382,7 +382,7 @@ function commitBeforeMutationEffects_complete() {
382382

383383
const sibling = fiber.sibling;
384384
if (sibling !== null) {
385-
ensureCorrectReturnPointer(sibling, fiber.return);
385+
sibling.return = fiber.return;
386386
nextEffect = sibling;
387387
return;
388388
}
@@ -2185,7 +2185,7 @@ function commitMutationEffects_begin(root: FiberRoot, lanes: Lanes) {
21852185

21862186
const child = fiber.child;
21872187
if ((fiber.subtreeFlags & MutationMask) !== NoFlags && child !== null) {
2188-
ensureCorrectReturnPointer(child, fiber);
2188+
child.return = fiber;
21892189
nextEffect = child;
21902190
} else {
21912191
commitMutationEffects_complete(root, lanes);
@@ -2207,7 +2207,7 @@ function commitMutationEffects_complete(root: FiberRoot, lanes: Lanes) {
22072207

22082208
const sibling = fiber.sibling;
22092209
if (sibling !== null) {
2210-
ensureCorrectReturnPointer(sibling, fiber.return);
2210+
sibling.return = fiber.return;
22112211
nextEffect = sibling;
22122212
return;
22132213
}
@@ -2425,7 +2425,7 @@ function commitLayoutEffects_begin(
24252425
}
24262426

24272427
if ((fiber.subtreeFlags & LayoutMask) !== NoFlags && firstChild !== null) {
2428-
ensureCorrectReturnPointer(firstChild, fiber);
2428+
firstChild.return = fiber;
24292429
nextEffect = firstChild;
24302430
} else {
24312431
commitLayoutMountEffects_complete(subtreeRoot, root, committedLanes);
@@ -2459,7 +2459,7 @@ function commitLayoutMountEffects_complete(
24592459

24602460
const sibling = fiber.sibling;
24612461
if (sibling !== null) {
2462-
ensureCorrectReturnPointer(sibling, fiber.return);
2462+
sibling.return = fiber.return;
24632463
nextEffect = sibling;
24642464
return;
24652465
}
@@ -2628,7 +2628,7 @@ function commitPassiveMountEffects_begin(
26282628
const fiber = nextEffect;
26292629
const firstChild = fiber.child;
26302630
if ((fiber.subtreeFlags & PassiveMask) !== NoFlags && firstChild !== null) {
2631-
ensureCorrectReturnPointer(firstChild, fiber);
2631+
firstChild.return = fiber;
26322632
nextEffect = firstChild;
26332633
} else {
26342634
commitPassiveMountEffects_complete(subtreeRoot, root, committedLanes);
@@ -2662,7 +2662,7 @@ function commitPassiveMountEffects_complete(
26622662

26632663
const sibling = fiber.sibling;
26642664
if (sibling !== null) {
2665-
ensureCorrectReturnPointer(sibling, fiber.return);
2665+
sibling.return = fiber.return;
26662666
nextEffect = sibling;
26672667
return;
26682668
}
@@ -2849,7 +2849,7 @@ function commitPassiveUnmountEffects_begin() {
28492849
}
28502850

28512851
if ((fiber.subtreeFlags & PassiveMask) !== NoFlags && child !== null) {
2852-
ensureCorrectReturnPointer(child, fiber);
2852+
child.return = fiber;
28532853
nextEffect = child;
28542854
} else {
28552855
commitPassiveUnmountEffects_complete();
@@ -2868,7 +2868,7 @@ function commitPassiveUnmountEffects_complete() {
28682868

28692869
const sibling = fiber.sibling;
28702870
if (sibling !== null) {
2871-
ensureCorrectReturnPointer(sibling, fiber.return);
2871+
sibling.return = fiber.return;
28722872
nextEffect = sibling;
28732873
return;
28742874
}
@@ -2923,7 +2923,7 @@ function commitPassiveUnmountEffectsInsideOfDeletedTree_begin(
29232923
// TODO: Only traverse subtree if it has a PassiveStatic flag. (But, if we
29242924
// do this, still need to handle `deletedTreeCleanUpLevel` correctly.)
29252925
if (child !== null) {
2926-
ensureCorrectReturnPointer(child, fiber);
2926+
child.return = fiber;
29272927
nextEffect = child;
29282928
} else {
29292929
commitPassiveUnmountEffectsInsideOfDeletedTree_complete(
@@ -2961,7 +2961,7 @@ function commitPassiveUnmountEffectsInsideOfDeletedTree_complete(
29612961
}
29622962

29632963
if (sibling !== null) {
2964-
ensureCorrectReturnPointer(sibling, returnFiber);
2964+
sibling.return = returnFiber;
29652965
nextEffect = sibling;
29662966
return;
29672967
}
@@ -3039,23 +3039,6 @@ function commitPassiveUnmountInsideDeletedTreeOnFiber(
30393039
}
30403040
}
30413041

3042-
let didWarnWrongReturnPointer = false;
3043-
function ensureCorrectReturnPointer(fiber, expectedReturnFiber) {
3044-
if (__DEV__) {
3045-
if (!didWarnWrongReturnPointer && fiber.return !== expectedReturnFiber) {
3046-
didWarnWrongReturnPointer = true;
3047-
console.error(
3048-
'Internal React error: Return pointer is inconsistent ' +
3049-
'with parent.',
3050-
);
3051-
}
3052-
}
3053-
3054-
// TODO: Remove this assignment once we're confident that it won't break
3055-
// anything, by checking the warning logs for the above invariant
3056-
fiber.return = expectedReturnFiber;
3057-
}
3058-
30593042
// TODO: Reuse reappearLayoutEffects traversal here?
30603043
function invokeLayoutEffectMountInDEV(fiber: Fiber): void {
30613044
if (__DEV__ && enableStrictEffects) {

0 commit comments

Comments
 (0)