Skip to content

Find references from the declaration a member stands for - #9525

Draft
cknitt wants to merge 1 commit into
masterfrom
references-root-declaration
Draft

cknitt wants to merge 1 commit into
masterfrom
references-root-declaration

Conversation

@cknitt

@cknitt cknitt commented Dec 4, 2026

Copy link
Copy Markdown
Member

Fixes #9524, the follow-up to #9331.

Find References now starts from the declaration a member stands for, then gathers everything that stands for the same declaration in one pass over the project. Before, it walked from the queried member to its copies, but never back. So:

  • From a declaration, it now finds the uses through every module that includes it, e.g. Main.x for include Lib.
  • From one included copy, it now finds the uses of the other copies of the same member.
  • module type of M connects to M's members. Before, their uses pointed to a wrong location in the file that used module type of. The module still keeps its own definition, hover and completion.
  • An interface member whose implementation is a module alias (module X = N) connects to N's member.

This replaces the walk over families of copies, its recursion across files and its visited set.

Some details for review:

  • The other file of a .res/.resi pair is read from the same .cmt as the pass, including the incremental one, so stamps agree. Reading the .cmt on disk could match a different binding of a file that's being edited.
  • A local declaration only reads its own module, as before.
  • On the analysis test project, queries take the same time as master, to within 1–3 ms.

New tests: IncludeDeclarationsRoot*.res, and alias cases in PathRefsIntf. The other snapshot changes are gains (sibling copies of a field now connect) or order only.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gGgQuXy1xmZbjATZsB

Why:
    Find References on a member started from the member and walked to its
    copies: the copies in its file, the other file of a .res/.resi pair, and
    the files the copies come from. It never walked the other way, so from a
    declaration it missed the uses through every module that includes it, and
    from one copy it missed the uses of another copy of the same member.
    `module type of M` registered M's members as declarations of the module
    it typed, with M's stamps from another compilation, so their uses were
    not connected to M.

References modified (from the declaration):
Example:
    // Lib.res
    let x = 1
    module Make = (X: {}) => {
      let made = 1
    }

    // Main.res
    include Lib
    include Lib.Make({})

    // Use.res
    let a = Main.x
    let d = Main.made
Before (text): line:column, from 0
    x in Lib      Lib 0:4
    made in Lib   Lib 2:6
After (text):
    x in Lib      Lib 0:4, Use 0:13
    made in Lib   Lib 2:6, Use 1:13

References modified (copies of one member):
Example:
    // Lib.res
    module MakeMore = (X: {}) => {
      type lr = {lf: int}
    }

    // A.res
    include Lib.MakeMore({})
    let a = {lf: 1}

    // B.res
    module M = {
      include Lib.MakeMore({})
    }
    let b = {M.lf: 1}
Before (text): lf in A, line:column, from 0
    A 1:9, Lib 1:13
After (text):
    A 1:9, Lib 1:13, B 3:11

References modified (module type of):
Example:
    // Lib.res
    let x = 1

    // Main.res
    module TypeOf: module type of Lib = Lib

    // Use.res
    let e = Main.TypeOf.x
Before (text): Main.TypeOf.x in Use, line:column, from 0
    Use 0:20, Main 0:4
After (text):
    Use 0:20, Lib 0:4

References modified (alias in the implementation):
Example:
    // A.res
    module N = {
      let s = 1
    }
    module X = N

    // A.resi
    module N: {
      let s: int
    }
    module X: {
      let s: int
    }

    // Use.res
    let u = A.X.s
Before (text): s in A.res, line:column, from 0
    A.res 1:6, A.resi 1:6
After (text):
    A.res 1:6, A.resi 1:6, A.resi 4:6, Use 0:12

Every member now leads to its root declaration: through its chain of
includes, across files, and for a member of an interface, to the same
declaration in the implementation, also through a module alias. One pass
gathers the references of every member with that root, the uses recorded
under their paths, and the uses whose paths resolve to one of them. A
member of the other file that is a member of a module type, such as S.s for
N.s in `module N: S`, is shared with every module of that type and only
adds its own references, as before.

The pass reads the project and the root's module. For a declaration that
is not visible outside its module, it reads only that module.

The members of `module type of M`, for a module path M, are recorded as
copies of M's members, as for `include M`. The module stays a module of its
own for definition, hover and completion.

This replaces the walk over families of copies, the recursion across files
and its visited set.

Review: Which .cmt is the other file of a pair read from? The one the pass
reads: the incremental .cmt when the editor wrote one. Stamps depend on the
.cmt, so the root found through the interface matches the members the pass
finds. Reading the .cmt on disk instead would match another binding of the
edited file by stamp.

Review: What does a query cost? On the analysis test project (157 files),
best of 9 with both binaries in one directory: a local binding 12–14 ms,
an exported member 45–50 ms, both within 1–3 ms of master. The cache of
roots holds booleans, not files. Holding the root's file for each
same-named member kept every scanned file alive and cost 5–8 ms in garbage
collection.

Review: Why does a snapshot of tests-namespaced-references change? It logs
the .cmt files read, and the pass now reads them in another order.

