Skip to content

Commit

Permalink
[compile] Error on fire outside of effects and ensure correct compila…
Browse files Browse the repository at this point in the history
…tion

Traverse the compiled functions to ensure there are no lingering fires and that all
fire calls are inside an effect lambda.


--
  • Loading branch information
jbrown215 committed Dec 16, 2024
1 parent 5795b48 commit c8d53fc
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,14 @@ export function printSourceLocation(loc: SourceLocation): string {
}
}

export function printSourceLocationLine(loc: SourceLocation): string {
if (typeof loc === 'symbol') {
return 'generated';
} else {
return `${loc.start.line}:${loc.end.line}`;
}
}

export function printAliases(aliases: DisjointSet<Identifier>): string {
const aliasSets = aliases.buildSets();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
* LICENSE file in the root directory of this source tree.
*/

import {CompilerError, CompilerErrorDetailOptions, ErrorSeverity} from '..';
import {
CompilerError,
CompilerErrorDetailOptions,
ErrorSeverity,
SourceLocation,
} from '..';
import {
CallExpression,
Effect,
Expand All @@ -28,14 +33,11 @@ import {
import {createTemporaryPlace, markInstructionIds} from '../HIR/HIRBuilder';
import {getOrInsertWith} from '../Utils/utils';
import {BuiltInFireId, DefaultNonmutatingHook} from '../HIR/ObjectShape';
import {eachInstructionOperand} from '../HIR/visitors';
import {printSourceLocationLine} from '../HIR/PrintHIR';

/*
* TODO(jmbrown):
* In this stack:
* - Assert no lingering fire calls
* - Ensure a fired function is not called regularly elsewhere in the same effect
*
* Future:
* - rewrite dep arrays
* - traverse object methods
* - method calls
Expand Down Expand Up @@ -187,6 +189,7 @@ type FireCalleesToFireFunctionBinding = Map<
{
fireFunctionBinding: Place;
capturedCalleeIdentifier: Identifier;
fireLoc: SourceLocation;
}
>;

Expand Down Expand Up @@ -295,7 +298,11 @@ class Context {
return this.#loadLocals.get(id);
}

getOrGenerateFireFunctionBinding(callee: Place, env: Environment): Place {
getOrGenerateFireFunctionBinding(
callee: Place,
env: Environment,
fireLoc: SourceLocation,
): Place {
const fireFunctionBinding = getOrInsertWith(
this.#fireCalleesToFireFunctions,
callee.identifier.id,
Expand All @@ -305,6 +312,7 @@ class Context {
this.#capturedCalleeIdentifierIds.set(callee.identifier.id, {
fireFunctionBinding,
capturedCalleeIdentifier: callee.identifier,
fireLoc,
});

return fireFunctionBinding;
Expand Down Expand Up @@ -346,8 +354,12 @@ class Context {
return this.#loadGlobalInstructionIds.get(id);
}

hasErrors(): boolean {
return this.#errors.hasErrors();
}

throwIfErrorsFound(): void {
if (this.#errors.hasErrors()) throw this.#errors;
if (this.hasErrors()) throw this.#errors;
}
}

Expand Down Expand Up @@ -510,6 +522,11 @@ function replaceFireFunctions(fn: HIRFunction, context: Context): void {
rewriteInstrs.set(loadUseEffectInstrId, newInstrs);
}
}
ensureNoRemainingCalleeCaptures(
lambda.loweredFunc.func,
context,
capturedCallees,
);
}
} else if (
value.kind === 'CallExpression' &&
Expand Down Expand Up @@ -551,6 +568,7 @@ function replaceFireFunctions(fn: HIRFunction, context: Context): void {
context.getOrGenerateFireFunctionBinding(
{...loadLocal.place},
fn.env,
value.loc,
);

loadLocal.place = {...fireFunctionBinding};
Expand Down Expand Up @@ -626,8 +644,74 @@ function replaceFireFunctions(fn: HIRFunction, context: Context): void {
}
}

/*
* eachInstructionOperand is not sufficient for our cases because:
* 1. fire is a global, which will not appear
* 2. The HIR may be malformed, so can't rely on function deps and must
* traverse the whole function.
*/
function* eachReachablePlace(fn: HIRFunction): Iterable<Place> {
for (const [, block] of fn.body.blocks) {
for (const instr of block.instructions) {
if (
instr.value.kind === 'FunctionExpression' ||
instr.value.kind === 'ObjectMethod'
) {
yield* eachReachablePlace(instr.value.loweredFunc.func);
} else {
yield* eachInstructionOperand(instr);
}
}
}
}

function ensureNoRemainingCalleeCaptures(
fn: HIRFunction,
context: Context,
capturedCallees: FireCalleesToFireFunctionBinding,
): void {
for (const place of eachReachablePlace(fn)) {
const calleeInfo = capturedCallees.get(place.identifier.id);
if (calleeInfo != null) {
const calleeName =
calleeInfo.capturedCalleeIdentifier.name?.kind === 'named'
? calleeInfo.capturedCalleeIdentifier.name.value
: '<unknown>';
context.pushError({
loc: place.loc,
description: `All uses of ${calleeName} must be either used with a fire() call in \
this effect or not used with a fire() call at all. ${calleeName} was used with fire() on line \
${printSourceLocationLine(calleeInfo.fireLoc)} in this effect`,
severity: ErrorSeverity.InvalidReact,
reason: CANNOT_COMPILE_FIRE,
suggestions: null,
});
}
}
}

function ensureNoMoreFireUses(fn: HIRFunction, context: Context): void {
for (const place of eachReachablePlace(fn)) {
if (
place.identifier.type.kind === 'Function' &&
place.identifier.type.shapeId === BuiltInFireId
) {
context.pushError({
loc: place.identifier.loc,
description: 'Cannot use `fire` outside of a useEffect function',
severity: ErrorSeverity.Invariant,
reason: CANNOT_COMPILE_FIRE,
suggestions: null,
});
}
}
}

export function transformFire(fn: HIRFunction): void {
const context = new Context();
replaceFireFunctions(fn, context);
if (!context.hasErrors()) {
ensureNoMoreFireUses(fn, context);
}
context.throwIfErrorsFound();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

## Input

```javascript
// @enableFire
import {fire} from 'react';

function Component(props) {
const foo = props => {
console.log(props);
};
useEffect(() => {
function nested() {
fire(foo(props));
foo(props);
}

nested();
});

return null;
}

```


## Error

```
9 | function nested() {
10 | fire(foo(props));
> 11 | foo(props);
| ^^^ InvalidReact: Cannot compile `fire`. All uses of foo must be either used with a fire() call in this effect or not used with a fire() call at all. foo was used with fire() on line 10:10 in this effect (11:11)
12 | }
13 |
14 | nested();
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// @enableFire
import {fire} from 'react';

function Component(props) {
const foo = props => {
console.log(props);
};
useEffect(() => {
function nested() {
fire(foo(props));
foo(props);
}

nested();
});

return null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

## Input

```javascript
// @enableFire
import {fire, useCallback} from 'react';

function Component({props, bar}) {
const foo = () => {
console.log(props);
};
fire(foo(props));

useCallback(() => {
fire(foo(props));
}, [foo, props]);

return null;
}

```


## Error

```
6 | console.log(props);
7 | };
> 8 | fire(foo(props));
| ^^^^ Invariant: Cannot compile `fire`. Cannot use `fire` outside of a useEffect function (8:8)
Invariant: Cannot compile `fire`. Cannot use `fire` outside of a useEffect function (11:11)
9 |
10 | useCallback(() => {
11 | fire(foo(props));
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @enableFire
import {fire, useCallback} from 'react';

function Component({props, bar}) {
const foo = () => {
console.log(props);
};
fire(foo(props));

useCallback(() => {
fire(foo(props));
}, [foo, props]);

return null;
}

0 comments on commit c8d53fc

Please sign in to comment.