Skip to content

[compiler] Track locations for dependencies#523

Closed
everettbu wants to merge 1 commit into
mainfrom
pr35794
Closed

[compiler] Track locations for dependencies#523
everettbu wants to merge 1 commit into
mainfrom
pr35794

Conversation

@everettbu

Copy link
Copy Markdown

Mirror of facebook/react#35794
Original author: josephsavona


Tracks locations for reactive scope dependencies, both on the deps and portions of the path. The immediate need for this is a non-public experiment where we're exploring type-directed compilation, and sometimes look up the types of expressions by location. We need to preserve locations accurately for that to work, including the locations of the deps.

Test Plan

Locations for dependencies are not easy to test: i manually spot-checked the new fixture to ensure that the deps look right. This is fine as best-effort since it doesn't impact any of our core compilation logic, i may fix forward if there are issues and will think about how to test.

Tracks locations for reactive scope dependencies, both on the deps and portions of the path. The immediate need for this is a non-public experiment where we're exploring type-directed compilation, and sometimes look up the types of expressions by location. We need to preserve locations accurately for that to work, including the locations of the deps.

## Test Plan

Locations for dependencies are not easy to test: i manually spot-checked the new fixture to ensure that the deps look right. This is fine as best-effort since it doesn't impact any of our core compilation logic, i may fix forward if there are issues and will think about how to test.
@everettbu everettbu added CLA Signed React Core Team Opened by a member of the React Core Team labels Feb 16, 2026
@greptile-apps

greptile-apps Bot commented Feb 16, 2026

Copy link
Copy Markdown

Greptile Summary

