Skip to content

Commit fab748d

Browse files
committed
[compiler] More claude config
1 parent 20b98f0 commit fab748d

File tree

100 files changed

+2464
-2023
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+2464
-2023
lines changed

compiler/.claude/settings.local.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
"Bash(done)",
1010
"Bash(cat:*)",
1111
"Bash(sl revert:*)",
12-
"Bash(yarn workspace snap run build:*)"
12+
"Bash(yarn workspace snap run build:*)",
13+
"Bash(yarn tsc:*)",
14+
"Bash(yarn snap:build)",
15+
"Bash(timeout 30 yarn snap:*)"
1316
],
1417
"deny": [],
1518
"ask": []

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,6 @@ function runWithEnvironment(
271271
assertValidMutableRanges(hir);
272272
}
273273

274-
if (env.config.validateRefAccessDuringRender) {
275-
validateNoRefAccessInRender(hir).unwrap();
276-
}
277-
278274
if (env.config.validateNoSetStateInRender) {
279275
validateNoSetStateInRender(hir).unwrap();
280276
}
@@ -296,10 +292,17 @@ function runWithEnvironment(
296292
env.logErrors(validateNoJSXInTryStatement(hir));
297293
}
298294

299-
if (env.config.validateNoImpureFunctionsInRender) {
295+
if (
296+
env.config.validateNoImpureFunctionsInRender ||
297+
env.config.validateRefAccessDuringRender
298+
) {
300299
validateNoImpureValuesInRender(hir).unwrap();
301300
}
302301

302+
if (env.config.validateRefAccessDuringRender) {
303+
validateNoRefAccessInRender(hir).unwrap();
304+
}
305+
303306
validateNoFreezingKnownMutableFunctions(hir).unwrap();
304307
}
305308

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

