Extend ValueReason w context identifiers
ghstack-source-id: 5157b988b0ff15299d585aa906831c1d640f6a40 Pull Request resolved: https://github.com/facebook/react-forget/pull/2878
Joe Savona committed
Apr 19, 2024 at 17:44 UTC
12f314fa9878ac500777e41f27fd978a26f54411
2 files changed
+63
-6
compiler/packages/babel-plugin-react-forget/src/HIR/HIR.ts
+1
@@ -1149,6 +1149,7 @@ export function isPromotedJsxTemporary(name: string): boolean {
1149
export type AbstractValue = {
1150
kind: ValueKind;
1151
reason: ReadonlySet<ValueReason>;
1152
+ context: ReadonlySet<Place>;
1153
};
1154
1155
/**
compiler/packages/babel-plugin-react-forget/src/Inference/InferReferenceEffects.ts
+62
-6
@@ -112,6 +112,7 @@ export default function inferReferenceEffects(
112
initialState.initialize(value, {
113
kind: ValueKind.Frozen,
114
reason: new Set([ValueReason.Other]),
115
+ context: new Set(),
116
});
117
118
for (const ref of fn.context) {
@@ -124,6 +125,7 @@ export default function inferReferenceEffects(
125
initialState.initialize(value, {
126
kind: ValueKind.Context,
127
reason: new Set([ValueReason.Other]),
128
+ context: new Set([ref]),
129
});
130
initialState.define(ref, value);
131
}
@@ -132,10 +134,12 @@ export default function inferReferenceEffects(
134
? {
135
kind: ValueKind.Mutable,
136
reason: new Set([ValueReason.Other]),
137
+ context: new Set(),
138
}
139
: {
140
kind: ValueKind.Frozen,
141
reason: new Set([ValueReason.ReactiveFunctionArgument]),
142
+ context: new Set(),
143
};
144
145
if (fn.fnType === "Component") {
@@ -171,6 +175,7 @@ export default function inferReferenceEffects(
175
initialState.initialize(value, {
176
kind: ValueKind.Mutable,
177
reason: new Set([ValueReason.Other]),
178
+ context: new Set(),
179
});
180
initialState.define(place, value);
181
}
@@ -405,11 +410,13 @@ class InferenceState {
410
valueKind = {
411
kind: ValueKind.Frozen,
412
reason: reasonSet,
413
+ context: new Set(),
414
};
415
values.forEach((value) => {
416
this.#values.set(value, {
417
kind: ValueKind.Frozen,
418
reason: reasonSet,
419
+ context: new Set(),
420
});
421
422
if (
@@ -798,14 +805,23 @@ function mergeAbstractValues(
805
b: AbstractValue
806
): AbstractValue {
807
const kind = mergeValues(a.kind, b.kind);
801
- if (kind === a.kind && kind === b.kind && isSuperset(a.reason, b.reason)) {
808
+ if (
809
+ kind === a.kind &&
810
+ kind === b.kind &&
811
+ isSuperset(a.reason, b.reason) &&
812
+ isSuperset(a.context, b.context)
813
+ ) {
814
return a;
815
}
816
const reason = new Set(a.reason);
817
for (const r of b.reason) {
818
reason.add(r);
819
}
808
- return { kind, reason };
820
+ const context = new Set(a.context);
821
+ for (const c of b.context) {
822
+ context.add(c);
823
+ }
824
+ return { kind, reason, context };
825
}
826
827
/*
@@ -832,6 +848,7 @@ function inferBlock(
848
valueKind = {
849
kind: ValueKind.Immutable,
850
reason: new Set([ValueReason.Other]),
851
+ context: new Set(),
852
};
853
effect = {
854
kind: Effect.Read,
@@ -844,8 +861,13 @@ function inferBlock(
861
? {
862
kind: ValueKind.Context,
863
reason: new Set([ValueReason.Other]),
864
+ context: new Set(),
865
}
848
- : { kind: ValueKind.Mutable, reason: new Set([ValueReason.Other]) };
866
+ : {
867
+ kind: ValueKind.Mutable,
868
+ reason: new Set([ValueReason.Other]),
869
+ context: new Set(),
870
+ };
871
effect = { kind: Effect.Capture, reason: ValueReason.Other };
872
lvalueEffect = Effect.Store;
873
break;
@@ -867,6 +889,7 @@ function inferBlock(
889
valueKind = {
890
kind: ValueKind.Mutable,
891
reason: new Set([ValueReason.Other]),
892
+ context: new Set(),
893
};
894
state.reference(
895
instrValue.callee,
@@ -894,8 +917,13 @@ function inferBlock(
917
? {
918
kind: ValueKind.Context,
919
reason: new Set([ValueReason.Other]),
920
+ context: new Set(),
921
}
898
- : { kind: ValueKind.Mutable, reason: new Set([ValueReason.Other]) };
922
+ : {
923
+ kind: ValueKind.Mutable,
924
+ reason: new Set([ValueReason.Other]),
925
+ context: new Set(),
926
+ };
927
928
for (const property of instrValue.properties) {
929
switch (property.kind) {
@@ -946,6 +974,7 @@ function inferBlock(
974
valueKind = {
975
kind: ValueKind.Immutable,
976
reason: new Set([ValueReason.Other]),
977
+ context: new Set(),
978
};
979
effect = { kind: Effect.Read, reason: ValueReason.Other };
980
break;
@@ -955,6 +984,7 @@ function inferBlock(
984
valueKind = {
985
kind: ValueKind.Mutable,
986
reason: new Set([ValueReason.Other]),
987
+ context: new Set(),
988
};
989
break;
990
}
@@ -962,6 +992,7 @@ function inferBlock(
992
valueKind = {
993
kind: ValueKind.Frozen,
994
reason: new Set([ValueReason.Other]),
995
+ context: new Set(),
996
};
997
effect = { kind: Effect.Freeze, reason: ValueReason.JsxCaptured };
998
break;
@@ -970,6 +1001,7 @@ function inferBlock(
1001
valueKind = {
1002
kind: ValueKind.Frozen,
1003
reason: new Set([ValueReason.Other]),
1004
+ context: new Set(),
1005
};
1006
effect = {
1007
kind: Effect.Freeze,
@@ -981,6 +1013,7 @@ function inferBlock(
1013
valueKind = {
1014
kind: ValueKind.Mutable,
1015
reason: new Set([ValueReason.Other]),
1016
+ context: new Set(),
1017
};
1018
effect = {
1019
kind: Effect.ConditionallyMutate,
@@ -996,6 +1029,7 @@ function inferBlock(
1029
valueKind = {
1030
kind: ValueKind.Immutable,
1031
reason: new Set([ValueReason.Other]),
1032
+ context: new Set(),
1033
};
1034
effect = { kind: Effect.Read, reason: ValueReason.Other };
1035
break;
@@ -1005,6 +1039,7 @@ function inferBlock(
1039
valueKind = {
1040
kind: ValueKind.Mutable,
1041
reason: new Set([ValueReason.Other]),
1042
+ context: new Set(),
1043
};
1044
effect = {
1045
kind: Effect.ConditionallyMutate,
@@ -1016,6 +1051,7 @@ function inferBlock(
1051
valueKind = {
1052
kind: ValueKind.Immutable,
1053
reason: new Set([ValueReason.Global]),
1054
+ context: new Set(),
1055
};
1056
break;
1057
case "Debugger":
@@ -1024,6 +1060,7 @@ function inferBlock(
1060
valueKind = {
1061
kind: ValueKind.Immutable,
1062
reason: new Set([ValueReason.Other]),
1063
+ context: new Set(),
1064
};
1065
break;
1066
}
@@ -1046,6 +1083,7 @@ function inferBlock(
1083
state.initialize(instrValue, {
1084
kind: hasMutableOperand ? ValueKind.Mutable : ValueKind.Frozen,
1085
reason: new Set([ValueReason.Other]),
1086
+ context: new Set(),
1087
});
1088
state.define(instr.lvalue, instrValue);
1089
instr.lvalue.effect = Effect.Store;
@@ -1067,8 +1105,13 @@ function inferBlock(
1105
signature.returnValueReason ??
1106
ValueReason.KnownReturnSignature,
1107
]),
1108
+ context: new Set(),
1109
}
1071
- : { kind: ValueKind.Mutable, reason: new Set([ValueReason.Other]) };
1110
+ : {
1111
+ kind: ValueKind.Mutable,
1112
+ reason: new Set([ValueReason.Other]),
1113
+ context: new Set(),
1114
+ };
1115
let hasCaptureArgument = false;
1116
let isUseEffect = isEffectHook(instrValue.callee.identifier);
1117
for (let i = 0; i < instrValue.args.length; i++) {
@@ -1151,8 +1194,13 @@ function inferBlock(
1194
? {
1195
kind: signature.returnValueKind,
1196
reason: new Set([ValueReason.Other]),
1197
+ context: new Set(),
1198
}
1155
- : { kind: ValueKind.Mutable, reason: new Set([ValueReason.Other]) };
1199
+ : {
1200
+ kind: ValueKind.Mutable,
1201
+ reason: new Set([ValueReason.Other]),
1202
+ context: new Set(),
1203
+ };
1204
1205
if (
1206
signature !== null &&
@@ -1278,6 +1326,7 @@ function inferBlock(
1326
valueKind = {
1327
kind: ValueKind.Immutable,
1328
reason: new Set([ValueReason.Other]),
1329
+ context: new Set(),
1330
};
1331
effect = { kind: Effect.Mutate, reason: ValueReason.Other };
1332
break;
@@ -1340,6 +1389,7 @@ function inferBlock(
1389
state.initialize(instrValue, {
1390
kind: ValueKind.Immutable,
1391
reason: new Set([ValueReason.Other]),
1392
+ context: new Set(),
1393
});
1394
state.define(instr.lvalue, instrValue);
1395
instr.lvalue.effect = Effect.Mutate;
@@ -1427,6 +1477,7 @@ function inferBlock(
1477
state.initialize(instrValue, {
1478
kind: ValueKind.Immutable,
1479
reason: new Set([ValueReason.Other]),
1480
+ context: new Set(),
1481
});
1482
state.define(lvalue, instrValue);
1483
continue;
@@ -1483,10 +1534,12 @@ function inferBlock(
1534
? {
1535
kind: ValueKind.Mutable,
1536
reason: new Set([ValueReason.Other]),
1537
+ context: new Set(),
1538
}
1539
: {
1540
kind: ValueKind.Immutable,
1541
reason: new Set([ValueReason.Other]),
1542
+ context: new Set(),
1543
}
1544
);
1545
state.define(instrValue.lvalue.place, value);
@@ -1496,6 +1549,7 @@ function inferBlock(
1549
state.initialize(instrValue, {
1550
kind: ValueKind.Mutable,
1551
reason: new Set([ValueReason.Other]),
1552
+ context: new Set(),
1553
});
1554
state.define(instrValue.lvalue.place, instrValue);
1555
continue;
@@ -1633,6 +1687,7 @@ function inferBlock(
1687
valueKind = {
1688
kind: ValueKind.Mutable,
1689
reason: new Set([ValueReason.Other]),
1690
+ context: new Set(),
1691
};
1692
break;
1693
}
@@ -1642,6 +1697,7 @@ function inferBlock(
1697
valueKind = {
1698
kind: ValueKind.Immutable,
1699
reason: new Set([ValueReason.Other]),
1700
+ context: new Set(),
1701
};
1702
break;
1703
}