Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Qq/Match.lean
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions QqTest/matchLevelMVar.lean
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lean-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
leanprover/lean4:v4.36.0-rc1
leanprover/lean4:v4.37.0-rc1