Skip to content

Commit 5063b32

Browse files
authored
[compiler] Remove now-unused FunctionEffect type (#34029)
The new mutation/aliasing model significantly expands on the idea of FunctionEffect. The type (and its usage in HIRFunction.effects) was only necessary for the now-deleted old inference model so we can clean up this code now.
1 parent eaf6adb commit 5063b32

File tree

5 files changed

+11
-64
lines changed

5 files changed

+11
-64
lines changed

compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import {BindingKind} from '@babel/traverse';
99
import * as t from '@babel/types';
10-
import {CompilerError, CompilerErrorDetailOptions} from '../CompilerError';
10+
import {CompilerError} from '../CompilerError';
1111
import {assertExhaustive} from '../Utils/utils';
1212
import {Environment, ReactFunctionType} from './Environment';
1313
import type {HookKind} from './ObjectShape';
@@ -282,30 +282,13 @@ export type HIRFunction = {
282282
returnTypeAnnotation: t.FlowType | t.TSType | null;
283283
returns: Place;
284284
context: Array<Place>;
285-
effects: Array<FunctionEffect> | null;
286285
body: HIR;
287286
generator: boolean;
288287
async: boolean;
289288
directives: Array<string>;
290-
aliasingEffects?: Array<AliasingEffect> | null;
289+
aliasingEffects: Array<AliasingEffect> | null;
291290
};
292291

293-
export type FunctionEffect =
294-
| {
295-
kind: 'GlobalMutation';
296-
error: CompilerErrorDetailOptions;
297-
}
298-
| {
299-
kind: 'ReactMutation';
300-
error: CompilerErrorDetailOptions;
301-
}
302-
| {
303-
kind: 'ContextMutation';
304-
places: ReadonlySet<Place>;
305-
effect: Effect;
306-
loc: SourceLocation;
307-
};
308-
309292
/*
310293
* Each reactive scope may have its own control-flow, so the instructions form
311294
* a control-flow graph. The graph comprises a set of basic blocks which reference

compiler/packages/babel-plugin-react-compiler/src/HIR/PrintHIR.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -554,23 +554,11 @@ export function printInstructionValue(instrValue: ReactiveValue): string {
554554
const context = instrValue.loweredFunc.func.context
555555
.map(dep => printPlace(dep))
556556
.join(',');
557-
const effects =
558-
instrValue.loweredFunc.func.effects
559-
?.map(effect => {
560-
if (effect.kind === 'ContextMutation') {
561-
return `ContextMutation places=[${[...effect.places]
562-
.map(place => printPlace(place))
563-
.join(', ')}] effect=${effect.effect}`;
564-
} else {
565-
return `GlobalMutation`;
566-
}
567-
})
568-
.join(', ') ?? '';
569557
const aliasingEffects =
570558
instrValue.loweredFunc.func.aliasingEffects
571559
?.map(printAliasingEffect)
572560
?.join(', ') ?? '';
573-
value = `${kind} ${name} @context[${context}] @effects[${effects}] @aliasingEffects=[${aliasingEffects}]\n${fn}`;
561+
value = `${kind} ${name} @context[${context}] @aliasingEffects=[${aliasingEffects}]\n${fn}`;
574562
break;
575563
}
576564
case 'TaggedTemplateExpression': {

compiler/packages/babel-plugin-react-compiler/src/Optimization/LowerContextAccess.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,14 @@ function emitSelectorFn(env: Environment, keys: Array<string>): Instruction {
255255
returnTypeAnnotation: null,
256256
returns: createTemporaryPlace(env, GeneratedSource),
257257
context: [],
258-
effects: null,
259258
body: {
260259
entry: block.id,
261260
blocks: new Map([[block.id, block]]),
262261
},
263262
generator: false,
264263
async: false,
265264
directives: [],
265+
aliasingEffects: [],
266266
};
267267

268268
reversePostorderBlocks(fn.body);

compiler/packages/babel-plugin-react-compiler/src/Optimization/OutlineJsx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,14 +370,14 @@ function emitOutlinedFn(
370370
returnTypeAnnotation: null,
371371
returns: createTemporaryPlace(env, GeneratedSource),
372372
context: [],
373-
effects: null,
374373
body: {
375374
entry: block.id,
376375
blocks: new Map([[block.id, block]]),
377376
},
378377
generator: false,
379378
async: false,
380379
directives: [],
380+
aliasingEffects: [],
381381
};
382382
return fn;
383383
}

compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateNoFreezingKnownMutableFunctions.ts

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,17 @@
77

88
import {CompilerDiagnostic, CompilerError, Effect, ErrorSeverity} from '..';
99
import {
10-
FunctionEffect,
1110
HIRFunction,
1211
IdentifierId,
13-
isMutableEffect,
1412
isRefOrRefLikeMutableType,
1513
Place,
1614
} from '../HIR';
1715
import {
1816
eachInstructionValueOperand,
1917
eachTerminalOperand,
2018
} from '../HIR/visitors';
19+
import {AliasingEffect} from '../Inference/AliasingEffects';
2120
import {Result} from '../Utils/Result';
22-
import {Iterable_some} from '../Utils/utils';
2321

2422
/**
2523
* Validates that functions with known mutations (ie due to types) cannot be passed
@@ -50,14 +48,14 @@ export function validateNoFreezingKnownMutableFunctions(
5048
const errors = new CompilerError();
5149
const contextMutationEffects: Map<
5250
IdentifierId,
53-
Extract<FunctionEffect, {kind: 'ContextMutation'}>
51+
Extract<AliasingEffect, {kind: 'Mutate'} | {kind: 'MutateTransitive'}>
5452
> = new Map();
5553

5654
function visitOperand(operand: Place): void {
5755
if (operand.effect === Effect.Freeze) {
5856
const effect = contextMutationEffects.get(operand.identifier.id);
5957
if (effect != null) {
60-
const place = [...effect.places][0];
58+
const place = effect.value;
6159
const variable =
6260
place != null &&
6361
place.identifier.name != null &&
@@ -77,7 +75,7 @@ export function validateNoFreezingKnownMutableFunctions(
7775
})
7876
.withDetail({
7977
kind: 'error',
80-
loc: effect.loc,
78+
loc: effect.value.loc,
8179
message: `This modifies ${variable}`,
8280
}),
8381
);
@@ -108,24 +106,7 @@ export function validateNoFreezingKnownMutableFunctions(
108106
break;
109107
}
110108
case 'FunctionExpression': {
111-
const knownMutation = (value.loweredFunc.func.effects ?? []).find(
112-
effect => {
113-
return (
114-
effect.kind === 'ContextMutation' &&
115-
(effect.effect === Effect.Store ||
116-
effect.effect === Effect.Mutate) &&
117-
Iterable_some(effect.places, place => {
118-
return (
119-
isMutableEffect(place.effect, place.loc) &&
120-
!isRefOrRefLikeMutableType(place.identifier.type)
121-
);
122-
})
123-
);
124-
},
125-
);
126-
if (knownMutation && knownMutation.kind === 'ContextMutation') {
127-
contextMutationEffects.set(lvalue.identifier.id, knownMutation);
128-
} else if (value.loweredFunc.func.aliasingEffects != null) {
109+
if (value.loweredFunc.func.aliasingEffects != null) {
129110
const context = new Set(
130111
value.loweredFunc.func.context.map(p => p.identifier.id),
131112
);
@@ -146,12 +127,7 @@ export function validateNoFreezingKnownMutableFunctions(
146127
context.has(effect.value.identifier.id) &&
147128
!isRefOrRefLikeMutableType(effect.value.identifier.type)
148129
) {
149-
contextMutationEffects.set(lvalue.identifier.id, {
150-
kind: 'ContextMutation',
151-
effect: Effect.Mutate,
152-
loc: effect.value.loc,
153-
places: new Set([effect.value]),
154-
});
130+
contextMutationEffects.set(lvalue.identifier.id, effect);
155131
break effects;
156132
}
157133
break;

0 commit comments

Comments
 (0)