fix: improve error message at invalid match-expr

The location is not perfect because we only have a `ref` for the whole alternative.
This commit is contained in:
Leonardo de Moura 2020-12-29 13:48:37 -08:00
parent 64f7af9da5
commit 39e374e7cd
3 changed files with 26 additions and 1 deletions

View file

@ -498,6 +498,21 @@ def isCurrVarInductive (p : Problem) : MetaM Bool := do
let val? ← getInductiveVal? x
pure val?.isSome
private def checkNextPatternTypes (p : Problem) : MetaM Unit := do
match p.vars with
| [] => return ()
| x::_ => withGoalOf p do
for alt in p.alts do
withRef alt.ref do
match alt.patterns with
| [] => pure ()
| p::_ =>
let e ← p.toExpr
let xType ← inferType x
let eType ← inferType e
unless (← isDefEq xType eType) do
throwError! "pattern{indentExpr e}\n{← mkHasTypeButIsExpectedMsg eType xType}"
private partial def process (p : Problem) : StateRefT State MetaM Unit := withIncRecDepth do
traceState p
let isInductive ← liftM $ isCurrVarInductive p
@ -528,7 +543,8 @@ private partial def process (p : Problem) : StateRefT State MetaM Unit := withIn
let ps ← processArrayLit p
ps.forM process
else
liftM $ throwNonSupported p
checkNextPatternTypes p
throwNonSupported p
private def getUElimPos? (matcherLevels : List Level) (uElim : Level) : MetaM (Option Nat) :=
if uElim == levelZero then

View file

@ -0,0 +1,3 @@
def f : (α : Type) × α → Nat
| ⟨_, true⟩ => 1
| _ => 2

View file

@ -0,0 +1,6 @@
typeIncorrectPat.lean:2:1: error: pattern
true
has type
Bool
but is expected to have type
fst✝