Lines changed: 78 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ const RenderHookAliasing: (
664664

665665
const EffectHookAliasing: AliasingSignatureConfig = {
666666
receiver: '@receiver',
667-
params: [],
667+
params: ['@fn', '@deps'],
668668
rest: '@rest',
669669
returns: '@returns',
670670
temporaries: ['@effect'],
@@ -675,6 +675,21 @@ const EffectHookAliasing: AliasingSignatureConfig = {
675675
value: '@rest',
676676
reason: ValueReason.Effect,
677677
},
678+
{
679+
kind: 'Freeze',
680+
value: '@fn',
681+
reason: ValueReason.Effect,
682+
},
683+
{
684+
kind: 'Freeze',
685+
value: '@deps',
686+
reason: ValueReason.Effect,
687+
},
688+
// Deps are accessed during render
689+
{
690+
kind: 'Render',
691+
place: '@deps',
692+
},
678693
// Internally creates an effect object that captures the function and deps
679694
{
680695
kind: 'Create',
@@ -688,6 +703,11 @@ const EffectHookAliasing: AliasingSignatureConfig = {
688703
from: '@rest',
689704
into: '@effect',
690705
},
706+
{
707+
kind: 'Capture',
708+
from: '@fn',
709+
into: '@effect',
710+
},
691711
// Returns undefined
692712
{
693713
kind: 'Create',
@@ -703,6 +723,39 @@ const EffectHookAliasing: AliasingSignatureConfig = {
703723
* now that FeatureFlag `enableTreatHooksAsFunctions` is removed we can
704724
* use positional params too (?)
705725
*/
726+
const useEffectEvent = addHook(
727+
DEFAULT_SHAPES,
728+
{
729+
positionalParams: [],
730+
restParam: Effect.Freeze,
731+
returnType: {
732+
kind: 'Function',
733+
return: {kind: 'Poly'},
734+
shapeId: BuiltInEffectEventId,
735+
isConstructor: false,
736+
},
737+
calleeEffect: Effect.Read,
738+
hookKind: 'useEffectEvent',
739+
// Frozen because it should not mutate any locally-bound values
740+
returnValueKind: ValueKind.Frozen,
741+
aliasing: {
742+
receiver: '@receiver',
743+
params: ['@value'],
744+
rest: null,
745+
returns: '@return',
746+
temporaries: [],
747+
effects: [
748+
{kind: 'Assign', from: '@value', into: '@return'},
749+
{
750+
kind: 'Freeze',
751+
value: '@value',
752+
reason: ValueReason.HookCaptured,
753+
},
754+
],
755+
},
756+
},
757+
BuiltInUseEffectEventId,
758+
);
706759
const REACT_APIS: Array<[string, BuiltInType]> = [
707760
[
708761
'useContext',
@@ -769,6 +822,22 @@ const REACT_APIS: Array<[string, BuiltInType]> = [
769822
calleeEffect: Effect.Read,
770823
hookKind: 'useRef',
771824
returnValueKind: ValueKind.Mutable,
825+
aliasing: {
826+
receiver: '@receiver',
827+
params: [],
828+
rest: '@rest',
829+
returns: '@return',
830+
temporaries: [],
831+
effects: [
832+
{
833+
kind: 'Create',
834+
into: '@return',
835+
value: ValueKind.Mutable,
836+
reason: ValueReason.KnownReturnSignature,
837+
},
838+
{kind: 'Capture', from: '@rest', into: '@return'},
839+
],
840+
},
772841
}),
773842
],
774843
[
@@ -799,7 +868,12 @@ const REACT_APIS: Array<[string, BuiltInType]> = [
799868
addHook(DEFAULT_SHAPES, {
800869
positionalParams: [],
801870
restParam: Effect.Freeze,
802-
returnType: {kind: 'Poly'},
871+
returnType: {
872+
kind: 'Function',
873+
isConstructor: false,
874+
return: {kind: 'Poly'},
875+
shapeId: null,
876+
},
803877
calleeEffect: Effect.Read,
804878
hookKind: 'useCallback',
805879
returnValueKind: ValueKind.Frozen,
@@ -915,27 +989,8 @@ const REACT_APIS: Array<[string, BuiltInType]> = [
915989
BuiltInFireId,
916990
),
917991
],
918-
[
919-
'useEffectEvent',
920-
addHook(
921-
DEFAULT_SHAPES,
922-
{
923-
positionalParams: [],
924-
restParam: Effect.Freeze,
925-
returnType: {
926-
kind: 'Function',
927-
return: {kind: 'Poly'},
928-
shapeId: BuiltInEffectEventId,
929-
isConstructor: false,
930-
},
931-
calleeEffect: Effect.Read,
932-
hookKind: 'useEffectEvent',
933-
// Frozen because it should not mutate any locally-bound values
934-
returnValueKind: ValueKind.Frozen,
935-
},
936-
BuiltInUseEffectEventId,
937-
),
938-
],
992+
['useEffectEvent', useEffectEvent],
993+
['experimental_useEffectEvent', useEffectEvent],
939994
['AUTODEPS', addObject(DEFAULT_SHAPES, BuiltInAutodepsId, [])],
940995
];
941996

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1879,7 +1879,15 @@ export function isRefValueType(id: Identifier): boolean {
18791879
}
18801880

18811881
export function isUseRefType(id: Identifier): boolean {
1882-
return id.type.kind === 'Object' && id.type.shapeId === 'BuiltInUseRefId';
1882+
return isUseRefType_(id.type);
1883+
}
1884+
1885+
export function isUseRefType_(type: Type): boolean {
1886+
return (
1887+
(type.kind === 'Object' && type.shapeId === 'BuiltInUseRefId') ||
1888+
(type.kind === 'Phi' &&
1889+
type.operands.some(operand => isUseRefType_(operand)))
1890+
);
18831891
}
18841892

18851893
export function isUseStateType(id: Identifier): boolean {
@@ -1890,6 +1898,13 @@ export function isJsxType(type: Type): boolean {
18901898
return type.kind === 'Object' && type.shapeId === 'BuiltInJsx';
18911899
}
18921900

1901+
export function isJsxOrJsxUnionType(type: Type): boolean {
1902+
return (
1903+
(type.kind === 'Object' && type.shapeId === 'BuiltInJsx') ||
1904+
(type.kind === 'Phi' && type.operands.some(op => isJsxOrJsxUnionType(op)))
1905+
);
1906+
}
1907+
18931908
export function isRefOrRefValue(id: Identifier): boolean {
18941909
return isUseRefType(id) || isRefValueType(id);
18951910
}
@@ -2058,4 +2073,23 @@ export function getHookKindForType(
20582073
return null;
20592074
}
20602075

2076+
export function areEqualSourceLocations(
2077+
loc1: SourceLocation,
2078+
loc2: SourceLocation,
2079+
): boolean {
2080+
if (typeof loc1 === 'symbol' || typeof loc2 === 'symbol') {
2081+
return false;
2082+
}
2083+
return (
2084+
loc1.filename === loc2.filename &&
2085+
loc1.identifierName === loc2.identifierName &&
2086+
loc1.start.line === loc2.start.line &&
2087+
loc1.start.column === loc2.start.column &&
2088+
loc1.start.index === loc2.start.index &&
2089+
loc1.end.line === loc2.end.line &&
2090+
loc1.end.column === loc2.end.column &&
2091+
loc1.end.index === loc2.end.index
2092+
);
2093+
}
2094+
20612095
export * from './Types';

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ export function createTemporaryPlace(
988988
identifier: makeTemporaryIdentifier(env.nextIdentifierId, loc),
989989
reactive: false,
990990
effect: Effect.Unknown,
991-
loc: GeneratedSource,
991+
loc,
992992
};
993993
}
994994

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {CompilerError} from '../CompilerError';
8+
import {CompilerError, ErrorCategory} from '../CompilerError';
99
import {AliasingEffect, AliasingSignature} from '../Inference/AliasingEffects';
1010
import {assertExhaustive} from '../Utils/utils';
1111
import {
@@ -194,8 +194,11 @@ function parseAliasingSignatureConfig(
194194
return {
195195
kind: 'Impure',
196196
into,
197+
category: ErrorCategory.Purity,
197198
description: effect.description,
198199
reason: effect.reason,
200+
sourceMessage: effect.sourceMessage,
201+
usageMessage: effect.usageMessage,
199202
};
200203
}
201204
case 'Render': {

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -983,15 +983,15 @@ export function printAliasingEffect(effect: AliasingEffect): string {
983983
return `...${printPlaceForAliasEffect(arg.place)}`;
984984
})
985985
.join(', ');
986-
let signature = '';
987-
if (effect.signature != null) {
988-
if (effect.signature.aliasing != null) {
989-
signature = printAliasingSignature(effect.signature.aliasing);
990-
} else {
991-
signature = JSON.stringify(effect.signature, null, 2);
992-
}
993-
}
994-
return `Apply ${printPlaceForAliasEffect(effect.into)} = ${receiverCallee}(${args})${signature != '' ? '\n ' : ''}${signature}`;
986+
// let signature = '';
987+
// if (effect.signature != null) {
988+
// if (effect.signature.aliasing != null) {
989+
// signature = printAliasingSignature(effect.signature.aliasing);
990+
// } else {
991+
// signature = JSON.stringify(effect.signature, null, 2);
992+
// }
993+
// }
994+
return `Apply ${printPlaceForAliasEffect(effect.into)} = ${receiverCallee}(${args})`;
995995
}
996996
case 'Freeze': {
997997
return `Freeze ${printPlaceForAliasEffect(effect.value)} ${effect.reason}`;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,17 @@ export type ImpureEffectConfig = {
188188
into: string;
189189
reason: string;
190190
description: string;
191+
sourceMessage: string;
192+
usageMessage: string;
191193
};
192194

193195
export const ImpureEffectSchema: z.ZodType<ImpureEffectConfig> = z.object({
194196
kind: z.literal('Impure'),
195197
into: LifetimeIdSchema,
196198
reason: z.string(),
197199
description: z.string(),
200+
sourceMessage: z.string(),
201+
usageMessage: z.string(),
198202
});
199203

200204
export type RenderEffectConfig = {

compiler/packages/babel-plugin-react-compiler/src/Inference/AliasingEffects.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {CompilerDiagnostic} from '../CompilerError';
8+
import {CompilerDiagnostic, ErrorCategory} from '../CompilerError';
99
import {
1010
FunctionExpression,
1111
GeneratedSource,
@@ -162,7 +162,15 @@ export type AliasingEffect =
162162
/**
163163
* Indicates a side-effect that is not safe during render
164164
*/
165-
| {kind: 'Impure'; into: Place; reason: string; description: string}
165+
| {
166+
kind: 'Impure';
167+
into: Place;
168+
category: ErrorCategory;
169+
reason: string;
170+
description: string;
171+
usageMessage: string;
172+
sourceMessage: string;
173+
}
166174
/**
167175
* Indicates that a given place is accessed during render. Used to distingush
168176
* hook arguments that are known to be called immediately vs those used for
@@ -227,6 +235,8 @@ export function hashEffect(effect: AliasingEffect): string {
227235
effect.into.identifier.id,
228236
effect.reason,
229237
effect.description,
238+
effect.usageMessage,
239+
effect.sourceMessage,
230240
].join(':');
231241
case 'Render': {
232242
return [effect.kind, effect.place.identifier.id].join(':');

0 commit comments

Comments
 (0)