Fix scope dependency collection ordering bug
Dependency collection has to visit the instruction id first before evaluating the instruction, in order to completely any scopes that would end at that instruction. Note the removed dependencies that don't appear within the scopes.
Joe Savona committed
Dec 14, 2022 at 08:42 UTC
d909ae04349b5a781409929acd18447e80ada4e9
13 files changed
+26
-53
compiler/forget/src/HIR/InferReactiveScopeDependencies.ts
+1
-2
@@ -127,6 +127,7 @@ class ScopeDependenciesVisitor
127
}
128
129
visitInstruction(instr: Instruction, _value: InstructionValue): void {
130
+ this.#visitId(instr.id);
131
const { lvalue, value } = instr;
132
if (lvalue !== null && lvalue.place.memberPath === null) {
133
if (!this.#identifiers.has(lvalue.place.identifier)) {
@@ -173,8 +174,6 @@ class ScopeDependenciesVisitor
174
}
175
}
176
}
176
-
177
- this.#visitId(instr.id);
177
}
178
179
enterBlock(): void {}
compiler/forget/src/__tests__/fixtures/hir/hook-call.expect.md
+1
-2
@@ -81,7 +81,6 @@ bb0:
81
[8] Return read $16_@2
82
scope1 [2:3]:
83
- dependency: freeze x$11_@0
84
- - dependency: read x$11_@0
84
scope2 [7:8]:
85
- dependency: read Component$0
86
- dependency: read $13
@@ -100,7 +99,7 @@ function Component(
99
scope @0 [1:2] deps=[] {
100
[1] Const mutate x$11_@0 = Array []
101
}
103
- scope @1 [2:3] deps=[freeze x$11_@0, read x$11_@0] {
102
+ scope @1 [2:3] deps=[freeze x$11_@0] {
103
[2] Const mutate y$12_@1 = Call read useFreeze$4:TFunction(freeze x$11_@0)
104
}
105
[3] Call mutate foo$5:TFunction(read y$12_@1, read x$11_@0)
compiler/forget/src/__tests__/fixtures/hir/overlapping-scopes-shadowed.expect.md
+3
-4
@@ -21,11 +21,10 @@ bb0:
21
[4] Call mutate x$7_@0.push(read a$5)
22
[5] Return
23
scope0 [1:5]:
24
- - dependency: read b$6
24
- dependency: read a$5
25
+ - dependency: read b$6
26
scope1 [2:4]:
27
- dependency: read b$6
28
- - dependency: read a$5
28
```
29
30
## Reactive Scopes
@@ -35,9 +34,9 @@ function foo(
34
a,
35
b,
36
) {
38
- scope @0 [1:5] deps=[read b$6, read a$5] {
37
+ scope @0 [1:5] deps=[read a$5, read b$6] {
38
[1] Const mutate x$7_@0:TFunction[1:5] = Array []
40
- scope @1 [2:4] deps=[read b$6, read a$5] {
39
+ scope @1 [2:4] deps=[read b$6] {
40
[2] Const mutate y$8_@1:TFunction[2:4] = Array []
41
[3] Call mutate y$8_@1.push(read b$6)
42
}
compiler/forget/src/__tests__/fixtures/hir/reactive-scope-grouping.expect.md
+3
-6
@@ -24,10 +24,7 @@ bb0:
24
[4] Call mutate y$5_@1.push(mutate z$6_@1:TObject)
25
[5] Reassign mutate x$4_@0.y[1:6] = read y$5_@1:TFunction
26
[6] Return freeze x$4_@0:TObject
27
-scope0 [1:6]:
28
- - dependency: mutate x$4_@0.y
29
-scope1 [2:5]:
30
- - dependency: mutate x$4_@0.y
27
+
28
```
29
30
## Reactive Scopes
@@ -35,9 +32,9 @@ scope1 [2:5]:
32
```
33
function foo(
34
) {
38
- scope @0 [1:6] deps=[mutate x$4_@0.y] {
35
+ scope @0 [1:6] deps=[] {
36
[1] Const mutate x$4_@0:TObject[1:6] = Object { }
40
- scope @1 [2:5] deps=[mutate x$4_@0.y] {
37
+ scope @1 [2:5] deps=[] {
38
[2] Const mutate y$5_@1:TFunction[2:5] = Array []
39
[3] Const mutate z$6_@1:TObject[2:5] = Object { }
40
[4] Call mutate y$5_@1.push(mutate z$6_@1:TObject)
compiler/forget/src/__tests__/fixtures/hir/reassignment-conditional.expect.md
+1
-3
@@ -43,8 +43,6 @@ scope0 [1:7]:
43
scope1 [7:8]:
44
- dependency: read Component$0
45
- dependency: freeze x$7_@0:TFunction
46
- - dependency: read y$8.push
47
- - dependency: read props$6.p2
46
scope2 [9:10]:
47
- dependency: read Component$0
48
- dependency: read x$7_@0:TFunction
@@ -65,7 +63,7 @@ function Component(
63
[5] Reassign mutate x$7_@0:TFunction[1:7] = Array []
64
}
65
}
68
- scope @1 [7:8] deps=[read Component$0, freeze x$7_@0:TFunction, read y$8.push, read props$6.p2] {
66
+ scope @1 [7:8] deps=[read Component$0, freeze x$7_@0:TFunction] {
67
[7] Const mutate _$12_@1 = JSX <read Component$0 x={freeze x$7_@0:TFunction} ></read Component$0>
68
}
69
[8] Call read y$8.push(read props$6.p2)
compiler/forget/src/__tests__/fixtures/hir/reassignment.expect.md
+3
-4
@@ -31,13 +31,12 @@ bb0:
31
[8] Return read $11_@3
32
scope0 [1:7]:
33
- dependency: read props$6.p0
34
+ - dependency: read props$6.p1
35
- dependency: read Component$0
36
- dependency: freeze x$9_@1
36
- - dependency: read props$6.p1
37
scope2 [5:6]:
38
- dependency: read Component$0
39
- dependency: freeze x$9_@1
40
- - dependency: read props$6.p1
40
scope3 [7:8]:
41
- dependency: read Component$0
42
- dependency: read x$9_@1
@@ -50,14 +49,14 @@ scope3 [7:8]:
49
function Component(
50
props,
51
) {
53
- scope @0 [1:7] deps=[read props$6.p0, read Component$0, freeze x$9_@1, read props$6.p1] {
52
+ scope @0 [1:7] deps=[read props$6.p0, read props$6.p1, read Component$0, freeze x$9_@1] {
53
[1] Const mutate x$7_@0:TFunction[1:7] = Array []
54
[2] Call mutate x$7_@0.push(read props$6.p0)
55
[3] Const mutate y$8_@0:TFunction[1:7] = read x$7_@0:TFunction
56
scope @1 [4:5] deps=[] {
57
[4] Const mutate x$9_@1 = Array []
58
}
60
- scope @2 [5:6] deps=[read Component$0, freeze x$9_@1, read props$6.p1] {
59
+ scope @2 [5:6] deps=[read Component$0, freeze x$9_@1] {
60
[5] Const mutate _$10_@2 = JSX <read Component$0 x={freeze x$9_@1} ></read Component$0>
61
}
62
[6] Call mutate y$8_@0.push(read props$6.p1)
compiler/forget/src/__tests__/fixtures/hir/ssa-property-alias-if.expect.md
+3
-9
@@ -37,12 +37,6 @@ bb1:
37
[9] Return freeze x$6_@0:TObject
38
scope0 [1:9]:
39
- dependency: read a$5
40
- - dependency: mutate x$6_@0.y
41
- - dependency: mutate x$6_@0.z
42
-scope1 [3:4]:
43
- - dependency: mutate x$6_@0.y
44
-scope2 [6:7]:
45
- - dependency: mutate x$6_@0.z
40
```
41
42
## Reactive Scopes
@@ -51,15 +45,15 @@ scope2 [6:7]:
45
function foo(
46
a,
47
) {
54
- scope @0 [1:9] deps=[read a$5, mutate x$6_@0.y, mutate x$6_@0.z] {
48
+ scope @0 [1:9] deps=[read a$5] {
49
[1] Const mutate x$6_@0:TObject[1:9] = Object { }
50
if (read a$5) {
57
- scope @1 [3:4] deps=[mutate x$6_@0.y] {
51
+ scope @1 [3:4] deps=[] {
52
[3] Const mutate y$7_@1:TObject = Object { }
53
}
54
[4] Reassign mutate x$6_@0.y[1:9] = read y$7_@1:TObject
55
} else {
62
- scope @2 [6:7] deps=[mutate x$6_@0.z] {
56
+ scope @2 [6:7] deps=[] {
57
[6] Const mutate z$8_@2:TObject = Object { }
58
}
59
[7] Reassign mutate x$6_@0.z[1:9] = read z$8_@2:TObject
compiler/forget/src/__tests__/fixtures/hir/ssa-property-alias-mutate-inside-if.expect.md
+2
-5
@@ -39,9 +39,6 @@ bb1:
39
[10] Return freeze x$7_@0:TObject
40
scope0 [1:10]:
41
- dependency: read a$6
42
- - dependency: mutate x$7_@0.z
43
-scope1 [7:8]:
44
- - dependency: mutate x$7_@0.z
42
```
43
44
## Reactive Scopes
@@ -50,14 +47,14 @@ scope1 [7:8]:
47
function foo(
48
a,
49
) {
53
- scope @0 [1:10] deps=[read a$6, mutate x$7_@0.z] {
50
+ scope @0 [1:10] deps=[read a$6] {
51
[1] Const mutate x$7_@0:TObject[1:10] = Object { }
52
if (read a$6) {
53
[3] Const mutate y$8_@0:TObject[1:10] = Object { }
54
[4] Reassign mutate x$7_@0.y[1:10] = read y$8_@0:TObject
55
[5] Call mutate mutate$4:TFunction(mutate y$8_@0:TObject)
56
} else {
60
- scope @1 [7:8] deps=[mutate x$7_@0.z] {
57
+ scope @1 [7:8] deps=[] {
58
[7] Const mutate z$9_@1:TObject = Object { }
59
}
60
[8] Reassign mutate x$7_@0.z[1:10] = read z$9_@1:TObject
compiler/forget/src/__tests__/fixtures/hir/switch-non-final-default.expect.md
+1
-3
@@ -61,8 +61,6 @@ bb1:
61
scope2 [12:13]:
62
- dependency: read Component$0
63
- dependency: freeze x$10_@1:TFunction
64
- - dependency: read y$11_@1.push
65
- - dependency: read props$9.p4
64
scope3 [14:15]:
65
- dependency: read Component$0
66
- dependency: freeze y$11_@1:TPrimitive
@@ -98,7 +96,7 @@ function Component(
96
}
97
}
98
}
101
- scope @2 [12:13] deps=[read Component$0, freeze x$10_@1:TFunction, read y$11_@1.push, read props$9.p4] {
99
+ scope @2 [12:13] deps=[read Component$0, freeze x$10_@1:TFunction] {
100
[12] Const mutate child$19_@2 = JSX <read Component$0 data={freeze x$10_@1:TFunction} ></read Component$0>
101
}
102
[13] Call read y$11_@1.push(read props$9.p4)
compiler/forget/src/__tests__/fixtures/hir/switch.expect.md
+1
-3
@@ -56,8 +56,6 @@ bb1:
56
scope3 [12:13]:
57
- dependency: read Component$0
58
- dependency: freeze x$9_@1:TFunction
59
- - dependency: read y$10_@1.push
60
- - dependency: read props$8.p4
59
scope4 [14:15]:
60
- dependency: read Component$0
61
- dependency: read y$10_@1:TPrimitive
@@ -88,7 +86,7 @@ function Component(
86
}
87
}
88
}
91
- scope @3 [12:13] deps=[read Component$0, freeze x$9_@1:TFunction, read y$10_@1.push, read props$8.p4] {
89
+ scope @3 [12:13] deps=[read Component$0, freeze x$9_@1:TFunction] {
90
[12] Const mutate child$19_@3 = JSX <read Component$0 data={freeze x$9_@1:TFunction} ></read Component$0>
91
}
92
[13] Call read y$10_@1.push(read props$8.p4)
compiler/forget/src/__tests__/fixtures/hir/type-binary-operator.expect.md
+2
-3
@@ -27,8 +27,7 @@ bb2:
27
bb1:
28
predecessor blocks: bb2 bb0
29
[7] Return
30
-scope1 [2:3]:
31
- - dependency: read a$7_@0:TPrimitive
30
+
31
```
32
33
## Reactive Scopes
@@ -39,7 +38,7 @@ function component(
38
scope @0 [1:2] deps=[] {
39
[1] Const mutate a$7_@0:TPrimitive = Call mutate some$2:TFunction()
40
}
42
- scope @1 [2:3] deps=[read a$7_@0:TPrimitive] {
41
+ scope @1 [2:3] deps=[] {
42
[2] Const mutate b$8_@1:TPrimitive = Call mutate someOther$4:TFunction()
43
}
44
[3] Const mutate $9:TPrimitive = Binary read a$7_@0:TPrimitive > read b$8_@1:TPrimitive
compiler/forget/src/__tests__/fixtures/hir/type-test-field-store.expect.md
+3
-6
@@ -20,10 +20,7 @@ bb0:
20
[3] Reassign mutate x$4_@0.t[1:4] = read q$5_@1:TObject
21
[4] Const mutate z$6:TObject = read x$4_@0.t
22
[5] Return
23
-scope0 [1:4]:
24
- - dependency: mutate x$4_@0.t
25
-scope1 [2:3]:
26
- - dependency: mutate x$4_@0.t
23
+
24
```
25
26
## Reactive Scopes
@@ -31,9 +28,9 @@ scope1 [2:3]:
28
```
29
function component(
30
) {
34
- scope @0 [1:4] deps=[mutate x$4_@0.t] {
31
+ scope @0 [1:4] deps=[] {
32
[1] Const mutate x$4_@0:TObject[1:4] = Object { }
36
- scope @1 [2:3] deps=[mutate x$4_@0.t] {
33
+ scope @1 [2:3] deps=[] {
34
[2] Const mutate q$5_@1:TObject = Object { }
35
}
36
[3] Reassign mutate x$4_@0.t[1:4] = read q$5_@1:TObject
compiler/forget/src/__tests__/fixtures/hir/type-test-return-type-inference.expect.md
+2
-3
@@ -30,8 +30,7 @@ bb1:
30
predecessor blocks: bb2 bb0
31
[7] Const mutate z$12_@3:TPrimitive = Call mutate foo$2:TFunction()
32
[8] Return
33
-scope1 [2:3]:
34
- - dependency: read x$7_@0:TPrimitive
33
+
34
```
35
36
## Reactive Scopes
@@ -42,7 +41,7 @@ function component(
41
scope @0 [1:2] deps=[] {
42
[1] Const mutate x$7_@0:TPrimitive = Call mutate foo$2:TFunction()
43
}
45
- scope @1 [2:3] deps=[read x$7_@0:TPrimitive] {
44
+ scope @1 [2:3] deps=[] {
45
[2] Const mutate y$8_@1:TPrimitive = Call mutate foo$2:TFunction()
46
}
47
[3] Const mutate $9:TPrimitive = Binary read x$7_@0:TPrimitive > read y$8_@1:TPrimitive