chore: restore correct position for match errors

This commit is contained in:
Sebastian Ullrich 2020-12-15 18:26:50 +01:00
parent f9dcbbddc4
commit 4812f2aa64
4 changed files with 21 additions and 17 deletions

View file

@ -11,6 +11,7 @@ import Lean.Parser.Term
namespace Lean.Elab.Term
open Meta
open Lean.Parser.Term
/- This modules assumes "match"-expressions use the following syntax.
@ -25,6 +26,7 @@ structure MatchAltView where
ref : Syntax
patterns : Array Syntax
rhs : Syntax
deriving Inhabited
private def expandSimpleMatch (stx discr lhsVar rhs : Syntax) (expectedType? : Option Expr) : TermElabM Expr := do
let newStx ← `(let $lhsVar := $discr; $rhs)
@ -115,13 +117,15 @@ def expandMacrosInPatterns (matchAlts : Array MatchAltView) : MacroM (Array Matc
/- Given `stx` a match-expression, return its alternatives. -/
private def getMatchAlts : Syntax → Array MatchAltView
| `(match $discrs,* $[: $ty?]? with $[| $patternss,* => $rhss]*) =>
patternss.zipWith rhss fun patterns rhs => {
ref := patterns[0],
patterns := patterns,
rhs := rhs
}
| stx => dbgTrace s!"{stx}" fun _ => unreachable!
| `(match $discrs,* $[: $ty?]? with $alts:matchAlt*) =>
alts.map fun alt => match alt with
| `(matchAltExpr| | $patterns,* => $rhs) => {
ref := alt,
patterns := patterns,
rhs := rhs
}
| _ => unreachable!
| _ => unreachable!
/--
Auxiliary annotation used to mark terms marked with the "inaccessible" annotation `.(t)` and
@ -451,7 +455,7 @@ partial def collect : Syntax → M Syntax
discard <| processVar id
let pat := stx[2]
let pat ← collect pat
`(namedPattern $id $pat)
`(_root_.namedPattern $id $pat)
else if k == `Lean.Parser.Term.inaccessible then
pure stx
else if k == strLitKind then

View file

@ -10,16 +10,16 @@
4
---- inv
10
match1.lean:82:2: error: type mismatch during dependent match-elimination at pattern variable 'w' with type
match1.lean:82:0: error: type mismatch during dependent match-elimination at pattern variable 'w' with type
VecPred P Vec.nil
expected type
VecPred P tail✝
[false, true, true]
match1.lean:113:2: error: dependent match elimination failed, inaccessible pattern found
match1.lean:113:0: error: dependent match elimination failed, inaccessible pattern found
.(j + j)
constructor expected
[false, true, true]
match1.lean:124:11: error: invalid match-expression, type of pattern variable 'a' contains metavariables
match1.lean:124:0: error: invalid match-expression, type of pattern variable 'a' contains metavariables
?m
fun (x : ?m × ?m) => ?m x : ?m × ?m → ?m
fun (x : Nat × Nat) =>

View file

@ -1,19 +1,19 @@
syntax "[" ident "↦" term "]" : term
macro_rules `([$x ↦ $v]) => `(fun $x => $v)
macro_rules | `([$x ↦ $v]) => `(fun $x => $v)
#check [x ↦ x + 1]
syntax "case!" ident ":" term "with" term "," term : term
macro_rules `(case! $h : $c with $t, $e) => `((fun $h => cond $h $t $e) $c)
macro_rules | `(case! $h : $c with $t, $e) => `((fun $h => cond $h $t $e) $c)
#check case! h : 0 == 0 with h, not h
syntax "case2!" ident ":" term "with" term "," term : term
macro_rules `(case2! $h : $c with $t, $e) => `(let $h := $c; cond $h $t $e)
macro_rules | `(case2! $h : $c with $t, $e) => `(let $h := $c; cond $h $t $e)
#check case2! h : 0 == 0 with h, not h
syntax "test" term : term
macro_rules `(test $x:ident) => `(let $x := 0; $x)
macro_rules | `(test $x:ident) => `(let $x := 0; $x)
#check fun (x : Nat) => test x

View file

@ -17,8 +17,8 @@ def x := 1
#check foo x x
#check match 1 with x => x + 1
#check match 1 : Int with x => x + 1
#check match 1 with | x => x + 1
#check match 1 : Int with | x => x + 1
#check match 1 with | x => x + 1
#check match 1 : Int with | x => x + 1