Skip to content

fix(compiler): use globalThis.Symbol to prevent shadowing by user components - #263

Closed
everettbu wants to merge 2 commits into
mainfrom
fix/compiler-symbol-shadowing
Closed

fix(compiler): use globalThis.Symbol to prevent shadowing by user components#263
everettbu wants to merge 2 commits into
mainfrom
fix/compiler-symbol-shadowing

Conversation

@everettbu

Copy link
Copy Markdown

Mirror of facebook/react#35383
Original author: gowthamrdyy


Summary

Fixes a bug where React Compiler generates code that uses Symbol.for() without qualifying it with globalThis, causing runtime errors when a user-defined component is named Symbol.

The Problem:
When a user defines a component named Symbol, it shadows the global built-in Symbol. The compiler-generated code (e.g., for memo cache sentinels) tries to call Symbol.for() but instead accesses the user's component, resulting in "Symbol.for is not a function".

Playground repro: Link

The Fix:
Updated CodegenReactiveFunction.ts to use globalThis.Symbol.for() instead of bare Symbol.for() references. This ensures the built-in global is always used, regardless of local variable names.

How did you test this change?

  1. Added a new test fixture (component-named-symbol-shadows-global.js) that defines a component named Symbol.
    • Verified that this test case previously failed (or would produce broken code) and now compiles to correct code using globalThis.Symbol.
  2. Verified generated output: checked that the compiled output for cache sentinels and early return sentinels now explicitly uses globalThis.Symbol.
  3. Manual verification: Confirmed the fix addresses the issue reported in the playground.

@everettbu

Copy link
Copy Markdown
Author

Upstream PR was closed or merged. Code is synced via branch mirror.

@everettbu everettbu closed this Jan 18, 2026
@everettbu
everettbu deleted the fix/compiler-symbol-shadowing branch January 18, 2026 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants