[hir] Correctly identify mutated identifiers
mutableRange.end is exclusive so account for this extra 1 instruction.
Sathya Gunasekaran committed
Dec 1, 2022 at 19:03 UTC
00f74d4e70bd6496a788bb0d0a32a7d5e102f0ff
5 files changed
+73
-64
compiler/forget/src/HIR/InferMutableLifetimes.ts
+1
-1
@@ -132,7 +132,7 @@ export function inferMutableRanges(func: HIRFunction) {
132
// Update mutableRange.end only if the identifiers have actually been
133
// mutated.
134
const haveIdentifiersBeenMutated = [...aliasSet].some(
135
- (id) => id.mutableRange.end > id.mutableRange.start
135
+ (id) => id.mutableRange.end - id.mutableRange.start > 1
136
);
137
138
if (haveIdentifiersBeenMutated) {
compiler/forget/src/__tests__/fixtures/hir/frozen-after-alias.expect.md
+9
-7
@@ -18,12 +18,14 @@ function foo(x) {}
18
19
```
20
bb0:
21
- [1] Const mutate a$5_@0[1:3] = Array []
22
- [2] Const mutate b$6_@0[1:3] = read a$5_@0
21
+ [1] Const mutate a$5_@0 = Array []
22
+ [2] Const mutate b$6_@1 = read a$5_@0
23
[3] Call read useFreeze$3(freeze a$5_@0)
24
- [4] Call mutate foo$4_@1(read b$6_@0)
24
+ [4] Call mutate foo$4_@2(read b$6_@1)
25
[5] Return
26
-
26
+scope1 [2:3]:
27
+ - read a$5_@0
28
+ - freeze a$5_@0
29
```
30
31
### CFG
@@ -33,10 +35,10 @@ flowchart TB
35
%% Basic Blocks
36
subgraph bb0
37
bb0_instrs["
36
- [1] Const mutate a$5_@0[1:3] = Array []
37
- [2] Const mutate b$6_@0[1:3] = read a$5_@0
38
+ [1] Const mutate a$5_@0 = Array []
39
+ [2] Const mutate b$6_@1 = read a$5_@0
40
[3] Call read useFreeze$3(freeze a$5_@0)
39
- [4] Call mutate foo$4_@1(read b$6_@0)
41
+ [4] Call mutate foo$4_@2(read b$6_@1)
42
"]
43
bb0_instrs --> bb0_terminal(["Return"])
44
end
compiler/forget/src/__tests__/fixtures/hir/hooks-freeze-possibly-mutable-arguments.expect.md
+18
-17
@@ -27,29 +27,30 @@ function call(x) {}
27
```
28
bb0:
29
[1] Const mutate cond$8_@0 = read props$7.cond
30
- [2] Const mutate x$9_@1[2:9] = read props$7.x
30
+ [2] Const mutate x$9_@1 = read props$7.x
31
[3] Let mutate a$10_@2 = undefined
32
[4] If (read cond$8_@0) then:bb2 else:bb3
33
bb2:
34
predecessor blocks: bb0
35
- [5] Reassign mutate a$11_@1[2:9] = read x$9_@1
35
+ [5] Reassign mutate a$11_@3[5:8] = read x$9_@1
36
[6] Goto bb1
37
bb3:
38
predecessor blocks: bb0
39
- [7] Reassign mutate a$12_@1[2:9] = Array []
39
+ [7] Reassign mutate a$12_@3[5:8] = Array []
40
[8] Goto bb1
41
bb1:
42
predecessor blocks: bb2 bb3
43
- a$14_@1[2:9]: phi(bb2: a$11_@1, bb3: a$12_@1)
44
- [9] Call read useFreeze$5(freeze a$14_@1)
45
- [10] Call read useFreeze$5(read a$14_@1)
46
- [11] Call mutate call$6_@3(read a$14_@1)
47
- [12] Return read a$14_@1
43
+ a$14_@3[5:8]: phi(bb2: a$11_@3, bb3: a$12_@3)
44
+ [9] Call read useFreeze$5(freeze a$14_@3)
45
+ [10] Call read useFreeze$5(read a$14_@3)
46
+ [11] Call mutate call$6_@4(read a$14_@3)
47
+ [12] Return read a$14_@3
48
scope0 [1:2]:
49
- read props$7.cond
50
-scope1 [2:9]:
50
+scope1 [2:3]:
51
- read props$7.x
52
- - read cond$8_@0
52
+scope3 [5:8]:
53
+ - read x$9_@1
54
```
55
56
### CFG
@@ -60,30 +61,30 @@ flowchart TB
61
subgraph bb0
62
bb0_instrs["
63
[1] Const mutate cond$8_@0 = read props$7.cond
63
- [2] Const mutate x$9_@1[2:9] = read props$7.x
64
+ [2] Const mutate x$9_@1 = read props$7.x
65
[3] Let mutate a$10_@2 = undefined
66
"]
67
bb0_instrs --> bb0_terminal(["If (read cond$8_@0)"])
68
end
69
subgraph bb2
70
bb2_instrs["
70
- [5] Reassign mutate a$11_@1[2:9] = read x$9_@1
71
+ [5] Reassign mutate a$11_@3[5:8] = read x$9_@1
72
"]
73
bb2_instrs --> bb2_terminal(["Goto"])
74
end
75
subgraph bb3
76
bb3_instrs["
76
- [7] Reassign mutate a$12_@1[2:9] = Array []
77
+ [7] Reassign mutate a$12_@3[5:8] = Array []
78
"]
79
bb3_instrs --> bb3_terminal(["Goto"])
80
end
81
subgraph bb1
82
bb1_instrs["
82
- [9] Call read useFreeze$5(freeze a$14_@1)
83
- [10] Call read useFreeze$5(read a$14_@1)
84
- [11] Call mutate call$6_@3(read a$14_@1)
83
+ [9] Call read useFreeze$5(freeze a$14_@3)
84
+ [10] Call read useFreeze$5(read a$14_@3)
85
+ [11] Call mutate call$6_@4(read a$14_@3)
86
"]
86
- bb1_instrs --> bb1_terminal(["Return read a$14_@1"])
87
+ bb1_instrs --> bb1_terminal(["Return read a$14_@3"])
88
end
89
90
%% Jumps
compiler/forget/src/__tests__/fixtures/hir/logical-expression.expect.md
+34
-32
@@ -23,20 +23,20 @@ function g() {}
23
24
```
25
bb0:
26
- [1] Const mutate $5_@0[0:7] = Call mutate f$1_@0()
26
+ [1] Const mutate $5_@0[0:2] = Call mutate f$1_@0()
27
[2] If (read $5_@0) then:bb2 else:bb3
28
bb2:
29
predecessor blocks: bb0
30
- [3] Const mutate $6_@0[0:7] = Call mutate g$4_@0()
30
+ [3] Const mutate $6_@1[0:6] = Call mutate g$4_@1()
31
[4] Goto bb1
32
bb3:
33
predecessor blocks: bb0
34
- [5] Const mutate $7_@0[0:7] = read $5_@0
34
+ [5] Const mutate $7_@1[0:6] = read $5_@0
35
[6] Goto bb1
36
bb1:
37
predecessor blocks: bb2 bb3
38
- $8_@0[0:7]: phi(bb2: $6_@0, bb3: $7_@0)
39
- [7] Return freeze $8_@0
38
+ $8_@1[0:6]: phi(bb2: $6_@1, bb3: $7_@1)
39
+ [7] Return freeze $8_@1
40
41
```
42
@@ -47,24 +47,24 @@ flowchart TB
47
%% Basic Blocks
48
subgraph bb0
49
bb0_instrs["
50
- [1] Const mutate $5_@0[0:7] = Call mutate f$1_@0()
50
+ [1] Const mutate $5_@0[0:2] = Call mutate f$1_@0()
51
"]
52
bb0_instrs --> bb0_terminal(["If (read $5_@0)"])
53
end
54
subgraph bb2
55
bb2_instrs["
56
- [3] Const mutate $6_@0[0:7] = Call mutate g$4_@0()
56
+ [3] Const mutate $6_@1[0:6] = Call mutate g$4_@1()
57
"]
58
bb2_instrs --> bb2_terminal(["Goto"])
59
end
60
subgraph bb3
61
bb3_instrs["
62
- [5] Const mutate $7_@0[0:7] = read $5_@0
62
+ [5] Const mutate $7_@1[0:6] = read $5_@0
63
"]
64
bb3_instrs --> bb3_terminal(["Goto"])
65
end
66
subgraph bb1
67
- bb1_terminal(["Return freeze $8_@0"])
67
+ bb1_terminal(["Return freeze $8_@1"])
68
end
69
70
%% Jumps
@@ -91,20 +91,20 @@ function And$0() {
91
92
```
93
bb0:
94
- [1] Const mutate $5_@0[0:7] = Call mutate f$1_@0()
94
+ [1] Const mutate $5_@0[0:2] = Call mutate f$1_@0()
95
[2] If (read $5_@0) then:bb2 else:bb3
96
bb2:
97
predecessor blocks: bb0
98
- [3] Const mutate $6_@0[0:7] = read $5_@0
98
+ [3] Const mutate $6_@1[0:6] = read $5_@0
99
[4] Goto bb1
100
bb3:
101
predecessor blocks: bb0
102
- [5] Const mutate $7_@0[0:7] = Call mutate g$4_@0()
102
+ [5] Const mutate $7_@1[0:6] = Call mutate g$4_@1()
103
[6] Goto bb1
104
bb1:
105
predecessor blocks: bb2 bb3
106
- $8_@0[0:7]: phi(bb2: $6_@0, bb3: $7_@0)
107
- [7] Return freeze $8_@0
106
+ $8_@1[0:6]: phi(bb2: $6_@1, bb3: $7_@1)
107
+ [7] Return freeze $8_@1
108
109
```
110
@@ -115,24 +115,24 @@ flowchart TB
115
%% Basic Blocks
116
subgraph bb0
117
bb0_instrs["
118
- [1] Const mutate $5_@0[0:7] = Call mutate f$1_@0()
118
+ [1] Const mutate $5_@0[0:2] = Call mutate f$1_@0()
119
"]
120
bb0_instrs --> bb0_terminal(["If (read $5_@0)"])
121
end
122
subgraph bb2
123
bb2_instrs["
124
- [3] Const mutate $6_@0[0:7] = read $5_@0
124
+ [3] Const mutate $6_@1[0:6] = read $5_@0
125
"]
126
bb2_instrs --> bb2_terminal(["Goto"])
127
end
128
subgraph bb3
129
bb3_instrs["
130
- [5] Const mutate $7_@0[0:7] = Call mutate g$4_@0()
130
+ [5] Const mutate $7_@1[0:6] = Call mutate g$4_@1()
131
"]
132
bb3_instrs --> bb3_terminal(["Goto"])
133
end
134
subgraph bb1
135
- bb1_terminal(["Return freeze $8_@0"])
135
+ bb1_terminal(["Return freeze $8_@1"])
136
end
137
138
%% Jumps
@@ -159,23 +159,25 @@ function Or$0() {
159
160
```
161
bb0:
162
- [1] Const mutate $9_@0[0:9] = Call mutate f$2_@0()
162
+ [1] Const mutate $9_@0[0:2] = Call mutate f$2_@0()
163
[2] Const mutate $10_@1 = null
164
- [3] Const mutate $11_@0[0:9] = Binary read $9_@0 != read $10_@1
165
- [4] If (read $11_@0) then:bb2 else:bb3
164
+ [3] Const mutate $11_@2 = Binary read $9_@0 != read $10_@1
165
+ [4] If (read $11_@2) then:bb2 else:bb3
166
bb2:
167
predecessor blocks: bb0
168
- [5] Const mutate $12_@0[0:9] = read $9_@0
168
+ [5] Const mutate $12_@3[0:8] = read $9_@0
169
[6] Goto bb1
170
bb3:
171
predecessor blocks: bb0
172
- [7] Const mutate $13_@0[0:9] = Call mutate g$7_@0()
172
+ [7] Const mutate $13_@3[0:8] = Call mutate g$7_@3()
173
[8] Goto bb1
174
bb1:
175
predecessor blocks: bb2 bb3
176
- $14_@0[0:9]: phi(bb2: $12_@0, bb3: $13_@0)
177
- [9] Return freeze $14_@0
178
-
176
+ $14_@3[0:8]: phi(bb2: $12_@3, bb3: $13_@3)
177
+ [9] Return freeze $14_@3
178
+scope2 [3:4]:
179
+ - read $9_@0
180
+ - read $10_@1
181
```
182
183
### CFG
@@ -185,26 +187,26 @@ flowchart TB
187
%% Basic Blocks
188
subgraph bb0
189
bb0_instrs["
188
- [1] Const mutate $9_@0[0:9] = Call mutate f$2_@0()
190
+ [1] Const mutate $9_@0[0:2] = Call mutate f$2_@0()
191
[2] Const mutate $10_@1 = null
190
- [3] Const mutate $11_@0[0:9] = Binary read $9_@0 != read $10_@1
192
+ [3] Const mutate $11_@2 = Binary read $9_@0 != read $10_@1
193
"]
192
- bb0_instrs --> bb0_terminal(["If (read $11_@0)"])
194
+ bb0_instrs --> bb0_terminal(["If (read $11_@2)"])
195
end
196
subgraph bb2
197
bb2_instrs["
196
- [5] Const mutate $12_@0[0:9] = read $9_@0
198
+ [5] Const mutate $12_@3[0:8] = read $9_@0
199
"]
200
bb2_instrs --> bb2_terminal(["Goto"])
201
end
202
subgraph bb3
203
bb3_instrs["
202
- [7] Const mutate $13_@0[0:9] = Call mutate g$7_@0()
204
+ [7] Const mutate $13_@3[0:8] = Call mutate g$7_@3()
205
"]
206
bb3_instrs --> bb3_terminal(["Goto"])
207
end
208
subgraph bb1
207
- bb1_terminal(["Return freeze $14_@0"])
209
+ bb1_terminal(["Return freeze $14_@3"])
210
end
211
212
%% Jumps
compiler/forget/src/__tests__/fixtures/hir/ssa-if-else.expect.md
+11
-7
@@ -19,21 +19,25 @@ function foo() {
19
20
```
21
bb0:
22
- [1] Let mutate x$5_@0[1:8] = 1
22
+ [1] Let mutate x$5_@0 = 1
23
[2] Let mutate y$6_@1 = 2
24
[3] If (read y$6_@1) then:bb2 else:bb3
25
bb2:
26
predecessor blocks: bb0
27
- [4] Let mutate z$7_@0[1:8] = Binary read x$5_@0 + read y$6_@1
27
+ [4] Let mutate z$7_@2 = Binary read x$5_@0 + read y$6_@1
28
[5] Goto bb1
29
bb3:
30
predecessor blocks: bb0
31
- [6] Let mutate z$8_@0[1:8] = read x$5_@0
31
+ [6] Let mutate z$8_@3 = read x$5_@0
32
[7] Goto bb1
33
bb1:
34
predecessor blocks: bb2 bb3
35
[8] Return
36
-
36
+scope2 [4:5]:
37
+ - read x$5_@0
38
+ - read y$6_@1
39
+scope3 [6:7]:
40
+ - read x$5_@0
41
```
42
43
### CFG
@@ -43,20 +47,20 @@ flowchart TB
47
%% Basic Blocks
48
subgraph bb0
49
bb0_instrs["
46
- [1] Let mutate x$5_@0[1:8] = 1
50
+ [1] Let mutate x$5_@0 = 1
51
[2] Let mutate y$6_@1 = 2
52
"]
53
bb0_instrs --> bb0_terminal(["If (read y$6_@1)"])
54
end
55
subgraph bb2
56
bb2_instrs["
53
- [4] Let mutate z$7_@0[1:8] = Binary read x$5_@0 + read y$6_@1
57
+ [4] Let mutate z$7_@2 = Binary read x$5_@0 + read y$6_@1
58
"]
59
bb2_instrs --> bb2_terminal(["Goto"])
60
end
61
subgraph bb3
62
bb3_instrs["
59
- [6] Let mutate z$8_@0[1:8] = read x$5_@0
63
+ [6] Let mutate z$8_@3 = read x$5_@0
64
"]
65
bb3_instrs --> bb3_terminal(["Goto"])
66
end