Skip to content

fix: record level-variable TermInfos at antiquotations, not the quotation body - #174

Open
josaum wants to merge 3 commits into
leanprover-community:masterfrom
josaum:fix/level-var-highlighting
Open

josaum wants to merge 3 commits into
leanprover-community:masterfrom
josaum:fix/level-var-highlighting

Conversation

@josaum

@josaum josaum commented Nov 3, 2026

Copy link
Copy Markdown

Fixes #98.

Problem

With

variable {u : Lean.Level} {α : Q(Type u)} (a : Q($α))
example : Q(Prop) := q($a = $a)

clicking the level variable u highlighted the entire quotation body $a = $a, and clicking the a binder produced no highlights at all.

Root cause: after elaborating a quotation, UnquoteState.withLevelNames records a TermInfo for each referenced level variable with addTermInfo' (← getRef) fvar. Inside q(...) elaboration, getRef is the whole quotation body (withRef t in Impl.macro). So whenever a level variable is only referenced transitively through antiquotations (here u appears in the type of α, hence in the type of the antiquotation $a), a spurious TermInfo for u is attached to the compound body syntax $a = $a, and Lean's occurrence linker links that whole region to u.

Fix

Before recording the level-variable infos, Impl.macro now walks the macro-expanded quotation body and, for each identifier that resolves to a free variable of the quoted context, collects the level parameters transitively mentioned by its quoted type (following the types of referenced antiquotations — $a's type mentions $α, whose type mentions u). withLevelNames then records the TermInfo for u at exactly those identifiers (both $a occurrences), instead of at the body.

Two pre-existing behaviours are deliberately preserved:

  • Levels that occur syntactically in the body (e.g. Type u) still fall back to recording at getRef, i.e. the whole Type u node.
  • ql(...) elaboration records at its own ref as before.

After the fix, clicking u highlights its binder, the Type u occurrences, and precisely the two $a antiquotations; clicking the a binder highlights the binder and both $a uses.

Tests

New QqTest/highlight.lean (picked up by the QqTest.+ glob): elaborates the issue's scenario programmatically (elabCommandTopLevel discards preceding commands' info trees, so run_cmd drives IO.processCommands itself) and asserts that no TermInfo for u is attached to compound quotation-body syntax, and that u is referenced at the $a antiquotations. On unmodified master it fails with the bogus whole-body info (level variable referenced at non-identifier syntax: (Term.type _ = _ ...)); with the fix it passes.

Verification (toolchain leanprover/lean4:v4.36.0-rc2, the repo pin)

  • lake build — clean (30 jobs)
  • lake test — exit 0, full QqTest suite including the new file, no errors or warnings
  • Regression test re-run against unmodified master (teeth check): fails as expected
  • Manual LSP verification with lake env lean --server JSON-RPC documentHighlight probes: before the fix the u binder's highlights include the whole $a = $a region and the a binder yields no highlights; after the fix, the groups are exactly {u binder, Type u sites, both $a antiquotations} and {a binder, both $a uses}. An offline findModuleRefs computation over the same file confirms the same reference groups.
  • No sorry/admit.

…tion body

When a level variable is only referenced transitively through
antiquotations (e.g. `q($a = $a)` with `a : Q($α)` and `α : Q(Type u)`),
the quotation elaborator used to record a TermInfo for `u` whose syntax was
the entire quotation body. This made clicking `u` highlight the whole body
and made occurrences look unrelated to the actual antiquotations.

Now collect, for each identifier in the macro-expanded quotation body that
resolves to a free variable of the quoted context, the level parameters
transitively mentioned by its quoted type (following types of referenced
antiquotations), and record the level-variable TermInfos at those
identifiers instead. Syntactic level occurrences (e.g. `Type u` inside the
body) keep the previous behaviour.

Fixes leanprover-community#98.
…th master

Upstream master bumped the toolchain past this branch (v4.36.0-rc2 ->
v4.36.0 -> v4.37.0-rc1 in leanprover-community#179/leanprover-community#180), conflicting with the rc1->rc2 bump
this branch carried from the fork's merge-base. Setting the branch's
toolchain to master's makes the merge clean; the PR diff relative to the
merge result is unchanged.

Verified locally on leanprover/lean4:v4.37.0-rc1 (upstream master
ad0c9f8 + this branch, toolchain conflict resolved to master's version):
`lake build --wfail` passes (31 jobs, no warnings) and `lake test`
passes (exit 0, incl. QqTest.highlight).
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.

Incorrect highlighting with variables

1 participant