Skip to content

fix: don't leak metavariables from postponed elaboration in ~q match bodies - #172

Open
josaum wants to merge 5 commits into
leanprover-community:masterfrom
josaum:fix/qq-match-nested-quotation
Open

josaum wants to merge 5 commits into
leanprover-community:masterfrom
josaum:fix/qq-match-nested-quotation

Conversation

@josaum

@josaum josaum commented Nov 1, 2026 •

Copy link
Copy Markdown

Fixes #126.

Problem

The issue's partial def failed with (kernel) declaration has metavariables 'proveFoo._unsafe_rec'. Minimizing the report showed that recursion is not actually required: a plain def fails the same way whenever a q(·) quotation is elaborated inside the body of a nested ~q match:

opaque Foo {α β : Type} (f : α → β) : Prop
opaque someProp : Q(Prop)

-- (kernel) declaration has metavariables 'nestedMatchQuotation'
def nestedMatchQuotation {α β : Q(Type)} (f : Q($α → $β)) : MetaM Q(Foo $f) := do
  match β with
  | ~q(Nat) => failure
  | ~q(Int) =>
    let t : Q(Prop) := someProp
    let ~q(@Foo $α'' $β'' $f'') := t | failure
    let _x := q($f)
    failure

Root cause

q(·) postpones its own elaboration when the expected type or the local context still contains metavariables (Qq/Macro.lean). Inside a nested _qq_match, the branch-local context built by makeMatchCode (the pattern-variable lets and the match_eq hypothesis) routinely contains metavariables, so a quotation in the branch body gets postponed.

The postponed work was then synthesized only after the match code had been generated (at the end of the enclosing declaration's elaboration), and metavariables from that late elaboration survived into the compiled declaration — the kernel rejects it with declaration has metavariables. (With debug.skipKernelTC, the same path panics in MkBinding with unknown metavariable _uniq.N, i.e. the leaked ids have no declaration in the final metavariable context.)

Fix

One call in makeMatchCode, immediately after the branch body is elaborated (k expectedType) and before the result is instantiated and embedded into the generated match code:

synthesizeSyntheticMVars (postpone := .no)

Postponed elaboration is now forced to complete while the match's own local context is still in scope, so no metavariables leak into the generated code. This mirrors what elabPat and the q(·) elaborator already do after elaborating their subterms.

Tests

New QqTest/matchNestedQuotation.lean (picked up by the QqTest.+ glob):

  • nestedMatchQuotation — the minimized case above (plain def, no recursion);
  • proveFoo — the shape from the original report: a partial recursive definition with nested ~q matches and proveFoo q($f) in the innermost body.

Both fail on current master with exactly the issue's kernel error (declaration has metavariables 'nestedMatchQuotation' / 'proveFoo._unsafe_rec') and compile with this fix.

Verification

On the pinned toolchain (leanprover/lean4:v4.36.0-rc2):

  • Reproduced the issue's original MWE on unmodified master, then minimized it through ~15 variants to isolate the actual trigger (nested ~q match + q(·) in the branch body); all variants now compile with the fix.
  • Both new regression tests verified to fail pre-fix (reverting Qq/Match.lean to master) and pass post-fix.
  • lake build --wfail — passes (30 jobs, no warnings).
  • lake test — passes (exit 0; full QqTest suite, 59 jobs, including the new regression file).
  • No sorry/admit introduced; the change is one elaboration call in Qq/Match.lean plus tests.
  • Both pushed files verified byte-identical (git hash-object) to the locally built and tested sources.

Branch note: my fork was one commit behind upstream, and the GitHub API offers no way to fast-forward it or to rebase this branch onto b025662 (the org's update-branch endpoint is OAuth-restricted). The PR diff therefore also shows the one-line lean-toolchain change from #170, which upstream already has — merging is content-neutral for that file. The intended change is exactly Qq/Match.lean (+6 lines) and the new QqTest/matchNestedQuotation.lean; feel free to rebase/squash as usual.

No code change; keeps the branch's Match.lean byte-identical to the locally
tested file.
Match the upstream text exactly; the only intended change on this branch is
the synthesizeSyntheticMVars call in makeMatchCode.
…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.matchNestedQuotation).
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.

declaration has metavariables error with matching and recursion

1 participant