This PR adds loc: SourceLocation fields to ReactiveScopeDependency and DependencyPathEntry types, then threads source locations through the entire dependency collection and minimization pipeline. The change supports a non-public type-directed compilation experiment that needs to look up expression types by location.

  • Core type changes in HIR.ts add loc to both DependencyPathEntry (per-property location) and ReactiveScopeDependency (overall dependency location)
  • All dependency construction sites across 7 files are updated to populate the new loc fields, using instruction/value locations from the HIR
  • DeriveMinimalDependenciesHIR.ts adds loc to TreeNode and threads it through tree construction and collection, though merge cases (makeOrMergeProperty, #getOrCreateRoot) retain the first-seen location rather than updating it
  • printDependency now appends _<source-location> to its output, which changes the debug IR format used by printReactiveScopeSummary
  • New test fixture repeated-dependencies-more-precise demonstrates dependency tracking for repeated accesses at different precision levels

Confidence Score: 4/5

  • This PR is safe to merge — it adds metadata (locations) to existing data structures without changing core compilation logic.
  • The changes are additive, threading a new loc field through existing dependency tracking infrastructure. No compilation logic is altered. The two minor concerns are: (1) printDependency output format change may affect downstream consumers of the debug IR, and (2) merge cases in the dependency tree don't update loc, but both are acknowledged as "best effort" by the author.
  • Pay attention to PrintReactiveFunction.ts (output format change) and DeriveMinimalDependenciesHIR.ts (loc not updated on merge).

Important Files Changed

Filename Overview
compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts Adds loc: SourceLocation field to both DependencyPathEntry and ReactiveScopeDependency types. Clean type-level change.
compiler/packages/babel-plugin-react-compiler/src/HIR/PropagateScopeDependenciesHIR.ts Threads loc through getProperty, visitProperty, visitDependency, and visitReassignment to populate the new location fields on dependencies. Consistent and straightforward changes.
compiler/packages/babel-plugin-react-compiler/src/HIR/DeriveMinimalDependenciesHIR.ts Adds loc to TreeNode, #getOrCreateRoot, makeOrMergeProperty, and collectMinimalDependenciesInSubtree. Note: makeOrMergeProperty and #getOrCreateRoot do not update loc when merging existing nodes, meaning the first-seen location persists.
compiler/packages/babel-plugin-react-compiler/src/HIR/CollectHoistablePropertyLoads.ts Threads loc through PropertyPathRegistry, getOrCreateIdentifier, getMaybeNonNullInInstruction, and reduceMaybeOptionalChains. Renames instr to value in getMaybeNonNullInInstruction for clarity.
compiler/packages/babel-plugin-react-compiler/src/HIR/CollectOptionalChainDependencies.ts Adds propertyLoadLoc to matchOptionalTestBlock return type and threads loc through optional chain traversal and dependency construction.
compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PrintReactiveFunction.ts Modifies printDependency to append _${printSourceLocation(dependency.loc)} to output. This changes the debug IR format for all dependencies.

Last reviewed commit: 6616fc6

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

11 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

printIdentifier(dependency.identifier) +
printType(dependency.identifier.type);
return `${identifier}${dependency.path.map(token => `${token.optional ? '?.' : '.'}${token.property}`).join('')}`;
return `${identifier}${dependency.path.map(token => `${token.optional ? '?.' : '.'}${token.property}`).join('')}_${printSourceLocation(dependency.loc)}`;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Location suffix changes debug IR output format

printDependency now appends _${printSourceLocation(dependency.loc)} (e.g. _3:5:3:12) to every dependency string. This function is used in printReactiveScopeSummary (which feeds into printReactiveFunction, exported publicly and used by the snap test runner) and in debug logging in CollectHoistablePropertyLoads.

Is it intentional for this location suffix to appear in the debug IR output? If so, the underscore separator could be ambiguous with identifiers that contain underscores. Consider using a more distinct delimiter (e.g., @ or wrapping in brackets like [3:5:3:12]).

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PrintReactiveFunction.ts
Line: 118:118

Comment:
**Location suffix changes debug IR output format**

`printDependency` now appends `_${printSourceLocation(dependency.loc)}` (e.g. `_3:5:3:12`) to every dependency string. This function is used in `printReactiveScopeSummary` (which feeds into `printReactiveFunction`, exported publicly and used by the snap test runner) and in debug logging in `CollectHoistablePropertyLoads`.

Is it intentional for this location suffix to appear in the debug IR output? If so, the underscore separator could be ambiguous with identifiers that contain underscores. Consider using a more distinct delimiter (e.g., `@` or wrapping in brackets like `[3:5:3:12]`).

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

@greptile-apps

greptile-apps Bot commented Feb 16, 2026

Copy link
Copy Markdown
Additional Comments (1)

compiler/packages/babel-plugin-react-compiler/src/HIR/DeriveMinimalDependenciesHIR.ts
loc not updated when merging existing nodes

When makeOrMergeProperty finds an existing child node, accessType is merged via merge() but loc is left as-is from the first encounter. The same pattern applies in #getOrCreateRoot (line 94-100). This means the location on a merged node is determined by whichever dependency was processed first, which could be arbitrary depending on traversal order.

Given the PR description mentions this is "best effort" and the locations are used for a non-public type-directed compilation experiment, this is likely acceptable — but worth being aware of if location accuracy becomes important for merged nodes.

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/HIR/DeriveMinimalDependenciesHIR.ts
Line: 385:387

Comment:
**`loc` not updated when merging existing nodes**

When `makeOrMergeProperty` finds an existing child node, `accessType` is merged via `merge()` but `loc` is left as-is from the first encounter. The same pattern applies in `#getOrCreateRoot` (line 94-100). This means the location on a merged node is determined by whichever dependency was processed first, which could be arbitrary depending on traversal order.

Given the PR description mentions this is "best effort" and the locations are used for a non-public type-directed compilation experiment, this is likely acceptable — but worth being aware of if location accuracy becomes important for merged nodes.

How can I resolve this? If you propose a fix, please make it concise.

@everettbu

Copy link
Copy Markdown
Author

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

@everettbu everettbu closed this Feb 17, 2026
@everettbu
everettbu deleted the pr35794 branch February 17, 2026 22:19
@everettbu
everettbu restored the pr35794 branch February 17, 2026 23:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed React Core Team Opened by a member of the React Core Team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants