Skip to content

Commit e10440a

Browse files
committed
[compiler] rewrite invariant in InferReferenceEffects
Summary: Test Plan: Reviewers: Subscribers: Tasks: Tags:
1 parent 1185f88 commit e10440a

File tree

2 files changed

+47
-33
lines changed

2 files changed

+47
-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: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -857,17 +857,19 @@ function inferBlock(
857857
break;
858858
}
859859
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-
};
860+
const contextRefOperands = getContextRefOperand(state, instrValue);
861+
const valueKind: AbstractValue =
862+
contextRefOperands.length > 0
863+
? {
864+
kind: ValueKind.Context,
865+
reason: new Set([ValueReason.Other]),
866+
context: new Set(contextRefOperands),
867+
}
868+
: {
869+
kind: ValueKind.Mutable,
870+
reason: new Set([ValueReason.Other]),
871+
context: new Set(),
872+
};
871873
continuation = {
872874
kind: 'initialize',
873875
valueKind,
@@ -918,17 +920,19 @@ function inferBlock(
918920
break;
919921
}
920922
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-
};
923+
const contextRefOperands = getContextRefOperand(state, instrValue);
924+
const valueKind: AbstractValue =
925+
contextRefOperands.length > 0
926+
? {
927+
kind: ValueKind.Context,
928+
reason: new Set([ValueReason.Other]),
929+
context: new Set(contextRefOperands),
930+
}
931+
: {
932+
kind: ValueKind.Mutable,
933+
reason: new Set([ValueReason.Other]),
934+
context: new Set(),
935+
};
932936

933937
for (const property of instrValue.properties) {
934938
switch (property.kind) {
@@ -1593,15 +1597,20 @@ function inferBlock(
15931597
}
15941598
case 'LoadLocal': {
15951599
const lvalue = instr.lvalue;
1596-
const effect =
1597-
state.isDefined(lvalue) &&
1598-
state.kind(lvalue).kind === ValueKind.Context
1599-
? Effect.ConditionallyMutate
1600-
: Effect.Capture;
1600+
CompilerError.invariant(
1601+
!(
1602+
state.isDefined(lvalue) &&
1603+
state.kind(lvalue).kind === ValueKind.Context
1604+
),
1605+
{
1606+
reason: 'Unexpected LoadLocal with context lvalue',
1607+
loc: lvalue.loc,
1608+
},
1609+
);
16011610
state.referenceAndRecordEffects(
16021611
freezeActions,
16031612
instrValue.place,
1604-
effect,
1613+
Effect.Capture,
16051614
ValueReason.Other,
16061615
);
16071616
lvalue.effect = Effect.ConditionallyMutate;
@@ -1932,19 +1941,20 @@ function inferBlock(
19321941
);
19331942
}
19341943

1935-
function hasContextRefOperand(
1944+
function getContextRefOperand(
19361945
state: InferenceState,
19371946
instrValue: InstructionValue,
1938-
): boolean {
1947+
): Array<Place> {
1948+
const result = [];
19391949
for (const place of eachInstructionValueOperand(instrValue)) {
19401950
if (
19411951
state.isDefined(place) &&
19421952
state.kind(place).kind === ValueKind.Context
19431953
) {
1944-
return true;
1954+
result.push(place);
19451955
}
19461956
}
1947-
return false;
1957+
return result;
19481958
}
19491959

19501960
export function getFunctionCallSignature(

0 commit comments

Comments
 (0)