Skip to content

Commit 9c1760b

Browse files
committed
[compiler] rewrite invariant in InferReferenceEffects
Summary: Test Plan: Reviewers: Subscribers: Tasks: Tags:
1 parent fc8a898 commit 9c1760b

File tree

2 files changed

+48
-33
lines changed

2 files changed

+48
-33
lines changed

compiler/packages/babel-plugin-react-compiler/src/Inference/InferFunctionEffects.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,15 @@ function inferOperandEffect(state: State, place: Place): null | FunctionEffect {
4141
if (isRefOrRefValue(place.identifier)) {
4242
break;
4343
} else if (value.kind === ValueKind.Context) {
44+
CompilerError.invariant(value.context.size > 0, {
45+
reason: 'Expected context value places',
46+
loc: place.loc,
47+
});
4448
return {
4549
kind: 'ContextMutation',
4650
loc: place.loc,
4751
effect: place.effect,
48-
places: value.context.size === 0 ? new Set([place]) : value.context,
52+
places: value.context,
4953
};
5054
} else if (
5155
value.kind !== ValueKind.Mutable &&

compiler/packages/babel-plugin-react-compiler/src/Inference/InferReferenceEffects.ts

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
FunctionEffect,
1717
GeneratedSource,
1818
HIRFunction,
19+
Identifier,
1920
IdentifierId,
2021
InstructionKind,
2122
InstructionValue,
@@ -857,17 +858,19 @@ function inferBlock(
857858
break;
858859
}
859860
case 'ArrayExpression': {
860-
const valueKind: AbstractValue = hasContextRefOperand(state, instrValue)
861-
? {
862-
kind: ValueKind.Context,
863-
reason: new Set([ValueReason.Other]),
864-
context: new Set(),
865-
}
866-
: {
867-
kind: ValueKind.Mutable,
868-
reason: new Set([ValueReason.Other]),
869-
context: new Set(),
870-
};
861+
const contextRefOperands = getContextRefOperand(state, instrValue);
862+
const valueKind: AbstractValue =
863+
contextRefOperands.length > 0
864+
? {
865+
kind: ValueKind.Context,
866+
reason: new Set([ValueReason.Other]),
867+
context: new Set(contextRefOperands),
868+
}
869+
: {
870+
kind: ValueKind.Mutable,
871+
reason: new Set([ValueReason.Other]),
872+
context: new Set(),
873+
};
871874
continuation = {
872875
kind: 'initialize',
873876
valueKind,
@@ -918,17 +921,19 @@ function inferBlock(
918921
break;
919922
}
920923
case 'ObjectExpression': {
921-
const valueKind: AbstractValue = hasContextRefOperand(state, instrValue)
922-
? {
923-
kind: ValueKind.Context,
924-
reason: new Set([ValueReason.Other]),
925-
context: new Set(),
926-
}
927-
: {
928-
kind: ValueKind.Mutable,
929-
reason: new Set([ValueReason.Other]),
930-
context: new Set(),
931-
};
924+
const contextRefOperands = getContextRefOperand(state, instrValue);
925+
const valueKind: AbstractValue =
926+
contextRefOperands.length > 0
927+
? {
928+
kind: ValueKind.Context,
929+
reason: new Set([ValueReason.Other]),
930+
context: new Set(contextRefOperands),
931+
}
932+
: {
933+
kind: ValueKind.Mutable,
934+
reason: new Set([ValueReason.Other]),
935+
context: new Set(),
936+
};
932937

933938
for (const property of instrValue.properties) {
934939
switch (property.kind) {
@@ -1593,15 +1598,20 @@ function inferBlock(
15931598
}
15941599
case 'LoadLocal': {
15951600
const lvalue = instr.lvalue;
1596-
const effect =
1597-
state.isDefined(lvalue) &&
1598-
state.kind(lvalue).kind === ValueKind.Context
1599-
? Effect.ConditionallyMutate
1600-
: Effect.Capture;
1601+
CompilerError.invariant(
1602+
!(
1603+
state.isDefined(lvalue) &&
1604+
state.kind(lvalue).kind === ValueKind.Context
1605+
),
1606+
{
1607+
reason: 'Unexpected LoadLocal with context lvalue',
1608+
loc: lvalue.loc,
1609+
},
1610+
);
16011611
state.referenceAndRecordEffects(
16021612
freezeActions,
16031613
instrValue.place,
1604-
effect,
1614+
Effect.Capture,
16051615
ValueReason.Other,
16061616
);
16071617
lvalue.effect = Effect.ConditionallyMutate;
@@ -1932,19 +1942,20 @@ function inferBlock(
19321942
);
19331943
}
19341944

1935-
function hasContextRefOperand(
1945+
function getContextRefOperand(
19361946
state: InferenceState,
19371947
instrValue: InstructionValue,
1938-
): boolean {
1948+
): Array<Place> {
1949+
const result = [];
19391950
for (const place of eachInstructionValueOperand(instrValue)) {
19401951
if (
19411952
state.isDefined(place) &&
19421953
state.kind(place).kind === ValueKind.Context
19431954
) {
1944-
return true;
1955+
result.push(place);
19451956
}
19461957
}
1947-
return false;
1958+
return result;
19481959
}
19491960

19501961
export function getFunctionCallSignature(

0 commit comments

Comments
 (0)