diff --git a/Qq/Match.lean b/Qq/Match.lean index 5282c3d..045b45b 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 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 diff --git a/lean-toolchain b/lean-toolchain index 2afd387..0e037f5 100644 --- a/lean-toolchain +++ b/lean-toolchain @@ -1 +1 @@ -leanprover/lean4:v4.36.0-rc1 +leanprover/lean4:v4.37.0-rc1