Skip to content

Commit

Permalink
fix: ensure explicit pattern variables provided by the uses are indee…
Browse files Browse the repository at this point in the history
…d pattern variables
  • Loading branch information
leodemoura committed Mar 16, 2022
1 parent 90c442d commit 05a2778
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Lean/Elab/Match.lean
Expand Up @@ -620,6 +620,10 @@ def main (patternVarDecls : Array PatternVarDecl) (ps : Array Expr) (matchType :
let explicitPatternVars := patternVarDecls.map fun decl => decl.fvarId
let (ps, s) ← ps.mapM normalize |>.run { explicitPatternVars } |>.run {}
let patternVars ← topSort s.patternVars
for explicit in explicitPatternVars do
unless patternVars.any (· == mkFVar explicit) do
withInPattern do
throwError "invalid patterns, `{mkFVar explicit}` is an explicit pattern variable, but it only occurs in positions that are inaccessible to pattern matching{indentD (MessageData.joinSep (ps.toList.map (MessageData.ofExpr .)) m!"\n\n")}"
let packed ← mkLambdaFVars patternVars (← packMatchTypePatterns matchType ps) (binderInfoForMVars := BinderInfo.default)
let lctx := explicitPatternVars.foldl (init := (← getLCtx)) fun lctx d => lctx.erase d
withTheReader Meta.Context (fun ctx => { ctx with lctx := lctx }) do
Expand Down
13 changes: 13 additions & 0 deletions tests/lean/invalidPatternIssue.lean
@@ -0,0 +1,13 @@
structure Pos where
protected succ ::
protected pred : Nat

protected def Pos.add : Pos → Pos → Pos
| Pos.succ x, Pos.succ y => Pos.succ (x + y).succ
instance : Add Pos := ⟨Pos.add⟩

instance (x : Nat) : OfNat Pos x.succ := ⟨Pos.succ x⟩

def f : Pos → Pos
| 1 => 1
| x+1 => f x + x + 1
2 changes: 2 additions & 0 deletions tests/lean/invalidPatternIssue.lean.expected.out
@@ -0,0 +1,2 @@
invalidPatternIssue.lean:13:0-13:20: error: invalid patterns, `x` is an explicit pattern variable, but it only occurs in positions that are inaccessible to pattern matching
{ pred := Nat.succ .(x.1) }

0 comments on commit 05a2778

Please sign in to comment.