From 859a21471671ff5453fe17caa70ba87a6e735392 Mon Sep 17 00:00:00 2001 From: Dionisio Chiuratto Agourakis Date: Sun, 1 Nov 2026 06:08:21 -0300 Subject: [PATCH 1/5] chore: bump toolchain to v4.36.0-rc2 (#170) Sync with upstream leanprover-community/quote4@b025662. --- lean-toolchain | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lean-toolchain b/lean-toolchain index 2afd387..0fc4336 100644 --- a/lean-toolchain +++ b/lean-toolchain @@ -1 +1 @@ -leanprover/lean4:v4.36.0-rc1 +leanprover/lean4:v4.36.0-rc2 From 34046b2bfc24cd9c12943bb6642cb3fbc0333ae1 Mon Sep 17 00:00:00 2001 From: Dionisio Chiuratto Agourakis Date: Wed, 4 Nov 2026 22:34:08 -0300 Subject: [PATCH 2/5] fix: determine the `~q` discriminant's universe level from its unquoted type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Elaborating the discriminant never constrains the level metavariable (`Quoted` is `implicit_reducible`), so it survived into the generated `match_eq` hypothesis and made `match` generalization fail with "type of pattern variable 'match_eq✝' contains metavariables". Fixes #30. --- Qq/Match.lean | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/Qq/Match.lean b/Qq/Match.lean index 5282c3d..97bd7b5 100644 --- a/Qq/Match.lean +++ b/Qq/Match.lean @@ -234,6 +234,25 @@ scoped elab "_qq_match" pat:term " ← " e:term " | " alt:term " in " body:term let argTyExpr ← instantiateMVarsQ argTyExpr let ((lctx, localInsts, type), s) ← (unquoteForMatch argTyExpr).run { mayPostpone := (← read).mayPostpone } + + -- Elaborating `e` does not necessarily determine the universe level of the discriminant's + -- type: `Quoted` is `implicit_reducible`, so unifying the expected and inferred types of `e` + -- never constrains `argLvlExpr`. If it is still a metavariable here, it survives into the + -- `match_eq` hypothesis generated by `makeMatchCode`, and `match` generalization fails with + -- "type of pattern variable 'match_eq✝' contains metavariables" (quote4#30). + -- Determine the level from the unquoted type instead, mirroring `unquoteLCtx`. + let argLvlExpr ← instantiateMVarsQ argLvlExpr + let argLvlExpr : Q(Level) ← if argLvlExpr.isMVar then + let u ← withLCtx lctx localInsts do instantiateLevelMVars (← getLevel type) + if u.hasMVar then + pure argLvlExpr -- still undetermined; keep the previous behavior + else + match ← observing? ((quoteLevel u).run s) with + | some uE => pure uE + | none => pure argLvlExpr + else + pure argLvlExpr + let (pat, patVarDecls, newLevels) ← elabPat pat lctx localInsts type s.levelNames let mut s := s @@ -348,12 +367,11 @@ partial def unpackParensIdent : Syntax → Option Syntax | stx => if stx.isIdent then some stx else none private partial def floatLevelAntiquot (stx : Syntax.Level) : StateT (Array Lift) MacroM Syntax.Level := - if stx.1.isAntiquot && !stx.1.isEscapedAntiquot then - if !stx.1.getAntiquotTerm.isIdent then - withFreshMacroScope do - let n : Ident ← `(u) - push (n, ← `(Level), ⟨stx.1.getAntiquotTerm⟩) - `(level| u) + if stx.1.isAntiquot && !stx.1.getAntiquotTerm.isIdent then + withFreshMacroScope do + let n : Ident ← `(u) + push (n, ← `(Level), ⟨stx.1.getAntiquotTerm⟩) + `(level| u) else pure stx else From 19d6f3ca5889f513e28a6adc1ccb015698f3b1b6 Mon Sep 17 00:00:00 2001 From: Dionisio Chiuratto Agourakis Date: Wed, 4 Nov 2026 22:42:45 -0300 Subject: [PATCH 3/5] fixup: restore floatLevelAntiquot escaped-antiquot check The previous commit's transcription dropped the `isEscapedAntiquot` guard; this restores the file to exactly the tested content. --- Qq/Match.lean | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Qq/Match.lean b/Qq/Match.lean index 97bd7b5..045b45b 100644 --- a/Qq/Match.lean +++ b/Qq/Match.lean @@ -367,11 +367,12 @@ partial def unpackParensIdent : Syntax → Option Syntax | stx => if stx.isIdent then some stx else none private partial def floatLevelAntiquot (stx : Syntax.Level) : StateT (Array Lift) MacroM Syntax.Level := - if stx.1.isAntiquot && !stx.1.getAntiquotTerm.isIdent then - withFreshMacroScope do - let n : Ident ← `(u) - push (n, ← `(Level), ⟨stx.1.getAntiquotTerm⟩) - `(level| u) + if stx.1.isAntiquot && !stx.1.isEscapedAntiquot then + if !stx.1.getAntiquotTerm.isIdent then + withFreshMacroScope do + let n : Ident ← `(u) + push (n, ← `(Level), ⟨stx.1.getAntiquotTerm⟩) + `(level| u) else pure stx else From 41d946e1b994c237678c0680cf94de31b46927d6 Mon Sep 17 00:00:00 2001 From: Dionisio Chiuratto Agourakis Date: Wed, 4 Nov 2026 22:43:08 -0300 Subject: [PATCH 4/5] test: regression tests for `match_eq` universe level metavariable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Covers the issue MWE (nested `match` in a `~q` branch) and the multi-discriminant variant where the `~q` pattern is not in the last position; both failed with "type of pattern variable 'match_eq✝' contains metavariables" before the fix. --- QqTest/matchLevelMVar.lean | 53 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 QqTest/matchLevelMVar.lean diff --git a/QqTest/matchLevelMVar.lean b/QqTest/matchLevelMVar.lean new file mode 100644 index 0000000..aa6fb17 --- /dev/null +++ b/QqTest/matchLevelMVar.lean @@ -0,0 +1,53 @@ +/- +Copyright (c) 2026 Lean FRO, LLC and contributors. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Dionisio Chiuratto Agourakis +-/ +import Qq + +open Qq Lean.Meta + +/-! +Regression tests for https://github.com/leanprover-community/quote4/issues/30 + +The universe level of the discriminant's type was left as an unassigned metavariable +(`Quoted` is `implicit_reducible`, so elaborating the discriminant never constrains it), +and it survived into the `match_eq` hypothesis generated for the `~q` pattern. Any nested +`match` (or a multi-discriminant `match` with the `~q` pattern in a non-final position) +then failed with "type of pattern variable 'match_eq✝' contains metavariables". +-/ + +/-- The issue's MWE: a `~q` match whose branch contains another `match`. -/ +example (qo : Q(Option Nat)) (o : Option Nat) : MetaM Unit := do + match qo with + | ~q(some $qx) => match o with + | some _ => pure () + | _ => pure () + | _ => pure () + +/-- 0art0's variant from the issue discussion: the `~q` pattern is not in the last +discriminant position. This failed with "type of pattern variable 'match_eq✝' contains +metavariables". -/ +def matchQPropFirst : (P : Q(Prop)) → (args : List Nat) → MetaM String + | ~q($A ∧ $B), [] => return "and" + | _, _ => return "other" + +/-- Control: the argument order that already worked before the fix. -/ +def matchQPropLast : (args : List Nat) → (P : Q(Prop)) → MetaM String + | [], ~q($A ∧ $B) => return "and" + | _, _ => return "other" + +/-- The matched functions still compute as expected. -/ +def exAnd : Q(Prop) := q(True ∧ True) + +/-- info: "and" -/ +#guard_msgs in +#eval show MetaM _ from matchQPropFirst exAnd [] + +/-- info: "other" -/ +#guard_msgs in +#eval show MetaM _ from matchQPropFirst q(True) [1] + +/-- info: "and" -/ +#guard_msgs in +#eval show MetaM _ from matchQPropLast [] exAnd From 59d652db4791ab0f4c6511c62cb3daabc4cac53d Mon Sep 17 00:00:00 2001 From: Dionisio Chiuratto Agourakis Date: Sat, 21 Nov 2026 22:29:10 -0300 Subject: [PATCH 5/5] chore: bump toolchain to v4.37.0-rc1, resolving the merge conflict with master Upstream master bumped the toolchain past this branch (v4.36.0-rc2 -> v4.36.0 -> v4.37.0-rc1 in #179/#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.matchLevelMVar). --- lean-toolchain | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lean-toolchain b/lean-toolchain index 0fc4336..0e037f5 100644 --- a/lean-toolchain +++ b/lean-toolchain @@ -1 +1 @@ -leanprover/lean4:v4.36.0-rc2 +leanprover/lean4:v4.37.0-rc1