Skip to content

Commit c681973

Browse files
committed
[compiler] Fixes to enableTreatRefLikeIdentifiersAsRefs (#34000)
We added the `@enableTreatRefLikeIdentifiersAsRefs` feature a while back but never enabled it. Since then we've continued to see examples that motivate this mode, so here we're fixing it up to prepare to enable by default. It now works as follows: * If we find a property load or property store where both a) the object's name is ref-like (`ref` or `-Ref`) and b) the property is `current`, we infer the object itself as a ref and the value of the property as a ref value. Originally the feature only detected property loads, not stores. * Inferred refs are not considered stable (this is a change from the original implementation). The only way to get a stable ref is by calling `useRef()`. We've seen issues with assuming refs are stable. With this change, cases like the following now correctly error: ```js function Foo(props) { const fooRef = props.fooRef; fooRef.current = true; ^^^^^^^^^^^^^^ cannot modify ref in render } ``` --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/34000). * #34027 * #34026 * #34025 * #34024 * #34005 * #34006 * #34004 * #34003 * __->__ #34000 DiffTrain build for [85bbe39](85bbe39)
1 parent 16f8eb9 commit c681973

35 files changed

+99
-93
lines changed

compiled/eslint-plugin-react-hooks/index.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22073,6 +22073,7 @@ addObject(BUILTIN_SHAPES, BuiltInUseRefId, [
2207322073
addObject(BUILTIN_SHAPES, BuiltInRefValueId, [
2207422074
['*', { kind: 'Object', shapeId: BuiltInRefValueId }],
2207522075
]);
22076+
addObject(BUILTIN_SHAPES, ReanimatedSharedValueId, []);
2207622077
addFunction(BUILTIN_SHAPES, [], {
2207722078
positionalParams: [],
2207822079
restParam: Effect.ConditionallyMutate,
@@ -44110,12 +44111,6 @@ class StableSidemap {
4411044111
});
4411144112
}
4411244113
}
44113-
else if (this.env.config.enableTreatRefLikeIdentifiersAsRefs &&
44114-
isUseRefType(lvalue.identifier)) {
44115-
this.map.set(lvalue.identifier.id, {
44116-
isStable: true,
44117-
});
44118-
}
4411944114
break;
4412044115
}
4412144116
case 'Destructure':
@@ -47372,7 +47367,18 @@ function* generateInstructionTypes(env, names, instr) {
4737247367
yield equation(left, returnType);
4737347368
break;
4737447369
}
47375-
case 'PropertyStore':
47370+
case 'PropertyStore': {
47371+
yield equation(makeType(), {
47372+
kind: 'Property',
47373+
objectType: value.object.identifier.type,
47374+
objectName: getName(names, value.object.identifier.id),
47375+
propertyName: {
47376+
kind: 'literal',
47377+
value: value.property,
47378+
},
47379+
});
47380+
break;
47381+
}
4737647382
case 'DeclareLocal':
4737747383
case 'RegExpLiteral':
4737847384
case 'MetaProperty':

compiled/facebook-www/REVISION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
820af2097103309fdc5675d2bde744103a439eff
1+
85bbe39ef8e24a192b5e9f2987b1babf8ce772e1
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
820af2097103309fdc5675d2bde744103a439eff
1+
85bbe39ef8e24a192b5e9f2987b1babf8ce772e1

compiled/facebook-www/React-dev.classic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,7 @@ __DEV__ &&
14341434
exports.useTransition = function () {
14351435
return resolveDispatcher().useTransition();
14361436
};
1437-
exports.version = "19.2.0-www-classic-820af209-20250729";
1437+
exports.version = "19.2.0-www-classic-85bbe39e-20250729";
14381438
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
14391439
"function" ===
14401440
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/React-dev.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,7 @@ __DEV__ &&
14341434
exports.useTransition = function () {
14351435
return resolveDispatcher().useTransition();
14361436
};
1437-
exports.version = "19.2.0-www-modern-820af209-20250729";
1437+
exports.version = "19.2.0-www-modern-85bbe39e-20250729";
14381438
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
14391439
"function" ===
14401440
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/React-prod.classic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,4 +610,4 @@ exports.useSyncExternalStore = function (
610610
exports.useTransition = function () {
611611
return ReactSharedInternals.H.useTransition();
612612
};
613-
exports.version = "19.2.0-www-classic-820af209-20250729";
613+
exports.version = "19.2.0-www-classic-85bbe39e-20250729";

compiled/facebook-www/React-prod.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,4 +610,4 @@ exports.useSyncExternalStore = function (
610610
exports.useTransition = function () {
611611
return ReactSharedInternals.H.useTransition();
612612
};
613-
exports.version = "19.2.0-www-modern-820af209-20250729";
613+
exports.version = "19.2.0-www-modern-85bbe39e-20250729";

compiled/facebook-www/React-profiling.classic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ exports.useSyncExternalStore = function (
614614
exports.useTransition = function () {
615615
return ReactSharedInternals.H.useTransition();
616616
};
617-
exports.version = "19.2.0-www-classic-820af209-20250729";
617+
exports.version = "19.2.0-www-classic-85bbe39e-20250729";
618618
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
619619
"function" ===
620620
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/React-profiling.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ exports.useSyncExternalStore = function (
614614
exports.useTransition = function () {
615615
return ReactSharedInternals.H.useTransition();
616616
};
617-
exports.version = "19.2.0-www-modern-820af209-20250729";
617+
exports.version = "19.2.0-www-modern-85bbe39e-20250729";
618618
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
619619
"function" ===
620620
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/ReactART-dev.classic.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19318,10 +19318,10 @@ __DEV__ &&
1931819318
(function () {
1931919319
var internals = {
1932019320
bundleType: 1,
19321-
version: "19.2.0-www-classic-820af209-20250729",
19321+
version: "19.2.0-www-classic-85bbe39e-20250729",
1932219322
rendererPackageName: "react-art",
1932319323
currentDispatcherRef: ReactSharedInternals,
19324-
reconcilerVersion: "19.2.0-www-classic-820af209-20250729"
19324+
reconcilerVersion: "19.2.0-www-classic-85bbe39e-20250729"
1932519325
};
1932619326
internals.overrideHookState = overrideHookState;
1932719327
internals.overrideHookStateDeletePath = overrideHookStateDeletePath;
@@ -19355,7 +19355,7 @@ __DEV__ &&
1935519355
exports.Shape = Shape;
1935619356
exports.Surface = Surface;
1935719357
exports.Text = Text;
19358-
exports.version = "19.2.0-www-classic-820af209-20250729";
19358+
exports.version = "19.2.0-www-classic-85bbe39e-20250729";
1935919359
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
1936019360
"function" ===
1936119361
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

0 commit comments

Comments
 (0)