Fixes: #9524
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gGgQuXy1xmZbjATZsB
Signed-off-by: Christoph Knittel <ck@cca.io>
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Editor: find references and rename also find the uses of a member through every module that includes it, and through module type of.

Semantic Diff

1 commit · Full page

Find references from the declaration a member stands for

6fb9d6c · cknitt + Claude · Dec 04, 2026, 13:43 UTC

Why

Find References on a member started from the member and walked to its copies: the copies in its file, the other file of a .res/.resi pair, and the files the copies come from. It never walked the other way, so from a declaration it missed the uses through every module that includes it, and from one copy it missed the uses of another copy of the same member. module type of M registered M's members as declarations of the module it typed, with M's stamps from another compilation, so their uses were not connected to M.

References modified (from the declaration)

// Lib.res
let x = 1
module Make = (X: {}) => {
  let made = 1
}

// Main.res
include Lib
include Lib.Make({})

// Use.res
let a = Main.x
let d = Main.made

text · Before: line:column, from 0

-x in Lib      Lib 0:4
-made in Lib   Lib 2:6
+x in Lib      Lib 0:4, Use 0:13
+made in Lib   Lib 2:6, Use 1:13

References modified (copies of one member)

// Lib.res
module MakeMore = (X: {}) => {
  type lr = {lf: int}
}

// A.res
include Lib.MakeMore({})
let a = {lf: 1}

// B.res
module M = {
  include Lib.MakeMore({})
}
let b = {M.lf: 1}

text · Before: lf in A, line:column, from 0

-A 1:9, Lib 1:13
+A 1:9, Lib 1:13, B 3:11

References modified (module type of)

// Lib.res
let x = 1

// Main.res
module TypeOf: module type of Lib = Lib

// Use.res
let e = Main.TypeOf.x

text · Before: Main.TypeOf.x in Use, line:column, from 0

-Use 0:20, Main 0:4
+Use 0:20, Lib 0:4

References modified (alias in the implementation)

// A.res
module N = {
  let s = 1
}
module X = N

// A.resi
module N: {
  let s: int
}
module X: {
  let s: int
}

// Use.res
let u = A.X.s

text · Before: s in A.res, line:column, from 0

-A.res 1:6, A.resi 1:6
+A.res 1:6, A.resi 1:6, A.resi 4:6, Use 0:12

Every member now leads to its root declaration: through its chain of includes, across files, and for a member of an interface, to the same declaration in the implementation, also through a module alias. One pass gathers the references of every member with that root, the uses recorded under their paths, and the uses whose paths resolve to one of them. A member of the other file that is a member of a module type, such as S.s for N.s in module N: S, is shared with every module of that type and only adds its own references, as before.

The pass reads the project and the root's module. For a declaration that is not visible outside its module, it reads only that module.

The members of module type of M, for a module path M, are recorded as copies of M's members, as for include M. The module stays a module of its own for definition, hover and completion.

This replaces the walk over families of copies, the recursion across files and its visited set.

Review

  • Which .cmt is the other file of a pair read from? The one the pass reads: the incremental .cmt when the editor wrote one. Stamps depend on the .cmt, so the root found through the interface matches the members the pass finds. Reading the .cmt on disk instead would match another binding of the edited file by stamp.
  • What does a query cost? On the analysis test project (157 files), best of 9 with both binaries in one directory: a local binding 12–14 ms, an exported member 45–50 ms, both within 1–3 ms of master. The cache of roots holds booleans, not files. Holding the root's file for each same-named member kept every scanned file alive and cost 5–8 ms in garbage collection.
  • Why does a snapshot of tests-namespaced-references change? It logs the .cmt files read, and the pass now reads them in another order.

@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 656,879,538 → 656,880,184 (±0.00%).

Generated projects: 629,104,946 → 629,104,116 (±0.00%).

None of the 265 benchmarks changed by 0.1% or more.

Instructions

In total: 8,976,934,188 → 8,976,934,188 (±0.00%).

None of the 33 benchmarks changed by 0.1% or more.

Output size

In total: 27,594,641 → 27,594,641 (±0.00%).

None of the 13 benchmarks changed by 0.1% or more.

Pull request 6fb9d6c compared with base 4375d6e in this run.

@codecov

codecov Bot commented Dec 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.21739% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.66%. Comparing base (2796a44) to head (6fb9d6c).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
analysis/src/references.ml 86.40% 14 Missing ⚠️
analysis/src/cmt.ml 75.00% 2 Missing ⚠️
analysis/src/process_cmt.ml 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9525      +/-   ##
==========================================
- Coverage   82.67%   82.66%   -0.01%     
==========================================
  Files         469      469              
  Lines       66553    66599      +46     
==========================================
+ Hits        55023    55057      +34     
- Misses      11530    11542      +12     
Files with missing lines Coverage Δ
analysis/src/process_cmt.ml 87.11% <75.00%> (-0.19%) ⬇️
analysis/src/cmt.ml 87.50% <75.00%> (-0.74%) ⬇️
analysis/src/references.ml 77.02% <86.40%> (-2.19%) ⬇️

... and 6 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

pkg-pr-new Bot commented Dec 4, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9525

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9525

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9525

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9525

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9525

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9525

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9525

commit: 6fb9d6c

@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Find references of included members from their root declaration

1 participant