Skip to content

fix: keep declarations that cannot be auto-quoted in the unquoted local context - #177

Open
josaum wants to merge 5 commits into
leanprover-community:masterfrom
josaum:fix/unquote-lctx-keep-unquotable-decls
Open

josaum wants to merge 5 commits into
leanprover-community:masterfrom
josaum:fix/unquote-lctx-keep-unquotable-decls

Conversation

@josaum

@josaum josaum commented Nov 8, 2026

Copy link
Copy Markdown

Fixes #8.

Problem

The issue's MWE fails with unknown free variable '_uniq.419' at the $F antiquotation, and the presence of a shadowing, unannotated let in a do block is what triggers it:

def this_fails : Expr → List Expr → TermElabM Expr
  | Expr.app F a, args => do
    let args := a :: args        -- unannotated shadowing let
    let depth : Nat := args.length
    let F : Q(CurriedTypeFun.{0,0} $depth) := F
    let F : Q(TypeFun.{0,0} $depth) := q(@TypeFun.ofCurried $depth $F)  -- unknown free variable
    pure F
  | _, _ => throwError ""

(Still reproducible on current master/v4.36.0-rc2; QQ.check from the 2023 report no longer exists and is not needed to reproduce.)

Root cause

unquoteLCtx (Qq/Macro.lean) copies the ambient local context into the quotation's unquoted context. The fallback branch for declarations that are not Quoted _/QuotedDefEq ../Level-typed tries to auto-quote via ToExpr:

let .succ u ← getLevel ty | pure ()
let LOption.some inst ← trySynthInstance (mkApp (.const ``ToExpr [u]) ty) | pure ()

— and silently drops the declaration otherwise.

The catch: the inferred type of an unannotated do-block let is still an unassigned metavariable while a subsequent Q( )/q( ) ascription is being elaborated (annotating the let, as in the issue's this_works₂, assigns it eagerly — which is why that variant worked). getLevel ?m returns a fresh level metavariable rather than .succ _, so the declaration — here the shadowing args — is dropped. But the next let's value (args.length) refers to it, so the unquoted context ends up with a dangling free variable, which surfaces later as the unknown free variable error at an unrelated antiquotation. (Instrumenting unquoteLCtx shows exactly the dropped declaration's fvar being reported.)

Fix

A declaration that cannot be auto-quoted (no ToExpr instance, or a still-unassigned metavariable type) is now kept in the unquoted context unchanged instead of being dropped. It cannot be used in antiquotations (no exprBackSubst entry), but later declarations that refer to it in their types or let-values stay well-formed. Two categories keep being skipped deliberately:

A side effect worth noting: class-typed hypotheses that previously vanished from the unquoted context (e.g. a local [Decidable p]) are now kept, so they are available to instance resolution while the quotation body is elaborated — matching the ambient context more faithfully.

Tests

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

  • the issue's MWE shape (shadowedUnannotatedDoLet), plus a run_cmd that evaluates it and checks the resulting Expr is exactly @TypeFun.ofCurried 1 Nat.succ (in core's type-correct OfNat.ofNat numeral representation);
  • chainedUnannotatedDoLets: a chain of dependent unannotated lets feeding a quotation, with a run_cmd checking the quoted value.

Both fail on unmodified master (with unknown free variable '_fvar.N' and Unknown identifier «$m», respectively) and pass with this fix.

Verification

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

  • Reproduced the issue's error on unmodified master first; annotated-let and inlined variants from the issue confirmed working there, as reported.
  • lake build --wfail — passes (30 jobs, no warnings).
  • lake test — passes (exit 0; full QqTest suite, 59 jobs, including the new file).
  • Teeth check: reverting Qq/Macro.lean makes the new regression file fail with the errors above.
  • No sorry/admit introduced; the change is meta-only in Qq/Macro.lean plus the new test file.
  • Both pushed files verified byte-identical (git hash-object) to the locally built and tested sources: Qq/Macro.lean = 06c0bf75868e27e53079e873867bd3328f3a5cbc, QqTest/doLetShadowing.lean = 526112846e7900b7e023eeca3c841b1b214fc8d9. (Three commits because the file upload API is per-file and the first push had two transcription slips in unrelated lines, caught by the hash check and fully corrected in the second; feel free to squash.)

Branch note: the branch is based on my fork's master (859a214), whose tree is identical to upstream b025662 (verified via the Qq/Macro.lean base blob 80dd267), so the PR diff is exactly the two files above.

…al context

A local declaration whose type is not `Quoted _`/`QuotedDefEq ..`/`Level` and
has no `ToExpr` instance used to be silently dropped from a quotation's
unquoted local context. Later declarations may still refer to it in their
types or let-values, and dropping it left those references dangling,
producing `unknown free variable` errors (issue leanprover-community#8) — e.g. an unannotated
shadowing `let args := a :: args` in a `do` block, whose inferred type is
still a metavariable while a quotation is being elaborated.

Keep such declarations in the unquoted context unchanged; they simply cannot
be used in antiquotations. `Level`/`QuotedLevelDefEq` declarations (handled
by `unquoteLevelLCtx`) and auxiliary declarations (which require
`auxDeclToFullName` registration, issue leanprover-community#105) keep being skipped.

Fixes leanprover-community#8
…gnature

The previous commit accidentally changed `unquoteLevel (e.getArg! 1)` to
`unquoteExpr (e.getArg! 1)` in `unquoteLevelList` and mangled the result
type of `floatExprAntiquot'`; restore both to the exact master text.
…`do`-block `let`s)

The MWE from the issue (a shadowing `let args := a :: args` whose inferred
type is still a metavariable during quotation elaboration) failed with
`unknown free variable` before the fix; a chain of dependent unannotated
`let`s feeding a quotation is covered as well.
@josaum

josaum commented Nov 8, 2026

Copy link
Copy Markdown
Author

Small correction to the branch note above: because the PR diff is computed against the merge-base of my fork's master (859a214) and upstream master (b025662), the diff also shows the one-line lean-toolchain bump v4.36.0-rc1 → v4.36.0-rc2 from #170, which upstream already has — merging is content-neutral for that file. The intended change is exactly Qq/Macro.lean and the new QqTest/doLetShadowing.lean.

…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.doLetShadowing).
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.

Weird unknown free variable '_uniq.419' error

1 participant