diff --git a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts
index 787d9e7047..d69dee527d 100644
--- a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts
+++ b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts
@@ -1143,7 +1143,7 @@ function checkFunctionReferencedBeforeDeclarationAtTopLevel(
* A null scope means there's no function scope, which means we're at the
* top level scope.
*/
- if (scope === null) {
+ if (scope === null && id.isReferencedIdentifier()) {
errors.pushErrorDetail(
new CompilerErrorDetail({
reason: `Encountered a function used before its declaration, which breaks Forget's gating codegen due to hoisting`,
diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/gating/gating-nonreferenced-identifier-collision.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/gating/gating-nonreferenced-identifier-collision.expect.md
new file mode 100644
index 0000000000..a8f2a8dc58
--- /dev/null
+++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/gating/gating-nonreferenced-identifier-collision.expect.md
@@ -0,0 +1,60 @@
+
+## Input
+
+```javascript
+// @gating
+import {identity, useHook as useRenamed} from 'shared-runtime';
+const _ = {
+ useHook: () => {},
+};
+identity(_.useHook);
+
+function useHook() {
+ useRenamed();
+ return
hello world!
;
+}
+
+export const FIXTURE_ENTRYPOINT = {
+ fn: useHook,
+ params: [{}],
+};
+
+```
+
+## Code
+
+```javascript
+import { isForgetEnabled_Fixtures } from "ReactForgetFeatureFlag";
+import { c as _c } from "react/compiler-runtime"; // @gating
+import { identity, useHook as useRenamed } from "shared-runtime";
+const _ = {
+ useHook: isForgetEnabled_Fixtures() ? () => {} : () => {},
+};
+identity(_.useHook);
+const useHook = isForgetEnabled_Fixtures()
+ ? function useHook() {
+ const $ = _c(1);
+ useRenamed();
+ let t0;
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
+ t0 = hello world!
;
+ $[0] = t0;
+ } else {
+ t0 = $[0];
+ }
+ return t0;
+ }
+ : function useHook() {
+ useRenamed();
+ return hello world!
;
+ };
+
+export const FIXTURE_ENTRYPOINT = {
+ fn: useHook,
+ params: [{}],
+};
+
+```
+
+### Eval output
+(kind: ok) hello world!
\ No newline at end of file
diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/gating/gating-nonreferenced-identifier-collision.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/gating/gating-nonreferenced-identifier-collision.js
new file mode 100644
index 0000000000..f5d557978b
--- /dev/null
+++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/gating/gating-nonreferenced-identifier-collision.js
@@ -0,0 +1,16 @@
+// @gating
+import {identity, useHook as useRenamed} from 'shared-runtime';
+const _ = {
+ useHook: () => {},
+};
+identity(_.useHook);
+
+function useHook() {
+ useRenamed();
+ return hello world!
;
+}
+
+export const FIXTURE_ENTRYPOINT = {
+ fn: useHook,
+ params: [{}],
+};