Commit f0e4d14
committed
[compiler][poc] Improve impurity/ref tracking
We currently model impure functions like `Date.now()` with an `Impure` effect, but the way this works is that the effect is considered an error if it's called at all. But we really want to reflect the fact that the _value_ is impure, and is okay to be used outside of render. For example, `useRef(Date.now())` should be fine.
So this PR changes the Impure effect to describe that an impure value flows `into: Place`. We flow impurity through the data flow graph during InferMutationAliasingRanges, including propagating this to the function's effects. So if a function expression returns a transitively impure value, that's expressed in our inferred signature for that function. Calling it propagates the impurity via our effect system.
We stop this propagation when reaching a ref, allowing `useRef(Date.now())` or `useRef(localFunctionThatReturnsDateNow())`.
This lets us also model accessing a ref as an impure value - we just emit an `Impure` event for PropertyLoad/ComputedLoad off of a ref, and the above tracking kicks in.
A final piece is that we can also use our existing machinery to disallow writing to global values during render to handle writing to refs - except that we need to allow the lazy init pattern. So it probably makes sense to keep the ref validation pass, but scope it back to just handling writes and not reads.
This definitely needs more polish, the error messages would ideally be something like:
```
Error: cannot call impure function in render
Blah blah description of why this is bad...
foo.js:0:0
<Foo x={x} />
^ This value reads from an impure function
foo.js:0:0
x = Date.now();
^^^^^^^^ The value derives from Date.now which is impure
```
Ie show you both the local site (where impurity flows into render) and the ultiamte source of the impure value.1 parent 0526c79 commit f0e4d14
File tree
23 files changed
+623
-155
lines changed- compiler/packages/babel-plugin-react-compiler/src
- Entrypoint
- HIR
- Inference
- Validation
- __tests__/fixtures/compiler
- new-mutability
23 files changed
+623
-155
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: 0 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
99 | | - | |
100 | 99 | | |
101 | 100 | | |
102 | 101 | | |
| |||
293 | 292 | | |
294 | 293 | | |
295 | 294 | | |
296 | | - | |
297 | | - | |
298 | | - | |
299 | | - | |
300 | 295 | | |
301 | 296 | | |
302 | 297 | | |
| |||
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: 27 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
9 | 13 | | |
10 | 14 | | |
11 | 15 | | |
| |||
190 | 194 | | |
191 | 195 | | |
192 | 196 | | |
193 | | - | |
| 197 | + | |
194 | 198 | | |
195 | 199 | | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
200 | 209 | | |
201 | 210 | | |
202 | 211 | | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
203 | 219 | | |
204 | 220 | | |
205 | 221 | | |
| |||
1513 | 1529 | | |
1514 | 1530 | | |
1515 | 1531 | | |
| 1532 | + | |
| 1533 | + | |
| 1534 | + | |
| 1535 | + | |
| 1536 | + | |
1516 | 1537 | | |
1517 | 1538 | | |
1518 | 1539 | | |
| |||
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: 15 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
185 | 185 | | |
186 | 186 | | |
187 | 187 | | |
188 | | - | |
| 188 | + | |
| 189 | + | |
189 | 190 | | |
190 | 191 | | |
191 | 192 | | |
192 | 193 | | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
193 | 205 | | |
194 | 206 | | |
195 | 207 | | |
| |||
204 | 216 | | |
205 | 217 | | |
206 | 218 | | |
207 | | - | |
| 219 | + | |
| 220 | + | |
208 | 221 | | |
209 | 222 | | |
210 | 223 | | |
| |||
Lines changed: 9 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 | + | |
| 231 | + | |
| 232 | + | |
225 | 233 | | |
226 | 234 | | |
227 | 235 | | |
| |||
0 commit comments