Use implicit return (undefined)

Making ReturnTerminal.value non-nullable broke our optimization to elide final 
value-less return statements. We now check if a return value is explicitly 
`undefined` and elide the value in this case, which then also propagates to 
allow removing the final `return` statement of a function if the value is 
missing.
This commit is contained in:
Joe Savona
2023-04-18 11:12:27 -07:00
parent b10f60f2b9
commit de89ed32a2
51 changed files with 82 additions and 158 deletions
@@ -388,6 +388,10 @@ function codegenTerminal(
}
case "return": {
const value = codegenPlace(cx, terminal.value);
if (value.type === "Identifier" && value.name === "undefined") {
// Use implicit undefined
return t.returnStatement();
}
return t.returnStatement(value);
}
case "switch": {
@@ -26,7 +26,6 @@ function Component() {
import * as React from "react"; // Should print A, B, arg, original
function changeF(o) {
o.f = () => console.log("new");
return undefined;
}
function Component() {
@@ -26,7 +26,6 @@ import * as React from "react"; // Should print A, arg, original
function changeF(o) {
o.f = () => console.log("new");
return undefined;
}
function Component() {
@@ -50,9 +50,7 @@ function foo(cond) {
return a;
}
function mutate(x, y) {
return undefined;
}
function mutate(x, y) {}
```
@@ -19,9 +19,7 @@ function Component(props) {
```javascript
import * as React from "react";
function foo() {
return undefined;
}
function foo() {}
function Component(props) {
const $ = React.unstable_useMemoCache(3);
@@ -105,12 +105,8 @@ function ComponentB(props) {
return t0;
}
function Foo() {
return undefined;
}
function mayMutate() {
return undefined;
}
function Foo() {}
function mayMutate() {}
```
@@ -19,9 +19,7 @@ function Component(props) {
```javascript
import * as React from "react";
function Foo() {
return undefined;
}
function Foo() {}
function Component(props) {
const $ = React.unstable_useMemoCache(3);
@@ -18,9 +18,9 @@ function MyApp(props) {
```javascript
function MyApp(props) {
if (props.cond) {
return undefined;
return;
} else {
return undefined;
return;
}
}
@@ -15,9 +15,8 @@ function Component(props) {
```javascript
function Component(props) {
for (const i = 0; 0 < props.count; ) {
return undefined;
return;
}
return undefined;
}
```
@@ -35,12 +35,8 @@ function Component() {
return b;
}
function useFreeze() {
return undefined;
}
function foo(x) {
return undefined;
}
function useFreeze() {}
function foo(x) {}
```
@@ -23,12 +23,8 @@ function Component(props) {
```javascript
import * as React from "react";
function useFreeze() {
return undefined;
}
function foo() {
return undefined;
}
function useFreeze() {}
function foo() {}
function Component(props) {
const $ = React.unstable_useMemoCache(3);
@@ -35,12 +35,8 @@ function Component() {
return a;
}
function useFreeze(x) {
return undefined;
}
function call(x) {
return undefined;
}
function useFreeze(x) {}
function call(x) {}
```
@@ -50,12 +50,8 @@ function Component(props) {
return a;
}
function useFreeze(x) {
return undefined;
}
function call(x) {
return undefined;
}
function useFreeze(x) {}
function call(x) {}
```
@@ -37,18 +37,10 @@ function Component(props) {
```javascript
import * as React from "react";
function compute() {
return undefined;
}
function mutate() {
return undefined;
}
function foo() {
return undefined;
}
function Foo() {
return undefined;
}
function compute() {}
function mutate() {}
function foo() {}
function Foo() {}
/**
* Should produce 3 scopes:
@@ -74,15 +74,9 @@ function Component(props) {
return t2;
}
function compute() {
return undefined;
}
function foo() {
return undefined;
}
function Foo() {
return undefined;
}
function compute() {}
function foo() {}
function Foo() {}
```
@@ -31,15 +31,9 @@ function Component(props) {
```javascript
import * as React from "react";
function compute() {
return undefined;
}
function foo() {
return undefined;
}
function Foo() {
return undefined;
}
function compute() {}
function foo() {}
function Foo() {}
/**
* Should produce 1 scope:
@@ -69,15 +69,9 @@ function Component(props) {
return t0;
}
function compute() {
return undefined;
}
function foo() {
return undefined;
}
function Foo() {
return undefined;
}
function compute() {}
function foo() {}
function Foo() {}
```
@@ -17,7 +17,6 @@ function Component(c) {
function Component(c) {
const x = { c };
mutate(x);
return undefined;
}
```
@@ -42,12 +42,8 @@ function Component(props) {
## Code
```javascript
function mutate(x, y) {
return undefined;
}
function cond(x) {
return undefined;
}
function mutate(x, y) {}
function cond(x) {}
function Component(props) {
let a = {};
@@ -75,7 +71,6 @@ function Component(props) {
}
mutate(d, null);
return undefined;
}
```
@@ -38,9 +38,7 @@ function Component(props) {
## Code
```javascript
function mutate(x, y) {
return undefined;
}
function mutate(x, y) {}
function Component(props) {
const a = {};
@@ -63,7 +61,6 @@ function Component(props) {
}
mutate(x, null);
return undefined;
}
```
@@ -37,12 +37,8 @@ function Component(props) {
## Code
```javascript
function mutate() {
return undefined;
}
function cond() {
return undefined;
}
function mutate() {}
function cond() {}
function Component(props) {
const a = {};
@@ -65,7 +61,6 @@ function Component(props) {
}
mutate(d, null);
return undefined;
}
```
@@ -15,7 +15,6 @@ function foo(a, b, c) {
function foo(a, b, c) {
a[b] = c[b];
a[3] = c[b * 4];
return undefined;
}
```
@@ -19,7 +19,6 @@ function foo(a, b, c) {
const y = b.c.d;
y.z = c.d.e;
foo(a.b.c);
return undefined;
}
```
@@ -26,7 +26,6 @@ function foo(a, b, c) {
y.push(a);
x.push(b);
return undefined;
}
```
@@ -19,7 +19,6 @@ function foo(a, b) {
const y = [];
x.push(a);
y.push(b);
return undefined;
}
```
@@ -19,7 +19,6 @@ function foo(a, b) {
const y = [];
y.push(b);
x.push(a);
return undefined;
}
```
@@ -23,7 +23,6 @@ function foo(a, b, c) {
y.push(b);
x.push(a);
}
return undefined;
}
```
@@ -0,0 +1,25 @@
## Input
```javascript
function Component(props) {
if (props.cond) {
return undefined;
}
return props.value;
}
```
## Code
```javascript
function Component(props) {
if (props.cond) {
return;
}
return props.value;
}
```
@@ -0,0 +1,6 @@
function Component(props) {
if (props.cond) {
return undefined;
}
return props.value;
}
@@ -54,7 +54,6 @@ function Component(props) {
} else {
}
}
return undefined;
}
```
@@ -42,9 +42,7 @@ function sequence(props) {
return x;
}
function foo() {
return undefined;
}
function foo() {}
```
@@ -20,9 +20,7 @@ function foo() {
```javascript
import * as React from "react";
function mutate() {
return undefined;
}
function mutate() {}
function foo() {
const $ = React.unstable_useMemoCache(1);
let c;
@@ -22,9 +22,7 @@ function Component(props) {
```javascript
import * as React from "react"; // @Pass runMutableRangeAnalysis
function foo() {
return undefined;
}
function foo() {}
function Component(props) {
const $ = React.unstable_useMemoCache(3);
@@ -19,9 +19,7 @@ function Component(props) {
```javascript
import * as React from "react";
function foo() {
return undefined;
}
function foo() {}
function Component(props) {
const $ = React.unstable_useMemoCache(3);
@@ -20,9 +20,7 @@ function foo() {
## Code
```javascript
function foo() {
return undefined;
}
function foo() {}
```
@@ -17,9 +17,7 @@ function foo() {
## Code
```javascript
function foo() {
return undefined;
}
function foo() {}
```
@@ -18,9 +18,7 @@ function foo() {
## Code
```javascript
function foo() {
return undefined;
}
function foo() {}
```
@@ -26,7 +26,6 @@ function foo(a, b, c) {
}
return x;
}
return undefined;
}
```
@@ -17,9 +17,7 @@ function Component(props) {
```javascript
import * as React from "react";
function Foo() {
return undefined;
}
function Foo() {}
function Component(props) {
const $ = React.unstable_useMemoCache(1);
@@ -20,9 +20,7 @@ function Foo(cond) {
## Code
```javascript
function log() {
return undefined;
}
function log() {}
function Foo(cond) {
let str = "";
@@ -33,7 +31,6 @@ function Foo(cond) {
}
log(str);
return undefined;
}
```
@@ -27,9 +27,7 @@ function foo(a, b, c, d) {
## Code
```javascript
function foo(a, b, c, d) {
return undefined;
}
function foo(a, b, c, d) {}
```
@@ -19,9 +19,7 @@ function foo() {
## Code
```javascript
function foo() {
return undefined;
}
function foo() {}
```
@@ -12,9 +12,7 @@ function foo() {
## Code
```javascript
function foo() {
return undefined;
}
function foo() {}
```
@@ -16,9 +16,7 @@ function foo() {
## Code
```javascript
function foo() {
return undefined;
}
function foo() {}
```
@@ -38,7 +38,6 @@ function foo() {
default: {
}
}
return undefined;
}
```
@@ -55,7 +55,6 @@ function foo(x) {
default: {
}
}
return undefined;
}
```
@@ -31,7 +31,6 @@ function component() {
q.y = p.y;
mutate(q);
return undefined;
}
```
@@ -16,7 +16,6 @@ function component(a, b) {
function component(a, b) {
if (a > b) {
}
return undefined;
}
```
@@ -20,7 +20,6 @@ function component() {
const b = someOther();
if (a > b) {
}
return undefined;
}
```
@@ -18,7 +18,6 @@ function component(a) {
// @inlineUseMemo
function component(a) {
mutate(a);
return undefined;
}
```
@@ -28,7 +28,6 @@ function foo(a, b, c, d) {
}
d();
return undefined;
}
```