lean4-htt/tests/lean/run/matchPatternVarErrors.lean
jrr6 d1ec806834
feat: improve error messages in invalid match alternatives (#8368)
This PR improves the error messages produced by invalid pattern-match
alternatives and improves parity in error placement between
pattern-matching tactics and elaborators.

Closes #7170
2025-05-19 17:40:41 +00:00

74 lines
1.9 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/-! # Errors in match patterns
This file tests various error messages that arise from missing, extra, uninferrable, or otherwise
erroneous arguments in match patterns.
-/
inductive T where
| exp (a b c : Nat)
| imp {a b c : Nat}
| mix {a b : Nat} (c : Nat)
/-- error: Invalid pattern: Too many arguments to 'exp'; expected 3 explicit arguments -/
#guard_msgs in
def T.mixNamedPositional₁ : T → Unit
| exp (b := x) y z w => ()
| _ => ()
/-- error: Invalid pattern: Too many arguments to 'imp'; expected 0 explicit arguments -/
#guard_msgs in
def T.mixNamedPositional₂ : T → Unit
| imp (a := x) y => ()
| _ => ()
/--
error: Invalid pattern: Not enough arguments to 'imp'; expected 3 arguments
Hint: To ignore all remaining arguments, use the ellipsis notation `..`
-/
#guard_msgs in
def T.mixNamedPositional₃ : T → Unit
| @imp (a := x) y => ()
| _ => ()
/-- error: Invalid pattern: Too many arguments to 'mix'; expected 1 explicit argument -/
#guard_msgs in
def T.mixNamedPositional₄ : T → Unit
| mix (b := x) y z => ()
| _ => ()
def matchExplicitValid (n : Nat) :=
match n with
| @Nat.zero => ()
| _ => ()
/-- error: Invalid argument names 'invalidName1' and 'invalidName2' for function 'List.cons' -/
#guard_msgs in
def invalidArgNames (xs : List Nat) :=
match xs with
| List.cons (invalidName1 := x) (invalidName2 := y) .. => true
| _ => false
/-- error: Invalid pattern variable: Variable name 'xs' was already used -/
#guard_msgs in
def dupVar (xs : List Nat) : List Nat :=
match xs with
| List.cons xs xs => true
| _ => false
/--
error: Invalid pattern variable: Variable name must be atomic, but 'x.y' has multiple components
-/
#guard_msgs in
def compVarName : Unit × Unit → Unit
| (_, x.y) => x.y
/--
error: Invalid match expression: This pattern contains metavariables:
[]
-/
#guard_msgs in
def List.len x :=
match x with
| [] => 0
| _ :: xs => 1 + len xs