Commit 4c35df6
committed
[compiler] Improve impurity/ref tracking
note: This implements the idea discussed in reactwg/react#389 (comment)
Currently we create `Impure` effects for impure functions like `Date.now()` or `Math.random()`, and then throw if the effect is reachable during render. However, impurity is a property of the resulting value: if the value isn't accessed during render then it's okay: maybe you're console-logging the time while debugging (fine), or storing the impure value into a ref and only accessing it in an effect or event handler (totally ok).
This PR updates to validate that impure values are not transitively consumed during render, building on the `Render` effect. The validation currently uses an algorithm very similar to that of InferReactivePlaces - building a set of known impure values, starting with `Impure` effects as the sources and then flowing outward via data flow and mutations. If a transitively impure value reaches a `Render` effect, it's an error. We record both the source of the impure value and where it gets rendered to make it easier to understand and fix errors:
```
Error: Cannot access impure value during render
Calling an impure function can produce unstable results that update unpredictably when the component happens to re-render. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#components-and-hooks-must-be-idempotent).
error.invalid-impure-functions-in-render-via-render-helper.ts:10:25
8 | const array = makeArray(now);
9 | const hasDate = identity(array);
> 10 | return <Bar hasDate={hasDate} />;
| ^^^^^^^ Cannot access impure value during render
11 | };
12 | return <Foo renderItem={renderItem} />;
13 | }
error.invalid-impure-functions-in-render-via-render-helper.ts:6:14
4 |
5 | function Component() {
> 6 | const now = Date.now();
| ^^^^^^^^^^ `Date.now` is an impure function.
7 | const renderItem = () => {
8 | const array = makeArray(now);
9 | const hasDate = identity(array);
```
Impure values are allowed to flow into refs, meaning that we now allow `useRef(Date.now())` or `useRef(localFunctionThatReturnsMathDotRandom())` which would have errored previously. The next PR reuses this improved impurity tracking to validate ref access in render as well.1 parent d963d42 commit 4c35df6
File tree
33 files changed
+1057
-199
lines changed- compiler/packages
- babel-plugin-react-compiler/src
- Entrypoint
- HIR
- Inference
- Validation
- __tests__/fixtures/compiler
- new-mutability
- eslint-plugin-react-compiler/__tests__
33 files changed
+1057
-199
lines changedLines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
132 | 132 | | |
133 | 133 | | |
134 | 134 | | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
135 | 141 | | |
136 | 142 | | |
137 | 143 | | |
| |||
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
99 | | - | |
100 | 99 | | |
101 | 100 | | |
102 | 101 | | |
| |||
107 | 106 | | |
108 | 107 | | |
109 | 108 | | |
| 109 | + | |
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
| |||
297 | 297 | | |
298 | 298 | | |
299 | 299 | | |
300 | | - | |
| 300 | + | |
301 | 301 | | |
302 | 302 | | |
303 | 303 | | |
| |||
Lines changed: 85 additions & 36 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | | - | |
| 41 | + | |
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| |||
626 | 626 | | |
627 | 627 | | |
628 | 628 | | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
629 | 701 | | |
630 | 702 | | |
631 | 703 | | |
| |||
644 | 716 | | |
645 | 717 | | |
646 | 718 | | |
| 719 | + | |
647 | 720 | | |
648 | 721 | | |
649 | 722 | | |
| |||
658 | 731 | | |
659 | 732 | | |
660 | 733 | | |
| 734 | + | |
661 | 735 | | |
662 | 736 | | |
663 | 737 | | |
| |||
670 | 744 | | |
671 | 745 | | |
672 | 746 | | |
| 747 | + | |
673 | 748 | | |
674 | 749 | | |
675 | 750 | | |
| |||
682 | 757 | | |
683 | 758 | | |
684 | 759 | | |
| 760 | + | |
685 | 761 | | |
686 | 762 | | |
687 | 763 | | |
| |||
715 | 791 | | |
716 | 792 | | |
717 | 793 | | |
| 794 | + | |
718 | 795 | | |
719 | 796 | | |
720 | 797 | | |
| |||
726 | 803 | | |
727 | 804 | | |
728 | 805 | | |
| 806 | + | |
729 | 807 | | |
730 | 808 | | |
731 | 809 | | |
| |||
739 | 817 | | |
740 | 818 | | |
741 | 819 | | |
742 | | - | |
743 | | - | |
744 | | - | |
745 | | - | |
746 | | - | |
747 | | - | |
748 | | - | |
749 | | - | |
750 | | - | |
751 | | - | |
752 | | - | |
753 | | - | |
754 | | - | |
755 | | - | |
756 | | - | |
757 | | - | |
758 | | - | |
759 | | - | |
760 | | - | |
761 | | - | |
762 | | - | |
763 | | - | |
764 | | - | |
765 | | - | |
766 | | - | |
767 | | - | |
768 | | - | |
769 | | - | |
770 | | - | |
771 | | - | |
772 | | - | |
773 | | - | |
774 | | - | |
775 | | - | |
776 | | - | |
| 820 | + | |
777 | 821 | | |
778 | 822 | | |
779 | 823 | | |
| |||
789 | 833 | | |
790 | 834 | | |
791 | 835 | | |
| 836 | + | |
792 | 837 | | |
793 | 838 | | |
794 | 839 | | |
| |||
804 | 849 | | |
805 | 850 | | |
806 | 851 | | |
| 852 | + | |
807 | 853 | | |
808 | 854 | | |
809 | 855 | | |
| |||
817 | 863 | | |
818 | 864 | | |
819 | 865 | | |
| 866 | + | |
820 | 867 | | |
821 | 868 | | |
822 | 869 | | |
| |||
829 | 876 | | |
830 | 877 | | |
831 | 878 | | |
| 879 | + | |
832 | 880 | | |
833 | 881 | | |
834 | 882 | | |
| |||
842 | 890 | | |
843 | 891 | | |
844 | 892 | | |
| 893 | + | |
845 | 894 | | |
846 | 895 | | |
847 | 896 | | |
| |||
Lines changed: 15 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
190 | 190 | | |
191 | 191 | | |
192 | 192 | | |
193 | | - | |
| 193 | + | |
194 | 194 | | |
195 | 195 | | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
196 | 205 | | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | 206 | | |
202 | 207 | | |
203 | 208 | | |
| |||
1513 | 1518 | | |
1514 | 1519 | | |
1515 | 1520 | | |
| 1521 | + | |
| 1522 | + | |
| 1523 | + | |
| 1524 | + | |
| 1525 | + | |
1516 | 1526 | | |
1517 | 1527 | | |
1518 | 1528 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1009 | 1009 | | |
1010 | 1010 | | |
1011 | 1011 | | |
1012 | | - | |
| 1012 | + | |
1013 | 1013 | | |
1014 | 1014 | | |
1015 | 1015 | | |
| |||
Lines changed: 17 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
185 | 185 | | |
186 | 186 | | |
187 | 187 | | |
188 | | - | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
189 | 191 | | |
190 | 192 | | |
191 | 193 | | |
192 | 194 | | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
193 | 207 | | |
194 | 208 | | |
195 | 209 | | |
| |||
204 | 218 | | |
205 | 219 | | |
206 | 220 | | |
207 | | - | |
| 221 | + | |
| 222 | + | |
208 | 223 | | |
209 | 224 | | |
210 | 225 | | |
| |||
Lines changed: 7 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
162 | 162 | | |
163 | 163 | | |
164 | 164 | | |
165 | | - | |
| 165 | + | |
166 | 166 | | |
167 | 167 | | |
168 | 168 | | |
| |||
222 | 222 | | |
223 | 223 | | |
224 | 224 | | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
225 | 231 | | |
226 | 232 | | |
227 | 233 | | |
| |||
0 commit comments